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

[NIT-2798] Test Stylus gas usage #2708

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

gligneul
Copy link
Contributor

@gligneul gligneul commented Sep 27, 2024

These tests compare the gas usage of EVM opcodes and Stylus HostIOs.

This PR depends on the following contracts PR: OffchainLabs/nitro-contracts#255

@cla-bot cla-bot bot added the s Automatically added by the CLA bot if the creator of a PR is registered as having signed the CLA. label Sep 27, 2024
@gligneul gligneul force-pushed the gligneul/program-gas-tests branch 2 times, most recently from 238015a to f0a9350 Compare September 27, 2024 15:03
These tests compare the gas usage of EVM opcodes and Stylus HostIOs.

Co-authored-by: Aman Sanghi <[email protected]>
Co-authored-by: Tsahi Zidenberg <[email protected]>
@gligneul gligneul marked this pull request as ready for review September 30, 2024 16:08
Copy link
Member

@eljobe eljobe left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@tsahee tsahee left a comment

Choose a reason for hiding this comment

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

Great work. Well structured and thorough. A few comments. The ones about maxdiff formulae is more important.

t.Cleanup(cleanup)
l2info = builder.L2Info
l2client = builder.L2.Client
auth = builder.L2Info.GetDefaultTransactOpts("Owner", ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: cleaner to just return builder.
I'm in favor of tests using builder.l2info, builder.L2.Client etc directly. the very common l2info = builder.L2Info was added when we created builder

Copy link
Contributor

Choose a reason for hiding this comment

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

not critical, as it's very common in other tests.

{hostio: "contract_address", opcode: vm.ADDRESS, maxDiff: 0.5},
{hostio: "math_div", opcode: vm.DIV, params: []any{big.NewInt(1), big.NewInt(3)}},
{hostio: "math_mod", opcode: vm.MOD, params: []any{big.NewInt(1), big.NewInt(3)}},
{hostio: "math_pow", opcode: vm.EXP, params: []any{big.NewInt(1), new(big.Int).Lsh(big.NewInt(1), 255)}, maxDiff: 2}, // worst case
Copy link
Contributor

Choose a reason for hiding this comment

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

maxDiff of 2 is pretty much checking nothing with current formula.
I think math_ functions will eventually be tested in our wasm gas test vs. execution time and not vs. EVM.
In the meantime, it's good to have them here, but if we don't have a good bound on math_pow vm it's better to compare it to a known constant than to EVM, with a comment explaining it.

if maxAllowedDifference == 0 {
maxAllowedDifference = 0.25
}
percentageDifference := math.Abs(a-b) / ((a + b) / 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem with this formulae is it's a little deceiving. No two numbers will give a result greater than 2.
Maybe do something like [(max(a,b) / min(a,b)) - 1]. It will be close in the relevant area but much easier to comprehend.


// sendAndEnsureTransaction sends a transaction, ensures it succeed, and returns the total gas cost.
func sendAndEnsureTransaction(t *testing.T, ctx context.Context, client *ethclient.Client, tx *types.Transaction) uint64 {
t.Helper()
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: if you hold on to builder, you can use builder.L2.SendWaitTestTransactions(t, )
I am hoping to have more tests build using these utils, and possibly adding more.. as it is I think we have too many variats of functions sending and ensuring transactions..

Copy link
Contributor

Choose a reason for hiding this comment

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

just realised that SendWaitTestTransactions doesn't return receipt. Maybe we need a single tx function that does, or SendWaiTest could return an array of receipts.. feel free to either do something with it or ignore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s Automatically added by the CLA bot if the creator of a PR is registered as having signed the CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants