Example #1
0
        internal OrderedBalanceChange(DynamicTableEntity entity)
        {
            var splitted = entity.RowKey.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);

            Height    = Helper.StringToHeight(splitted[1]);
            BalanceId = BalanceId.Parse(splitted[0]);

            var locator     = BalanceLocator.Parse(string.Join("-", splitted.Skip(1).ToArray()), true);
            var confLocator = locator as ConfirmedBalanceLocator;

            if (confLocator != null)
            {
                Height        = confLocator.Height;
                TransactionId = confLocator.TransactionId;
                BlockId       = confLocator.BlockHash;
            }

            var unconfLocator = locator as UnconfirmedBalanceLocator;

            if (unconfLocator != null)
            {
                TransactionId = unconfLocator.TransactionId;
            }

            SeenUtc = entity.Properties["s"].DateTime.Value;

            _SpentOutpoints = Helper.DeserializeList <OutPoint>(Helper.GetEntityProperty(entity, "a"));

            if (entity.Properties.ContainsKey("b0"))
            {
                _SpentCoins = new CoinCollection(Helper.DeserializeList <Spendable>(Helper.GetEntityProperty(entity, "b")).Select(s => new Coin()
                {
                    Outpoint = s.OutPoint,
                    TxOut    = s.TxOut
                }).ToList());
            }
            else if (_SpentOutpoints.Count == 0)
            {
                _SpentCoins = new CoinCollection();
            }

            _SpentIndices = Helper.DeserializeList <IntCompactVarInt>(Helper.GetEntityProperty(entity, "ss")).Select(i => (uint)i.ToLong()).ToList();

            var receivedIndices = Helper.DeserializeList <IntCompactVarInt>(Helper.GetEntityProperty(entity, "c")).Select(i => (uint)i.ToLong()).ToList();
            var receivedTxOuts  = Helper.DeserializeList <TxOut>(Helper.GetEntityProperty(entity, "d"));

            _ReceivedCoins = new CoinCollection();
            for (int i = 0; i < receivedIndices.Count; i++)
            {
                _ReceivedCoins.Add(new Coin()
                {
                    Outpoint = new OutPoint(TransactionId, receivedIndices[i]),
                    TxOut    = receivedTxOuts[i]
                });
            }

            var flags = entity.Properties["e"].StringValue;

            HasOpReturn = flags[0] == 'o';
            IsCoinbase  = flags[1] == 'o';

            _MatchedRules = Helper.DeserializeObject <List <MatchedRule> >(entity.Properties["f"].StringValue).ToList();

            if (entity.Properties.ContainsKey("g"))
            {
                var ctx = new ColoredTransaction();
                ctx.FromBytes(entity.Properties["g"].BinaryValue);
                ColoredTransaction = ctx;
            }

            if (entity.Properties.ContainsKey("h"))
            {
                _ScriptPubKey = new Script(entity.Properties["h"].BinaryValue);
            }

            var data = Helper.GetEntityProperty(entity, "cu");

            if (data != null)
            {
                CustomData = Encoding.UTF8.GetString(data);
            }
        }
Example #2
0
        public async Task <bool> EnsureSpentCoinsLoadedAsync(ITransactionRepository transactions)
        {
            if (SpentCoins != null)
            {
                return(true);
            }

            bool           cleanSpent = false;
            CoinCollection result     = new CoinCollection();

            for (int i = 0; i < SpentOutpoints.Count; i++)
            {
                var outpoint = SpentOutpoints[i];
                if (outpoint.IsNull)
                {
                    continue;
                }
                var prev = await transactions.GetAsync(outpoint.Hash).ConfigureAwait(false);

                if (prev == null)
                {
                    return(false);
                }

                var coin = new Coin(outpoint, prev.Outputs[SpentOutpoints[i].N]);
                if (coin.ScriptPubKey != GetScriptPubkey(i))
                {
                    cleanSpent        = true;
                    SpentOutpoints[i] = null;
                }
                else
                {
                    result.Add(coin);
                }
            }

            if (cleanSpent)
            {
                List <uint>        spentIndices   = new List <uint>();
                List <OutPoint>    spentOutpoints = new List <OutPoint>();
                List <MatchedRule> matchedRules   = new List <MatchedRule>();
                for (int i = 0; i < SpentOutpoints.Count; i++)
                {
                    if (SpentOutpoints[i] != null)
                    {
                        spentIndices.Add(SpentIndices[i]);
                        spentOutpoints.Add(SpentOutpoints[i]);
                        if (MatchedRules != null && MatchedRules.Count != 0)
                        {
                            matchedRules.Add(MatchedRules[i]);
                        }
                    }
                }
                SpentIndices   = spentIndices;
                SpentOutpoints = spentOutpoints;
                MatchedRules   = matchedRules;
            }

            SpentCoins = result;
            UpdateToScriptCoins();
            return(true);
        }