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

Include withdrawals in payload id computation #5587

Merged
merged 3 commits into from
Apr 18, 2023
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
29 changes: 29 additions & 0 deletions src/Nethermind/Nethermind.Consensus/Producers/PayloadAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System.Buffers.Binary;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Core.Specs;
using Nethermind.State.Proofs;
using Nethermind.Trie;

namespace Nethermind.Consensus.Producers;

Expand Down Expand Up @@ -46,6 +51,30 @@ public string ToString(string indentation)

public static class PayloadAttributesExtensions
{
public static string ComputePayloadId(this PayloadAttributes payloadAttributes, BlockHeader parentHeader)
{
bool hasWithdrawals = payloadAttributes.Withdrawals is not null;
Span<byte> inputSpan = stackalloc byte[32 + 32 + 32 + 20 + (hasWithdrawals ? 32 : 0)];

parentHeader.Hash!.Bytes.CopyTo(inputSpan[..32]);
BinaryPrimitives.WriteUInt64BigEndian(inputSpan.Slice(56, 8), payloadAttributes.Timestamp);
payloadAttributes.PrevRandao.Bytes.CopyTo(inputSpan.Slice(64, 32));
payloadAttributes.SuggestedFeeRecipient.Bytes.CopyTo(inputSpan.Slice(96, 20));

if (hasWithdrawals)
{
var withdrawalsRootHash = payloadAttributes.Withdrawals.Count == 0
? PatriciaTree.EmptyTreeHash
: new WithdrawalTrie(payloadAttributes.Withdrawals).RootHash;

withdrawalsRootHash.Bytes.CopyTo(inputSpan[116..]);
}

ValueKeccak inputHash = ValueKeccak.Compute(inputSpan);

return inputHash.BytesAsSpan[..8].ToHexString(true);
}

public static int GetVersion(this PayloadAttributes executionPayload) =>
executionPayload.Withdrawals is null ? 1 : 2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public partial class EngineModuleTests
"0x1c53bdbf457025f80c6971a9cf50986974eed02f0a9acaeeb49cafef10efd133",
"0x6d8a107ccab7a785de89f58db49064ee091df5d2b6306fe55db666e75a0e9f68",
"0x03e662d795ee2234c492ca4a08de03b1d7e3e0297af81a76582e16de75cdfc51",
"0x6454408c425ddd96")]
"0x5009aaf2fdcd600e")]
public virtual async Task Should_process_block_as_expected_V2(string latestValidHash, string blockHash, string stateRoot, string payloadId)
{
using MergeTestBlockchain chain = await CreateShanghaiBlockChain(new MergeConfig { TerminalTotalDifficulty = "0" });
Expand Down Expand Up @@ -721,6 +721,23 @@ public void Should_print_payload_attributes_as_expected()
$"PayloadAttributes {{Timestamp: {attrs.Timestamp}, PrevRandao: {attrs.PrevRandao}, SuggestedFeeRecipient: {attrs.SuggestedFeeRecipient}, Withdrawals count: {attrs.Withdrawals.Count}}}");
}

[TestCaseSource(nameof(PayloadIdTestCases))]
public void Should_compute_payload_id_with_withdrawals((IList<Withdrawal>? Withdrawals, string PayloadId) input)
{
var blockHeader = Build.A.BlockHeader.TestObject;
var payloadAttributes = new PayloadAttributes
{
PrevRandao = Keccak.Zero,
SuggestedFeeRecipient = Address.Zero,
Timestamp = 0,
Withdrawals = input.Withdrawals
};

var payloadId = payloadAttributes.ComputePayloadId(blockHeader);

payloadId.Should().Be(input.PayloadId);
}

private static async Task<GetPayloadV2Result> BuildAndGetPayloadResultV2(
IEngineRpcModule rpc, MergeTestBlockchain chain, PayloadAttributes payloadAttributes)
{
Expand Down Expand Up @@ -864,4 +881,14 @@ bool isValid
Enumerable.Repeat(result, 5)
);
}

protected static IEnumerable<(
IList<Withdrawal>? Withdrawals,
string payloadId
)> PayloadIdTestCases()
{
yield return (null, "0xd0666188af58eb6f");
yield return (Array.Empty<Withdrawal>(), "0xb5f89745e4cfaec0");
yield return (new[] { Build.A.Withdrawal.TestObject }, "0x0628b8a79468163e");
}
}
Loading