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); }
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); }
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; } } } }
public async Task <uint256> GetBestBlockAsync() { uint256.MutableUint256 block = await this.Index.GetAsync <uint256.MutableUint256>("B").ConfigureAwait(false); return(block == null ? uint256.Zero : block.Value); }