public static (ChainedHeader ChainedHeader, Block Block) AppendBlock(Network network, ChainedHeader previous, ChainIndexer chainIndexer) { ChainedHeader last = null; uint nonce = RandomUtils.GetUInt32(); Block block = network.CreateBlock(); block.AddTransaction(network.CreateTransaction()); block.UpdateMerkleRoot(); block.Header.HashPrevBlock = previous == null ? chainIndexer.Tip.HashBlock : previous.HashBlock; block.Header.Nonce = nonce; if (!chainIndexer.TrySetTip(block.Header, out last)) { throw new InvalidOperationException("Previous not existing"); } return(last, block); }
public static Block AppendTransactionInNewBlockToChain(ChainIndexer chainIndexer, Transaction transaction) { ChainedHeader last = null; uint nonce = RandomUtils.GetUInt32(); Block block = chainIndexer.Network.Consensus.ConsensusFactory.CreateBlock(); block.AddTransaction(transaction); block.UpdateMerkleRoot(); block.Header.HashPrevBlock = chainIndexer.Tip.HashBlock; block.Header.Nonce = nonce; if (!chainIndexer.TrySetTip(block.Header, out last)) { throw new InvalidOperationException("Previous not existing"); } return(block); }
/// <summary> /// Sets the tip of this chain based upon another block header. /// </summary> /// <param name="header">The block header to set to tip.</param> /// <returns>Whether the tip was set successfully.</returns> public static bool SetTip(this ChainIndexer chainIndexer, BlockHeader header) { ChainedHeader chainedHeader; return(chainIndexer.TrySetTip(header, out chainedHeader)); }