public void RoundTrip() { string rlp = "f90172820457f9016cf90169f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"; byte[] bytes = Bytes.FromHexString(rlp); var serializer63 = new Network.P2P.Subprotocols.Eth.V63.Messages.ReceiptsMessageSerializer(MainnetSpecProvider.Instance); ReceiptsMessageSerializer serializer = new(serializer63); ReceiptsMessage deserializedMessage = serializer.Deserialize(bytes); byte[] serialized = serializer.Serialize(deserializedMessage); Assert.AreEqual(bytes, serialized); Network.P2P.Subprotocols.Eth.V63.Messages.ReceiptsMessage ethMessage = deserializedMessage.EthMessage; TxReceipt txReceipt = ethMessage.TxReceipts[0][0]; txReceipt.StatusCode.Should().Be(0); txReceipt.GasUsedTotal.Should().Be(1); txReceipt.Bloom.Should().Be(Bloom.Empty); txReceipt.Logs[0].LoggersAddress.Should().BeEquivalentTo(new Address("0x0000000000000000000000000000000000000011")); txReceipt.Logs[0].Topics[0].Should().BeEquivalentTo(new Keccak("0x000000000000000000000000000000000000000000000000000000000000dead")); txReceipt.Logs[0].Topics[1].Should().BeEquivalentTo(new Keccak("0x000000000000000000000000000000000000000000000000000000000000beef")); txReceipt.Logs[0].Data.Should().BeEquivalentTo(Bytes.FromHexString("0x0100ff")); txReceipt.BlockNumber.Should().Be(0x0); txReceipt.TxHash.Should().BeNull(); txReceipt.BlockHash.Should().BeNull(); txReceipt.Index.Should().Be(0x0); ReceiptsMessage message = new(1111, ethMessage); SerializerTester.TestZero(serializer, message, rlp); }
public async Task <TxReceipt[][]> BuildReceiptsResponse(BlockHeader[] headers, BlockBody[] bodies, Response flags = Response.AllCorrect) { if (headers.Length != bodies.Length + 1) { throw new InvalidDataException("Headers and bodies response length should be the same for receipt tests"); } TxReceipt[][] receipts = new TxReceipt[headers.Length - 1][]; for (int i = 1; i < headers.Length; i++) { receipts[i - 1] = bodies[i - 1].Transactions .Select(t => Build.A.Receipt .WithStatusCode(StatusCode.Success) .WithGasUsed(10) .WithBloom(Bloom.Empty) .WithLogs(Build.A.LogEntry.WithAddress(t.SenderAddress).WithTopics(TestItem.KeccakA).TestObject) .TestObject) .ToArray(); headers[i].ReceiptsRoot = flags.HasFlag(Response.IncorrectReceiptRoot) ? Keccak.EmptyTreeHash : BlockExtensions.CalculateReceiptRoot(headers[i].Number, MainNetSpecProvider.Instance, receipts[i - 1]); } ReceiptsMessage message = new ReceiptsMessage(receipts); byte[] messageSerialized = _receiptsSerializer.Serialize(message); return(await Task.FromResult(_receiptsSerializer.Deserialize(messageSerialized).TxReceipts)); }
public void Sets_values_from_contructor_argument() { TransactionReceipt[][] data = { new[] { new TransactionReceipt(), new TransactionReceipt() }, new[] { new TransactionReceipt(), new TransactionReceipt() } }; ReceiptsMessage message = new ReceiptsMessage(data); Assert.AreSame(data, message.Receipts); }
public void Accepts_nulls_inside() { TransactionReceipt[][] data = { new[] { new TransactionReceipt(), new TransactionReceipt() }, null }; ReceiptsMessage message = new ReceiptsMessage(data); Assert.AreSame(data, message.Receipts); }
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(txReceipts); ReceiptsMessageSerializer serializer = new(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.Messages.ReceiptsMessage ethMessage = new(data); ReceiptsMessage receiptsMessage = new(ethMessage, 1, 2000); ReceiptsMessageSerializer serializer = new(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 Accepts_nulls_top_level() { ReceiptsMessage message = new ReceiptsMessage(null); Assert.AreEqual(0, message.Receipts.Length); }
public override void HandleMessage(ZeroPacket message) { int size = message.Content.ReadableBytes; switch (message.PacketType) { case Eth66MessageCode.GetBlockHeaders: GetBlockHeadersMessage getBlockHeadersMessage = Deserialize <GetBlockHeadersMessage>(message.Content); Metrics.Eth66GetBlockHeadersReceived++; ReportIn(getBlockHeadersMessage); Handle(getBlockHeadersMessage); break; case Eth66MessageCode.BlockHeaders: BlockHeadersMessage headersMsg = Deserialize <BlockHeadersMessage>(message.Content); Metrics.Eth66BlockHeadersReceived++; ReportIn(headersMsg); Handle(headersMsg.EthMessage, size); break; case Eth66MessageCode.GetBlockBodies: GetBlockBodiesMessage getBodiesMsg = Deserialize <GetBlockBodiesMessage>(message.Content); Metrics.Eth66GetBlockBodiesReceived++; ReportIn(getBodiesMsg); Handle(getBodiesMsg); break; case Eth66MessageCode.BlockBodies: BlockBodiesMessage bodiesMsg = Deserialize <BlockBodiesMessage>(message.Content); Metrics.Eth66BlockBodiesReceived++; ReportIn(bodiesMsg); HandleBodies(bodiesMsg.EthMessage, size); break; case Eth66MessageCode.GetPooledTransactions: GetPooledTransactionsMessage getPooledTxMsg = Deserialize <GetPooledTransactionsMessage>(message.Content); Metrics.Eth66GetPooledTransactionsReceived++; ReportIn(getPooledTxMsg); Handle(getPooledTxMsg); break; case Eth66MessageCode.PooledTransactions: PooledTransactionsMessage pooledTxMsg = Deserialize <PooledTransactionsMessage>(message.Content); Metrics.Eth66PooledTransactionsReceived++; ReportIn(pooledTxMsg); Handle(pooledTxMsg.EthMessage); break; case Eth66MessageCode.GetReceipts: GetReceiptsMessage getReceiptsMessage = Deserialize <GetReceiptsMessage>(message.Content); Metrics.Eth66GetReceiptsReceived++; ReportIn(getReceiptsMessage); Handle(getReceiptsMessage); break; case Eth66MessageCode.Receipts: ReceiptsMessage receiptsMessage = Deserialize <ReceiptsMessage>(message.Content); Metrics.Eth66ReceiptsReceived++; ReportIn(receiptsMessage); Handle(receiptsMessage.EthMessage, size); break; case Eth66MessageCode.GetNodeData: GetNodeDataMessage getNodeDataMessage = Deserialize <GetNodeDataMessage>(message.Content); Metrics.Eth66GetNodeDataReceived++; ReportIn(getNodeDataMessage); Handle(getNodeDataMessage); break; case Eth66MessageCode.NodeData: NodeDataMessage nodeDataMessage = Deserialize <NodeDataMessage>(message.Content); Metrics.Eth66NodeDataReceived++; ReportIn(nodeDataMessage); Handle(nodeDataMessage.EthMessage, size); break; default: base.HandleMessage(message); break; } }
public void To_string() { ReceiptsMessage statusMessage = new ReceiptsMessage(null); _ = statusMessage.ToString(); }