From de61f8a957c6a3771e19e486371f0d65073f43e9 Mon Sep 17 00:00:00 2001 From: MarekM25 Date: Fri, 13 Jan 2023 09:57:27 +0100 Subject: [PATCH 1/4] withdrawals wei -> gwei --- .../Withdrawals/WithdrawalProcessor.cs | 9 ++++-- .../Nethermind.Core.Test/Builders/TestItem.cs | 14 ++++----- .../Builders/WithdrawalBuilder.cs | 4 +-- .../Encoding/WithdrawalDecoderTests.cs | 30 +++++++++---------- src/Nethermind/Nethermind.Core/Withdrawal.cs | 3 +- .../EngineModuleTests.V2.cs | 4 +-- .../WithdrawalDecoder.cs | 4 +-- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs b/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs index 190ef8a9111..38706acdec2 100644 --- a/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs @@ -5,6 +5,7 @@ using System.Numerics; using Nethermind.Core; using Nethermind.Core.Specs; +using Nethermind.Int256; using Nethermind.Logging; using Nethermind.State; @@ -34,15 +35,17 @@ 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.Amount} 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 + UInt256 withdrawalAmountInWei = withdrawal.Amount * Unit.GWei; if (_stateProvider.AccountExists(withdrawal.Address)) { - _stateProvider.AddToBalance(withdrawal.Address, withdrawal.Amount, spec); + _stateProvider.AddToBalance(withdrawal.Address, withdrawalAmountInWei, spec); } else { - _stateProvider.CreateAccount(withdrawal.Address, withdrawal.Amount); + _stateProvider.CreateAccount(withdrawal.Address, withdrawalAmountInWei); } } } diff --git a/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs b/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs index 523e39be8d1..88756d2fa5d 100644 --- a/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs +++ b/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs @@ -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() { @@ -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, Amount = 1_000_000_000 }; + public static Withdrawal WithdrawalB_2Eth = new() { Address = AddressB, Index = 2, ValidatorIndex = 2002, Amount = 2_000_000_000 }; + public static Withdrawal WithdrawalC_3Eth = new() { Address = AddressC, Index = 3, ValidatorIndex = 2003, Amount = 3_000_000_000 }; + public static Withdrawal WithdrawalD_4Eth = new() { Address = AddressD, Index = 4, ValidatorIndex = 2004, Amount = 4_000_000_000 }; + public static Withdrawal WithdrawalE_5Eth = new() { Address = AddressE, Index = 5, ValidatorIndex = 2005, Amount = 5_000_000_000 }; + public static Withdrawal WithdrawalF_6Eth = new() { Address = AddressF, Index = 6, ValidatorIndex = 2006, Amount = 6_000_000_000 }; public static IPEndPoint IPEndPointA = IPEndPoint.Parse("10.0.0.1"); public static IPEndPoint IPEndPointB = IPEndPoint.Parse("10.0.0.2"); diff --git a/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs b/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs index 0c0c6c64063..6d9af0c0156 100644 --- a/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs +++ b/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs @@ -1,15 +1,13 @@ // 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 { public WithdrawalBuilder() => TestObject = new(); - public WithdrawalBuilder WithAmount(UInt256 amount) + public WithdrawalBuilder WithAmount(ulong amount) { TestObject.Amount = amount; diff --git a/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs b/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs index 25e8deaa805..50f4af9cbe3 100644 --- a/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs +++ b/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs @@ -15,14 +15,14 @@ public class WithdrawalDecoderTests [Test] public void Should_encode() { - var withdrawal = new Withdrawal + Withdrawal withdrawal = new() { Index = 1, ValidatorIndex = 2, Address = Address.SystemUser, Amount = 3 }; - var rlp = Rlp.Encode(withdrawal).Bytes; + byte[] rlp = Rlp.Encode(withdrawal).Bytes; rlp.ToHexString().Should().BeEquivalentTo("d8010294fffffffffffffffffffffffffffffffffffffffe03"); } @@ -30,15 +30,15 @@ public void Should_encode() [Test] public void Should_decode() { - var withdrawal = new Withdrawal + Withdrawal withdrawal = new() { Index = 1, ValidatorIndex = 2, Address = new Address("0x773f86fb098bb19f228f441a7715daa13d10a751"), Amount = 3 }; - var rlp = Rlp.Encode(withdrawal).Bytes; - var decoded = Rlp.Decode(rlp); + byte[] rlp = Rlp.Encode(withdrawal).Bytes; + Withdrawal decoded = Rlp.Decode(rlp); decoded.Should().BeEquivalentTo(withdrawal); } @@ -46,20 +46,20 @@ public void Should_decode() [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 + Amount = 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); } @@ -67,15 +67,15 @@ public void Should_decode_with_ValueDecoderContext() [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 + Amount = 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); } diff --git a/src/Nethermind/Nethermind.Core/Withdrawal.cs b/src/Nethermind/Nethermind.Core/Withdrawal.cs index b0c47386bd4..a62f0a92bee 100644 --- a/src/Nethermind/Nethermind.Core/Withdrawal.cs +++ b/src/Nethermind/Nethermind.Core/Withdrawal.cs @@ -2,7 +2,6 @@ // SPDX-License-Identifier: LGPL-3.0-only using System.Text; -using Nethermind.Int256; namespace Nethermind.Core; @@ -28,7 +27,7 @@ public class Withdrawal /// /// Gets or sets the withdrawal amount in Wei. /// - public UInt256 Amount { get; set; } + public ulong Amount { get; set; } public override string ToString() => ToString(string.Empty); diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs index 6f8c012f8b3..47ad4df5654 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs @@ -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, @@ -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(), Array.Empty(), diff --git a/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs b/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs index bf1fc751a0f..fbd079aa840 100644 --- a/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs +++ b/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs @@ -23,7 +23,7 @@ public class WithdrawalDecoder : IRlpStreamDecoder, IRlpValueDecoder Index = rlpStream.DecodeULong(), ValidatorIndex = rlpStream.DecodeULong(), Address = rlpStream.DecodeAddress(), - Amount = rlpStream.DecodeUInt256() + Amount = rlpStream.DecodeUlong() }; } @@ -43,7 +43,7 @@ public class WithdrawalDecoder : IRlpStreamDecoder, IRlpValueDecoder Index = decoderContext.DecodeULong(), ValidatorIndex = decoderContext.DecodeULong(), Address = decoderContext.DecodeAddress(), - Amount = decoderContext.DecodeUInt256() + Amount = decoderContext.DecodeULong() }; } From ff65bfcf7f3b567503af5e90c03389ca35a8935e Mon Sep 17 00:00:00 2001 From: MarekM25 Date: Fri, 13 Jan 2023 15:01:12 +0100 Subject: [PATCH 2/4] review changes --- .../Withdrawals/WithdrawalProcessor.cs | 7 +++---- .../Nethermind.Core.Test/Builders/TestItem.cs | 12 ++++++------ .../Builders/WithdrawalBuilder.cs | 2 +- .../Encoding/WithdrawalDecoderTests.cs | 8 ++++---- .../Nethermind.Core/Nethermind.Core.csproj | 1 + src/Nethermind/Nethermind.Core/Withdrawal.cs | 13 ++++++++++--- .../EngineModuleTests.V2.cs | 2 +- .../WithdrawalDecoder.cs | 8 ++++---- 8 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs b/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs index 38706acdec2..4afd3d7f3ac 100644 --- a/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs @@ -35,17 +35,16 @@ public void ProcessWithdrawals(Block block, IReleaseSpec spec) { foreach (var withdrawal in block.Withdrawals) { - if (_logger.IsTrace) _logger.Trace($" {withdrawal.Amount} Gwei 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 - UInt256 withdrawalAmountInWei = withdrawal.Amount * Unit.GWei; if (_stateProvider.AccountExists(withdrawal.Address)) { - _stateProvider.AddToBalance(withdrawal.Address, withdrawalAmountInWei, spec); + _stateProvider.AddToBalance(withdrawal.Address, withdrawal.AmountInWei, spec); } else { - _stateProvider.CreateAccount(withdrawal.Address, withdrawalAmountInWei); + _stateProvider.CreateAccount(withdrawal.Address, withdrawal.AmountInWei); } } } diff --git a/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs b/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs index 88756d2fa5d..9556a082496 100644 --- a/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs +++ b/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs @@ -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_000_000_000 }; - public static Withdrawal WithdrawalB_2Eth = new() { Address = AddressB, Index = 2, ValidatorIndex = 2002, Amount = 2_000_000_000 }; - public static Withdrawal WithdrawalC_3Eth = new() { Address = AddressC, Index = 3, ValidatorIndex = 2003, Amount = 3_000_000_000 }; - public static Withdrawal WithdrawalD_4Eth = new() { Address = AddressD, Index = 4, ValidatorIndex = 2004, Amount = 4_000_000_000 }; - public static Withdrawal WithdrawalE_5Eth = new() { Address = AddressE, Index = 5, ValidatorIndex = 2005, Amount = 5_000_000_000 }; - public static Withdrawal WithdrawalF_6Eth = new() { Address = AddressF, Index = 6, ValidatorIndex = 2006, Amount = 6_000_000_000 }; + 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"); diff --git a/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs b/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs index 6d9af0c0156..47e5f2232c5 100644 --- a/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs +++ b/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs @@ -9,7 +9,7 @@ public class WithdrawalBuilder : BuilderBase public WithdrawalBuilder WithAmount(ulong amount) { - TestObject.Amount = amount; + TestObject.AmountInGWei = amount; return this; } diff --git a/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs b/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs index 50f4af9cbe3..70af7619883 100644 --- a/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs +++ b/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs @@ -20,7 +20,7 @@ public void Should_encode() Index = 1, ValidatorIndex = 2, Address = Address.SystemUser, - Amount = 3 + AmountInGWei = 3 }; byte[] rlp = Rlp.Encode(withdrawal).Bytes; @@ -35,7 +35,7 @@ public void Should_decode() Index = 1, ValidatorIndex = 2, Address = new Address("0x773f86fb098bb19f228f441a7715daa13d10a751"), - Amount = 3 + AmountInGWei = 3 }; byte[] rlp = Rlp.Encode(withdrawal).Bytes; Withdrawal decoded = Rlp.Decode(rlp); @@ -51,7 +51,7 @@ public void Should_decode_with_ValueDecoderContext() Index = long.MaxValue, ValidatorIndex = int.MaxValue, Address = new Address("0x773f86fb098bb19f228f441a7715daa13d10a751"), - Amount = ulong.MaxValue + AmountInGWei = ulong.MaxValue }; RlpStream stream = new(1024); WithdrawalDecoder codec = new(); @@ -72,7 +72,7 @@ public void Should_encode_same_for_Rlp_Encode_and_WithdrawalDecoder_Encode() Index = long.MaxValue, ValidatorIndex = int.MaxValue, Address = new Address("0x7e24b8f924a82df020eef45c320deb224559f13e"), - Amount = ulong.MaxValue + AmountInGWei = ulong.MaxValue }; byte[] rlp1 = new WithdrawalDecoder().Encode(withdrawal).Bytes; byte[] rlp2 = Rlp.Encode(withdrawal).Bytes; diff --git a/src/Nethermind/Nethermind.Core/Nethermind.Core.csproj b/src/Nethermind/Nethermind.Core/Nethermind.Core.csproj index 0796c45057e..8858a4dd535 100644 --- a/src/Nethermind/Nethermind.Core/Nethermind.Core.csproj +++ b/src/Nethermind/Nethermind.Core/Nethermind.Core.csproj @@ -13,6 +13,7 @@ + diff --git a/src/Nethermind/Nethermind.Core/Withdrawal.cs b/src/Nethermind/Nethermind.Core/Withdrawal.cs index a62f0a92bee..6e4feafd2be 100644 --- a/src/Nethermind/Nethermind.Core/Withdrawal.cs +++ b/src/Nethermind/Nethermind.Core/Withdrawal.cs @@ -2,6 +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; @@ -25,9 +28,13 @@ public class Withdrawal public Address Address { get; set; } = Address.Zero; /// - /// Gets or sets the withdrawal amount in Wei. + /// Gets or sets the withdrawal amount in GWei. /// - public ulong 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); @@ -35,6 +42,6 @@ public class Withdrawal .Append($"{nameof(Index)}: {Index}, ") .Append($"{nameof(ValidatorIndex)}: {ValidatorIndex}, ") .Append($"{nameof(Address)}: {Address}, ") - .Append($"{nameof(Amount)}: {Amount}}}") + .Append($"{nameof(AmountInGWei)}: {AmountInGWei}}}") .ToString(); } diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs index 47ad4df5654..aae63c6c983 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs @@ -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 { diff --git a/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs b/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs index fbd079aa840..cca8b0cd041 100644 --- a/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs +++ b/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs @@ -23,7 +23,7 @@ public class WithdrawalDecoder : IRlpStreamDecoder, IRlpValueDecoder Index = rlpStream.DecodeULong(), ValidatorIndex = rlpStream.DecodeULong(), Address = rlpStream.DecodeAddress(), - Amount = rlpStream.DecodeUlong() + AmountInGWei = rlpStream.DecodeUlong() }; } @@ -43,7 +43,7 @@ public class WithdrawalDecoder : IRlpStreamDecoder, IRlpValueDecoder Index = decoderContext.DecodeULong(), ValidatorIndex = decoderContext.DecodeULong(), Address = decoderContext.DecodeAddress(), - Amount = decoderContext.DecodeULong() + AmountInGWei = decoderContext.DecodeULong() }; } @@ -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) @@ -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)); } From b515e824ec16f0ff24b54b497c67794e032b165b Mon Sep 17 00:00:00 2001 From: MarekM25 Date: Mon, 16 Jan 2023 17:50:37 +0100 Subject: [PATCH 3/4] review changes --- .../Withdrawals/WithdrawalProcessor.cs | 2 +- .../Nethermind.Core.Test/Builders/TestItem.cs | 12 ++++++------ .../Builders/WithdrawalBuilder.cs | 2 +- .../Encoding/WithdrawalDecoderTests.cs | 8 ++++---- src/Nethermind/Nethermind.Core/Withdrawal.cs | 8 ++++---- .../EngineModuleTests.V2.cs | 2 +- .../WithdrawalDecoder.cs | 8 ++++---- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs b/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs index 4afd3d7f3ac..55a729f51b4 100644 --- a/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Withdrawals/WithdrawalProcessor.cs @@ -35,7 +35,7 @@ public void ProcessWithdrawals(Block block, IReleaseSpec spec) { foreach (var withdrawal in block.Withdrawals) { - if (_logger.IsTrace) _logger.Trace($" {withdrawal.AmountInGWei} GWei 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)) diff --git a/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs b/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs index 9556a082496..5621152c697 100644 --- a/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs +++ b/src/Nethermind/Nethermind.Core.Test/Builders/TestItem.cs @@ -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, 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 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"); diff --git a/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs b/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs index 47e5f2232c5..63eea5281a1 100644 --- a/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs +++ b/src/Nethermind/Nethermind.Core.Test/Builders/WithdrawalBuilder.cs @@ -9,7 +9,7 @@ public class WithdrawalBuilder : BuilderBase public WithdrawalBuilder WithAmount(ulong amount) { - TestObject.AmountInGWei = amount; + TestObject.AmountInGwei = amount; return this; } diff --git a/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs b/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs index 70af7619883..56be1dc3da2 100644 --- a/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs +++ b/src/Nethermind/Nethermind.Core.Test/Encoding/WithdrawalDecoderTests.cs @@ -20,7 +20,7 @@ public void Should_encode() Index = 1, ValidatorIndex = 2, Address = Address.SystemUser, - AmountInGWei = 3 + AmountInGwei = 3 }; byte[] rlp = Rlp.Encode(withdrawal).Bytes; @@ -35,7 +35,7 @@ public void Should_decode() Index = 1, ValidatorIndex = 2, Address = new Address("0x773f86fb098bb19f228f441a7715daa13d10a751"), - AmountInGWei = 3 + AmountInGwei = 3 }; byte[] rlp = Rlp.Encode(withdrawal).Bytes; Withdrawal decoded = Rlp.Decode(rlp); @@ -51,7 +51,7 @@ public void Should_decode_with_ValueDecoderContext() Index = long.MaxValue, ValidatorIndex = int.MaxValue, Address = new Address("0x773f86fb098bb19f228f441a7715daa13d10a751"), - AmountInGWei = ulong.MaxValue + AmountInGwei = ulong.MaxValue }; RlpStream stream = new(1024); WithdrawalDecoder codec = new(); @@ -72,7 +72,7 @@ public void Should_encode_same_for_Rlp_Encode_and_WithdrawalDecoder_Encode() Index = long.MaxValue, ValidatorIndex = int.MaxValue, Address = new Address("0x7e24b8f924a82df020eef45c320deb224559f13e"), - AmountInGWei = ulong.MaxValue + AmountInGwei = ulong.MaxValue }; byte[] rlp1 = new WithdrawalDecoder().Encode(withdrawal).Bytes; byte[] rlp2 = Rlp.Encode(withdrawal).Bytes; diff --git a/src/Nethermind/Nethermind.Core/Withdrawal.cs b/src/Nethermind/Nethermind.Core/Withdrawal.cs index 6e4feafd2be..3d0e9da19b2 100644 --- a/src/Nethermind/Nethermind.Core/Withdrawal.cs +++ b/src/Nethermind/Nethermind.Core/Withdrawal.cs @@ -30,11 +30,11 @@ public class Withdrawal /// /// Gets or sets the withdrawal amount in GWei. /// - [JsonProperty(PropertyName = "Amount")] - public ulong AmountInGWei { get; set; } + [JsonProperty(PropertyName = "amount")] + public ulong AmountInGwei { get; set; } [JsonIgnore] - public UInt256 AmountInWei => AmountInGWei * 1.GWei(); + public UInt256 AmountInWei => AmountInGwei * 1.GWei(); public override string ToString() => ToString(string.Empty); @@ -42,6 +42,6 @@ public class Withdrawal .Append($"{nameof(Index)}: {Index}, ") .Append($"{nameof(ValidatorIndex)}: {ValidatorIndex}, ") .Append($"{nameof(Address)}: {Address}, ") - .Append($"{nameof(AmountInGWei)}: {AmountInGWei}}}") + .Append($"{nameof(AmountInGwei)}: {AmountInGwei}}}") .ToString(); } diff --git a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs index aae63c6c983..3ad4927029e 100644 --- a/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs +++ b/src/Nethermind/Nethermind.Merge.Plugin.Test/EngineModuleTests.V2.cs @@ -44,7 +44,7 @@ public virtual async Task Should_process_block_as_expected_V2() }; Withdrawal[] withdrawals = new[] { - new Withdrawal { Index = 1, AmountInGWei = 3, Address = TestItem.AddressB, ValidatorIndex = 2 } + new Withdrawal { Index = 1, AmountInGwei = 3, Address = TestItem.AddressB, ValidatorIndex = 2 } }; var payloadAttrs = new { diff --git a/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs b/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs index cca8b0cd041..33b5388cfd5 100644 --- a/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs +++ b/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs @@ -23,7 +23,7 @@ public class WithdrawalDecoder : IRlpStreamDecoder, IRlpValueDecoder Index = rlpStream.DecodeULong(), ValidatorIndex = rlpStream.DecodeULong(), Address = rlpStream.DecodeAddress(), - AmountInGWei = rlpStream.DecodeUlong() + AmountInGwei = rlpStream.DecodeUlong() }; } @@ -43,7 +43,7 @@ public class WithdrawalDecoder : IRlpStreamDecoder, IRlpValueDecoder Index = decoderContext.DecodeULong(), ValidatorIndex = decoderContext.DecodeULong(), Address = decoderContext.DecodeAddress(), - AmountInGWei = decoderContext.DecodeULong() + AmountInGwei = decoderContext.DecodeULong() }; } @@ -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.AmountInGWei); + stream.Encode(item.AmountInGwei); } public Rlp Encode(Withdrawal? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None) @@ -77,7 +77,7 @@ private static int GetContentLength(Withdrawal item) => Rlp.LengthOf(item.Index) + Rlp.LengthOf(item.ValidatorIndex) + Rlp.LengthOfAddressRlp + - Rlp.LengthOf(item.AmountInGWei); + Rlp.LengthOf(item.AmountInGwei); public int GetLength(Withdrawal item, RlpBehaviors _) => Rlp.LengthOfSequence(GetContentLength(item)); } From afa52f393a9628ac53547d8f729e8aeda3812b09 Mon Sep 17 00:00:00 2001 From: MarekM25 Date: Mon, 16 Jan 2023 17:56:47 +0100 Subject: [PATCH 4/4] adjust DecodeUlong --- .../Nethermind.Serialization.Rlp/WithdrawalDecoder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs b/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs index 33b5388cfd5..343f88afa5b 100644 --- a/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs +++ b/src/Nethermind/Nethermind.Serialization.Rlp/WithdrawalDecoder.cs @@ -23,7 +23,7 @@ public class WithdrawalDecoder : IRlpStreamDecoder, IRlpValueDecoder Index = rlpStream.DecodeULong(), ValidatorIndex = rlpStream.DecodeULong(), Address = rlpStream.DecodeAddress(), - AmountInGwei = rlpStream.DecodeUlong() + AmountInGwei = rlpStream.DecodeULong() }; }