public async Task <uint256> InsertMerkleProof() { var blockStream = await RpcClient.GetBlockAsStreamAsync(await RpcClient.GetBestBlockHashAsync()); var firstBlock = HelperTools.ParseByteStreamToBlock(blockStream); var block = firstBlock.CreateNextBlockWithCoinbase(firstBlock.Transactions.First().Outputs.First().ScriptPubKey.GetDestinationPublicKeys().First(), new Money(50, MoneyUnit.MilliBTC), new ConsensusFactory()); var firstBlockHash = firstBlock.GetHash(); var tx = Transaction.Parse(Tx1Hex, Network.Main); block.AddTransaction(tx); tx = Transaction.Parse(Tx2Hex, Network.Main); block.AddTransaction(tx); tx = Transaction.Parse(Tx3Hex, Network.Main); block.AddTransaction(tx); tx = Transaction.Parse(Tx4Hex, Network.Main); block.AddTransaction(tx); tx = Transaction.Parse(Tx5Hex, Network.Main); block.AddTransaction(tx); rpcClientFactoryMock.AddKnownBlock((await RpcClient.GetBlockCountAsync()) + 1, block.ToBytes()); var node = NodeRepository.GetNodes().First(); var rpcClient = rpcClientFactoryMock.Create(node.Host, node.Port, node.Username, node.Password); PublishBlockHashToEventBus(await rpcClient.GetBestBlockHashAsync()); return(firstBlockHash); }
public async Task <(NBitcoin.Block, string)> MineNextBlockAsync(IEnumerable <NBitcoin.Transaction> transactions, bool throwOnError = true, string parentBlockHash = null) { if (string.IsNullOrEmpty(parentBlockHash)) { parentBlockHash = await rpcClient0.GetBestBlockHashAsync(); } var parentBlockBytes = await rpcClient0.GetBlockAsBytesAsync(parentBlockHash); var parentBlock = NBitcoin.Block.Load(parentBlockBytes, Network.RegTest); var parentBlockHeight = (await rpcClient0.GetBlockHeaderAsync(parentBlockHash)).Height; return(await MineNextBlockAsync(transactions, throwOnError, parentBlock, parentBlockHeight)); }
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); }