示例#1
0
 public Chain(BlockHeader blockHeader, int height, ObjectStream<ChainChange> changes)
 {
     if(changes == null)
         changes = new StreamObjectStream<ChainChange>();
     AssertEmpty(changes);
     _Changes = changes;
     Initialize(blockHeader, height);
 }
示例#2
0
		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();
		}
示例#3
0
        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);
            }
        }
示例#4
0
		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;
		}
示例#5
0
        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);
        }
示例#6
0
		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());
		}
示例#7
0
		// 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());
		}
示例#8
0
        // (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");
            }
        }
示例#9
0
        // 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());
        }
示例#10
0
 public static MtpData?GetMtpData(this NBitcoin.BlockHeader header) => ((BlockHeader)header).MtpData;
示例#11
0
 public static void SetReserved2(this NBitcoin.BlockHeader header, uint256 value) =>
 ((BlockHeader)header).Reserved2 = value;
示例#12
0
 public static void SetMtpVersion(this NBitcoin.BlockHeader header, int value) =>
 ((BlockHeader)header).MtpVersion = value;
示例#13
0
文件: Block.cs 项目: sotblad/NBitcoin
 public static Block CreateBlock(BlockHeader header, Network network)
 {
     return(CreateBlock(header, network.Consensus.ConsensusFactory));
 }
示例#14
0
 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));
        }
示例#16
0
 public Block(BlockHeader blockHeader)
 {
     SetNull();
     header = blockHeader;
 }
 public ConcurrentChain(BlockHeader genesis)
 {
     SetTip(new ChainedBlock(genesis, 0));
 }
示例#18
0
 public Chain(BlockHeader genesis, ObjectStream<ChainChange> changes)
     : this(genesis, 0, changes)
 {
 }
示例#19
0
 public Chain(BlockHeader genesis)
     : this(genesis, 0, null)
 {
 }
示例#20
0
 public static uint256 GetMtpHash(this NBitcoin.BlockHeader header) => ((BlockHeader)header).MtpHash;
示例#21
0
		public ConcurrentChain(BlockHeader genesis)
		{
			SetTip(new ChainedBlock(genesis, 0));
		}
示例#22
0
 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();
     }
 }
示例#23
0
		public Block(BlockHeader blockHeader)
		{
			SetNull();
			header = blockHeader;
		}
 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));
 }
示例#25
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;
        }
示例#26
0
		public bool SetTip(BlockHeader header)
		{
			ChainedBlock chainedHeader;
			return TrySetTip(header, out chainedHeader);
		}
示例#27
0
 public static int GetMtpVersion(this NBitcoin.BlockHeader header) => ((BlockHeader)header).MtpVersion;
示例#28
0
 public static uint256 GetReserved2(this NBitcoin.BlockHeader header) => ((BlockHeader)header).Reserved2;
示例#29
0
 public static bool IsMtp(this NBitcoin.BlockHeader header) => ((BlockHeader)header).IsMtp;
示例#30
0
 public void WriteBlockHeader(BlockHeader header)
 {
     Block block = new Block(header);
     _HeaderStore.Put(block);
 }
示例#31
0
 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()
                };
 }
示例#33
0
 public static void SetMtpHash(this NBitcoin.BlockHeader header, uint256 value) =>
 ((BlockHeader)header).MtpHash = value;
示例#34
0
 public ChainedBlock(BlockHeader header, int height)
 {
     nHeight = height;
     //this.nDataPos = pos;
     this.header = header;
     this.phashBlock = header.GetHash();
 }
示例#35
0
 public Block(BlockHeader blockHeader)
 {
     this.SetNull();
     this.header = blockHeader;
 }