public async Task GetMultipleBlocksAtOnce() { var privateKey = new PrivateKey(); BlockChain <DumbAction> chainA = _blockchains[0]; BlockChain <DumbAction> chainB = _blockchains[1]; Swarm <DumbAction> swarmA = _swarms[0]; Swarm <DumbAction> swarmB = new Swarm <DumbAction>( chainB, privateKey, 1, host: IPAddress.Loopback.ToString()); Block <DumbAction> genesis = chainA.MineBlock(_fx1.Address1); chainB.Append(genesis); // chainA and chainB shares genesis block. chainA.MineBlock(_fx1.Address1); chainA.MineBlock(_fx1.Address1); try { await StartAsync(swarmA); await StartAsync(swarmB); var peer = swarmA.AsPeer; await swarmB.AddPeersAsync(new[] { peer }); IEnumerable <HashDigest <SHA256> > hashes = await swarmB.GetBlockHashesAsync( peer, new BlockLocator(new[] { genesis.Hash }), null); var netMQAddress = $"tcp://{peer.EndPoint.Host}:{peer.EndPoint.Port}"; using (var socket = new DealerSocket(netMQAddress)) { var request = new GetBlocks(hashes, 2); await socket.SendMultipartMessageAsync( request.ToNetMQMessage(privateKey)); NetMQMessage response = await socket.ReceiveMultipartMessageAsync(); Message parsedMessage = Message.Parse(response, true); Libplanet.Net.Messages.Blocks blockMessage = (Libplanet.Net.Messages.Blocks)parsedMessage; Assert.Equal(2, blockMessage.Payloads.Count); response = await socket.ReceiveMultipartMessageAsync(); parsedMessage = Message.Parse(response, true); blockMessage = (Libplanet.Net.Messages.Blocks)parsedMessage; Assert.Single(blockMessage.Payloads); } } finally { await Task.WhenAll( swarmA.StopAsync(), swarmB.StopAsync()); } }
private void TransferBlocks(GetBlocks getData) { string identityHex = ByteUtil.Hex(getData.Identity); _logger.Verbose( $"Preparing a {nameof(Blocks)} message to reply to {{Identity}}...", identityHex ); var blocks = new List <byte[]>(); List <HashDigest <SHA256> > hashes = getData.BlockHashes.ToList(); int i = 1; int total = hashes.Count; const string logMsg = "Fetching a block #{Index}/{Total} ({Hash}) to include to " + "a reply to {Identity}..."; foreach (HashDigest <SHA256> hash in hashes) { _logger.Verbose(logMsg, i, total, hash, identityHex); if (_store.ContainsBlock(hash)) { Block <T> block = _store.GetBlock <T>(hash); byte[] payload = block.Serialize(); blocks.Add(payload); } if (blocks.Count == getData.ChunkSize) { var response = new Messages.Blocks(blocks) { Identity = getData.Identity, }; _logger.Verbose( "Enqueuing a blocks reply (...{Index}/{Total})...", i, total ); Transport.ReplyMessage(response); blocks.Clear(); } i++; } if (blocks.Any()) { var response = new Messages.Blocks(blocks) { Identity = getData.Identity, }; _logger.Verbose( "Enqueuing a blocks reply (...{Index}/{Total}) to {Identity}...", total, total, identityHex ); Transport.ReplyMessage(response); } _logger.Debug("Blocks were transferred to {Identity}.", identityHex); }
private async Task TransferBlocksAsync(GetBlocks getData) { string identityHex = ByteUtil.Hex(getData.Identity); _logger.Verbose( "Preparing a {MessageType} message to reply to {Identity}...", nameof(Messages.Blocks), identityHex ); var blocks = new List <byte[]>(); List <BlockHash> hashes = getData.BlockHashes.ToList(); int i = 1; int total = hashes.Count; const string logMsg = "Fetching block {Index}/{Total} {Hash} to include in " + "a reply to {Identity}..."; foreach (BlockHash hash in hashes) { _logger.Verbose(logMsg, i, total, hash, identityHex); if (_store.GetBlock <T>(BlockChain.Policy.GetHashAlgorithm, hash) is { } block) { byte[] payload = Codec.Encode(block.MarshalBlock()); blocks.Add(payload); } if (blocks.Count == getData.ChunkSize) { var response = new Messages.Blocks(blocks) { Identity = getData.Identity, }; _logger.Verbose( "Enqueuing a blocks reply (...{Index}/{Total})...", i, total ); await Transport.ReplyMessageAsync(response, default); blocks.Clear(); } i++; } if (blocks.Any()) { var response = new Messages.Blocks(blocks) { Identity = getData.Identity, }; _logger.Verbose( "Enqueuing a blocks reply (...{Index}/{Total}) to {Identity}...", total, total, identityHex ); await Transport.ReplyMessageAsync(response, default); } _logger.Debug("Blocks were transferred to {Identity}.", identityHex); }