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

Withdrawals wei -> gwei #5139

Merged
merged 5 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,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);
}
}
}
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, 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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Withdrawal>
{
public WithdrawalBuilder() => TestObject = new();

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

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
};
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
};
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
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);
}

[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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Nethermind/Nethermind.Core/Withdrawal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.Text;
using Nethermind.Int256;

namespace Nethermind.Core;

Expand All @@ -28,7 +27,7 @@ public class Withdrawal
/// <summary>
/// Gets or sets the withdrawal amount in Wei.
/// </summary>
public UInt256 Amount { get; set; }
public ulong Amount { get; set; }
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
Original file line number Diff line number Diff line change
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()
Amount = rlpStream.DecodeUlong()
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
};
}

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

Expand Down