Skip to content

Commit

Permalink
Add tests; fix messages
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 committed Jan 21, 2025
1 parent 215156d commit 05866ba
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static long AccessListCost(Transaction transaction, IReleaseSpec releaseS
[StackTraceHidden]
static void ThrowInvalidDataException(IReleaseSpec releaseSpec)
{
throw new InvalidDataException($"Transaction with an authorization list received within the context of {releaseSpec.Name}. Eip-7702 is not enabled.");
throw new InvalidDataException($"Transaction with an access list received within the context of {releaseSpec.Name}. EIP-2930 is not enabled.");
}
}

Expand All @@ -92,7 +92,7 @@ private static long AuthorizationListCost(Transaction transaction, IReleaseSpec
[StackTraceHidden]
static void ThrowInvalidDataException(IReleaseSpec releaseSpec)
{
throw new InvalidDataException($"Transaction with an authorization list received within the context of {releaseSpec.Name}. Eip-7702 is not enabled.");
throw new InvalidDataException($"Transaction with an authorization list received within the context of {releaseSpec.Name}. EIP-7702 is not enabled.");
}
}

Expand Down
21 changes: 5 additions & 16 deletions src/Nethermind/Nethermind.JsonRpc.Test/Data/Eip2930Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void can_deserialize_null_accessList()
[Test]
public void can_deserialize_no_accessList()
{
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null,"type":"0x0"}""";
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null}""";

TransactionForRpc transactionForRpc = _serializer.Deserialize<TransactionForRpc>(json);
var transaction = transactionForRpc.ToTransaction();
Expand Down Expand Up @@ -138,7 +138,7 @@ public void can_serialize_empty_accessList(TxType txType, string txJson)
[Test]
public void can_deserialize_empty_accessList()
{
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null,"type":"0x01","accessList":[]}""";
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null,"accessList":[]}""";
TransactionForRpc transactionForRpc = _serializer.Deserialize<TransactionForRpc>(json);

transactionForRpc.Type.Should().Be(TxType.AccessList);
Expand Down Expand Up @@ -172,7 +172,7 @@ public void can_serialize_accessList_with_empty_storageKeys(TxType txType, strin
[Test]
public void can_deserialize_accessList_with_empty_storageKeys()
{
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null,"type":"0x01","accessList":[{"address":"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099","storageKeys":[]}]}""";
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null,"accessList":[{"address":"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099","storageKeys":[]}]}""";
TransactionForRpc transactionForRpc = _serializer.Deserialize<TransactionForRpc>(json);

transactionForRpc.Type.Should().Be(TxType.AccessList);
Expand All @@ -185,7 +185,7 @@ public void can_deserialize_accessList_with_empty_storageKeys()
[Test]
public void can_deserialize_accessList_with_null_storageKeys()
{
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null,"type":"0x01","accessList":[{"address":"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099"}]}""";
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null,"accessList":[{"address":"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099"}]}""";
TransactionForRpc transactionForRpc = _serializer.Deserialize<TransactionForRpc>(json);

transactionForRpc.Type.Should().Be(TxType.AccessList);
Expand All @@ -198,7 +198,7 @@ public void can_deserialize_accessList_with_null_storageKeys()
[Test]
public void can_deserialize_accessList_with_null_storageKeys_and_eip1559_txType()
{
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","maxFeePerGas":"0x10","gas":"0x0","input":null,"type":"0x02","accessList":[{"address":"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099"}]}""";
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","maxFeePerGas":"0x10","gas":"0x0","input":null,"accessList":[{"address":"0xb7705ae4c6f81b66cdb323c65f4e8133690fc099"}]}""";
TransactionForRpc transactionForRpc = _serializer.Deserialize<TransactionForRpc>(json);

transactionForRpc.Type.Should().Be(TxType.EIP1559);
Expand All @@ -219,17 +219,6 @@ public void can_deserialize_not_provided_txType()
transactionForRpc.Type.Should().Be(TxType.Legacy);
}

[Test]
public void can_deserialize_direct_null_txType()
{
string json = """{"nonce":"0x0","blockHash":null,"blockNumber":null,"transactionIndex":null,"to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null,"type":null}""";

TransactionForRpc transactionForRpc = _serializer.Deserialize<TransactionForRpc>(json);

// If there is not `TxType` provided, default value should be `TxType.Legacy`
transactionForRpc.Type.Should().Be(TxType.Legacy);
}

[TestCase(TxType.AccessList)]
[TestCase(TxType.EIP1559)]
public void can_convert_fromTransaction_toTransactionForRpc_and_back(TxType txType)
Expand Down
66 changes: 66 additions & 0 deletions src/Nethermind/Nethermind.JsonRpc.Test/Data/TxTypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core;
using Nethermind.Facade.Eth.RpcTransaction;
using Nethermind.Serialization.Json;
using NUnit.Framework;
using System.Collections;

namespace Nethermind.JsonRpc.Test.Data;

public class TransactionForRpcDeserializationTests
{
private readonly EthereumJsonSerializer _serializer = new();

[TestCaseSource(nameof(TxJsonTestCases))]
public TxType Test_TxTypeIsDetected_ForDifferentFieldSet(string txJson)
{
TransactionForRpc transactionForRpc = _serializer.Deserialize<TransactionForRpc>(txJson);
return transactionForRpc.ToTransaction().Type;
}

public static IEnumerable TxJsonTestCases
{
get
{
static TestCaseData Make(TxType expectedTxType, string json) => new(json) { TestName = $"Deserilizes into {expectedTxType} from {json}", ExpectedResult = expectedTxType };

yield return Make(TxType.Legacy, """{"nonce":"0x0","to":null,"value":"0x0","gasPrice":"0x0","gas":"0x0","input":null}""");
yield return Make(TxType.Legacy, """{"nonce":"0x0","to":null,"gasPrice":"0x0","gas":"0x0","input":null}""");
yield return Make(TxType.Legacy, """{"nonce":"0x0","to":null,"gasPrice":"0x0","gas":"0x0","input":null}""");
yield return Make(TxType.Legacy, """{"nonce":"0x0","input":null}""");
yield return Make(TxType.Legacy, """{}""");
yield return Make(TxType.Legacy, """{"additionalField":""}""");

yield return Make(TxType.AccessList, """{"nonce":"0x0","to":null,"value":"0x0","accessList":[]}""");
yield return Make(TxType.AccessList, """{"nonce":"0x0","to":null,"value":"0x0","accessList":null}""");
yield return Make(TxType.AccessList, """{"nonce":"0x0","to":null,"value":"0x0","AccessList":null}""");

yield return Make(TxType.EIP1559, """{"nonce":"0x0","to":null,"value":"0x0","accessList":[],"maxPriorityFeePerGas":"0x0"}""");
yield return Make(TxType.EIP1559, """{"nonce":"0x0","to":null,"value":"0x0","accessList":null,"maxPriorityFeePerGas":"0x0"}""");
yield return Make(TxType.EIP1559, """{"nonce":"0x0","to":null,"value":"0x0","maxPriorityFeePerGas":"0x0"}""");
yield return Make(TxType.EIP1559, """{"nonce":"0x0","to":null,"value":"0x0","maxFeePerGas":"0x0"}""");
yield return Make(TxType.EIP1559, """{"value":"0x0","maxPriorityFee":"0x0", "maxFeePerGas":"0x0"}""");
yield return Make(TxType.EIP1559, """{"maxPriorityFeePerGas":"0x0", "maxFeePerGas":"0x0"}""");
yield return Make(TxType.EIP1559, """{"maxFeePerGas":"0x0"}""");
yield return Make(TxType.EIP1559, """{"maxPriorityFeePerGas":"0x0"}""");
yield return Make(TxType.EIP1559, """{"MaxPriorityFeePerGas":"0x0"}""");


yield return Make(TxType.Blob, """{"nonce":"0x0","to":null,"value":"0x0","accessList":[],"maxFeePerBlobGas":"0x0"}""");
yield return Make(TxType.Blob, """{"nonce":"0x0","to":null,"value":"0x0","maxPriorityFeePerGas":"0x0", "maxFeePerGas":"0x0","maxFeePerBlobGas":"0x0"}""");
yield return Make(TxType.Blob, """{"maxFeePerBlobGas":"0x0", "blobVersionedHashes":[]}""");
yield return Make(TxType.Blob, """{"MaxFeePerBlobGas":"0x0"}""");
yield return Make(TxType.Blob, """{"blobVersionedHashes":[]}""");
yield return Make(TxType.Blob, """{"BlobVersionedHashes":null}""");
yield return Make(TxType.Blob, """{"blobVersionedHashes":["0x01f1872d656b7a820d763e6001728b9b883f829b922089ec6ad7f5f1665470dc"]}""");


yield return Make(TxType.SetCode, """{"nonce":"0x0","to":null,"value":"0x0","accessList":[],"authorizationList":[]}""");
yield return Make(TxType.SetCode, """{"nonce":"0x0","to":null,"value":"0x0","maxPriorityFeePerGas":"0x0", "maxFeePerGas":"0x0","authorizationList":[]}""");
yield return Make(TxType.SetCode, """{"authorizationList":null}""");
yield return Make(TxType.SetCode, """{"AuthorizationList":[]}""");
}
}
}

0 comments on commit 05866ba

Please sign in to comment.