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

Change for 7702 for devnet-4 #7590

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
9 changes: 5 additions & 4 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 UInt256 ChainId { get; set; }
public Address Address { get; set; }
public ulong Nonce { get; set; }
public UInt256 Nonce { get; set; }
public ulong V { get; set; }
ak88 marked this conversation as resolved.
Show resolved Hide resolved
public byte[] R { get; set; }
public byte[] S { get; set; }
public string R { get; set; }
public string S { get; set; }
public Address Signer { get; set; }
}
52 changes: 45 additions & 7 deletions src/Nethermind/Ethereum.Test.Base/JsonToEthereumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,51 @@ public static Transaction Convert(PostStateJson postStateJson, TransactionJson t
{
transaction.AuthorizationList =
transactionJson.AuthorizationList
.Select(i => new AuthorizationTuple(
i.ChainId,
i.Address,
i.Nonce,
i.V,
i.R,
i.S)).ToArray();
.Select(i =>
{
if (i.ChainId > ulong.MaxValue)
{
i.ChainId = 0;
transaction.SenderAddress = Address.Zero;
}
if (i.Nonce > ulong.MaxValue)
{
i.Nonce = 0;
transaction.SenderAddress = Address.Zero;
}
UInt256 s = UInt256.Zero;
if (i.S.Length > 66)
{
i.S = "0x0";
transaction.SenderAddress = Address.Zero;
}
else
{
s = UInt256.Parse(i.S);
}
UInt256 r = UInt256.Zero;
if (i.R.Length > 66)
{
i.R = "0x0";
transaction.SenderAddress = Address.Zero;
}
else
{
r = UInt256.Parse(i.R);
}
if (i.V > byte.MaxValue)
{
i.V = 0;
transaction.SenderAddress = Address.Zero;
}
return new AuthorizationTuple(
i.ChainId.u0,
i.Address,
i.Nonce.u0,
(byte)i.V,
r,
s);
}).ToArray();
if (transaction.AuthorizationList.Any())
{
transaction.Type = TxType.SetCode;
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 @@ -599,30 +600,6 @@ public void IsWellFormed_NullAuthorizationList_ReturnsFalse()
Assert.That(txValidator.IsWellFormed(tx, Prague.Instance).AsBool, Is.False);
}

private static object[] BadSignatures =
{
new object[] { 1ul, (UInt256)1, Secp256K1Curve.HalfNPlusOne, false},
new object[] { 1ul, UInt256.Zero, Secp256K1Curve.HalfN, true },
new object[] { 0ul, UInt256.Zero, UInt256.Zero, true },
};
[TestCaseSource(nameof(BadSignatures))]
public void IsWellFormed_AuthorizationTupleHasBadSignature_ReturnsFalse(ulong yParity, UInt256 r, UInt256 s, bool expected)
{
TransactionBuilder<Transaction> txBuilder = Build.A.Transaction
.WithType(TxType.SetCode)
.WithTo(TestItem.AddressA)
.WithAuthorizationCode(new AuthorizationTuple(0, Address.Zero, 0, new Signature(r, s, yParity + Signature.VOffset)))
.WithMaxFeePerGas(100000)
.WithGasLimit(1000000)
.WithChainId(TestBlockchainIds.ChainId)
.SignedAndResolved();

Transaction tx = txBuilder.TestObject;
TxValidator txValidator = new(TestBlockchainIds.ChainId);

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
9 changes: 0 additions & 9 deletions src/Nethermind/Nethermind.Consensus/Validators/TxValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,6 @@ public ValidationResult IsWellFormed(Transaction transaction, IReleaseSpec relea
transaction.AuthorizationList switch
{
null or { Length: 0 } => TxErrorMessages.MissingAuthorizationList,
var authorizationList when authorizationList.Any(a => !ValidateAuthoritySignature(a.AuthoritySignature)) =>
TxErrorMessages.InvalidAuthoritySignature,
_ => ValidationResult.Success
};

private bool ValidateAuthoritySignature(Signature signature)
{
UInt256 sValue = new(signature.SAsSpan, isBigEndian: true);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not needed anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It went from invalidating the tx to just invalidating the tuple instead, so the check is now in TransactionProcessor.

return sValue < Secp256K1Curve.HalfNPlusOne && signature.RecoveryId is 0 or 1;
}
}
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
Loading