public Chain(BlockHeader blockHeader, int height, ObjectStream<ChainChange> changes) { if(changes == null) changes = new StreamObjectStream<ChainChange>(); AssertEmpty(changes); _Changes = changes; Initialize(blockHeader, height); }
public ChainedBlock(BlockHeader header, int height) { if (header == null) throw new ArgumentNullException("header"); nHeight = height; //this.nDataPos = pos; this.header = header; this.phashBlock = header.GetHash(); }
public unsafe void BlockDisconnected(NBitcoin.BlockHeader blockHeader, uint height) { var blockHeaderBytes = blockHeader.ToBytes(); fixed(byte *b = blockHeaderBytes) { Interop.block_disconnected(b, (UIntPtr)blockHeaderBytes.Length, height, _handle); } }
public bool TrySetTip(BlockHeader header, out ChainedBlock chainedHeader) { if (header == null) throw new ArgumentNullException("header"); chainedHeader = null; var prev = GetBlock(header.HashPrevBlock); if(prev == null) return false; chainedHeader = new ChainedBlock(header, header.GetHash(), GetBlock(header.HashPrevBlock)); SetTip(chainedHeader); return true; }
private BlockHeader[] CreateHeaders(int size) { var array = new NBitcoin.BlockHeader[this.size]; for (int i = 0; i < array.Length; i++) { #pragma warning disable CS0618 // Type or member is obsolete array[i] = new BlockHeader(); #pragma warning restore CS0618 // Type or member is obsolete } return(array); }
public MerkleBlock(Block block, uint256[] txIds) { header = block.Header; List<bool> vMatch = new List<bool>(); List<uint256> vHashes = new List<uint256>(); for(int i = 0 ; i < block.Transactions.Count ; i++) { var hash = block.Transactions[i].GetHash(); vHashes.Add(hash); vMatch.Add(txIds.Contains(hash)); } _PartialMerkleTree = new PartialMerkleTree(vHashes.ToArray(), vMatch.ToArray()); }
// Create from a CBlock, filtering transactions according to filter // Note that this will call IsRelevantAndUpdate on the filter for each transaction, // thus the filter will likely be modified. public MerkleBlock(Block block, BloomFilter filter) { header = block.Header; List<bool> vMatch = new List<bool>(); List<uint256> vHashes = new List<uint256>(); for(uint i = 0 ; i < block.Transactions.Count ; i++) { uint256 hash = block.Transactions[(int)i].GetHash(); vMatch.Add(filter.IsRelevantAndUpdate(block.Transactions[(int)i])); vHashes.Add(hash); } _PartialMerkleTree = new PartialMerkleTree(vHashes.ToArray(), vMatch.ToArray()); }
// (memory only) Sequencial id assigned to distinguish order in which blocks are received. //uint nSequenceId; public ChainedBlock(BlockHeader header,uint256 headerHash, ChainedBlock previous) { if(previous != null) { nHeight = previous.Height + 1; } this.pprev = previous; //this.nDataPos = pos; this.header = header; this.phashBlock = headerHash ?? header.GetHash(); if(previous == null) { if(header.HashPrevBlock != 0) throw new ArgumentException("Only the genesis block can have no previous block"); } else { if(previous.HashBlock != header.HashPrevBlock) throw new ArgumentException("The previous block has not the expected hash"); } }
// Create from a CBlock, filtering transactions according to filter // Note that this will call IsRelevantAndUpdate on the filter for each transaction, // thus the filter will likely be modified. public MerkleBlock(Block block, BloomFilter filter) { header = block.Header; List<bool> vMatch = new List<bool>(); List<uint256> vHashes = new List<uint256>(); for(uint i = 0 ; i < block.Transactions.Length ; i++) { uint256 hash = block.Transactions[i].GetHash(); if(filter.IsRelevantAndUpdate(block.Transactions[i])) { vMatch.Add(true); vMatchedTxn.Add(Tuple.Create(i, hash)); } else vMatch.Add(false); vHashes.Add(hash); } txn = new PartialMerkleTree(vHashes.ToArray(), vMatch.ToArray()); }
public static MtpData?GetMtpData(this NBitcoin.BlockHeader header) => ((BlockHeader)header).MtpData;
public static void SetReserved2(this NBitcoin.BlockHeader header, uint256 value) => ((BlockHeader)header).Reserved2 = value;
public static void SetMtpVersion(this NBitcoin.BlockHeader header, int value) => ((BlockHeader)header).MtpVersion = value;
public static Block CreateBlock(BlockHeader header, Network network) { return(CreateBlock(header, network.Consensus.ConsensusFactory)); }
public void Initialize(BlockHeader header, int height) { if(Initialized) throw new InvalidOperationException("Already initialized"); var change = new ChainChange() { Add = true, BlockHeader = header, HeightOrBackstep = (uint)height }; ProcessAndRecord(change, header.GetHash()); }
/// <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 bool SetTip(BlockHeader header) { ChainedBlock chainedHeader; return(this.TrySetTip(header, out chainedHeader)); }
public Block(BlockHeader blockHeader) { SetNull(); header = blockHeader; }
public ConcurrentChain(BlockHeader genesis) { SetTip(new ChainedBlock(genesis, 0)); }
public Chain(BlockHeader genesis, ObjectStream<ChainChange> changes) : this(genesis, 0, changes) { }
public Chain(BlockHeader genesis) : this(genesis, 0, null) { }
public static uint256 GetMtpHash(this NBitcoin.BlockHeader header) => ((BlockHeader)header).MtpHash;
public Chain(BlockHeader blockHeader, int height, ObjectStream<ChainChange> changes) { if(changes == null) changes = new StreamObjectStream<ChainChange>(); _Changes = changes; changes.Rewind(); if(changes.EOF) { Initialize(blockHeader, height); } else { var first = changes.ReadNext(); if(first.BlockHeader.GetHash() != blockHeader.GetHash()) { throw new InvalidOperationException("The first block of this stream is different than the expected one at height " + height); } if(first.HeightOrBackstep != height) { throw new InvalidOperationException("The first block of this stream has height " + first.HeightOrBackstep + " but expected is " + height); } changes.Rewind(); Process(); } }
public ConcurrentChain(BlockHeader genesisHeader, Network network = null) // TODO: Remove the null default { this.network = network ?? Network.Main; this.SetTip(new ChainedBlock(genesisHeader, genesisHeader.GetHash(this.network.NetworkOptions), 0)); }
public ChainedBlock GetOrAdd(BlockHeader header) { AssertInitialized(); var headerHash = header.GetHash(); ChainedBlock pindex = GetBlock(headerHash, true); if(pindex != null) return pindex; ChainedBlock previous = GetBlock(header.HashPrevBlock, true); if(previous == null) { return null; } pindex = new ChainedBlock(header, headerHash, previous); if(previous.HashBlock == Tip.HashBlock) { var change = new ChainChange() { Add = true, BlockHeader = pindex.Header, HeightOrBackstep = (uint)pindex.Height }; ProcessAndRecord(change, pindex.HashBlock); } else { if(pindex.Height <= Tip.Height) { offchainIndex.Add(pindex.HashBlock, pindex); } else { var fork = FindFork(pindex.EnumerateToGenesis().Select(c => c.HashBlock)); var change = new ChainChange() { Add = false, HeightOrBackstep = (uint)(Tip.Height - fork.Height) }; ProcessAndRecord(change, null); foreach(var block in pindex.EnumerateToGenesis().TakeWhile(s => s != fork).Reverse()) { change = new ChainChange() { Add = true, BlockHeader = block.Header, HeightOrBackstep = (uint)block.Height }; ProcessAndRecord(change, block.HashBlock); } } } return pindex; }
public bool SetTip(BlockHeader header) { ChainedBlock chainedHeader; return TrySetTip(header, out chainedHeader); }
public static int GetMtpVersion(this NBitcoin.BlockHeader header) => ((BlockHeader)header).MtpVersion;
public static uint256 GetReserved2(this NBitcoin.BlockHeader header) => ((BlockHeader)header).Reserved2;
public static bool IsMtp(this NBitcoin.BlockHeader header) => ((BlockHeader)header).IsMtp;
public void WriteBlockHeader(BlockHeader header) { Block block = new Block(header); _HeaderStore.Put(block); }
public static void SetMtpData(this NBitcoin.BlockHeader header, MtpData?value) => ((BlockHeader)header).MtpData = value;
private ChainBlockHeader CreateChainChange(int height, BlockHeader block) { return new ChainBlockHeader() { Height = height, Header = block, BlockId = block.GetHash() }; }
public static void SetMtpHash(this NBitcoin.BlockHeader header, uint256 value) => ((BlockHeader)header).MtpHash = value;
public ChainedBlock(BlockHeader header, int height) { nHeight = height; //this.nDataPos = pos; this.header = header; this.phashBlock = header.GetHash(); }
public Block(BlockHeader blockHeader) { this.SetNull(); this.header = blockHeader; }