Skip to content

Commit

Permalink
Remove Data from JSON-RPC Transactions (#6036)
Browse files Browse the repository at this point in the history
* Handle `AccessList` differently depending on `TxType`

- Legacy does not have an `AccessList` field
- Others have an `AccessList` field and should be at least an empty array

* Remove all fields except serializer

- Can create issues if we don't properly reset the field

* Use raw strings everywhere

* Use file-scoped namespace

* Replace string comparisons with JSON equivalence

* Formatting

* Fix whitespace

* Remove 'Data' field from 'TransactionForRpc'

- It's a duplicate of 'Input'
- Spec does not define it for any type of Tx

* Update tests

- Removed 'Data' form 'TransactionForRpc'
- Replaced string comparison with JSON equivalence
  • Loading branch information
emlautarom1 authored Aug 31, 2023
1 parent 3c42dd2 commit bc70161
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task Eth_estimateGas_web3_sample_not_enough_gas_system_account()
using Context ctx = await Context.Create();
ctx.Test.ReadOnlyState.AccountExists(Address.SystemUser).Should().BeFalse();
TransactionForRpc transaction = ctx.Test.JsonSerializer.Deserialize<TransactionForRpc>(
"{\"gasPrice\":\"0x100000\", \"data\": \"0x70a082310000000000000000000000006c1f09f6271fbe133db38db9c9280307f5d22160\", \"to\": \"0x0d8775f648430679a709e98d2b0cb6250d2887ef\"}");
"{\"gasPrice\":\"0x100000\", \"input\": \"0x70a082310000000000000000000000006c1f09f6271fbe133db38db9c9280307f5d22160\", \"to\": \"0x0d8775f648430679a709e98d2b0cb6250d2887ef\"}");
string serialized =
await ctx.Test.TestEthRpc("eth_estimateGas", ctx.Test.JsonSerializer.Serialize(transaction));
Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":\"0x53b8\",\"id\":67}"));
Expand All @@ -57,7 +57,7 @@ public async Task Eth_estimateGas_web3_sample_not_enough_gas_other_account()
Address someAccount = new("0x0001020304050607080910111213141516171819");
ctx.Test.ReadOnlyState.AccountExists(someAccount).Should().BeFalse();
TransactionForRpc transaction = ctx.Test.JsonSerializer.Deserialize<TransactionForRpc>(
"{\"from\":\"0x0001020304050607080910111213141516171819\",\"gasPrice\":\"0x100000\", \"data\": \"0x70a082310000000000000000000000006c1f09f6271fbe133db38db9c9280307f5d22160\", \"to\": \"0x0d8775f648430679a709e98d2b0cb6250d2887ef\"}");
"{\"from\":\"0x0001020304050607080910111213141516171819\",\"gasPrice\":\"0x100000\", \"input\": \"0x70a082310000000000000000000000006c1f09f6271fbe133db38db9c9280307f5d22160\", \"to\": \"0x0d8775f648430679a709e98d2b0cb6250d2887ef\"}");
string serialized =
await ctx.Test.TestEthRpc("eth_estimateGas", ctx.Test.JsonSerializer.Serialize(transaction));
Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":\"0x53b8\",\"id\":67}"));
Expand All @@ -71,7 +71,7 @@ public async Task Eth_estimateGas_web3_above_block_gas_limit()
Address someAccount = new("0x0001020304050607080910111213141516171819");
ctx.Test.ReadOnlyState.AccountExists(someAccount).Should().BeFalse();
TransactionForRpc transaction = ctx.Test.JsonSerializer.Deserialize<TransactionForRpc>(
"{\"from\":\"0x0001020304050607080910111213141516171819\",\"gas\":\"0x100000000\",\"gasPrice\":\"0x100000\", \"data\": \"0x70a082310000000000000000000000006c1f09f6271fbe133db38db9c9280307f5d22160\", \"to\": \"0x0d8775f648430679a709e98d2b0cb6250d2887ef\"}");
"{\"from\":\"0x0001020304050607080910111213141516171819\",\"gas\":\"0x100000000\",\"gasPrice\":\"0x100000\", \"input\": \"0x70a082310000000000000000000000006c1f09f6271fbe133db38db9c9280307f5d22160\", \"to\": \"0x0d8775f648430679a709e98d2b0cb6250d2887ef\"}");
string serialized =
await ctx.Test.TestEthRpc("eth_estimateGas", ctx.Test.JsonSerializer.Serialize(transaction));
Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":\"0x53b8\",\"id\":67}"));
Expand Down Expand Up @@ -121,7 +121,7 @@ public async Task Eth_estimate_gas_with_accessList(bool senderAccessList, long g

TransactionForRpc transaction =
test.JsonSerializer.Deserialize<TransactionForRpc>(
$"{{\"type\":\"0x1\", \"data\": \"{code.ToHexString(true)}\"}}");
$"{{\"type\":\"0x1\", \"input\": \"{code.ToHexString(true)}\"}}");
string serialized = await test.TestEthRpc("eth_estimateGas", test.JsonSerializer.Serialize(transaction), "0x0");
Assert.That(
serialized, Is.EqualTo($"{{\"jsonrpc\":\"2.0\",\"result\":\"{gasPriceWithoutAccessList.ToHexString(true)}\",\"id\":67}}"));
Expand All @@ -143,7 +143,7 @@ public async Task Eth_estimate_gas_is_lower_with_optimized_access_list()

TransactionForRpc transaction =
test.JsonSerializer.Deserialize<TransactionForRpc>(
$"{{\"type\":\"0x1\", \"data\": \"{code.ToHexString(true)}\"}}");
$"{{\"type\":\"0x1\", \"input\": \"{code.ToHexString(true)}\"}}");
transaction.AccessList = accessList;
string serialized = await test.TestEthRpc("eth_estimateGas", test.JsonSerializer.Serialize(transaction), "0x0");
long estimateGas = Convert.ToInt64(JToken.Parse(serialized).Value<string>("result"), 16);
Expand Down Expand Up @@ -216,7 +216,7 @@ public async Task Estimate_gas_with_base_fee_opcode()

string dataStr = code.ToHexString();
TransactionForRpc transaction = ctx.Test.JsonSerializer.Deserialize<TransactionForRpc>(
$"{{\"from\": \"0x32e4e4c7c5d1cea5db5f9202a9e4d99e56c91a24\", \"type\": \"0x2\", \"data\": \"{dataStr}\"}}");
$"{{\"from\": \"0x32e4e4c7c5d1cea5db5f9202a9e4d99e56c91a24\", \"type\": \"0x2\", \"input\": \"{dataStr}\"}}");
string serialized = await ctx.Test.TestEthRpc("eth_estimateGas", ctx.Test.JsonSerializer.Serialize(transaction));
Assert.That(
serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":\"0xe891\",\"id\":67}"));
Expand All @@ -235,7 +235,7 @@ public async Task Estimate_gas_with_revert()

string dataStr = code.ToHexString();
TransactionForRpc transaction = ctx.Test.JsonSerializer.Deserialize<TransactionForRpc>(
$"{{\"from\": \"0x32e4e4c7c5d1cea5db5f9202a9e4d99e56c91a24\", \"type\": \"0x2\", \"data\": \"{dataStr}\"}}");
$"{{\"from\": \"0x32e4e4c7c5d1cea5db5f9202a9e4d99e56c91a24\", \"type\": \"0x2\", \"input\": \"{dataStr}\"}}");
string serialized = await ctx.Test.TestEthRpc("eth_estimateGas", ctx.Test.JsonSerializer.Serialize(transaction));
Assert.That(
serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32015,\"message\":\"revert\"},\"id\":67}"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task Eth_call_no_recipient_should_work_as_init()
using Context ctx = await Context.Create();
TransactionForRpc transaction = new(Keccak.Zero, 1L, 1, new Transaction());
transaction.From = TestItem.AddressA;
transaction.Data = new byte[] { 1, 2, 3 };
transaction.Input = new byte[] { 1, 2, 3 };

string serialized =
await ctx.Test.TestEthRpc("eth_call", ctx.Test.JsonSerializer.Serialize(transaction), "latest");
Expand Down Expand Up @@ -163,7 +163,7 @@ public async Task Eth_call_with_accessList()

TransactionForRpc transaction =
test.JsonSerializer.Deserialize<TransactionForRpc>(
$"{{\"type\":\"0x1\", \"data\": \"{code.ToHexString(true)}\"}}");
$"{{\"type\":\"0x1\", \"input\": \"{code.ToHexString(true)}\"}}");

transaction.AccessList = accessList;
string serialized = await test.TestEthRpc("eth_call", test.JsonSerializer.Serialize(transaction), "0x0");
Expand Down Expand Up @@ -231,7 +231,7 @@ public async Task Eth_call_with_base_fee_opcode()

string dataStr = code.ToHexString();
TransactionForRpc transaction = ctx.Test.JsonSerializer.Deserialize<TransactionForRpc>(
$"{{\"from\": \"0x32e4e4c7c5d1cea5db5f9202a9e4d99e56c91a24\", \"type\": \"0x2\", \"data\": \"{dataStr}\"}}");
$"{{\"from\": \"0x32e4e4c7c5d1cea5db5f9202a9e4d99e56c91a24\", \"type\": \"0x2\", \"input\": \"{dataStr}\"}}");
string serialized = await ctx.Test.TestEthRpc("eth_call", ctx.Test.JsonSerializer.Serialize(transaction));
Assert.That(
serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":\"0x000000000000000000000000000000000000000000000000000000002da282a8\",\"id\":67}"));
Expand All @@ -250,7 +250,7 @@ public async Task Eth_call_with_revert()

string dataStr = code.ToHexString();
TransactionForRpc transaction = ctx.Test.JsonSerializer.Deserialize<TransactionForRpc>(
$"{{\"from\": \"0x32e4e4c7c5d1cea5db5f9202a9e4d99e56c91a24\", \"type\": \"0x2\", \"data\": \"{dataStr}\"}}");
$"{{\"from\": \"0x32e4e4c7c5d1cea5db5f9202a9e4d99e56c91a24\", \"type\": \"0x2\", \"input\": \"{dataStr}\"}}");
string serialized = await ctx.Test.TestEthRpc("eth_call", ctx.Test.JsonSerializer.Serialize(transaction));
Assert.That(
serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32015,\"message\":\"VM execution error.\",\"data\":\"revert\"},\"id\":67}"));
Expand Down
Loading

0 comments on commit bc70161

Please sign in to comment.