Skip to content

Commit

Permalink
change nonce to ulong
Browse files Browse the repository at this point in the history
  • Loading branch information
ak88 committed Aug 22, 2024
1 parent f65e216 commit 29ea2ec
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public void Sign_generic_network()

public static IEnumerable<AuthorizationTuple> AuthorityTupleTestCaseSources()
{
yield return CreateAuthorizationTuple(ulong.MaxValue, Build.A.Address.TestObjectInternal, UInt256.MaxValue);
yield return CreateAuthorizationTuple(1, Address.Zero, null);
yield return CreateAuthorizationTuple(ulong.MaxValue, Build.A.Address.TestObjectInternal, ulong.MaxValue);
yield return CreateAuthorizationTuple(1, Address.Zero, 0);
}

[TestCaseSource(nameof(AuthorityTupleTestCaseSources))]
Expand All @@ -95,7 +95,7 @@ public void RecoverAddress_AuthorizationTupleOfDifferentSize_RecoversAddressCorr
Assert.That(authority, Is.EqualTo(authorization.Authority));
}

private static AuthorizationTuple CreateAuthorizationTuple(ulong chainId, Address codeAddress, UInt256? nonce)
private static AuthorizationTuple CreateAuthorizationTuple(ulong chainId, Address codeAddress, ulong nonce)
{
AuthorizationTupleDecoder decoder = new();
RlpStream rlp = decoder.EncodeWithoutSignature(chainId, codeAddress, nonce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public class AuthorizationTupleDecoderTests
{
public static IEnumerable<AuthorizationTuple> AuthorizationTupleEncodeCases()
{
yield return new AuthorizationTuple(0, Address.Zero, null, new Signature(new byte[64], 0));
yield return new AuthorizationTuple(0, Address.Zero, 0, new Signature(new byte[64], 0));
yield return new AuthorizationTuple(0, Address.Zero, 0, new Signature(new byte[64], 0));
yield return new AuthorizationTuple(
ulong.MaxValue,
new Address(Enumerable.Range(0, 20).Select(i => (byte)0xff).ToArray()),
UInt256.MaxValue,
ulong.MaxValue,
new Signature(Enumerable.Range(0, 64).Select(i => (byte)0xff).ToArray(), 1));
}

Expand All @@ -41,64 +41,36 @@ public void Encode_TupleHasValues_TupleCanBeDecodedToEquivalentTuple(Authorizati
sut.Decode(result).Should().BeEquivalentTo(item);
}


[Test]
public void Encode_NonceIsNull_NonceIsAlsoNullAfterDecoding()
{
AuthorizationTuple item = new(0, Address.Zero, null, new Signature(new byte[64], 0));
AuthorizationTupleDecoder sut = new();

RlpStream result = sut.Encode(item);
result.Position = 0;

Assert.That(sut.Decode(result).Nonce, Is.Null);
}

[Test]
public void Decode_NonceItemListIsGreaterThan1_ThrowsRlpException()
{
RlpStream stream = TupleRlpStreamWithTwoNonces();

AuthorizationTupleDecoder sut = new();

Assert.That(() => sut.Decode(stream), Throws.TypeOf<RlpException>());
}

[Test]
public void DecodeValueDecoderContext_NonceItemListIsGreaterThan1_ThrowsRlpException()
public void DecodeValueDecoderContext_CodeAddressIsNull_ThrowsArgumentNullException()
{
RlpStream stream = TupleRlpStreamWithTwoNonces();
RlpStream stream = TupleRlpStreamWithNull();

AuthorizationTupleDecoder sut = new();
Assert.That(() =>
{
Rlp.ValueDecoderContext decoderContext = new Rlp.ValueDecoderContext(stream.Data);
sut.Decode(ref decoderContext, RlpBehaviors.None);
}
, Throws.TypeOf<RlpException>());
, Throws.TypeOf<ArgumentNullException>());
}

private static RlpStream TupleRlpStreamWithTwoNonces()
private static RlpStream TupleRlpStreamWithNull()
{
ulong chainId = 0;
Address codeAddress = Address.Zero;
UInt256[] nonces = [0, 1];
Address? codeAddress = null;
Signature sig = new(new byte[64], 0);
int length =
+Rlp.LengthOf(chainId)
+ Rlp.LengthOf(1)
+ Rlp.LengthOf(codeAddress)
+ Rlp.LengthOfSequence(Rlp.LengthOf(nonces[0]) + Rlp.LengthOf(nonces[1]))
+ Rlp.LengthOf(0)
+ Rlp.LengthOf(sig.RecoveryId)
+ Rlp.LengthOf(sig.R)
+ Rlp.LengthOf(sig.S);

RlpStream stream = new RlpStream(Rlp.LengthOfSequence(length));
stream.StartSequence(length);
stream.Encode(chainId);
stream.Encode(1);
stream.Encode(codeAddress);
stream.StartSequence(Rlp.LengthOf(nonces[0]) + Rlp.LengthOf(nonces[1]));
stream.Encode(nonces[0]);
stream.Encode(nonces[1]);
stream.Encode(0);
stream.Encode(sig.RecoveryId);
stream.Encode(sig.R);
stream.Encode(sig.S);
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Core/AuthorizationTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace Nethermind.Core;
public class AuthorizationTuple(
ulong chainId,
Address codeAddress,
UInt256? nonce,
ulong nonce,
Signature sig,
Address? authority = null)
{
public AuthorizationTuple(
ulong chainId,
Address codeAddress,
UInt256? nonce,
ulong nonce,
ulong yParity,
byte[] r,
byte[] s,
Expand All @@ -28,7 +28,7 @@ public AuthorizationTuple(

public ulong ChainId { get; } = chainId;
public Address CodeAddress { get; } = codeAddress ?? throw new ArgumentNullException(nameof(codeAddress));
public UInt256? Nonce { get; } = nonce;
public ulong Nonce { get; } = nonce;
public Signature AuthoritySignature { get; } = sig ?? throw new ArgumentNullException(nameof(sig));

/// <summary>
Expand Down
28 changes: 14 additions & 14 deletions src/Nethermind/Nethermind.Evm.Test/CodeInfoRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void InsertFromAuthorizations_AuthorityTupleIsCorrect_CodeIsInserted()
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(authority, 1, TestItem.AddressB, (UInt256)0),
CreateAuthorizationTuple(authority, 1, TestItem.AddressB, 0),
};
CodeInsertResult result = sut.InsertFromAuthorizations(Substitute.For<IWorldState>(), tuples, Substitute.For<IReleaseSpec>());

Expand All @@ -45,19 +45,19 @@ public static IEnumerable<object[]> AuthorizationCases()
{
yield return new object[]
{
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressB, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressB, 0),
true
};
yield return new object[]
{
//Wrong chain id
CreateAuthorizationTuple(TestItem.PrivateKeyB, 2, TestItem.AddressB, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyB, 2, TestItem.AddressB, 0),
false
};
yield return new object[]
{
//wrong nonce
CreateAuthorizationTuple(TestItem.PrivateKeyC, 1, TestItem.AddressB, (UInt256)1),
CreateAuthorizationTuple(TestItem.PrivateKeyC, 1, TestItem.AddressB, 1),
false
};
}
Expand Down Expand Up @@ -87,7 +87,7 @@ public void InsertFromAuthorizations_AuthorityHasCode_NoCodeIsInserted()
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(authority, 1, codeSource, (UInt256)0),
CreateAuthorizationTuple(authority, 1, codeSource, 0),
};

sut.InsertFromAuthorizations(mockWorldState, tuples, Substitute.For<IReleaseSpec>());
Expand All @@ -108,7 +108,7 @@ public void InsertFromAuthorizations_AuthorityHasDelegatedCode_CodeIsInserted()
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(authority, 1, codeSource, (UInt256)0),
CreateAuthorizationTuple(authority, 1, codeSource, 0),
};

sut.InsertFromAuthorizations(mockWorldState, tuples, Substitute.For<IReleaseSpec>());
Expand All @@ -129,7 +129,7 @@ public void InsertFromAuthorizations_AuthorityHasZeroNonce_NonceIsIncrementedByO
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(authority, 1, codeSource, (UInt256)0),
CreateAuthorizationTuple(authority, 1, codeSource, 0),
};

sut.InsertFromAuthorizations(stateProvider, tuples, Substitute.For<IReleaseSpec>());
Expand All @@ -143,10 +143,10 @@ public void InsertFromAuthorizations_FourAuthorizationInTotalButTwoAreInvalid_Re
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressF, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyB, 1, TestItem.AddressF, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyC, 2, TestItem.AddressF, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyD, 1, TestItem.AddressF, (UInt256)1),
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressF, 0),
CreateAuthorizationTuple(TestItem.PrivateKeyB, 1, TestItem.AddressF, 0),
CreateAuthorizationTuple(TestItem.PrivateKeyC, 2, TestItem.AddressF, 0),
CreateAuthorizationTuple(TestItem.PrivateKeyD, 1, TestItem.AddressF, 1),
};
CodeInsertResult result = sut.InsertFromAuthorizations(Substitute.For<IWorldState>(), tuples, Substitute.For<IReleaseSpec>());

Expand All @@ -163,8 +163,8 @@ public void InsertFromAuthorizations_AuthorizationsHasOneExistingAccount_ResultH
CodeInfoRepository sut = new(1);
var tuples = new[]
{
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressF, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyB, 1, TestItem.AddressF, (UInt256)0),
CreateAuthorizationTuple(TestItem.PrivateKeyA, 1, TestItem.AddressF, 0),
CreateAuthorizationTuple(TestItem.PrivateKeyB, 1, TestItem.AddressF, 0),
};
stateProvider.CreateAccount(TestItem.AddressA, 0);

Expand All @@ -173,7 +173,7 @@ public void InsertFromAuthorizations_AuthorizationsHasOneExistingAccount_ResultH
result.Refunds.Should().Be(1);
}

private static AuthorizationTuple CreateAuthorizationTuple(PrivateKey signer, ulong chainId, Address codeAddress, UInt256? nonce)
private static AuthorizationTuple CreateAuthorizationTuple(PrivateKey signer, ulong chainId, Address codeAddress, ulong nonce)
{
AuthorizationTupleDecoder decoder = new();
RlpStream rlp = decoder.EncodeWithoutSignature(chainId, codeAddress, nonce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void Test(IReleaseSpec spec, bool isAfterRepricing)
[new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(32)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
Expand All @@ -135,14 +135,14 @@ [new AuthorizationTuple(
[new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(32)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10)),
new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(32)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
Expand All @@ -152,21 +152,21 @@ [new AuthorizationTuple(
[new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(32)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10)),
new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(32)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10)),
new AuthorizationTuple(
TestContext.CurrentContext.Random.NextULong(),
new Address(TestContext.CurrentContext.Random.NextBytes(20)),
new UInt256(TestContext.CurrentContext.Random.NextBytes(32)),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextULong(),
TestContext.CurrentContext.Random.NextBytes(10),
TestContext.CurrentContext.Random.NextBytes(10))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Execute_TxHasAuthorizationWithCodeThatSavesCallerAddress_ExpectedAdd
.WithType(TxType.SetCode)
.WithTo(signer.Address)
.WithGasLimit(60_000)
.WithAuthorizationCode(CreateAuthorizationTuple(signer, _specProvider.ChainId, codeSource, null))
.WithAuthorizationCode(CreateAuthorizationTuple(signer, _specProvider.ChainId, codeSource, 0))
.SignedAndResolved(_ethereumEcdsa, sender, true)
.TestObject;
Block block = Build.A.Block.WithNumber(long.MaxValue)
Expand Down Expand Up @@ -99,7 +99,7 @@ public void Execute_TxHasAuthorizationCodeButAuthorityHasCode_NoAuthorizedCodeIs
.WithType(TxType.SetCode)
.WithTo(signer.Address)
.WithGasLimit(60_000)
.WithAuthorizationCode(CreateAuthorizationTuple(signer, _specProvider.ChainId, codeSource, null))
.WithAuthorizationCode(CreateAuthorizationTuple(signer, _specProvider.ChainId, codeSource, 0))
.SignedAndResolved(_ethereumEcdsa, sender, true)
.TestObject;
Block block = Build.A.Block.WithNumber(long.MaxValue)
Expand Down Expand Up @@ -136,7 +136,7 @@ public void Execute_SenderAndSignerIsTheSameOrNotWithCodeThatSavesCallerAddress_
.WithType(TxType.SetCode)
.WithTo(signer.Address)
.WithGasLimit(60_000)
.WithAuthorizationCode(CreateAuthorizationTuple(signer, _specProvider.ChainId, codeSource, null))
.WithAuthorizationCode(CreateAuthorizationTuple(signer, _specProvider.ChainId, codeSource, 0))
.SignedAndResolved(_ethereumEcdsa, sender, true)
.TestObject;
Block block = Build.A.Block.WithNumber(long.MaxValue)
Expand All @@ -153,17 +153,17 @@ public void Execute_SenderAndSignerIsTheSameOrNotWithCodeThatSavesCallerAddress_
public static IEnumerable<object[]> DifferentCommitValues()
{
//Base case
yield return new object[] { 1ul, (UInt256)0, TestItem.AddressA.Bytes };
yield return new object[] { 1ul, 0, TestItem.AddressA.Bytes };
//Wrong nonce
yield return new object[] { 1ul, (UInt256)1, new[] { (byte)0x0 } };
yield return new object[] { 1ul, 1, new[] { (byte)0x0 } };
//Null nonce means it should be ignored
yield return new object[] { 1ul, null, TestItem.AddressA.Bytes };
yield return new object[] { 1ul, 0, TestItem.AddressA.Bytes };
//Wrong chain id
yield return new object[] { 2ul, (UInt256)0, new[] { (byte)0x0 } };
yield return new object[] { 2ul, 0, new[] { (byte)0x0 } };
}

[TestCaseSource(nameof(DifferentCommitValues))]
public void Execute_CommitMessageHasDifferentData_ExpectedAddressIsSavedInStorageSlot(ulong chainId, UInt256? nonce, byte[] expectedStorageValue)
public void Execute_CommitMessageHasDifferentData_ExpectedAddressIsSavedInStorageSlot(ulong chainId, ulong nonce, byte[] expectedStorageValue)
{
PrivateKey sender = TestItem.PrivateKeyA;
PrivateKey signer = TestItem.PrivateKeyB;
Expand Down Expand Up @@ -214,7 +214,7 @@ public void Execute_TxHasDifferentAmountOfAuthorizedCode_UsedGasIsExpected(int c
_specProvider.ChainId,
//Copy empty code so will not add to gas cost
TestItem.AddressC,
null)))
0)))
.SignedAndResolved(_ethereumEcdsa, sender, true)
.TestObject;
Block block = Build.A.Block.WithNumber(long.MaxValue)
Expand Down Expand Up @@ -252,7 +252,7 @@ public void Execute_TxAuthorizationListWithBALANCE_WarmAccountReadGasIsCharged()
signer,
_specProvider.ChainId,
codeSource,
null))
0))
.SignedAndResolved(_ethereumEcdsa, sender, true)
.TestObject;
Block block = Build.A.Block.WithNumber(long.MaxValue)
Expand Down Expand Up @@ -300,12 +300,12 @@ public void Execute_AuthorizationListHasSameAuthorityButDifferentCode_OnlyFirstI
signer,
_specProvider.ChainId,
firstCodeSource,
null),
0),
CreateAuthorizationTuple(
signer,
_specProvider.ChainId,
secondCodeSource,
null),
0),
];
if (reverseOrder)
{
Expand Down Expand Up @@ -354,7 +354,7 @@ public void Execute_FirstTxHasAuthorizedCodeThatIncrementsAndSecondDoesNot_Stora
signer,
_specProvider.ChainId,
codeSource,
null))
0))
.SignedAndResolved(_ethereumEcdsa, sender, true)
.TestObject;
Transaction tx2 = Build.A.Transaction
Expand Down Expand Up @@ -382,7 +382,7 @@ private void DeployCode(Address codeSource, byte[] code)
_stateProvider.InsertCode(codeSource, Keccak.Compute(code), code, _specProvider.GetSpec(MainnetSpecProvider.PragueActivation));
}

private AuthorizationTuple CreateAuthorizationTuple(PrivateKey signer, ulong chainId, Address codeAddress, UInt256? nonce)
private AuthorizationTuple CreateAuthorizationTuple(PrivateKey signer, ulong chainId, Address codeAddress, ulong nonce)
{
AuthorizationTupleDecoder decoder = new();
RlpStream rlp = decoder.EncodeWithoutSignature(chainId, codeAddress, nonce);
Expand Down
Loading

0 comments on commit 29ea2ec

Please sign in to comment.