Example #1
0
        public ApplyTransactionResult Apply(TrackedTransaction trackedTransaction)
        {
            var result = ApplyTransactionResult.Passed;
            var hash   = trackedTransaction.Key.TxId;

            foreach (var coin in trackedTransaction.ReceivedCoins)
            {
                if (UTXOByOutpoint.ContainsKey(coin.Outpoint))
                {
                    result = ApplyTransactionResult.Conflict;
                    Conflicts.Add(coin.Outpoint, hash);
                }
            }

            foreach (var spentOutpoint in trackedTransaction.SpentOutpoints)
            {
                if (_KnownInputs.Contains(spentOutpoint) ||
                    (!UTXOByOutpoint.ContainsKey(spentOutpoint) && SpentUTXOs.Contains(spentOutpoint)))
                {
                    result = ApplyTransactionResult.Conflict;
                    Conflicts.Add(spentOutpoint, hash);
                }
            }
            if (result == ApplyTransactionResult.Conflict)
            {
                return(result);
            }

            _TransactionTimes.Add(trackedTransaction.FirstSeen);

            foreach (var coin in trackedTransaction.ReceivedCoins)
            {
                UTXOByOutpoint.TryAdd(coin.Outpoint, coin);
            }

            if (trackedTransaction.ReceivedCoins.Count == 0 && trackedTransaction.Transaction != null)
            {
                UTXOByOutpoint.Prunable.Add(new Prunable()
                {
                    PrunedBy = hash, TransactionId = hash
                });
            }

            foreach (var spentOutpoint in trackedTransaction.SpentOutpoints)
            {
                if (UTXOByOutpoint.Remove(spentOutpoint, hash))
                {
                    SpentUTXOs.Add(spentOutpoint);
                }
                _KnownInputs.Add(spentOutpoint);
            }
            return(result);
        }
Example #2
0
        public ApplyTransactionResult Apply(TrackedTransaction trackedTransaction)
        {
            var result = ApplyTransactionResult.Passed;
            var hash   = trackedTransaction.Key.TxId;

            foreach (var coin in trackedTransaction.ReceivedCoins)
            {
                if (UTXOByOutpoint.ContainsKey(coin.Outpoint))
                {
                    result = ApplyTransactionResult.Conflict;
                }
            }

            foreach (var spentOutpoint in trackedTransaction.SpentOutpoints)
            {
                if (_KnownInputs.Contains(spentOutpoint) ||
                    (!UTXOByOutpoint.ContainsKey(spentOutpoint) && SpentUTXOs.Contains(spentOutpoint)))
                {
                    result = ApplyTransactionResult.Conflict;
                }
            }
            if (result == ApplyTransactionResult.Conflict)
            {
                return(result);
            }

            foreach (var coin in trackedTransaction.ReceivedCoins)
            {
                UTXOByOutpoint.TryAdd(coin.Outpoint, coin);
            }

            foreach (var spentOutpoint in trackedTransaction.SpentOutpoints)
            {
                if (UTXOByOutpoint.Remove(spentOutpoint))
                {
                    SpentUTXOs.Add(spentOutpoint);
                }
                _KnownInputs.Add(spentOutpoint);
            }
            return(result);
        }
 public UTXOByOutpoint(UTXOByOutpoint other)
 {
     _Inner = new Dictionary <OutPoint, ICoin>(other._Inner);
 }
Example #4
0
 public UTXOByOutpoint(UTXOByOutpoint other)
 {
     _Inner            = new Dictionary <OutPoint, Coin>(other._Inner);
     _AvailableOutputs = new Dictionary <uint256, int>(other._AvailableOutputs);
     Prunable          = new List <Prunable>(other.Prunable);
 }
Example #5
0
        public ApplyTransactionResult Apply(Transaction tx)
        {
            var result = ApplyTransactionResult.Passed;
            var hash   = tx.GetHash();

            for (int i = 0; i < tx.Outputs.Count; i++)
            {
                var output   = tx.Outputs[i];
                var outpoint = new OutPoint(hash, i);
                if (UTXOByOutpoint.ContainsKey(outpoint))
                {
                    result = ApplyTransactionResult.Conflict;
                    Conflicts.Add(outpoint, hash);
                }
            }

            for (int i = 0; i < tx.Inputs.Count; i++)
            {
                var input = tx.Inputs[i];
                if (_KnownInputs.Contains(input.PrevOut) ||
                    (!UTXOByOutpoint.ContainsKey(input.PrevOut) && SpentUTXOs.Contains(input.PrevOut)))
                {
                    result = ApplyTransactionResult.Conflict;
                    Conflicts.Add(input.PrevOut, hash);
                }
            }
            if (result == ApplyTransactionResult.Conflict)
            {
                return(result);
            }

            var matches = MatchScript == null ? null : MatchScript(tx.Outputs.Select(o => o.ScriptPubKey).ToArray());

            for (int i = 0; i < tx.Outputs.Count; i++)
            {
                var output  = tx.Outputs[i];
                var matched = matches == null ? true : matches[i];
                if (matched)
                {
                    var outpoint = new OutPoint(hash, i);
                    if (UTXOByOutpoint.TryAdd(outpoint, new Coin(outpoint, output)))
                    {
                        AddEvent(new UTXOEvent()
                        {
                            Received = true, Outpoint = outpoint, TxId = hash
                        });
                    }
                }
            }

            for (int i = 0; i < tx.Inputs.Count; i++)
            {
                var input = tx.Inputs[i];
                if (UTXOByOutpoint.Remove(input.PrevOut))
                {
                    AddEvent(new UTXOEvent()
                    {
                        Received = false, Outpoint = input.PrevOut, TxId = hash
                    });
                    SpentUTXOs.Add(input.PrevOut);
                }
                _KnownInputs.Add(input.PrevOut);
            }
            return(result);
        }
 public UTXOState(UTXOState other)
 {
     UTXOByOutpoint = new UTXOByOutpoint(other.UTXOByOutpoint);
     SpentUTXOs     = new HashSet <OutPoint>(other.SpentUTXOs);
     _KnownInputs   = new HashSet <OutPoint>(other._KnownInputs);
 }