Exemplo n.º 1
0
        public async Task <(long, string)> CreateAndPublishNewBlockWithTxs(IRpcClient rpcClient, long?blockHeightToStartFork, Transaction[] transactions, bool noPublish = false, bool bigBlock = false)
        {
            string blockHash   = null;
            long   blockHeight = await rpcClient.GetBlockCountAsync();

            if (blockHeight == 0)
            {
                var blockStream = await rpcClient.GetBlockAsStreamAsync(await rpcClient.GetBestBlockHashAsync());

                var firstBlock = HelperTools.ParseByteStreamToBlock(blockStream);
                rpcClientFactoryMock.AddKnownBlock(blockHeight, firstBlock.ToBytes());
                PublishBlockHashToEventBus(await rpcClient.GetBestBlockHashAsync());
            }
            PubKey pubKey = new Key().PubKey;

            if (transactions != null)
            {
                NBitcoin.Block lastBlock;
                if (blockHeightToStartFork.HasValue)
                {
                    lastBlock = NBitcoin.Block.Load(await rpcClient.GetBlockByHeightAsBytesAsync(blockHeightToStartFork.Value), Network.Main);
                }
                else
                {
                    lastBlock = HelperTools.ParseByteStreamToBlock(await rpcClient.GetBlockAsStreamAsync(await rpcClient.GetBestBlockHashAsync()));
                }
                var block = lastBlock.CreateNextBlockWithCoinbase(pubKey, new Money(50, MoneyUnit.MilliBTC), new ConsensusFactory());
                foreach (var transaction in transactions)
                {
                    block.AddTransaction(transaction);
                }

                if (!bigBlock)
                {
                    block.Check();
                }

                if (blockHeightToStartFork.HasValue)
                {
                    blockHeight = blockHeightToStartFork.Value;
                }

                if (bigBlock)
                {
                    rpcClientFactoryMock.AddBigKnownBlock(++blockHeight, block);
                }
                else
                {
                    rpcClientFactoryMock.AddKnownBlock(++blockHeight, block.ToBytes());
                }


                blockHash = block.GetHash().ToString();
                if (!noPublish)
                {
                    PublishBlockHashToEventBus(block.GetHash().ToString());
                }
            }

            return(blockHeight, blockHash);
        }