public static ChainIndexer Load(this ChainIndexer chainIndexer, BitcoinStream stream)
        {
            stream.ConsensusFactory = chainIndexer.Network.Consensus.ConsensusFactory;

            try
            {
                int height = 0;
                while (true)
                {
                    uint256.MutableUint256 id = null;
                    stream.ReadWrite <uint256.MutableUint256>(ref id);
                    BlockHeader header = null;
                    stream.ReadWrite(ref header);
                    if (height == 0)
                    {
                        Assert.True(header.GetHash() == chainIndexer.Tip.HashBlock);
                    }
                    else if (chainIndexer.Tip.HashBlock == header.HashPrevBlock && !(header.IsNull && header.Nonce == 0))
                    {
                        chainIndexer.Add(new ChainedHeader(header, id.Value, chainIndexer.Tip));
                    }
                    else
                    {
                        break;
                    }

                    height++;
                }
            }
            catch (EndOfStreamException)
            {
            }

            return(chainIndexer);
        }
Ejemplo n.º 2
0
        public void spanUintSerializationTests()
        {
            var v = new uint256(RandomUtils.GetBytes(32));

            Assert.Equal(v, new uint256(v.ToBytes()));
            AssertEx.CollectionEquals(v.ToBytes(), v.AsBitcoinSerializable().ToBytes());
            uint256.MutableUint256 mutable = new uint256.MutableUint256();
            mutable.ReadWrite(v.ToBytes(), Network.Main);
            Assert.Equal(v, mutable.Value);
        }
Ejemplo n.º 3
0
        public void Load()
        {
            if (File.Exists(this.context.Config.File("trxindex.dat")))
            {
                lock (LockObj)
                {
                    if (Table.Any())
                    {
                        return;
                    }

                    var bytes = File.ReadAllBytes(this.context.Config.File("trxindex.dat"));
                    using (var mem = new MemoryStream(bytes))
                    {
                        var stream = new BitcoinStream(mem, false);

                        try
                        {
                            this.Table.Clear();
                            while (true)
                            {
                                uint256.MutableUint256 key   = null;
                                uint256.MutableUint256 value = null;
                                stream.ReadWrite(ref key);
                                stream.ReadWrite(ref value);
                                if (!this.Table.TryAdd(key.Value, value.Value))
                                {
                                    throw new InvalidDataException();                                     // this is wrong it means duplicates where persisted to disk
                                }
                            }
                        }
                        catch (EndOfStreamException)
                        {
                        }
                    }

                    if (this.Table.Any())
                    {
                        this.LastBlockId = this.Table.Last().Value;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public async Task <uint256> GetBestBlockAsync()
        {
            uint256.MutableUint256 block = await this.Index.GetAsync <uint256.MutableUint256>("B").ConfigureAwait(false);

            return(block == null ? uint256.Zero : block.Value);
        }