From 7927b04333330398ad019cc2b917e6f4f1fe72eb Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Mon, 7 Nov 2022 17:22:15 +0100 Subject: [PATCH 1/2] fix eth_call mix_hash and beneficiary --- .../BlockchainBridgeTests.cs | 39 +++++++++++++-- .../Nethermind.Facade/BlockchainBridge.cs | 48 +++++++++++-------- .../Nethermind.Logging/PathUtils.cs | 20 ++++---- .../SyncReportTest.cs | 2 +- 4 files changed, 75 insertions(+), 34 deletions(-) diff --git a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs index ccb4fce6950..f07bd5aa544 100644 --- a/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs +++ b/src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs @@ -1,16 +1,16 @@ // Copyright (c) 2021 Demerzel Solutions Limited // This file is part of the Nethermind library. -// +// // The Nethermind library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. -// +// // The Nethermind library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. -// +// // You should have received a copy of the GNU Lesser General Public License // along with the Nethermind. If not, see . @@ -153,8 +153,7 @@ public void Call_uses_valid_block_number() _timestamper.UtcNow = DateTime.MinValue; _timestamper.Add(TimeSpan.FromDays(123)); BlockHeader header = Build.A.BlockHeader.WithNumber(10).TestObject; - Transaction tx = new(); - tx.GasLimit = Transaction.BaseTxGasCost; + Transaction tx = new() { GasLimit = Transaction.BaseTxGasCost }; _blockchainBridge.Call(header, tx, CancellationToken.None); _transactionProcessor.Received().CallAndRestore( @@ -163,6 +162,36 @@ public void Call_uses_valid_block_number() Arg.Any()); } + [Test] + public void Call_uses_valid_mix_hash() + { + _timestamper.UtcNow = DateTime.MinValue; + _timestamper.Add(TimeSpan.FromDays(123)); + BlockHeader header = Build.A.BlockHeader.WithMixHash(TestItem.KeccakA).TestObject; + Transaction tx = new() { GasLimit = Transaction.BaseTxGasCost }; + + _blockchainBridge.Call(header, tx, CancellationToken.None); + _transactionProcessor.Received().CallAndRestore( + tx, + Arg.Is(bh => bh.MixHash == TestItem.KeccakA), + Arg.Any()); + } + + [Test] + public void Call_uses_valid_beneficiary() + { + _timestamper.UtcNow = DateTime.MinValue; + _timestamper.Add(TimeSpan.FromDays(123)); + BlockHeader header = Build.A.BlockHeader.WithBeneficiary(TestItem.AddressB).TestObject; + Transaction tx = new() { GasLimit = Transaction.BaseTxGasCost }; + + _blockchainBridge.Call(header, tx, CancellationToken.None); + _transactionProcessor.Received().CallAndRestore( + tx, + Arg.Is(bh => bh.Beneficiary == TestItem.AddressB), + Arg.Any()); + } + [TestCase(7)] [TestCase(0)] public void Bridge_head_is_correct(long headNumber) diff --git a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs index 42a4286a9c6..4c0ae6d7de4 100644 --- a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs @@ -164,7 +164,7 @@ public CallOutput(byte[] outputData, long gasSpent, string error, bool inputErro public CallOutput Call(BlockHeader header, Transaction tx, CancellationToken cancellationToken) { CallOutputTracer callOutputTracer = new(); - (bool Success, string Error) tryCallResult = TryCallAndRestore(header, header.Timestamp, tx, false, + (bool Success, string Error) tryCallResult = TryCallAndRestore(header, tx, false, callOutputTracer.WithCancellation(cancellationToken)); return new CallOutput { @@ -182,7 +182,6 @@ public CallOutput EstimateGas(BlockHeader header, Transaction tx, CancellationTo EstimateGasTracer estimateGasTracer = new(); (bool Success, string Error) tryCallResult = TryCallAndRestore( header, - Math.Max(header.Timestamp + 1, _timestamper.UnixTime.Seconds), tx, true, estimateGasTracer.WithCancellation(cancellationToken)); @@ -206,7 +205,7 @@ public CallOutput CreateAccessList(BlockHeader header, Transaction tx, Cancellat tx.GetRecipient(tx.IsContractCreation ? _processingEnv.StateReader.GetNonce(header.StateRoot, tx.SenderAddress) : 0)) : new(); - (bool Success, string Error) tryCallResult = TryCallAndRestore(header, header.Timestamp, tx, false, + (bool Success, string Error) tryCallResult = TryCallAndRestore(header, tx, false, new CompositeTxTracer(callOutputTracer, accessTxTracer).WithCancellation(cancellationToken)); return new CallOutput @@ -221,14 +220,13 @@ public CallOutput CreateAccessList(BlockHeader header, Transaction tx, Cancellat private (bool Success, string Error) TryCallAndRestore( BlockHeader blockHeader, - ulong timestamp, Transaction transaction, bool treatBlockHeaderAsParentBlock, ITxTracer tracer) { try { - CallAndRestore(blockHeader, timestamp, transaction, treatBlockHeaderAsParentBlock, tracer); + CallAndRestore(blockHeader, transaction, treatBlockHeaderAsParentBlock, tracer); return (true, string.Empty); } catch (InsufficientBalanceException ex) @@ -239,7 +237,6 @@ public CallOutput CreateAccessList(BlockHeader header, Transaction tx, Cancellat private void CallAndRestore( BlockHeader blockHeader, - ulong timestamp, Transaction transaction, bool treatBlockHeaderAsParentBlock, ITxTracer tracer) @@ -257,19 +254,32 @@ private void CallAndRestore( transaction.Nonce = GetNonce(stateRoot, transaction.SenderAddress); } - BlockHeader callHeader = new( - blockHeader.Hash!, - Keccak.OfAnEmptySequenceRlp, - Address.Zero, - 0, - treatBlockHeaderAsParentBlock ? blockHeader.Number + 1 : blockHeader.Number, - blockHeader.GasLimit, - timestamp, - Array.Empty()); - - callHeader.BaseFeePerGas = treatBlockHeaderAsParentBlock - ? BaseFeeCalculator.Calculate(blockHeader, _specProvider.GetSpec(callHeader.Number)) - : blockHeader.BaseFeePerGas; + BlockHeader callHeader = treatBlockHeaderAsParentBlock + ? new( + blockHeader.Hash!, + Keccak.OfAnEmptySequenceRlp, + Address.Zero, + UInt256.Zero, + blockHeader.Number + 1, + blockHeader.GasLimit, + Math.Max(blockHeader.Timestamp + 1, _timestamper.UnixTime.Seconds), + Array.Empty()) + { + BaseFeePerGas = BaseFeeCalculator.Calculate(blockHeader, _specProvider.GetSpec(blockHeader.Number + 1)), + } + : new( + blockHeader.ParentHash!, + blockHeader.UnclesHash!, + blockHeader.Beneficiary!, + blockHeader.Difficulty, + blockHeader.Number, + blockHeader.GasLimit, + blockHeader.Timestamp, + blockHeader.ExtraData) + { + BaseFeePerGas = blockHeader.BaseFeePerGas, + MixHash = blockHeader.MixHash + }; transaction.Hash = transaction.CalculateHash(); transactionProcessor.CallAndRestore(transaction, callHeader, tracer); diff --git a/src/Nethermind/Nethermind.Logging/PathUtils.cs b/src/Nethermind/Nethermind.Logging/PathUtils.cs index d229c114b9a..a6ddac6e0c4 100644 --- a/src/Nethermind/Nethermind.Logging/PathUtils.cs +++ b/src/Nethermind/Nethermind.Logging/PathUtils.cs @@ -1,16 +1,16 @@ // Copyright (c) 2021 Demerzel Solutions Limited // This file is part of the Nethermind library. -// +// // The Nethermind library is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. -// +// // The Nethermind library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. -// +// // You should have received a copy of the GNU Lesser General Public License // along with the Nethermind. If not, see . @@ -28,15 +28,17 @@ public static class PathUtils static PathUtils() { - var process = Process.GetCurrentProcess(); - if (process.ProcessName.StartsWith("dotnet", StringComparison.InvariantCultureIgnoreCase)) + Process process = Process.GetCurrentProcess(); + if (process.ProcessName.StartsWith("dotnet", StringComparison.InvariantCultureIgnoreCase) + || process.ProcessName.Equals("ReSharperTestRunner", StringComparison.InvariantCultureIgnoreCase)) { ExecutingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - return; } - - ExecutingDirectory = Path.GetDirectoryName(process.MainModule.FileName); - Console.WriteLine($"Resolved executing directory as {ExecutingDirectory}."); + else + { + ExecutingDirectory = Path.GetDirectoryName(process.MainModule.FileName); + Console.WriteLine($"Resolved executing directory as {ExecutingDirectory}."); + } } public static string GetApplicationResourcePath(this string resourcePath, string overridePrefixPath = null) diff --git a/src/Nethermind/Nethermind.Synchronization.Test/SyncReportTest.cs b/src/Nethermind/Nethermind.Synchronization.Test/SyncReportTest.cs index fa05e95079f..8e938c65de1 100644 --- a/src/Nethermind/Nethermind.Synchronization.Test/SyncReportTest.cs +++ b/src/Nethermind/Nethermind.Synchronization.Test/SyncReportTest.cs @@ -96,7 +96,7 @@ public void Ancient_bodies_and_receipts_are_reported_correctly(bool setBarriers) } SyncReport syncReport = new(pool, Substitute.For(), selector, syncConfig, Substitute.For(), logManager, timerFactory); - selector.Current.Returns((ci) => SyncMode.FastHeaders | SyncMode.FastBodies | SyncMode.FastReceipts); + selector.Current.Returns(_ => SyncMode.FastHeaders | SyncMode.FastBodies | SyncMode.FastReceipts); timer.Elapsed += Raise.Event(); if (setBarriers) From 42fa9bb0d9aa34c0e604851eadd272cbd0c84683 Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Tue, 8 Nov 2022 11:13:45 +0100 Subject: [PATCH 2/2] always set MixHash --- src/Nethermind/Nethermind.Facade/BlockchainBridge.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs index 4c0ae6d7de4..2e75b47d921 100644 --- a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs @@ -278,9 +278,9 @@ private void CallAndRestore( blockHeader.ExtraData) { BaseFeePerGas = blockHeader.BaseFeePerGas, - MixHash = blockHeader.MixHash }; + callHeader.MixHash = blockHeader.MixHash; transaction.Hash = transaction.CalculateHash(); transactionProcessor.CallAndRestore(transaction, callHeader, tracer); }