Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Numerics;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.State;

Expand Down Expand Up @@ -34,15 +35,16 @@ public void ProcessWithdrawals(Block block, IReleaseSpec spec)
{
foreach (var withdrawal in block.Withdrawals)
{
if (_logger.IsTrace) _logger.Trace($" {(BigInteger)withdrawal.Amount / (BigInteger)Unit.Ether:N3}{Unit.EthSymbol} to account {withdrawal.Address}");
if (_logger.IsTrace) _logger.Trace($" {withdrawal.AmountInGwei} GWei to account {withdrawal.Address}");

// Consensus clients are using Gwei for withdrawals amount. We need to convert it to Wei before applying state changes https://github.com/ethereum/execution-apis/pull/354
if (_stateProvider.AccountExists(withdrawal.Address))
{
_stateProvider.AddToBalance(withdrawal.Address, withdrawal.Amount, spec);
_stateProvider.AddToBalance(withdrawal.Address, withdrawal.AmountInWei, spec);
}
else
{
_stateProvider.CreateAccount(withdrawal.Address, withdrawal.Amount);
_stateProvider.CreateAccount(withdrawal.Address, withdrawal.AmountInWei);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Nethermind.Core.Test.Builders
public static partial class TestItem
{
public static Random Random { get; } = new();
private static AccountDecoder _accountDecoder = new();
private static readonly AccountDecoder _accountDecoder = new();

static TestItem()
{
Expand Down Expand Up @@ -90,12 +90,12 @@ public static Keccak KeccakFromNumber(int i)
public static Address AddressE = PublicKeyE.Address;
public static Address AddressF = PublicKeyF.Address;

public static Withdrawal WithdrawalA_1Eth = new() { Address = AddressA, Index = 1, ValidatorIndex = 2001, Amount = 1.Ether() };
public static Withdrawal WithdrawalB_2Eth = new() { Address = AddressB, Index = 2, ValidatorIndex = 2002, Amount = 2.Ether() };
public static Withdrawal WithdrawalC_3Eth = new() { Address = AddressC, Index = 3, ValidatorIndex = 2003, Amount = 3.Ether() };
public static Withdrawal WithdrawalD_4Eth = new() { Address = AddressD, Index = 4, ValidatorIndex = 2004, Amount = 4.Ether() };
public static Withdrawal WithdrawalE_5Eth = new() { Address = AddressE, Index = 5, ValidatorIndex = 2005, Amount = 5.Ether() };
public static Withdrawal WithdrawalF_6Eth = new() { Address = AddressF, Index = 6, ValidatorIndex = 2006, Amount = 6.Ether() };
public static Withdrawal WithdrawalA_1Eth = new() { Address = AddressA, Index = 1, ValidatorIndex = 2001, AmountInGwei = 1_000_000_000 };
public static Withdrawal WithdrawalB_2Eth = new() { Address = AddressB, Index = 2, ValidatorIndex = 2002, AmountInGwei = 2_000_000_000 };
public static Withdrawal WithdrawalC_3Eth = new() { Address = AddressC, Index = 3, ValidatorIndex = 2003, AmountInGwei = 3_000_000_000 };
public static Withdrawal WithdrawalD_4Eth = new() { Address = AddressD, Index = 4, ValidatorIndex = 2004, AmountInGwei = 4_000_000_000 };
public static Withdrawal WithdrawalE_5Eth = new() { Address = AddressE, Index = 5, ValidatorIndex = 2005, AmountInGwei = 5_000_000_000 };
public static Withdrawal WithdrawalF_6Eth = new() { Address = AddressF, Index = 6, ValidatorIndex = 2006, AmountInGwei = 6_000_000_000 };

public static IPEndPoint IPEndPointA = IPEndPoint.Parse("10.0.0.1");
public static IPEndPoint IPEndPointB = IPEndPoint.Parse("10.0.0.2");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Int256;

namespace Nethermind.Core.Test.Builders;

public class WithdrawalBuilder : BuilderBase<Withdrawal>
{
public WithdrawalBuilder() => TestObject = new();

public WithdrawalBuilder WithAmount(UInt256 amount)
public WithdrawalBuilder WithAmount(ulong amount)
{
TestObject.Amount = amount;
TestObject.AmountInGwei = amount;

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,67 @@ public class WithdrawalDecoderTests
[Test]
public void Should_encode()
{
var withdrawal = new Withdrawal
Withdrawal withdrawal = new()
{
Index = 1,
ValidatorIndex = 2,
Address = Address.SystemUser,
Amount = 3
AmountInGwei = 3
};
var rlp = Rlp.Encode(withdrawal).Bytes;
byte[] rlp = Rlp.Encode(withdrawal).Bytes;

rlp.ToHexString().Should().BeEquivalentTo("d8010294fffffffffffffffffffffffffffffffffffffffe03");
}

[Test]
public void Should_decode()
{
var withdrawal = new Withdrawal
Withdrawal withdrawal = new()
{
Index = 1,
ValidatorIndex = 2,
Address = new Address("0x773f86fb098bb19f228f441a7715daa13d10a751"),
Amount = 3
AmountInGwei = 3
};
var rlp = Rlp.Encode(withdrawal).Bytes;
var decoded = Rlp.Decode<Withdrawal>(rlp);
byte[] rlp = Rlp.Encode(withdrawal).Bytes;
Withdrawal decoded = Rlp.Decode<Withdrawal>(rlp);

decoded.Should().BeEquivalentTo(withdrawal);
}

[Test]
public void Should_decode_with_ValueDecoderContext()
{
var withdrawal = new Withdrawal
Withdrawal withdrawal = new()
{
Index = long.MaxValue,
ValidatorIndex = int.MaxValue,
Address = new Address("0x773f86fb098bb19f228f441a7715daa13d10a751"),
Amount = UInt256.UInt128MaxValue
AmountInGwei = ulong.MaxValue
};
var stream = new RlpStream(1024);
var codec = new WithdrawalDecoder();
RlpStream stream = new(1024);
WithdrawalDecoder codec = new();

codec.Encode(stream, withdrawal);

var decoderContext = new Rlp.ValueDecoderContext(stream.Data.AsSpan());
var decoded = codec.Decode(ref decoderContext);
Rlp.ValueDecoderContext decoderContext = new(stream.Data.AsSpan());
Withdrawal? decoded = codec.Decode(ref decoderContext);

decoded.Should().BeEquivalentTo(withdrawal);
}

[Test]
public void Should_encode_same_for_Rlp_Encode_and_WithdrawalDecoder_Encode()
{
var withdrawal = new Withdrawal
Withdrawal withdrawal = new()
{
Index = long.MaxValue,
ValidatorIndex = int.MaxValue,
Address = new Address("0x7e24b8f924a82df020eef45c320deb224559f13e"),
Amount = UInt256.UInt128MaxValue
AmountInGwei = ulong.MaxValue
};
var rlp1 = new WithdrawalDecoder().Encode(withdrawal).Bytes;
var rlp2 = Rlp.Encode(withdrawal).Bytes;
byte[] rlp1 = new WithdrawalDecoder().Encode(withdrawal).Bytes;
byte[] rlp2 = Rlp.Encode(withdrawal).Bytes;

rlp1.Should().BeEquivalentTo(rlp2);
}
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Core/Nethermind.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ItemGroup>
<PackageReference Include="FastEnum" Version="1.8.0" />
<PackageReference Include="Nethermind.Numerics.Int256" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
<ProjectReference Include="..\Nethermind.Logging\Nethermind.Logging.csproj" />
<ProjectReference Include="..\Nethermind.Secp256k1\Nethermind.Secp256k1.csproj" />
Expand Down
12 changes: 9 additions & 3 deletions src/Nethermind/Nethermind.Core/Withdrawal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.Text;
using Nethermind.Core.Extensions;
using Nethermind.Int256;
using Newtonsoft.Json;

namespace Nethermind.Core;

Expand All @@ -26,16 +28,20 @@ public class Withdrawal
public Address Address { get; set; } = Address.Zero;

/// <summary>
/// Gets or sets the withdrawal amount in Wei.
/// Gets or sets the withdrawal amount in GWei.
/// </summary>
public UInt256 Amount { get; set; }
[JsonProperty(PropertyName = "amount")]
public ulong AmountInGwei { get; set; }

[JsonIgnore]
public UInt256 AmountInWei => AmountInGwei * 1.GWei();

public override string ToString() => ToString(string.Empty);

public string ToString(string indentation) => new StringBuilder($"{indentation}{nameof(Withdrawal)} {{")
.Append($"{nameof(Index)}: {Index}, ")
.Append($"{nameof(ValidatorIndex)}: {ValidatorIndex}, ")
.Append($"{nameof(Address)}: {Address}, ")
.Append($"{nameof(Amount)}: {Amount}}}")
.Append($"{nameof(AmountInGwei)}: {AmountInGwei}}}")
.ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public virtual async Task Should_process_block_as_expected_V2()
};
Withdrawal[] withdrawals = new[]
{
new Withdrawal { Index = 1, Amount = 3, Address = TestItem.AddressB, ValidatorIndex = 2 }
new Withdrawal { Index = 1, AmountInGwei = 3, Address = TestItem.AddressB, ValidatorIndex = 2 }
};
var payloadAttrs = new
{
Expand Down Expand Up @@ -79,7 +79,7 @@ public virtual async Task Should_process_block_as_expected_V2()
}
}));

Keccak blockHash = new("0xed14029504c440624047d5d0223899fb2c8abc4550464ac21e8f42ccdbb472d3");
Keccak blockHash = new("0x6d8a107ccab7a785de89f58db49064ee091df5d2b6306fe55db666e75a0e9f68");
Block block = new(
new(
startingHead,
Expand All @@ -98,7 +98,7 @@ public virtual async Task Should_process_block_as_expected_V2()
Hash = blockHash,
MixHash = prevRandao,
ReceiptsRoot = chain.BlockTree.Head!.ReceiptsRoot!,
StateRoot = new("0xde9a4fd5deef7860dc840612c5e960c942b76a9b2e710504de9bab8289156491"),
StateRoot = new("0x03e662d795ee2234c492ca4a08de03b1d7e3e0297af81a76582e16de75cdfc51"),
},
Array.Empty<Transaction>(),
Array.Empty<BlockHeader>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class WithdrawalDecoder : IRlpStreamDecoder<Withdrawal>, IRlpValueDecoder
Index = rlpStream.DecodeULong(),
ValidatorIndex = rlpStream.DecodeULong(),
Address = rlpStream.DecodeAddress(),
Amount = rlpStream.DecodeUInt256()
AmountInGwei = rlpStream.DecodeULong()
};
}

Expand All @@ -43,7 +43,7 @@ public class WithdrawalDecoder : IRlpStreamDecoder<Withdrawal>, IRlpValueDecoder
Index = decoderContext.DecodeULong(),
ValidatorIndex = decoderContext.DecodeULong(),
Address = decoderContext.DecodeAddress(),
Amount = decoderContext.DecodeUInt256()
AmountInGwei = decoderContext.DecodeULong()
};
}

Expand All @@ -61,7 +61,7 @@ public void Encode(RlpStream stream, Withdrawal? item, RlpBehaviors rlpBehaviors
stream.Encode(item.Index);
stream.Encode(item.ValidatorIndex);
stream.Encode(item.Address);
stream.Encode(item.Amount);
stream.Encode(item.AmountInGwei);
}

public Rlp Encode(Withdrawal? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
Expand All @@ -77,7 +77,7 @@ private static int GetContentLength(Withdrawal item) =>
Rlp.LengthOf(item.Index) +
Rlp.LengthOf(item.ValidatorIndex) +
Rlp.LengthOfAddressRlp +
Rlp.LengthOf(item.Amount);
Rlp.LengthOf(item.AmountInGwei);

public int GetLength(Withdrawal item, RlpBehaviors _) => Rlp.LengthOfSequence(GetContentLength(item));
}