Exemplo n.º 1
0
        /// <summary>
        /// Adds an already verified transaction to the memory pool.
        ///
        /// Note: This must only be called from a single thread (the Blockchain actor). To add a transaction to the pool
        ///       tell the Blockchain actor about the transaction.
        /// </summary>
        /// <param name="hash"></param>
        /// <param name="tx"></param>
        /// <returns></returns>
        internal bool TryAdd(UInt256 hash, Transaction tx)
        {
            var poolItem = new PoolItem(tx);

            if (_unsortedTransactions.ContainsKey(hash))
            {
                return(false);
            }

            List <Transaction> removedTransactions = null;

            _txRwLock.EnterWriteLock();
            try
            {
                _unsortedTransactions.Add(hash, poolItem);
                _sortedTransactions.Add(poolItem);

                if (Count > Capacity)
                {
                    removedTransactions = RemoveOverCapacity();
                }
            }
            finally
            {
                _txRwLock.ExitWriteLock();
            }

            foreach (IMemoryPoolTxObserverPlugin plugin in Plugin.TxObserverPlugins)
            {
                plugin.TransactionAdded(poolItem.Tx);
                if (removedTransactions != null)
                {
                    plugin.TransactionsRemoved(MemoryPoolTxRemovalReason.CapacityExceeded, removedTransactions);
                }
            }

            return(_unsortedTransactions.ContainsKey(hash));
        }
Exemplo n.º 2
0
        private PoolItem GetLowestFeeTransaction(SortedSet <PoolItem> verifiedTxSorted,
                                                 SortedSet <PoolItem> unverifiedTxSorted, out SortedSet <PoolItem> sortedPool)
        {
            PoolItem minItem = unverifiedTxSorted.Min;

            sortedPool = minItem != null ? unverifiedTxSorted : null;

            PoolItem verifiedMin = verifiedTxSorted.Min;

            if (verifiedMin == null)
            {
                return(minItem);
            }

            if (minItem != null && verifiedMin.CompareTo(minItem) >= 0)
            {
                return(minItem);
            }

            sortedPool = verifiedTxSorted;
            minItem    = verifiedMin;

            return(minItem);
        }