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

Test 🧪: add accessList gas cost tests #3134

Merged
merged 10 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions test/contracts/src/AccessListHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract AccessListHelper {
uint256 private storedValue;
pLabarta marked this conversation as resolved.
Show resolved Hide resolved

// Function to set the stored value
function setValue(uint256 _value) public {
storedValue = _value;
}

// Function to load the stored value multiple times using assembly and sload
function loadValueMultipleTimes(uint256 times) public view returns (uint256 total) {
total = 0;
uint256 loaded;
for (uint256 i = 0; i < times; i++) {
assembly {
loaded := sload(storedValue.slot)
}
total += loaded;
}

return total;
}
}

// pragma solidity ^0.8.0;

// import "./AccessListHelper.sol";

contract AccessListHelperProxy {
AccessListHelper accessListHelper;

constructor(address helper) {
accessListHelper = AccessListHelper(helper);
}

function callHelper(uint256 times) public view returns (uint256 value) {
value = accessListHelper.loadValueMultipleTimes(times);
}
}
189 changes: 189 additions & 0 deletions test/suites/dev/moonbase/test-eth-tx/test-eth-tx-access-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import "@moonbeam-network/api-augment";
import { beforeAll, deployCreateCompiledContract, describeSuite, expect } from "@moonwall/cli";
import { createViemTransaction } from "@moonwall/util";
import { error } from "node:console";

describeSuite({
id: "D011304",
title: "Ethereum Transaction - Access List",
foundationMethods: "dev",
testCases: ({ context, it }) => {
let data;
let helper;
let helperProxy;

beforeAll(async () => {
helper = await deployCreateCompiledContract(context, "AccessListHelper");
helperProxy = await deployCreateCompiledContract(context, "AccessListHelperProxy", {
args: [helper.contractAddress],
});
});

it({
id: "T01",
title: "after the 4th one, additional storage keys should cost 1900 gas",
test: async function () {
const keys = generateSequentialStorageKeys(100);

interface Results {
keys: number;
size: number;
gasWithAL: bigint;
}

const cases = Array.from({ length: 100 }, (_, i) => i + 1);

const results: Results[] = [];

for (const n of cases) {
const txWithAL = await createViemTransaction(context, {
to: helperProxy.contractAddress,
data: data,
gas: 1000000n,
accessList: [
{
address: helper.contractAddress,
storageKeys: keys.slice(0, n),
},
],
});

await context.createBlock(txWithAL);
const block = await context.viem().getBlock();
const receipt = await context
.viem()
.getTransactionReceipt({ hash: block.transactions[0] as `0x${string}` });
const gasCostWithAL = receipt.gasUsed;
const txSize = txWithAL.length;

results.push({
keys: n,
size: txSize,
gasWithAL: gasCostWithAL,
});
}

results.forEach((result, index) => {
const diff = index === 0 ? "" : result.gasWithAL - results[index - 1].gasWithAL;
if (result.keys > 4) {
expect(
diff,
`Expected gas did not match when including ${result.keys} storage keys`
).toBe(1900n);
}
});
},
});

it({
id: "T02",
title: "after the 4th one, additional addresses should cost 2400 gas",
test: async function () {
const addresses = randomAddresses(100);

interface Results {
addresses: number;
size: number;
gasWithAL: bigint;
}

interface Address {
address: `0x${string}`;
storageKeys: `0x${string}`[];
}

const cases = Array.from({ length: 100 }, (_, i) => i + 1);

const results: Results[] = [];

for (const n of cases) {
const accessList: Address[] = [];
for (let i = 0; i < n; i++) {
accessList.push({
address: addresses[i],
storageKeys: [],
});
}

const txWithAL = await createViemTransaction(context, {
to: helperProxy.contractAddress,
data: data,
gas: 1000000n,
accessList,
});

await context.createBlock(txWithAL);
const block = await context.viem().getBlock();
const receipt = await context
.viem()
.getTransactionReceipt({ hash: block.transactions[0] as `0x${string}` });
const gasCostWithAL = receipt.gasUsed;
const txSize = txWithAL.length;

results.push({
addresses: n,
size: txSize,
gasWithAL: gasCostWithAL,
});
}

results.forEach((result, index) => {
const diff = index === 0 ? 0n : result.gasWithAL - results[index - 1].gasWithAL;
if (result.addresses > 4) {
expect(
diff,
`Expected gas did not match when including ${result.addresses} addresses`
).toBe(2400n);
}
});
},
});

it({
id: "T03",
title: "transaction should not be gossiped if it exceeds the gas limit",
test: async function () {
const keys = generateSequentialStorageKeys(100);

const bigTxWithAL = await createViemTransaction(context, {
to: helperProxy.contractAddress,
data: data,
gas: 100000000n,
accessList: [
{
address: helper.contractAddress,
storageKeys: keys,
},
],
});

try {
await context.viem().sendRawTransaction({ serializedTransaction: bigTxWithAL });
error("Transaction should not have been gossiped");
} catch (e) {
expect(e.message).toContain("exceeds block gas limit");
}
},
});
},
});

function generateSequentialStorageKeys(n: number): `0x${string}`[] {
const keys: `0x${string}`[] = [];
for (let i = 0; i < n; i++) {
keys.push(`0x${i.toString().padStart(64, "0")}`);
}
return keys;
}

function randomAddresses(n: number): `0x${string}`[] {
const addresses: `0x${string}`[] = [];
for (let i = 0; i < n; i++) {
let current = "0x";
for (let j = 0; j < 40; j++) {
current += Math.floor(Math.random() * 16).toString(16);
}
addresses.push(current as `0x${string}`);
}
return addresses;
}
Loading