Skip to content

Commit

Permalink
Add withdrawals root to block header
Browse files Browse the repository at this point in the history
  • Loading branch information
rubo committed Oct 13, 2022
1 parent 26bac74 commit 8d3db64
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core/BlockBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public BlockBody() : this(null, null, null) { }

public Withdrawal[] Withdrawals { get; internal set; }

public static readonly BlockBody Empty = new();
public static BlockBody Empty { get; } = new();

public bool IsEmpty => Transactions.Length == 0 && Uncles.Length == 0 && Withdrawals.Length == 0;
}
Expand Down
23 changes: 8 additions & 15 deletions src/Nethermind/Nethermind.Core/BlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public BlockHeader(
public Address? GasBeneficiary => Author ?? Beneficiary;
public Keccak? StateRoot { get; set; }
public Keccak? TxRoot { get; set; }
public Keccak? WithdrawalsRoot { get; set; }
public Keccak? ReceiptsRoot { get; set; }
public Bloom? Bloom { get; set; }
public UInt256 Difficulty { get; set; }
Expand Down Expand Up @@ -99,6 +100,7 @@ public string ToString(string indent)
builder.AppendLine($"{indent}Nonce: {Nonce}");
builder.AppendLine($"{indent}Uncles Hash: {UnclesHash}");
builder.AppendLine($"{indent}Tx Root: {TxRoot}");
builder.AppendLine($"{indent}Withdrawals Root: {WithdrawalsRoot}");
builder.AppendLine($"{indent}Receipts Root: {ReceiptsRoot}");
builder.AppendLine($"{indent}State Root: {StateRoot}");
builder.AppendLine($"{indent}BaseFeePerGas: {BaseFeePerGas}");
Expand All @@ -108,23 +110,14 @@ public string ToString(string indent)
return builder.ToString();
}

public override string ToString()
{
return ToString(string.Empty);
}
public override string ToString() => ToString(string.Empty);

public string ToString(Format format)
public string ToString(Format format) => format switch
{
switch (format)
{
case Format.Full:
return ToString(string.Empty);
case Format.FullHashAndNumber:
return Hash == null ? $"{Number} null" : $"{Number} ({Hash})";
default:
return Hash == null ? $"{Number} null" : $"{Number} ({Hash.ToShortString()})";
}
}
Format.Full => ToString(string.Empty),
Format.FullHashAndNumber => Hash == null ? $"{Number} null" : $"{Number} ({Hash})",
_ => Hash == null ? $"{Number} null" : $"{Number} ({Hash.ToShortString()})",
};

[Todo(Improve.Refactor, "Use IFormattable here")]
public enum Format
Expand Down
18 changes: 17 additions & 1 deletion src/Nethermind/Nethermind.Serialization.Rlp/HeaderDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public class HeaderDecoder : IRlpValueDecoder<BlockHeader>, IRlpStreamDecoder<Bl
blockHeader.BaseFeePerGas = decoderContext.DecodeUInt256();
}

if (decoderContext.PeekPrefixAndContentLength().ContentLength == Keccak.Size)
{
blockHeader.WithdrawalsRoot = decoderContext.DecodeKeccak();
}

if ((rlpBehaviors & RlpBehaviors.AllowExtraData) != RlpBehaviors.AllowExtraData)
{
decoderContext.Check(headerCheck);
Expand Down Expand Up @@ -156,6 +161,11 @@ public class HeaderDecoder : IRlpValueDecoder<BlockHeader>, IRlpStreamDecoder<Bl
blockHeader.BaseFeePerGas = rlpStream.DecodeUInt256();
}

if (rlpStream.PeekPrefixAndContentLength().ContentLength == Keccak.Size)
{
blockHeader.WithdrawalsRoot = rlpStream.DecodeKeccak();
}

if ((rlpBehaviors & RlpBehaviors.AllowExtraData) != RlpBehaviors.AllowExtraData)
{
rlpStream.Check(headerCheck);
Expand Down Expand Up @@ -207,6 +217,11 @@ public void Encode(RlpStream rlpStream, BlockHeader? header, RlpBehaviors rlpBeh
{
rlpStream.Encode(header.BaseFeePerGas);
}

if (header.WithdrawalsRoot != null)
{
rlpStream.Encode(header.WithdrawalsRoot);
}
}

public Rlp Encode(BlockHeader? item, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
Expand Down Expand Up @@ -244,7 +259,8 @@ private static int GetContentLength(BlockHeader? item, RlpBehaviors rlpBehaviors
+ Rlp.LengthOf(item.GasUsed)
+ Rlp.LengthOf(item.Timestamp)
+ Rlp.LengthOf(item.ExtraData)
+ (item.Number < Eip1559TransitionBlock ? 0 : Rlp.LengthOf(item.BaseFeePerGas));
+ (item.Number < Eip1559TransitionBlock ? 0 : Rlp.LengthOf(item.BaseFeePerGas))
+ (item.WithdrawalsRoot is null ? 0 : Rlp.LengthOf(item.WithdrawalsRoot));

if (notForSealing)
{
Expand Down

0 comments on commit 8d3db64

Please sign in to comment.