internal static ListMessage FromReader(Address address, BinaryReader reader) { var kind = (RequestKind)reader.ReadByte(); var result = new ListMessage(address, kind); if (kind.HasFlag(RequestKind.Peers)) { var peerCount = reader.ReadByte(); var peers = new Endpoint[peerCount]; for (int i = 0; i < peerCount; i++) { peers[i] = Endpoint.Unserialize(reader); } result.SetPeers(peers); } if (kind.HasFlag(RequestKind.Chains)) { var chainCount = reader.ReadUInt16(); var chains = new ChainInfo[chainCount]; for (int i = 0; i < chainCount; i++) { chains[i] = ChainInfo.Unserialize(reader); } result.SetChains(chains); } if (kind.HasFlag(RequestKind.Mempool)) { var txCount = reader.ReadUInt16(); var txs = new string[txCount]; for (int i = 0; i < txCount; i++) { txs[i] = reader.ReadVarString(); } result.SetMempool(txs); } if (kind.HasFlag(RequestKind.Blocks)) { var chainCount = reader.ReadUInt16(); while (chainCount > 0) { var chainName = reader.ReadVarString(); var blockCount = (int)reader.ReadVarInt(); var blocks = new Block[blockCount]; var transactions = new Dictionary <Hash, Transaction>(); for (int i = 0; i < blockCount; i++) { var bytes = reader.ReadByteArray(); var block = Block.Unserialize(bytes); blocks[i] = block; foreach (var txHash in block.TransactionHashes) { bytes = reader.ReadByteArray(); var tx = Transaction.Unserialize(bytes); transactions[txHash] = tx; } } result.AddBlockRange(chainName, blocks, transactions); chainCount--; } } return(result); }
internal static ListMessage FromReader(Address address, BinaryReader reader) { var kind = (RequestKind)reader.ReadByte(); var result = new ListMessage(address, kind); if (kind.HasFlag(RequestKind.Peers)) { var peerCount = reader.ReadByte(); var peers = new Endpoint[peerCount]; for (int i = 0; i < peerCount; i++) { peers[i] = Endpoint.Unserialize(reader); } result.SetPeers(peers); } if (kind.HasFlag(RequestKind.Chains)) { var chainCount = reader.ReadUInt16(); var chains = new ChainInfo[chainCount]; for (int i = 0; i < chainCount; i++) { chains[i] = ChainInfo.Unserialize(reader); } result.SetChains(chains); } if (kind.HasFlag(RequestKind.Mempool)) { var txCount = reader.ReadUInt16(); var txs = new string[txCount]; for (int i = 0; i < txCount; i++) { txs[i] = reader.ReadVarString(); } result.SetMempool(txs); } if (kind.HasFlag(RequestKind.Blocks)) { var chainCount = reader.ReadUInt16(); while (chainCount > 0) { var chainName = reader.ReadVarString(); var blockHeight = reader.ReadUInt32(); var blockCount = reader.ReadUInt16(); var rawBlocks = new string[blockCount]; for (int i = 0; i < blockCount; i++) { rawBlocks[i] = reader.ReadVarString(); } result.AddBlockRange(chainName, blockHeight, rawBlocks); chainCount--; } } return(result); }