From 65d347d4f368a0275e8a1787e206712e56b33bde Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Mon, 4 Nov 2024 15:17:55 -0300 Subject: [PATCH 1/9] TestHostioWithoutEVMEquivalentCosts --- .../stylus/tests/hostio-test/src/main.rs | 35 +++++++++++++ system_tests/program_gas_test.go | 49 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/arbitrator/stylus/tests/hostio-test/src/main.rs b/arbitrator/stylus/tests/hostio-test/src/main.rs index 17a5d10266..cf6e70bf4c 100644 --- a/arbitrator/stylus/tests/hostio-test/src/main.rs +++ b/arbitrator/stylus/tests/hostio-test/src/main.rs @@ -204,4 +204,39 @@ impl HostioTest { fn tx_origin() -> Result
{ Ok(tx::origin()) } + + fn msg_reentrant() { + unsafe { + hostio::msg_reentrant(); + } + } + + fn storage_cache_bytes32() { + let key = B256::ZERO; + let val = B256::ZERO; + unsafe { + hostio::storage_cache_bytes32(key.as_ptr(), val.as_ptr()); + } + } + + fn pay_for_memory_grow() { + unsafe { + hostio::pay_for_memory_grow(100); + } + } + + fn write_result() { + let len = 10000; + let data = vec![0; len]; + unsafe { + hostio::write_result(data.as_ptr(), len); + } + } + + fn read_args() { + let mut data = vec![0; 10000]; + unsafe { + hostio::read_args(data.as_mut_ptr()); + } + } } diff --git a/system_tests/program_gas_test.go b/system_tests/program_gas_test.go index 10a371532d..ef82925b45 100644 --- a/system_tests/program_gas_test.go +++ b/system_tests/program_gas_test.go @@ -13,6 +13,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth/tracers/logger" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" @@ -23,6 +24,54 @@ import ( "github.com/offchainlabs/nitro/util/testhelpers" ) +func TestHostioWithoutEVMEquivalentCosts(t *testing.T) { + builder := setupGasCostTest(t) + auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) + stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) + matchSnake := regexp.MustCompile("_[a-z]") + + for _, tc := range []struct { + hostio string + expectedInc uint64 + }{ + {hostio: "read_args", expectedInc: 8400 + 5040}, + {hostio: "write_result", expectedInc: 8400 + (16381+55*(10000-32))*2}, + {hostio: "storage_cache_bytes32", expectedInc: 8400 + (13440-8400)*2}, + {hostio: "msg_reentrant", expectedInc: 8400}, + {hostio: "pay_for_memory_grow", expectedInc: 9320660000}, + } { + t.Run(tc.hostio, func(t *testing.T) { + funcName := matchSnake.ReplaceAllStringFunc(tc.hostio, func(s string) string { + return strings.ToUpper(strings.TrimPrefix(s, "_")) + }) + signature := fmt.Sprintf("%v()", funcName) + data := crypto.Keccak256([]byte(signature))[:4] + + const txGas uint64 = 32_000_000 + tx := builder.L2Info.PrepareTxTo("Owner", &stylusProgram, txGas, nil, data) + + err := builder.L2.Client.SendTransaction(builder.ctx, tx) + Require(t, err) + _, err = builder.L2.EnsureTxSucceeded(tx) + Require(t, err) + + stylusGasUsage, err := stylusHostiosGasUsage(builder.ctx, builder.L2.Client.Client(), tx) + Require(t, err) + + _, ok := stylusGasUsage[tc.hostio] + if !ok { + Fatal(t, "hostio not found in gas usage", "hostio", tc.hostio, "stylusGasUsage", stylusGasUsage) + } + + expectedGas := float64(tc.expectedInc) / 10000 + returnedGas := stylusGasUsage[tc.hostio][0] + if math.Abs(expectedGas-returnedGas) > 1e-9 { + Fatal(t, "unexpected gas usage", "hostio", tc.hostio, "expected", expectedGas, "returned", returnedGas) + } + }) + } +} + func TestProgramSimpleCost(t *testing.T) { builder := setupGasCostTest(t) auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) From 9dde2d3e95c8d6b9d81ed6df1ce4d411710c6c07 Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Mon, 4 Nov 2024 15:22:47 -0300 Subject: [PATCH 2/9] Renames test to TestGasUsageOfHostiosThatDontHaveGoodEVMEquivalents --- system_tests/program_gas_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system_tests/program_gas_test.go b/system_tests/program_gas_test.go index ef82925b45..b5f1a49a88 100644 --- a/system_tests/program_gas_test.go +++ b/system_tests/program_gas_test.go @@ -24,7 +24,7 @@ import ( "github.com/offchainlabs/nitro/util/testhelpers" ) -func TestHostioWithoutEVMEquivalentCosts(t *testing.T) { +func TestGasUsageOfHostiosThatDontHaveGoodEVMEquivalents(t *testing.T) { builder := setupGasCostTest(t) auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) From 3e9ef9e9f86b61dd77dacf0f22b879df93426aa3 Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Mon, 4 Nov 2024 15:30:42 -0300 Subject: [PATCH 3/9] expectedInc to expectedInk --- system_tests/program_gas_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/system_tests/program_gas_test.go b/system_tests/program_gas_test.go index b5f1a49a88..fc3c98bec1 100644 --- a/system_tests/program_gas_test.go +++ b/system_tests/program_gas_test.go @@ -32,13 +32,13 @@ func TestGasUsageOfHostiosThatDontHaveGoodEVMEquivalents(t *testing.T) { for _, tc := range []struct { hostio string - expectedInc uint64 + expectedInk uint64 }{ - {hostio: "read_args", expectedInc: 8400 + 5040}, - {hostio: "write_result", expectedInc: 8400 + (16381+55*(10000-32))*2}, - {hostio: "storage_cache_bytes32", expectedInc: 8400 + (13440-8400)*2}, - {hostio: "msg_reentrant", expectedInc: 8400}, - {hostio: "pay_for_memory_grow", expectedInc: 9320660000}, + {hostio: "read_args", expectedInk: 8400 + 5040}, + {hostio: "write_result", expectedInk: 8400 + (16381+55*(10000-32))*2}, + {hostio: "storage_cache_bytes32", expectedInk: 8400 + (13440-8400)*2}, + {hostio: "msg_reentrant", expectedInk: 8400}, + {hostio: "pay_for_memory_grow", expectedInk: 9320660000}, } { t.Run(tc.hostio, func(t *testing.T) { funcName := matchSnake.ReplaceAllStringFunc(tc.hostio, func(s string) string { @@ -63,7 +63,7 @@ func TestGasUsageOfHostiosThatDontHaveGoodEVMEquivalents(t *testing.T) { Fatal(t, "hostio not found in gas usage", "hostio", tc.hostio, "stylusGasUsage", stylusGasUsage) } - expectedGas := float64(tc.expectedInc) / 10000 + expectedGas := float64(tc.expectedInk) / 10000 returnedGas := stylusGasUsage[tc.hostio][0] if math.Abs(expectedGas-returnedGas) > 1e-9 { Fatal(t, "unexpected gas usage", "hostio", tc.hostio, "expected", expectedGas, "returned", returnedGas) From 6f43808abcc78a865c7097ea52f4d94abb519926 Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Fri, 8 Nov 2024 12:20:08 -0300 Subject: [PATCH 4/9] Split TestHostioWithoutEVMEquivalentCosts in multiple tests, test hostios with multiple arguments --- .../stylus/tests/hostio-test/src/main.rs | 36 ++-- system_tests/program_gas_test.go | 158 ++++++++++++++---- 2 files changed, 138 insertions(+), 56 deletions(-) diff --git a/arbitrator/stylus/tests/hostio-test/src/main.rs b/arbitrator/stylus/tests/hostio-test/src/main.rs index cf6e70bf4c..47b46daad2 100644 --- a/arbitrator/stylus/tests/hostio-test/src/main.rs +++ b/arbitrator/stylus/tests/hostio-test/src/main.rs @@ -205,12 +205,6 @@ impl HostioTest { Ok(tx::origin()) } - fn msg_reentrant() { - unsafe { - hostio::msg_reentrant(); - } - } - fn storage_cache_bytes32() { let key = B256::ZERO; let val = B256::ZERO; @@ -219,24 +213,28 @@ impl HostioTest { } } - fn pay_for_memory_grow() { + fn pay_for_memory_grow(pages: U256) { + let pages: u16 = pages.try_into().unwrap(); unsafe { - hostio::pay_for_memory_grow(100); + hostio::pay_for_memory_grow(pages); } } - fn write_result() { - let len = 10000; - let data = vec![0; len]; - unsafe { - hostio::write_result(data.as_ptr(), len); - } + fn write_result_empty() { } - fn read_args() { - let mut data = vec![0; 10000]; - unsafe { - hostio::read_args(data.as_mut_ptr()); - } + fn write_result(size: U256) -> Result> { + let size: usize = size.try_into().unwrap(); + let data = vec![0; size]; + Ok(data) + } + + fn read_args_no_args() { + } + + fn read_args_one_arg(_arg1: U256) { + } + + fn read_args_three_args(_arg1: U256, _arg2: U256, _arg3: U256) { } } diff --git a/system_tests/program_gas_test.go b/system_tests/program_gas_test.go index fc3c98bec1..8920c840b2 100644 --- a/system_tests/program_gas_test.go +++ b/system_tests/program_gas_test.go @@ -2,6 +2,7 @@ package arbtest import ( "context" + "encoding/binary" "fmt" "math" "math/big" @@ -24,52 +25,135 @@ import ( "github.com/offchainlabs/nitro/util/testhelpers" ) -func TestGasUsageOfHostiosThatDontHaveGoodEVMEquivalents(t *testing.T) { +func checkInkUsage( + t *testing.T, + builder *NodeBuilder, + stylusProgram common.Address, + hostio string, + signature string, + params []uint32, + expectedInk uint64, +) { + toU256ByteSlice := func(i uint32) []byte { + arr := make([]byte, 32) + binary.BigEndian.PutUint32(arr[28:32], i) + return arr[:] + } + + testName := fmt.Sprintf("%v_%v", signature, params) + + data := crypto.Keccak256([]byte(signature))[:4] + for _, p := range params { + data = append(data, toU256ByteSlice(p)...) + } + + const txGas uint64 = 32_000_000 + tx := builder.L2Info.PrepareTxTo("Owner", &stylusProgram, txGas, nil, data) + + err := builder.L2.Client.SendTransaction(builder.ctx, tx) + Require(t, err, "testName", testName) + _, err = builder.L2.EnsureTxSucceeded(tx) + Require(t, err, "testName", testName) + + stylusGasUsage, err := stylusHostiosGasUsage(builder.ctx, builder.L2.Client.Client(), tx) + Require(t, err, "testName", testName) + + _, ok := stylusGasUsage[hostio] + if !ok { + Fatal(t, "hostio not found in gas usage", "hostio", hostio, "stylusGasUsage", stylusGasUsage, "testName", testName) + } + + if len(stylusGasUsage[hostio]) != 1 { + Fatal(t, "unexpected number of gas usage", "hostio", hostio, "stylusGasUsage", stylusGasUsage, "testName", testName) + } + + expectedGas := float64(expectedInk) / 10000 + returnedGas := stylusGasUsage[hostio][0] + if math.Abs(expectedGas-returnedGas) > 1e-9 { + Fatal(t, "unexpected gas usage", "hostio", hostio, "expected", expectedGas, "returned", returnedGas, "testName", testName) + } +} + +func TestWriteResultGasUsage(t *testing.T) { builder := setupGasCostTest(t) auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) - matchSnake := regexp.MustCompile("_[a-z]") - for _, tc := range []struct { - hostio string - expectedInk uint64 - }{ - {hostio: "read_args", expectedInk: 8400 + 5040}, - {hostio: "write_result", expectedInk: 8400 + (16381+55*(10000-32))*2}, - {hostio: "storage_cache_bytes32", expectedInk: 8400 + (13440-8400)*2}, - {hostio: "msg_reentrant", expectedInk: 8400}, - {hostio: "pay_for_memory_grow", expectedInk: 9320660000}, - } { - t.Run(tc.hostio, func(t *testing.T) { - funcName := matchSnake.ReplaceAllStringFunc(tc.hostio, func(s string) string { - return strings.ToUpper(strings.TrimPrefix(s, "_")) - }) - signature := fmt.Sprintf("%v()", funcName) - data := crypto.Keccak256([]byte(signature))[:4] + hostio := "write_result" - const txGas uint64 = 32_000_000 - tx := builder.L2Info.PrepareTxTo("Owner", &stylusProgram, txGas, nil, data) + // writeResultEmpty doesn't return any value + signature := "writeResultEmpty()" + expectedInk := 8400 + 16381*2 + checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) - err := builder.L2.Client.SendTransaction(builder.ctx, tx) - Require(t, err) - _, err = builder.L2.EnsureTxSucceeded(tx) - Require(t, err) + // writeResult(uint256) returns an array of uint256 + signature = "writeResult(uint256)" + numberOfElementsInReturnedArray := 10000 + arrayOverhead := 32 + 32 // 32 bytes for the array length and 32 bytes for the array offset + expectedInk = 8400 + (16381+55*(32*numberOfElementsInReturnedArray+arrayOverhead-32))*2 + checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{uint32(numberOfElementsInReturnedArray)}, uint64(expectedInk)) - stylusGasUsage, err := stylusHostiosGasUsage(builder.ctx, builder.L2.Client.Client(), tx) - Require(t, err) + signature = "writeResult(uint256)" + numberOfElementsInReturnedArray = 0 + expectedInk = 8400 + (16381+55*(arrayOverhead-32))*2 + checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{uint32(numberOfElementsInReturnedArray)}, uint64(expectedInk)) +} - _, ok := stylusGasUsage[tc.hostio] - if !ok { - Fatal(t, "hostio not found in gas usage", "hostio", tc.hostio, "stylusGasUsage", stylusGasUsage) - } +func TestReadArgsGasUsage(t *testing.T) { + builder := setupGasCostTest(t) + auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) + stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) - expectedGas := float64(tc.expectedInk) / 10000 - returnedGas := stylusGasUsage[tc.hostio][0] - if math.Abs(expectedGas-returnedGas) > 1e-9 { - Fatal(t, "unexpected gas usage", "hostio", tc.hostio, "expected", expectedGas, "returned", returnedGas) - } - }) - } + hostio := "read_args" + + signature := "readArgsNoArgs()" + expectedInk := 8400 + 5040 + checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) + + signature = "readArgsOneArg(uint256)" + signatureOverhead := 4 + expectedInk = 8400 + 5040 + 30*(32+signatureOverhead-32) + checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{1}, uint64(expectedInk)) + + signature = "readArgsThreeArgs(uint256,uint256,uint256)" + expectedInk = 8400 + 5040 + 30*(3*32+signatureOverhead-32) + checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{1, 1, 1}, uint64(expectedInk)) +} + +func TestMsgReentrantGasUsage(t *testing.T) { + builder := setupGasCostTest(t) + auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) + stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) + + hostio := "msg_reentrant" + + signature := "writeResultEmpty()" + expectedInk := 8400 + checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) +} + +func TestStorageCacheBytes32GasUsage(t *testing.T) { + builder := setupGasCostTest(t) + auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) + stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) + + hostio := "storage_cache_bytes32" + + signature := "storageCacheBytes32()" + expectedInk := 8400 + (13440-8400)*2 + checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) +} + +func TestPayForMemoryGrowGasUsage(t *testing.T) { + builder := setupGasCostTest(t) + auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) + stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) + + hostio := "pay_for_memory_grow" + signature := "payForMemoryGrow(uint256)" + + expectedInk := 9320660000 + checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{100}, uint64(expectedInk)) } func TestProgramSimpleCost(t *testing.T) { From c502d68b54277b08c6cdd82101adc81dbe34376a Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Fri, 8 Nov 2024 15:27:11 -0300 Subject: [PATCH 5/9] Adds missing t.Parallel() to gas usage hostio tests --- system_tests/program_gas_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/system_tests/program_gas_test.go b/system_tests/program_gas_test.go index 8920c840b2..8c463a2c81 100644 --- a/system_tests/program_gas_test.go +++ b/system_tests/program_gas_test.go @@ -75,6 +75,8 @@ func checkInkUsage( } func TestWriteResultGasUsage(t *testing.T) { + t.Parallel() + builder := setupGasCostTest(t) auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) @@ -100,6 +102,8 @@ func TestWriteResultGasUsage(t *testing.T) { } func TestReadArgsGasUsage(t *testing.T) { + t.Parallel() + builder := setupGasCostTest(t) auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) @@ -121,6 +125,8 @@ func TestReadArgsGasUsage(t *testing.T) { } func TestMsgReentrantGasUsage(t *testing.T) { + t.Parallel() + builder := setupGasCostTest(t) auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) @@ -133,6 +139,8 @@ func TestMsgReentrantGasUsage(t *testing.T) { } func TestStorageCacheBytes32GasUsage(t *testing.T) { + t.Parallel() + builder := setupGasCostTest(t) auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) @@ -145,6 +153,8 @@ func TestStorageCacheBytes32GasUsage(t *testing.T) { } func TestPayForMemoryGrowGasUsage(t *testing.T) { + t.Parallel() + builder := setupGasCostTest(t) auth := builder.L2Info.GetDefaultTransactOpts("Owner", builder.ctx) stylusProgram := deployWasm(t, builder.ctx, auth, builder.L2.Client, rustFile("hostio-test")) From 2d901690e9ee442d027d7de336e1499222563cb8 Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Fri, 8 Nov 2024 15:41:46 -0300 Subject: [PATCH 6/9] TestPayForMemoryGrowGasUsage with zero pages --- arbitrator/wasm-libraries/user-host-trait/src/lib.rs | 2 +- system_tests/program_gas_test.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/arbitrator/wasm-libraries/user-host-trait/src/lib.rs b/arbitrator/wasm-libraries/user-host-trait/src/lib.rs index 35a4a31347..2f410849fc 100644 --- a/arbitrator/wasm-libraries/user-host-trait/src/lib.rs +++ b/arbitrator/wasm-libraries/user-host-trait/src/lib.rs @@ -936,7 +936,7 @@ pub trait UserHost: GasMeteredMachine { fn pay_for_memory_grow(&mut self, pages: u16) -> Result<(), Self::Err> { if pages == 0 { self.buy_ink(HOSTIO_INK)?; - return Ok(()); + return trace!("pay_for_memory_grow", self, be!(pages), &[]); } let gas_cost = self.evm_api().add_pages(pages); // no sentry needed since the work happens after the hostio self.buy_gas(gas_cost)?; diff --git a/system_tests/program_gas_test.go b/system_tests/program_gas_test.go index 8c463a2c81..264a6603fa 100644 --- a/system_tests/program_gas_test.go +++ b/system_tests/program_gas_test.go @@ -164,6 +164,9 @@ func TestPayForMemoryGrowGasUsage(t *testing.T) { expectedInk := 9320660000 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{100}, uint64(expectedInk)) + + expectedInk = 8400 + checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{0}, uint64(expectedInk)) } func TestProgramSimpleCost(t *testing.T) { From 28d71f1bb163583b9ec67d6fa6e1671116f6689c Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Fri, 8 Nov 2024 15:43:31 -0300 Subject: [PATCH 7/9] HOSTIO_INK const --- system_tests/program_gas_test.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/system_tests/program_gas_test.go b/system_tests/program_gas_test.go index 264a6603fa..68fd427526 100644 --- a/system_tests/program_gas_test.go +++ b/system_tests/program_gas_test.go @@ -25,6 +25,8 @@ import ( "github.com/offchainlabs/nitro/util/testhelpers" ) +const HOSTIO_INK = 8400 + func checkInkUsage( t *testing.T, builder *NodeBuilder, @@ -85,19 +87,19 @@ func TestWriteResultGasUsage(t *testing.T) { // writeResultEmpty doesn't return any value signature := "writeResultEmpty()" - expectedInk := 8400 + 16381*2 + expectedInk := HOSTIO_INK + 16381*2 checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) // writeResult(uint256) returns an array of uint256 signature = "writeResult(uint256)" numberOfElementsInReturnedArray := 10000 arrayOverhead := 32 + 32 // 32 bytes for the array length and 32 bytes for the array offset - expectedInk = 8400 + (16381+55*(32*numberOfElementsInReturnedArray+arrayOverhead-32))*2 + expectedInk = HOSTIO_INK + (16381+55*(32*numberOfElementsInReturnedArray+arrayOverhead-32))*2 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{uint32(numberOfElementsInReturnedArray)}, uint64(expectedInk)) signature = "writeResult(uint256)" numberOfElementsInReturnedArray = 0 - expectedInk = 8400 + (16381+55*(arrayOverhead-32))*2 + expectedInk = HOSTIO_INK + (16381+55*(arrayOverhead-32))*2 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{uint32(numberOfElementsInReturnedArray)}, uint64(expectedInk)) } @@ -111,16 +113,16 @@ func TestReadArgsGasUsage(t *testing.T) { hostio := "read_args" signature := "readArgsNoArgs()" - expectedInk := 8400 + 5040 + expectedInk := HOSTIO_INK + 5040 checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) signature = "readArgsOneArg(uint256)" signatureOverhead := 4 - expectedInk = 8400 + 5040 + 30*(32+signatureOverhead-32) + expectedInk = HOSTIO_INK + 5040 + 30*(32+signatureOverhead-32) checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{1}, uint64(expectedInk)) signature = "readArgsThreeArgs(uint256,uint256,uint256)" - expectedInk = 8400 + 5040 + 30*(3*32+signatureOverhead-32) + expectedInk = HOSTIO_INK + 5040 + 30*(3*32+signatureOverhead-32) checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{1, 1, 1}, uint64(expectedInk)) } @@ -134,7 +136,7 @@ func TestMsgReentrantGasUsage(t *testing.T) { hostio := "msg_reentrant" signature := "writeResultEmpty()" - expectedInk := 8400 + expectedInk := HOSTIO_INK checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) } @@ -148,7 +150,7 @@ func TestStorageCacheBytes32GasUsage(t *testing.T) { hostio := "storage_cache_bytes32" signature := "storageCacheBytes32()" - expectedInk := 8400 + (13440-8400)*2 + expectedInk := HOSTIO_INK + (13440-HOSTIO_INK)*2 checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) } @@ -165,7 +167,7 @@ func TestPayForMemoryGrowGasUsage(t *testing.T) { expectedInk := 9320660000 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{100}, uint64(expectedInk)) - expectedInk = 8400 + expectedInk = HOSTIO_INK checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{0}, uint64(expectedInk)) } From 6307447a2a651be0c91a2b07331e53ef3a6c57c7 Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Mon, 11 Nov 2024 09:55:33 -0300 Subject: [PATCH 8/9] Fix lint issues --- system_tests/program_gas_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/system_tests/program_gas_test.go b/system_tests/program_gas_test.go index 68fd427526..3260e91d51 100644 --- a/system_tests/program_gas_test.go +++ b/system_tests/program_gas_test.go @@ -39,7 +39,7 @@ func checkInkUsage( toU256ByteSlice := func(i uint32) []byte { arr := make([]byte, 32) binary.BigEndian.PutUint32(arr[28:32], i) - return arr[:] + return arr } testName := fmt.Sprintf("%v_%v", signature, params) @@ -88,6 +88,7 @@ func TestWriteResultGasUsage(t *testing.T) { // writeResultEmpty doesn't return any value signature := "writeResultEmpty()" expectedInk := HOSTIO_INK + 16381*2 + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) // writeResult(uint256) returns an array of uint256 @@ -95,11 +96,13 @@ func TestWriteResultGasUsage(t *testing.T) { numberOfElementsInReturnedArray := 10000 arrayOverhead := 32 + 32 // 32 bytes for the array length and 32 bytes for the array offset expectedInk = HOSTIO_INK + (16381+55*(32*numberOfElementsInReturnedArray+arrayOverhead-32))*2 + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{uint32(numberOfElementsInReturnedArray)}, uint64(expectedInk)) signature = "writeResult(uint256)" numberOfElementsInReturnedArray = 0 expectedInk = HOSTIO_INK + (16381+55*(arrayOverhead-32))*2 + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{uint32(numberOfElementsInReturnedArray)}, uint64(expectedInk)) } From 7c6cf629ebb829cc74eba0d2e48eb4e09d6a778d Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Mon, 11 Nov 2024 10:15:30 -0300 Subject: [PATCH 9/9] Fix lint issues --- system_tests/program_gas_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/system_tests/program_gas_test.go b/system_tests/program_gas_test.go index 3260e91d51..81d9a7a5f5 100644 --- a/system_tests/program_gas_test.go +++ b/system_tests/program_gas_test.go @@ -117,15 +117,18 @@ func TestReadArgsGasUsage(t *testing.T) { signature := "readArgsNoArgs()" expectedInk := HOSTIO_INK + 5040 + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) signature = "readArgsOneArg(uint256)" signatureOverhead := 4 expectedInk = HOSTIO_INK + 5040 + 30*(32+signatureOverhead-32) + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{1}, uint64(expectedInk)) signature = "readArgsThreeArgs(uint256,uint256,uint256)" expectedInk = HOSTIO_INK + 5040 + 30*(3*32+signatureOverhead-32) + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{1, 1, 1}, uint64(expectedInk)) } @@ -140,6 +143,7 @@ func TestMsgReentrantGasUsage(t *testing.T) { signature := "writeResultEmpty()" expectedInk := HOSTIO_INK + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) } @@ -154,6 +158,7 @@ func TestStorageCacheBytes32GasUsage(t *testing.T) { signature := "storageCacheBytes32()" expectedInk := HOSTIO_INK + (13440-HOSTIO_INK)*2 + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, nil, uint64(expectedInk)) } @@ -168,9 +173,11 @@ func TestPayForMemoryGrowGasUsage(t *testing.T) { signature := "payForMemoryGrow(uint256)" expectedInk := 9320660000 + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{100}, uint64(expectedInk)) expectedInk = HOSTIO_INK + // #nosec G115 checkInkUsage(t, builder, stylusProgram, hostio, signature, []uint32{0}, uint64(expectedInk)) }