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

Add stylus programSize and programMemoryFootprint precompiles #120

Merged
merged 11 commits into from
Sep 6, 2023
4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/create/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/erc20/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/evm-data/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/fallible/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/keccak-100/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/keccak/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/log/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/multicall/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/read-return-data/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/sdk-storage/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions arbitrator/stylus/tests/storage/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions arbos/programs/programs.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ func (p Programs) CodehashVersion(codeHash common.Hash) (uint16, error) {
return program.version, err
}

func (p Programs) CodehashSize(codeHash common.Hash) (uint32, error) {
program, err := p.deserializeProgram(codeHash)
// wasmSize represents the number of half kb units, return as bytes
return uint32(program.wasmSize) * 512, err
}

func (p Programs) CodehashMemoryFootprint(codeHash common.Hash) (uint16, error) {
program, err := p.deserializeProgram(codeHash)
return program.footprint, err
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's name these ProgramSize and ProgramMemoryFootprint, but still have them take codehashes

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

type goParams struct {
version uint16
maxDepth uint32
Expand Down
2 changes: 1 addition & 1 deletion contracts
18 changes: 18 additions & 0 deletions precompiles/ArbWasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ func (con ArbWasm) ProgramVersion(c ctx, evm mech, program addr) (uint16, error)
return con.CodehashVersion(c, evm, codehash)
}

// ProgramSize returns the uncompressed size of program at addr
func (con ArbWasm) ProgramSize(c ctx, _ mech, program addr) (uint32, error) {
codehash, err := c.GetCodeHash(program)
if err != nil {
return 0, err
}
return c.State.Programs().CodehashSize(codehash)
}

// ProgramMemoryFootprint returns the footprint of program at addr
func (con ArbWasm) ProgramMemoryFootprint(c ctx, _ mech, program addr) (uint16, error) {
codehash, err := c.GetCodeHash(program)
if err != nil {
return 0, err
}
return c.State.Programs().CodehashMemoryFootprint(codehash)
}

// Gets the added wasm call cost paid per half kb uncompressed wasm
func (con ArbWasm) CallScalar(c ctx, _ mech) (uint16, error) {
return c.State.Programs().CallScalar()
Expand Down
10 changes: 10 additions & 0 deletions system_tests/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ func keccakTest(t *testing.T, jit bool) {
if otherVersion != programVersion {
Fatal(t, "mismatched versions", stylusVersion, programVersion)
}
programSize, err := arbWasm.ProgramSize(nil, programAddress)
Require(t, err)
if programSize < 5000 || programSize > 20000 {
Fatal(t, "unexpected size", programSize)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we know the exact size and could check against it

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

programMemoryFootprint, err := arbWasm.ProgramMemoryFootprint(nil, programAddress)
Require(t, err)
if programMemoryFootprint != 1 {
Fatal(t, "unexpected memory footprint", programMemoryFootprint)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets move this to the memory test, which has an example that's known to be 120 pages :)

Copy link
Member Author

Choose a reason for hiding this comment

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

👍


preimage := []byte("°º¤ø,¸,ø¤°º¤ø,¸,ø¤°º¤ø,¸ nyan nyan ~=[,,_,,]:3 nyan nyan")
correct := crypto.Keccak256Hash(preimage)
Expand Down
Loading