Ejemplo n.º 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);
 }
Ejemplo n.º 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();
		}
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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;
		}
Ejemplo n.º 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);
        }
Ejemplo n.º 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());
		}
Ejemplo n.º 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());
		}
Ejemplo n.º 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");
            }
        }
Ejemplo n.º 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());
        }
Ejemplo n.º 10
0
 public static MtpData?GetMtpData(this NBitcoin.BlockHeader header) => ((BlockHeader)header).MtpData;
Ejemplo n.º 11
0
 public static void SetReserved2(this NBitcoin.BlockHeader header, uint256 value) =>
 ((BlockHeader)header).Reserved2 = value;
Ejemplo n.º 12
0
 public static void SetMtpVersion(this NBitcoin.BlockHeader header, int value) =>
 ((BlockHeader)header).MtpVersion = value;
Ejemplo n.º 13
0
 public static Block CreateBlock(BlockHeader header, Network network)
 {
     return(CreateBlock(header, network.Consensus.ConsensusFactory));
 }
Ejemplo n.º 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());
 }
Ejemplo n.º 15
0
        /// <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));
        }
Ejemplo n.º 16
0
 public Block(BlockHeader blockHeader)
 {
     SetNull();
     header = blockHeader;
 }
 public ConcurrentChain(BlockHeader genesis)
 {
     SetTip(new ChainedBlock(genesis, 0));
 }
Ejemplo n.º 18
0
 public Chain(BlockHeader genesis, ObjectStream<ChainChange> changes)
     : this(genesis, 0, changes)
 {
 }
Ejemplo n.º 19
0
 public Chain(BlockHeader genesis)
     : this(genesis, 0, null)
 {
 }
Ejemplo n.º 20
0
 public static uint256 GetMtpHash(this NBitcoin.BlockHeader header) => ((BlockHeader)header).MtpHash;
Ejemplo n.º 21
0
		public ConcurrentChain(BlockHeader genesis)
		{
			SetTip(new ChainedBlock(genesis, 0));
		}
Ejemplo n.º 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();
     }
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 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;
        }
Ejemplo n.º 26
0
		public bool SetTip(BlockHeader header)
		{
			ChainedBlock chainedHeader;
			return TrySetTip(header, out chainedHeader);
		}
Ejemplo n.º 27
0
 public static int GetMtpVersion(this NBitcoin.BlockHeader header) => ((BlockHeader)header).MtpVersion;
Ejemplo n.º 28
0
 public static uint256 GetReserved2(this NBitcoin.BlockHeader header) => ((BlockHeader)header).Reserved2;
Ejemplo n.º 29
0
 public static bool IsMtp(this NBitcoin.BlockHeader header) => ((BlockHeader)header).IsMtp;
Ejemplo n.º 30
0
 public void WriteBlockHeader(BlockHeader header)
 {
     Block block = new Block(header);
     _HeaderStore.Put(block);
 }
Ejemplo n.º 31
0
 public static void SetMtpData(this NBitcoin.BlockHeader header, MtpData?value) =>
 ((BlockHeader)header).MtpData = value;
Ejemplo n.º 32
0
 private ChainBlockHeader CreateChainChange(int height, BlockHeader block)
 {
     return new ChainBlockHeader()
                {
                    Height = height,
                    Header = block,
                    BlockId = block.GetHash()
                };
 }
Ejemplo n.º 33
0
 public static void SetMtpHash(this NBitcoin.BlockHeader header, uint256 value) =>
 ((BlockHeader)header).MtpHash = value;
Ejemplo n.º 34
0
 public ChainedBlock(BlockHeader header, int height)
 {
     nHeight = height;
     //this.nDataPos = pos;
     this.header = header;
     this.phashBlock = header.GetHash();
 }
Ejemplo n.º 35
0
 public Block(BlockHeader blockHeader)
 {
     this.SetNull();
     this.header = blockHeader;
 }