Skip to content

Commit

Permalink
add stylus wasm long term cache test
Browse files Browse the repository at this point in the history
  • Loading branch information
magicxyyz committed Oct 3, 2024
1 parent da58307 commit 6c69a96
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
7 changes: 7 additions & 0 deletions arbitrator/stylus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ pub extern "C" fn stylus_clear_lru_cache() {
InitCache::clear_lru_cache()
}

/// Clears long term cache.
/// Only used for testing purposes.
#[no_mangle]
pub extern "C" fn stylus_clear_long_term_cache(arbos_tag: u32) {
InitCache::clear_long_term(arbos_tag);
}

/// Gets entry size in bytes.
/// Only used for testing purposes.
#[no_mangle]
Expand Down
5 changes: 5 additions & 0 deletions arbos/programs/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ func ClearWasmLruCache() {
C.stylus_clear_lru_cache()
}

// Used for testing
func ClearWasmLongTermCache(arbos_tag uint32) {
C.stylus_clear_long_term_cache(u32(arbos_tag))
}

// Used for testing
func GetEntrySizeEstimateBytes(module []byte, version uint16, debug bool) uint64 {
return uint64(C.stylus_get_entry_size_estimate_bytes(goSlice(module), u16(version), cbool(debug)))
Expand Down
106 changes: 105 additions & 1 deletion system_tests/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ func TestProgramCacheManager(t *testing.T) {
isManager, err := arbWasmCache.IsCacheManager(nil, manager)
assert(!isManager, err)

// athorize the manager
// authorize the manager
ensure(arbOwner.AddWasmCacheManager(&ownerAuth, manager))
assert(arbWasmCache.IsCacheManager(nil, manager))
all, err := arbWasmCache.AllCacheManagers(nil)
Expand Down Expand Up @@ -2137,3 +2137,107 @@ func TestWasmLruCache(t *testing.T) {
t.Fatalf("lruMetrics.SizeBytes, expected: %v, actual: %v", keccakLruEntrySizeEstimateBytes+mathLruEntrySizeEstimateBytes, lruMetrics.SizeBytes)
}
}

func TestWasmLongTermCache(t *testing.T) {
builder, ownerAuth, cleanup := setupProgramTest(t, true)
ctx := builder.ctx
l2info := builder.L2Info
l2client := builder.L2.Client
defer cleanup()

ensure := func(tx *types.Transaction, err error) *types.Receipt {
t.Helper()
Require(t, err)
receipt, err := EnsureTxSucceeded(ctx, l2client, tx)
Require(t, err)
return receipt
}

manager, tx, _, err := mocksgen.DeploySimpleCacheManager(&ownerAuth, l2client)
ensure(tx, err)

arbWasmCache, err := pgen.NewArbWasmCache(types.ArbWasmCacheAddress, builder.L2.Client)
Require(t, err)
arbOwner, err := pgen.NewArbOwner(types.ArbOwnerAddress, builder.L2.Client)
Require(t, err)
ensure(arbOwner.SetInkPrice(&ownerAuth, 10_000))

ownerAuth.GasLimit = 32000000
ownerAuth.Value = oneEth

fallibleProgramAddress, fallibleEntrySize := deployWasmAndGetEntrySizeEstimateBytes(t, builder, ownerAuth, "fallible")
keccakProgramAddress, keccakEntrySize := deployWasmAndGetEntrySizeEstimateBytes(t, builder, ownerAuth, "keccak")
mathProgramAddress, mathEntrySize := deployWasmAndGetEntrySizeEstimateBytes(t, builder, ownerAuth, "math")
t.Log(
"lruEntrySizeEstimateBytes, ",
"fallible:", fallibleEntrySize,
"keccak:", keccakEntrySize,
"math:", mathEntrySize,
)

isManager, err := arbWasmCache.IsCacheManager(nil, manager)
Require(t, err)
t.Log("isManager", isManager)
ownerAuth.Value = common.Big0
ensure(arbOwner.AddWasmCacheManager(&ownerAuth, manager))

checkLongTermMetrics := func(expected programs.WasmLongTermCacheMetrics) {
t.Helper()
longTermMetrics := programs.GetWasmCacheMetrics().LongTerm
if longTermMetrics.Count != expected.Count {
t.Fatalf("longTermMetrics.Count, expected: %v, actual: %v", expected.Count, longTermMetrics.Count)
}
if longTermMetrics.SizeBytes != expected.SizeBytes {
t.Fatalf("longTermMetrics.SizeBytes, expected: %v, actual: %v", expected.SizeBytes, longTermMetrics.SizeBytes)
}
}

programs.ClearWasmLongTermCache(1)
checkLongTermMetrics(programs.WasmLongTermCacheMetrics{
Count: 0,
SizeBytes: 0,
})

// fallible wasm program will not be cached since caching is not set for this program
tx = l2info.PrepareTxTo("Owner", &fallibleProgramAddress, l2info.TransferGas, nil, []byte{0x01})
ensure(tx, l2client.SendTransaction(ctx, tx))
checkLongTermMetrics(programs.WasmLongTermCacheMetrics{
Count: 0,
SizeBytes: 0,
})

ensure(arbWasmCache.CacheProgram(&ownerAuth, fallibleProgramAddress))
// fallible wasm program will be cached
tx = l2info.PrepareTxTo("Owner", &fallibleProgramAddress, l2info.TransferGas, nil, []byte{0x01})
ensure(tx, l2client.SendTransaction(ctx, tx))
checkLongTermMetrics(programs.WasmLongTermCacheMetrics{
Count: 1,
SizeBytes: fallibleEntrySize,
})

// keccak wasm program will be cached
ensure(arbWasmCache.CacheProgram(&ownerAuth, keccakProgramAddress))
tx = l2info.PrepareTxTo("Owner", &keccakProgramAddress, l2info.TransferGas, nil, []byte{0x01})
ensure(tx, l2client.SendTransaction(ctx, tx))
checkLongTermMetrics(programs.WasmLongTermCacheMetrics{
Count: 2,
SizeBytes: fallibleEntrySize + keccakEntrySize,
})

// math wasm program will not be cached
tx = l2info.PrepareTxTo("Owner", &mathProgramAddress, l2info.TransferGas, nil, []byte{0x01})
ensure(tx, l2client.SendTransaction(ctx, tx))
checkLongTermMetrics(programs.WasmLongTermCacheMetrics{
Count: 2,
SizeBytes: fallibleEntrySize + keccakEntrySize,
})

// math wasm program will be cached
ensure(arbWasmCache.CacheProgram(&ownerAuth, mathProgramAddress))
tx = l2info.PrepareTxTo("Owner", &mathProgramAddress, l2info.TransferGas, nil, []byte{0x01})
ensure(tx, l2client.SendTransaction(ctx, tx))
checkLongTermMetrics(programs.WasmLongTermCacheMetrics{
Count: 3,
SizeBytes: fallibleEntrySize + keccakEntrySize + mathEntrySize,
})
}

0 comments on commit 6c69a96

Please sign in to comment.