Skip to content

Commit

Permalink
Merge branch 'feature/7702-devnet-4' into pectra_devnet_4
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 committed Oct 15, 2024
2 parents bb41884 + 937ac55 commit 546c975
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 50 deletions.
7 changes: 4 additions & 3 deletions src/Nethermind/Ethereum.Test.Base/AuthorizationListJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core;
using Nethermind.Int256;

namespace Ethereum.Test.Base;
public class AuthorizationListJson
{
public ulong ChainId { get; set; }
public Address Address { get; set; }
public ulong Nonce { get; set; }
public ulong V { get; set; }
public byte[] R { get; set; }
public byte[] S { get; set; }
public byte RecoveryId { get; set; }
public UInt256 R { get; set; }
public UInt256 S { get; set; }
public Address Signer { get; set; }
}
2 changes: 1 addition & 1 deletion src/Nethermind/Ethereum.Test.Base/JsonToEthereumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static Transaction Convert(PostStateJson postStateJson, TransactionJson t
i.ChainId,
i.Address,
i.Nonce,
i.V,
i.RecoveryId,
i.R,
i.S)).ToArray();
if (transaction.AuthorizationList.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Numerics;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc.Routing;
using Nethermind.Consensus.Messages;
using Nethermind.Consensus.Validators;
using Nethermind.Core;
Expand Down Expand Up @@ -528,7 +529,7 @@ public void IsWellFormed_CreateTxInSetCode_ReturnsFalse()
{
TransactionBuilder<Transaction> txBuilder = Build.A.Transaction
.WithType(TxType.SetCode)
.WithAuthorizationCode(new AuthorizationTuple(0, TestItem.AddressA, 0, 0, [], []))
.WithAuthorizationCode(new AuthorizationTuple(0, TestItem.AddressA, 0, 0, 0, 0))
.WithMaxFeePerGas(100000)
.WithGasLimit(1000000)
.WithChainId(TestBlockchainIds.ChainId)
Expand All @@ -552,7 +553,7 @@ public void IsWellFormed_AuthorizationListTxInPragueSpec_ReturnsTrue()
TransactionBuilder<Transaction> txBuilder = Build.A.Transaction
.WithType(TxType.SetCode)
.WithTo(TestItem.AddressA)
.WithAuthorizationCode(new AuthorizationTuple(0, TestItem.AddressA, 0, 0, [], []))
.WithAuthorizationCode(new AuthorizationTuple(0, TestItem.AddressA, 0, 0, 0, 0))
.WithMaxFeePerGas(100000)
.WithGasLimit(1000000)
.WithChainId(TestBlockchainIds.ChainId)
Expand Down Expand Up @@ -622,7 +623,6 @@ public void IsWellFormed_AuthorizationTupleHasBadSignature_ReturnsFalse(ulong yP

Assert.That(txValidator.IsWellFormed(tx, Prague.Instance).AsBool, Is.EqualTo(expected));
}

private static IEnumerable<TxType> NonSetCodeTypes() =>
Enum.GetValues<TxType>().Where(t => t != TxType.SetCode && t != TxType.DepositTx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,6 @@ private bool ValidateAuthoritySignature(Signature signature)
{
UInt256 sValue = new(signature.SAsSpan, isBigEndian: true);

return sValue < Secp256K1Curve.HalfNPlusOne && signature.RecoveryId is 0 or 1;
return sValue < Secp256K1Curve.HalfNPlusOne;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,87 @@ public void DecodeValueDecoderContext_CodeAddressIsNull_ThrowsRlpException()
, Throws.TypeOf<RlpException>());
}

public static IEnumerable<RlpStream> WrongSizeFieldsEncodedCases()
{
yield return TupleRlpStream(
//Wrong chain size
Enumerable.Range(0, 9).Select(i=> (byte) 0xFF).ToArray(),
Address.Zero.Bytes,
Enumerable.Range(0, 8).Select(i=> (byte) 0xFF).ToArray(),
Enumerable.Range(0, 1).Select(i=> (byte) 0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i=> (byte) 0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i=> (byte) 0xFF).ToArray()
);

yield return TupleRlpStream(
//Wrong address size
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 19).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 1).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray()
);

yield return TupleRlpStream(
//Wrong address size
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 21).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 1).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray()
);

yield return TupleRlpStream(
//Wrong nonce size
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Address.Zero.Bytes,
Enumerable.Range(0, 9).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 1).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray()
);

yield return TupleRlpStream(
//Wrong yParity size
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Address.Zero.Bytes,
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 2).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray()
);

yield return TupleRlpStream(
//Wrong R size
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Address.Zero.Bytes,
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 1).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 33).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray()
);

yield return TupleRlpStream(
//Wrong S size
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Address.Zero.Bytes,
Enumerable.Range(0, 8).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 1).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 32).Select(i => (byte)0xFF).ToArray(),
Enumerable.Range(0, 33).Select(i => (byte)0xFF).ToArray()
);
}

[TestCaseSource(nameof(WrongSizeFieldsEncodedCases))]
public void Encode_TupleHasFieldsOutsideBoundaries_ThrowsRlpException(RlpStream badEncoding)
{
AuthorizationTupleDecoder sut = new();

Assert.That(() => sut.Decode(badEncoding, RlpBehaviors.None), Throws.InstanceOf<RlpException>());
}

private static RlpStream TupleRlpStreamWithNull()
{
Address? codeAddress = null;
Expand All @@ -77,4 +158,25 @@ private static RlpStream TupleRlpStreamWithNull()
stream.Position = 0;
return stream;
}

private static RlpStream TupleRlpStream(byte[] chainId, byte[] address, byte[] nonce, byte[] yParity, byte[] r, byte[] s)
{
int length =
+Rlp.LengthOf(chainId)
+ Rlp.LengthOf(address)
+ Rlp.LengthOf(nonce)
+ Rlp.LengthOf(yParity)
+ Rlp.LengthOf(r)
+ Rlp.LengthOf(s);
RlpStream stream = new RlpStream(Rlp.LengthOfSequence(length));
stream.StartSequence(length);
stream.Encode(chainId);
stream.Encode(address);
stream.Encode(nonce);
stream.Encode(yParity);
stream.Encode(r);
stream.Encode(s);
stream.Position = 0;
return stream;
}
}
9 changes: 5 additions & 4 deletions src/Nethermind/Nethermind.Core/AuthorizationTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core.Crypto;
using Nethermind.Int256;
using System;

namespace Nethermind.Core;
Expand All @@ -16,10 +17,10 @@ public AuthorizationTuple(
ulong chainId,
Address codeAddress,
ulong nonce,
ulong yParity,
byte[] r,
byte[] s,
Address? authority = null) : this(chainId, codeAddress, nonce, new Signature(r, s, yParity + Signature.VOffset), authority)
byte yParity,
UInt256 r,
UInt256 s,
Address? authority = null) : this(chainId, codeAddress, nonce, new Signature(r, s, (ulong)yParity + Signature.VOffset), authority)
{ }

public ulong ChainId { get; } = chainId;
Expand Down
42 changes: 21 additions & 21 deletions src/Nethermind/Nethermind.Evm.Test/IntrinsicGasCalculatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,50 +125,50 @@ [new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
TestContext.CurrentContext.Random.NextByte(),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)))
],
GasCostOf.NewAccount);
yield return (
[new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10)),
TestContext.CurrentContext.Random.NextByte(),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10))),
new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
TestContext.CurrentContext.Random.NextByte(),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)))
],
GasCostOf.NewAccount * 2);
yield return (
[new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10)),
TestContext.CurrentContext.Random.NextByte(),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10))),
new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10)),
TestContext.CurrentContext.Random.NextByte(),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10))),
new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
TestContext.CurrentContext.Random.NextByte(),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)))
],
GasCostOf.NewAccount * 3);
}
Expand All @@ -192,9 +192,9 @@ public void Calculate_TxHasAuthorizationListBeforePrague_ThrowsInvalidDataExcept
0,
TestItem.AddressF,
0,
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
TestContext.CurrentContext.Random.NextByte(),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(10)))
)
.TestObject;

Expand Down
Loading

0 comments on commit 546c975

Please sign in to comment.