private void OnGetHeaders(RemoteNode remoteNode, GetBlocksPayload payload) { // if in comparison mode, synchronize all work before returning current headers if (this.Type == LocalClientType.ComparisonToolTestNet) { this.blockchainDaemon.WaitForFullUpdate(); } var currentBlockchainLocal = this.blockchainDaemon.CurrentBlockchain; var blockHeaders = new List <BlockHeader>(currentBlockchainLocal.BlockCount); foreach (var chainedBlock in currentBlockchainLocal.BlockList) { BlockHeader blockHeader; if (this.blockchainDaemon.CacheContext.BlockHeaderCache.TryGetValue(chainedBlock.BlockHash, out blockHeader)) { blockHeaders.Add(blockHeader); } else { Debugger.Break(); Debug.WriteLine("Couldn't generate getheaders response"); return; } } var payloadStream = new MemoryStream(); using (var payloadWriter = new BinaryWriter(payloadStream)) { payloadWriter.WriteVarInt((UInt64)blockHeaders.Count); foreach (var blockHeader in blockHeaders) { NetworkEncoder.EncodeBlockHeader(payloadStream, blockHeader); payloadWriter.WriteVarInt(0); } } remoteNode.Sender.SendMessageAsync(Messaging.ConstructMessage("headers", payloadStream.ToArray())).Wait(); }
public void TestWireDecodeBlockHeader() { var actual = NetworkEncoder.EncodeBlockHeader(NetworkEncoder.DecodeBlockHeader(BLOCK_HEADER_1_BYTES.ToArray().ToMemoryStream())); CollectionAssert.AreEqual(BLOCK_HEADER_1_BYTES.ToList(), actual.ToList()); }
public void TestWireEncodeBlockHeader() { var actual = NetworkEncoder.EncodeBlockHeader(BLOCK_HEADER_1); CollectionAssert.AreEqual(BLOCK_HEADER_1_BYTES.ToList(), actual.ToList()); }