Пример #1
0
        public void DelByHashIndex(IEnumerable <string> hashindexs)
        {
            var spents = spentUtxoSets.Where(x => hashindexs.Contains(x.Key)).ToList();

            if (!spents.Any())
            {
                return;
            }

            var hashes = spents.SelectMany(x => x.Value).ToList();

            var delMsgs = transactionMsgs.Where(x => hashes.Contains(x.Transaction.Hash)).ToList();

            long useAmount = 0;
            long addAmount = 0;

            var inputs   = delMsgs.SelectMany(x => x.Transaction.Inputs);
            var outputs  = delMsgs.SelectMany(x => x.Transaction.Outputs);
            var accounts = AccountDac.Default.GetAccountBook();

            var spentUtxos = inputs.Select(item => $"{item.OutputTransactionHash}_{item.OutputIndex}");
            var spentSets  = UtxoSetDac.Default.Get(spentUtxos);

            if (spentSets != null)
            {
                useAmount = spentSets.Where(x => accounts.Contains(x.Account)).Sum(x => x.Amount);
            }

            var addUtxos = outputs.Where(x => accounts.Contains(DtoExtensions.GetAccountByLockScript(x.LockScript)));

            addAmount = addUtxos.Sum(x => x.Amount);

            UseBalanceInPool -= useAmount;
            AddBalanceInPool -= addAmount;

            delMsgs.ForEach(x => {
                transactionMsgs.Remove(x);
                Storage.Instance.Delete(DbDomains.TxContainer, x.Transaction.Hash);
            });

            foreach (var hashindex in hashindexs)
            {
                spentUtxoSets.Remove(hashindex);
            }

            var delKeys = PaymentDac.Default.Payment_Mem.Where(x => hashes.Contains(x.txId)).Select(x => x.ToString()).ToArray();

            PaymentDac.Default.Del(delKeys);
        }
Пример #2
0
        public bool Insert(TransactionPoolItem msg)
        {
            if (transactionMsgs.Any(x => x.Transaction.Hash.Equals(msg.Transaction.Hash)))
            {
                return(false);
            }

            var  accounts  = AccountDac.Default.GetAccountBook();
            long useAmount = 0;
            long addAmount = 0;

            foreach (var item in msg.Transaction.Inputs)
            {
                var key = $"{item.OutputTransactionHash}_{item.OutputIndex}";
                if (spentUtxoSets.ContainsKey(key))
                {
                    spentUtxoSets[key].Add(msg.Transaction.Hash);
                }
                else
                {
                    spentUtxoSets.Add(key, new List <string>()
                    {
                        msg.Transaction.Hash
                    });
                }
            }

            var spentUtxos = msg.Transaction.Inputs.Select(item => $"{item.OutputTransactionHash}_{item.OutputIndex}");
            var spentSets  = UtxoSetDac.Default.Get(spentUtxos);

            if (spentSets != null && spentSets.Any())
            {
                useAmount = spentSets.Where(x => accounts.Contains(x.Account)).Sum(x => x.Amount);
            }

            var addUtxos = msg.Transaction.Outputs.Where(x => accounts.Contains(DtoExtensions.GetAccountByLockScript(x.LockScript)));

            if (addUtxos.Any())
            {
                addAmount = addUtxos.Sum(x => x.Amount);
            }
            UseBalanceInPool += useAmount;
            AddBalanceInPool += addAmount;
            transactionMsgs.Add(msg);
            return(true);
        }
Пример #3
0
        public void Del(string hash)
        {
            var item = transactionMsgs.FirstOrDefault(x => x.Transaction.Hash.Equals(hash));

            if (item == null)
            {
                return;
            }
            transactionMsgs.Remove(item);
            Storage.Instance.Delete(DbDomains.TxContainer, item.Transaction.Hash);
            var spentUtxos = item.Transaction.Inputs.Select(x => $"{x.OutputTransactionHash}_{x.OutputIndex}");

            foreach (var spentUtxo in spentUtxos)
            {
                if (spentUtxoSets.ContainsKey(spentUtxo))
                {
                    spentUtxoSets[spentUtxo].Remove(hash);
                }
            }

            long useAmount = 0;
            long addAmount = 0;

            var outputs  = item.Transaction.Outputs;
            var accounts = AccountDac.Default.GetAccountBook();

            var spentSets = UtxoSetDac.Default.Get(spentUtxos);

            if (spentSets != null)
            {
                useAmount = spentSets.Where(x => accounts.Contains(x.Account)).Sum(x => x.Amount);
            }

            var addUtxos = outputs.Where(x => accounts.Contains(DtoExtensions.GetAccountByLockScript(x.LockScript)));

            addAmount = addUtxos.Sum(x => x.Amount);

            UseBalanceInPool -= useAmount;
            AddBalanceInPool -= addAmount;

            var delKeys = PaymentDac.Default.Payment_Mem.Where(x => hash.Equals(x.txId)).Select(x => x.ToString());

            PaymentDac.Default.Del(delKeys);
        }