Example #1
0
        private async Task <(bool, TransactionManager, byte[])> ValidateTransactions(byte[] root, Block block, TransactionManager transact)
        {
            if (block.TransactionsDictionary.Count > 0)
            {
                Log.Debug("Validating transactions...");
                slCount++;
                var foundMineTransaction = false;

                Transaction miningTransaction = null;

                foreach (var tx in block.Transactions)
                {
                    if (CancelToken.IsCancellationRequested)
                    {
                        return(false, null, null);
                    }

                    root = CryptoHelper.Hash(Base58.Bitcoin.Encode(new Span <Byte>(root)) + tx.ToString());

                    if (!foundMineTransaction &&
                        String.IsNullOrEmpty(tx.Wallet) &&
                        String.IsNullOrEmpty(tx.Signature) &&
                        tx.Outputs.Count == 1 &&
                        tx.Outputs[0].Size == 100)
                    {
                        foundMineTransaction = true;
                        miningTransaction    = tx;
                        tx.Finish();
                        transact = transact.ProcessTransaction(tx);
                    }
                    else
                    {
                        if (!await Pool.Contains(tx))
                        {
                            Log.Error("Block with invalid transaction.");
                            Log.Error(JsonConvert.SerializeObject(tx));
                            Pool.TransactionPool.VerifyTX(tx);

                            var bk   = CurrentBlock;
                            var prev = this;

                            while (bk != null && !bk.Transactions.Any(t => t.HashStr == tx.HashStr))
                            {
                                Log.Error("NOT FOUND ON BLOCK " + bk.Header.Depth);
                                prev = prev.Previous;
                                if (prev != null)
                                {
                                    bk = prev.CurrentBlock;
                                }
                                else
                                {
                                    bk = null;
                                }
                            }

                            if (bk == null)
                            {
                                Log.Error("Not found on chain");
                            }
                            else
                            {
                                Log.Error("Found on chain");
                            }
                            return(false, null, null);
                        }


                        if (transact.CanProcess(tx))
                        {
                            transact = transact.ProcessTransaction(tx);
                        }
                        else
                        {
                            Log.Error("Block with Transaction without balance");
                            Log.Error("HasBalance " + transact.HasBalance(tx.Wallet, tx.Asset, tx.Outputs.Sum(o => o.Size)));
                            Log.Error("Wallet " + tx.Wallet);
                            Log.Error("Output " + tx.Outputs.Sum(o => o.Size));
                            return(false, null, null);
                        }
                    }
                }
            }

            Log.Debug("Transactions validated");
            return(true, transact, root);
        }