public void Will_not_serve_receipts_requests_above_512() { StatusMessageSerializer statusMessageSerializer = new StatusMessageSerializer(); ReceiptsMessageSerializer receiptMessageSerializer = new ReceiptsMessageSerializer(MainnetSpecProvider.Instance); GetReceiptsMessageSerializer getReceiptMessageSerializer = new GetReceiptsMessageSerializer(); MessageSerializationService serializationService = new MessageSerializationService(); serializationService.Register(statusMessageSerializer); serializationService.Register(receiptMessageSerializer); serializationService.Register(getReceiptMessageSerializer); ISyncServer syncServer = Substitute.For <ISyncServer>(); Eth63ProtocolHandler protocolHandler = new Eth63ProtocolHandler( _session, serializationService, Substitute.For <INodeStatsManager>(), syncServer, Substitute.For <ITxPool>(), LimboLogs.Instance); StatusMessage statusMessage = new StatusMessage(); Packet statusPacket = new Packet("eth", Eth62MessageCode.Status, statusMessageSerializer.Serialize(statusMessage)); GetReceiptsMessage getReceiptsMessage = new GetReceiptsMessage( Enumerable.Repeat(Keccak.Zero, 513).ToArray()); Packet getReceiptsPacket = new Packet("eth", Eth63MessageCode.GetReceipts, getReceiptMessageSerializer.Serialize(getReceiptsMessage)); protocolHandler.HandleMessage(statusPacket); Assert.Throws <EthSyncException>(() => protocolHandler.HandleMessage(getReceiptsPacket)); }
public void Roundtrip_mainnet_sample() { byte[] bytes = Bytes.FromHexString("f9012ef9012bf90128a08ccc6709a5df7acef07f97c5681356b6c37cfac15b554aff68e986f57116df2e825208b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0"); ReceiptsMessageSerializer serializer = new ReceiptsMessageSerializer(); ReceiptsMessage message = serializer.Deserialize(bytes); byte[] serialized = serializer.Serialize(message); Assert.AreEqual(bytes, serialized); }
private static void Test(TxReceipt[][] txReceipts) { ReceiptsMessage message = new ReceiptsMessage(txReceipts); ReceiptsMessageSerializer serializer = new ReceiptsMessageSerializer(RopstenSpecProvider.Instance); var serialized = serializer.Serialize(message); ReceiptsMessage deserialized = serializer.Deserialize(serialized); if (txReceipts == null) { Assert.AreEqual(0, deserialized.TxReceipts.Length); } else { Assert.AreEqual(txReceipts.Length, deserialized.TxReceipts.Length, "length"); for (int i = 0; i < txReceipts.Length; i++) { if (txReceipts[i] == null) { Assert.IsNull(deserialized.TxReceipts[i], $"receipts[{i}]"); } else { for (int j = 0; j < txReceipts[i].Length; j++) { if (txReceipts[i][j] == null) { Assert.IsNull(deserialized.TxReceipts[i][j], $"receipts[{i}][{j}]"); } else { Assert.AreEqual(txReceipts[i][j].TxType, deserialized.TxReceipts[i][j].TxType, $"receipts[{i}][{j}].TxType"); Assert.AreEqual(txReceipts[i][j].Bloom, deserialized.TxReceipts[i][j].Bloom, $"receipts[{i}][{j}].Bloom"); Assert.Null(deserialized.TxReceipts[i][j].Error, $"receipts[{i}][{j}].Error"); Assert.AreEqual(0, deserialized.TxReceipts[i][j].Index, $"receipts[{i}][{j}].Index"); Assert.AreEqual(txReceipts[i][j].Logs.Length, deserialized.TxReceipts[i][j].Logs.Length, $"receipts[{i}][{j}].Logs.Length"); Assert.Null(deserialized.TxReceipts[i][j].Recipient, $"receipts[{i}][{j}].Recipient"); Assert.Null(deserialized.TxReceipts[i][j].Sender, $"receipts[{i}][{j}].Sender"); Assert.Null(deserialized.TxReceipts[i][j].BlockHash, $"receipts[{i}][{j}].BlockHash"); Assert.AreEqual(0L, deserialized.TxReceipts[i][j].BlockNumber, $"receipts[{i}][{j}].BlockNumber"); Assert.Null(deserialized.TxReceipts[i][j].ContractAddress, $"receipts[{i}][{j}].ContractAddress"); Assert.AreEqual(0L, deserialized.TxReceipts[i][j].GasUsed, $"receipts[{i}][{j}].GasUsed"); Assert.AreEqual(txReceipts[i][j].BlockNumber < RopstenSpecProvider.ByzantiumBlockNumber ? 0 : txReceipts[i][j].StatusCode, deserialized.TxReceipts[i][j].StatusCode, $"receipts[{i}][{j}].StatusCode"); Assert.AreEqual(txReceipts[i][j].GasUsedTotal, deserialized.TxReceipts[i][j].GasUsedTotal, $"receipts[{i}][{j}].GasUsedTotal"); Assert.AreEqual(txReceipts[i][j].BlockNumber < RopstenSpecProvider.ByzantiumBlockNumber ? txReceipts[i][j].PostTransactionState : null, deserialized.TxReceipts[i][j].PostTransactionState, $"receipts[{i}][{j}].PostTransactionState"); } } } } } }
private static void Test(TransactionReceipt[][] receipts) { ReceiptsMessage message = new ReceiptsMessage(receipts); ReceiptsMessageSerializer serializer = new ReceiptsMessageSerializer(); var serialized = serializer.Serialize(message); ReceiptsMessage deserialized = serializer.Deserialize(serialized); if (receipts == null) { Assert.AreEqual(0, deserialized.Receipts.Length); } else { Assert.AreEqual(receipts.Length, deserialized.Receipts.Length, "length"); for (int i = 0; i < receipts.Length; i++) { if (receipts[i] == null) { Assert.IsNull(deserialized.Receipts[i], $"receipts[{i}]"); } else { for (int j = 0; j < receipts[i].Length; j++) { if (receipts[i][j] == null) { Assert.IsNull(deserialized.Receipts[i][j], $"receipts[{i}][{j}]"); } else { Assert.AreEqual(receipts[i][j].Bloom, deserialized.Receipts[i][j].Bloom, $"receipts[{i}][{j}].Bloom"); Assert.Null(deserialized.Receipts[i][j].Error, $"receipts[{i}][{j}].Error"); Assert.AreEqual(0, deserialized.Receipts[i][j].Index, $"receipts[{i}][{j}].Index"); Assert.AreEqual(receipts[i][j].Logs.Length, deserialized.Receipts[i][j].Logs.Length, $"receipts[{i}][{j}].Logs.Length"); Assert.Null(deserialized.Receipts[i][j].Recipient, $"receipts[{i}][{j}].Recipient"); Assert.Null(deserialized.Receipts[i][j].Sender, $"receipts[{i}][{j}].Sender"); Assert.Null(deserialized.Receipts[i][j].BlockHash, $"receipts[{i}][{j}].BlockHash"); Assert.AreEqual(UInt256.Zero, deserialized.Receipts[i][j].BlockNumber, $"receipts[{i}][{j}].BlockNumber"); Assert.Null(deserialized.Receipts[i][j].ContractAddress, $"receipts[{i}][{j}].ContractAddress"); Assert.AreEqual(0L, deserialized.Receipts[i][j].GasUsed, $"receipts[{i}][{j}].GasUsed"); Assert.AreEqual(receipts[i][j].StatusCode, deserialized.Receipts[i][j].StatusCode, $"receipts[{i}][{j}].StatusCode"); Assert.AreEqual(receipts[i][j].GasUsedTotal, deserialized.Receipts[i][j].GasUsedTotal, $"receipts[{i}][{j}].GasUsedTotal"); Assert.AreEqual(receipts[i][j].PostTransactionState, deserialized.Receipts[i][j].PostTransactionState, $"receipts[{i}][{j}].PostTransactionState"); } } } } } }
public void RoundTrip() { TxReceipt[][] data = { new[] { Build.A.Receipt.WithAllFieldsFilled.TestObject, Build.A.Receipt.WithAllFieldsFilled.WithBlockNumber(0).TestObject }, new[] { Build.A.Receipt.WithAllFieldsFilled.TestObject, Build.A.Receipt.WithAllFieldsFilled.TestObject }, new[] { Build.A.Receipt.WithAllFieldsFilled.WithTxType(TxType.AccessList).TestObject, Build.A.Receipt.WithAllFieldsFilled.TestObject } }; Network.P2P.Subprotocols.Eth.V63.ReceiptsMessage ethMessage = new Network.P2P.Subprotocols.Eth.V63.ReceiptsMessage(data); ReceiptsMessage receiptsMessage = new ReceiptsMessage(ethMessage, 1, 2000); ReceiptsMessageSerializer serializer = new ReceiptsMessageSerializer(RopstenSpecProvider.Instance); // Eth.ReceiptsMessageSerializer intentionally excludes fields when deserializing. // I think it's probably best to not copy the test logic checking for this here. byte[] bytes = serializer.Serialize(receiptsMessage); ReceiptsMessage deserialized = serializer.Deserialize(bytes); Assert.AreEqual(receiptsMessage.RequestId, deserialized.RequestId, "RequestId"); Assert.AreEqual(receiptsMessage.BufferValue, deserialized.BufferValue, "BufferValue"); }
public async Task Can_request_and_handle_receipts() { Context ctx = new Context(); StatusMessageSerializer statusMessageSerializer = new StatusMessageSerializer(); ReceiptsMessageSerializer receiptMessageSerializer = new ReceiptsMessageSerializer(MainnetSpecProvider.Instance); MessageSerializationService serializationService = new MessageSerializationService(); serializationService.Register(statusMessageSerializer); serializationService.Register(receiptMessageSerializer); Eth63ProtocolHandler protocolHandler = new Eth63ProtocolHandler( ctx.Session, serializationService, Substitute.For <INodeStatsManager>(), Substitute.For <ISyncServer>(), Substitute.For <ITxPool>(), LimboLogs.Instance); var receipts = Enumerable.Repeat( Enumerable.Repeat(Build.A.Receipt.WithAllFieldsFilled.TestObject, 100).ToArray(), 1000).ToArray(); // TxReceipt[1000][100] StatusMessage statusMessage = new StatusMessage(); Packet statusPacket = new Packet("eth", Eth62MessageCode.Status, statusMessageSerializer.Serialize(statusMessage)); ReceiptsMessage receiptsMsg = new ReceiptsMessage(receipts); Packet receiptsPacket = new Packet("eth", Eth63MessageCode.Receipts, receiptMessageSerializer.Serialize(receiptsMsg)); protocolHandler.HandleMessage(statusPacket); Task <TxReceipt[][]> task = protocolHandler.GetReceipts( Enumerable.Repeat(Keccak.Zero, 1000).ToArray(), CancellationToken.None); protocolHandler.HandleMessage(receiptsPacket); var result = await task; result.Should().HaveCount(1000); }
public void Will_not_send_messages_larger_than_2MB() { Context ctx = new Context(); StatusMessageSerializer statusMessageSerializer = new StatusMessageSerializer(); ReceiptsMessageSerializer receiptMessageSerializer = new ReceiptsMessageSerializer(MainnetSpecProvider.Instance); GetReceiptsMessageSerializer getReceiptMessageSerializer = new GetReceiptsMessageSerializer(); MessageSerializationService serializationService = new MessageSerializationService(); serializationService.Register(statusMessageSerializer); serializationService.Register(receiptMessageSerializer); serializationService.Register(getReceiptMessageSerializer); ISyncServer syncServer = Substitute.For <ISyncServer>(); Eth63ProtocolHandler protocolHandler = new Eth63ProtocolHandler( ctx.Session, serializationService, Substitute.For <INodeStatsManager>(), syncServer, Substitute.For <ITxPool>(), LimboLogs.Instance); syncServer.GetReceipts(Arg.Any <Keccak>()).Returns( Enumerable.Repeat(Build.A.Receipt.WithAllFieldsFilled.TestObject, 512).ToArray()); StatusMessage statusMessage = new StatusMessage(); Packet statusPacket = new Packet("eth", Eth62MessageCode.Status, statusMessageSerializer.Serialize(statusMessage)); GetReceiptsMessage getReceiptsMessage = new GetReceiptsMessage( Enumerable.Repeat(Keccak.Zero, 512).ToArray()); Packet getReceiptsPacket = new Packet("eth", Eth63MessageCode.GetReceipts, getReceiptMessageSerializer.Serialize(getReceiptsMessage)); protocolHandler.HandleMessage(statusPacket); protocolHandler.HandleMessage(getReceiptsPacket); ctx.Session.Received().DeliverMessage(Arg.Is <ReceiptsMessage>(r => r.TxReceipts.Length == 14)); }
public void Deserialize_empty() { ReceiptsMessageSerializer serializer = new ReceiptsMessageSerializer(RopstenSpecProvider.Instance); serializer.Deserialize(new byte[0]).TxReceipts.Should().HaveCount(0); }