Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix eth_call mix_hash and beneficiary #4873

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/Nethermind/Nethermind.Facade.Test/BlockchainBridgeTests.cs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

Expand Down Expand Up @@ -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(
Expand All @@ -163,6 +162,36 @@ public void Call_uses_valid_block_number()
Arg.Any<ITxTracer>());
}

[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<BlockHeader>(bh => bh.MixHash == TestItem.KeccakA),
Arg.Any<ITxTracer>());
}

[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<BlockHeader>(bh => bh.Beneficiary == TestItem.AddressB),
Arg.Any<ITxTracer>());
}

[TestCase(7)]
[TestCase(0)]
public void Bridge_head_is_correct(long headNumber)
Expand Down
48 changes: 29 additions & 19 deletions src/Nethermind/Nethermind.Facade/BlockchainBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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));
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -257,20 +254,33 @@ 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<byte>());

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<byte>())
{
BaseFeePerGas = BaseFeeCalculator.Calculate(blockHeader, _specProvider.GetSpec(blockHeader.Number + 1)),
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
}
: new(
blockHeader.ParentHash!,
blockHeader.UnclesHash!,
blockHeader.Beneficiary!,
blockHeader.Difficulty,
blockHeader.Number,
blockHeader.GasLimit,
blockHeader.Timestamp,
blockHeader.ExtraData)
{
BaseFeePerGas = blockHeader.BaseFeePerGas,
};

callHeader.MixHash = blockHeader.MixHash;
transaction.Hash = transaction.CalculateHash();
transactionProcessor.CallAndRestore(transaction, callHeader, tracer);
}
Expand Down
20 changes: 11 additions & 9 deletions src/Nethermind/Nethermind.Logging/PathUtils.cs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Ancient_bodies_and_receipts_are_reported_correctly(bool setBarriers)
}

SyncReport syncReport = new(pool, Substitute.For<INodeStatsManager>(), selector, syncConfig, Substitute.For<IPivot>(), 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)
Expand Down