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

Arbitrum Stylus #305

Merged
merged 158 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
158 commits
Select commit Hold shift + click to select a range
20705c4
mock program storage
rachel-bousfield Nov 30, 2022
4c23226
Merge branch 'master' into polyglot-viability
rachel-bousfield Jan 5, 2023
5f47e79
rename & bump initial ArbOS version
rachel-bousfield Jan 7, 2023
ce511b0
expose whether statedb is deterministic
rachel-bousfield Jan 12, 2023
6a3a58a
add in stylus eof machinery
rauljordan Jan 13, 2023
693d250
better program storage
rachel-bousfield Jan 14, 2023
74bbb9c
more addresses
rachel-bousfield Jan 14, 2023
1c0be5f
noncanonical program hash
rachel-bousfield Jan 14, 2023
d04a061
merge
rauljordan Jan 17, 2023
ff2540a
fix up edits
rauljordan Jan 17, 2023
ea29f9c
edits to eof checks from code review
rauljordan Jan 18, 2023
c4fc7d6
Merge pull request #4 from OffchainLabs/eof-machinery
rachel-bousfield Jan 18, 2023
8d167da
add in
rauljordan Jan 21, 2023
7211b9a
Merge branch 'polyglot-viability' into store-modules-rawdb
rauljordan Jan 21, 2023
7162591
necessary diff
rauljordan Jan 21, 2023
5ba141e
passing
rauljordan Jan 22, 2023
e10186e
Add piping for compiled wasm and wasm execution
hkalodner Jan 24, 2023
1911840
Fix compilation
hkalodner Jan 28, 2023
d292c0f
Revert customization to contract creation
hkalodner Jan 28, 2023
614d7d1
Fix stateObject CompiledWasmCode
hkalodner Jan 29, 2023
deefe42
Local execution working
hkalodner Jan 29, 2023
1deccc8
Fix snapshot conversion
hkalodner Jan 29, 2023
91fb639
Fix issues with compiled wasm
hkalodner Jan 29, 2023
c49edf2
Store compiled wasm with version
hkalodner Jan 29, 2023
de4a2c5
Fix: wrong var in IsCompiledWasmCodeKey
hkalodner Feb 1, 2023
0f3c7c0
move and document arbitrum code
rachel-bousfield Feb 2, 2023
3878535
move unexpectedBalanceDelta back
rachel-bousfield Feb 2, 2023
2e90f74
swap version & hash, polish
rachel-bousfield Feb 2, 2023
f0317de
Merge pull request #7 from OffchainLabs/execute-compiled-wasm
rachel-bousfield Feb 2, 2023
374fadd
fix cache version bug
rachel-bousfield Feb 2, 2023
df1701d
Merge pull request #6 from OffchainLabs/store-modules-rawdb
rachel-bousfield Feb 2, 2023
c19aea0
state storage cost functions
rachel-bousfield Feb 18, 2023
df0296f
move ExecuteWasm
rachel-bousfield Feb 20, 2023
0f0de0b
move stylus hook
rachel-bousfield Feb 21, 2023
c39800e
improve panic message
rachel-bousfield Mar 15, 2023
8e292d3
Merge pull request #8 from OffchainLabs/stylus-state-storage
rachel-bousfield Mar 15, 2023
2751178
Merge branch 'master' into stylus-master-896026b
rachel-bousfield Mar 15, 2023
55c15e9
Merge branch 'stylus-master-896026b' into stylus-state-storage
rachel-bousfield Mar 16, 2023
b49cf09
Merge pull request #10 from OffchainLabs/stylus-state-storage
rachel-bousfield Mar 16, 2023
5a17591
Merge pull request #9 from OffchainLabs/stylus-master-896026b
rachel-bousfield Mar 16, 2023
b598e49
wasm call costs
rachel-bousfield Mar 23, 2023
fa5c410
get and set interpreter's return data
rachel-bousfield Mar 28, 2023
62006e1
Merge pull request #11 from OffchainLabs/stylus-contract-calls
rachel-bousfield Mar 29, 2023
1addd8d
Add stylus hooks for hostio
joshuacolvin0 May 10, 2023
eba6289
Address code review comments
joshuacolvin0 May 11, 2023
c1b3988
document and rename
rachel-bousfield May 11, 2023
91f9040
Merge pull request #13 from OffchainLabs/stylus-hostio
rachel-bousfield May 11, 2023
2f65987
add memory model pointers
rachel-bousfield May 17, 2023
6d07282
merge nitro-geth master
rachel-bousfield May 18, 2023
77b53de
move away from fastcache
rachel-bousfield May 18, 2023
60f45c2
Merge branch 'stylus-master-737add4' into stylus-memory-model
rachel-bousfield May 22, 2023
44938ba
use proper key type
rachel-bousfield May 24, 2023
744ce92
Merge commit 'a23dc84' into stylus-master-737add4
rachel-bousfield May 24, 2023
8365a04
Merge pull request #14 from OffchainLabs/stylus-master-737add4
rachel-bousfield Jun 5, 2023
def271f
Merge branch 'stylus' into stylus-memory-model
rachel-bousfield Jun 7, 2023
c631d05
improve logging
rachel-bousfield Jun 7, 2023
6a54073
move away from pointers
rachel-bousfield Jun 13, 2023
c8c33a2
remove test function
rachel-bousfield Jun 13, 2023
f997cb3
handle new fields in statedb
rachel-bousfield Jun 13, 2023
e2d0d4a
Merge commit 'f214ae0' into stylus-master-152b9ff
rachel-bousfield Jun 20, 2023
8c236d4
reuse recompiled wasm changes
rauljordan Jun 27, 2023
c1a03e2
Merge pull request #15 from OffchainLabs/stylus-memory-model
rachel-bousfield Jun 30, 2023
c983ad5
Merge pull request #16 from OffchainLabs/stylus-master-152b9ff
rachel-bousfield Jun 30, 2023
348ab50
include latest
rauljordan Jul 1, 2023
9f0e26d
add in changes
rauljordan Jul 5, 2023
9e23c3f
rem print
rauljordan Jul 5, 2023
1bad5be
sync
rauljordan Jul 5, 2023
1a57717
Merge commit '8e6a8ad' into stylus-master-ca73cbc
rachel-bousfield Jul 6, 2023
3091f46
update setError
rachel-bousfield Jul 6, 2023
537ca3d
Merge pull request #18 from OffchainLabs/stylus-master-ca73cbc
rachel-bousfield Jul 7, 2023
447d3f8
Allow selecting partial`GetReturnData`
joshuacolvin0 Jul 6, 2023
5134184
Merge commit 'fcda31c' into stylus-master-16d0251
rachel-bousfield Jul 11, 2023
1808f2c
Address code review comments
joshuacolvin0 Jul 11, 2023
4cee549
Merge pull request #20 from OffchainLabs/stylus-master-16d0251
rachel-bousfield Jul 11, 2023
76fcb94
sync commits
rauljordan Jul 11, 2023
3bef341
fix broken
rauljordan Jul 11, 2023
fe79ae6
Merge branch 'stylus' into read-return-data-partial
joshuacolvin0 Jul 14, 2023
c51d75b
Merge pull request #19 from OffchainLabs/read-return-data-partial
joshuacolvin0 Jul 21, 2023
cde326b
stateDb userWasm: update fields
tsahee Jul 31, 2023
2deeb21
statedb_arbitrum: fix log format
tsahee Aug 2, 2023
cf4ad8f
Merge branch 'stylus' into reuse-recompiled
rauljordan Aug 4, 2023
69478f7
PushCaller => PushContract with stylus info
rachel-bousfield Aug 5, 2023
10b4729
track delegatecall and callcode in Contract's
rachel-bousfield Aug 5, 2023
ae0307b
add AuthModules and AuthOrigins params to stack config
magicxyyz Jul 17, 2023
16eb192
clean up todo comment
magicxyyz Jul 17, 2023
7a35694
Merge pull request #22 from OffchainLabs/stylus-reentrancy
rachel-bousfield Aug 8, 2023
f0a3fcd
Merge remote-tracking branch 'origin/reuse-recompiled' into compiled-…
tsahee Aug 8, 2023
76851cc
Merge branch 'stylus' into auth-rpc-params
joshuacolvin0 Aug 11, 2023
187eff2
Merge pull request #23 from OffchainLabs/auth-rpc-params
joshuacolvin0 Aug 14, 2023
0a7d2d1
Update stylus header to 0xEFF000
joshuacolvin0 Aug 14, 2023
90cd44f
Update actual stylus prefix
joshuacolvin0 Aug 14, 2023
5185e75
Fix stylus prefix handling
joshuacolvin0 Aug 14, 2023
a289b4c
Merge pull request #24 from OffchainLabs/stylus-header
rachel-bousfield Aug 14, 2023
f9925db
Merge remote-tracking branch 'origin/stylus' into reuse-recompiled
tsahee Aug 18, 2023
576989d
compiled wasm version is uint16
tsahee Aug 18, 2023
02ceb03
Merge branch 'reuse-recompiled' into compiled-onchain
tsahee Aug 21, 2023
315fc9d
address review comments
rachel-bousfield Aug 25, 2023
9ed6e38
Merge commit 'd312a' into nitro_merge
tsahee Aug 25, 2023
588b51b
Merge pull request #17 from OffchainLabs/reuse-recompiled
rachel-bousfield Aug 27, 2023
1464ecf
Merge commit 'c905292f8' into HEAD
tsahee Aug 28, 2023
002ce7c
Merge commit '588b51b97' into nitro_merge
tsahee Aug 28, 2023
37a797f
lint fixes
tsahee Aug 28, 2023
69c097f
Merge pull request #25 from OffchainLabs/nitro_merge
rachel-bousfield Aug 28, 2023
628a6b7
Merge remote-tracking branch 'origin/stylus' into compiled-onchain
tsahee Sep 5, 2023
89c159f
hostio tracer
rachel-bousfield Sep 14, 2023
4020bfd
add hostio topic
rachel-bousfield Sep 14, 2023
27113b8
remove empty line
rachel-bousfield Sep 14, 2023
4d61d86
Merge pull request #26 from OffchainLabs/hostio-tracer
rachel-bousfield Sep 14, 2023
5c95759
Merge remote-tracking branch 'origin/stylus' into compiled-onchain
tsahee Sep 27, 2023
b1ed2b4
extend hostio tracing
rachel-bousfield Oct 1, 2023
b9c8fa5
Merge pull request #28 from OffchainLabs/hostio-gdb
rachel-bousfield Oct 3, 2023
35fead9
Merge remote-tracking branch 'stylus/stylus' into compiled-onchain
rachel-bousfield Oct 3, 2023
97f1a4d
rename modulehash
rachel-bousfield Oct 12, 2023
0175464
add moduleasm
rachel-bousfield Oct 12, 2023
83e9aec
rename to Get/Set ActivateAsm
rachel-bousfield Oct 12, 2023
d659255
split asm and module caching
rachel-bousfield Oct 12, 2023
8ba7603
rawdb module hash
rachel-bousfield Oct 12, 2023
d16318a
moduleHash based db access
rachel-bousfield Oct 13, 2023
548c774
preserve nilness
rachel-bousfield Oct 15, 2023
4ae0e64
address review comments
rachel-bousfield Oct 16, 2023
e081057
Merge pull request #29 from OffchainLabs/onchain-compiled-edits
rachel-bousfield Oct 16, 2023
991e082
Merge pull request #21 from OffchainLabs/compiled-onchain
rachel-bousfield Oct 16, 2023
9fd0e40
go build: js->wasm
tsahee Jan 2, 2024
66adea6
add ArbOwnerPublic
rachel-bousfield Jan 10, 2024
94817b1
Merge remote-tracking branch 'origin/activation-pricing' into go-wasi
tsahee Jan 19, 2024
098f174
simplify GetReturnData() and improve WasmAccountTouchCost
rachel-bousfield Jan 30, 2024
2e1774e
Merge pull request #32 from OffchainLabs/hostio-code
rachel-bousfield Jan 30, 2024
3dd61fe
Merge commit '098f174c05572c2f3da1e34a5d0a48005573dd84' into go-wasi
tsahee Feb 5, 2024
188c241
Merge pull request #33 from OffchainLabs/go-wasi-edits
rachel-bousfield Mar 8, 2024
dc7a87a
merge geth from nitro 2.2.5 and stylus
tsahee Mar 11, 2024
6160fda
WasmTouchCost - read maxCodeSize from chainconfig
tsahee Mar 11, 2024
3c95595
add Stylus EOF byte
rachel-bousfield Mar 20, 2024
9102492
address review comments
rachel-bousfield Mar 21, 2024
fb7c414
Merge pull request #34 from OffchainLabs/stylus-dictionaries
rachel-bousfield Mar 21, 2024
8f86e72
Merge commit '9102492c7' into nitro-2.2.5-merge
tsahee Mar 25, 2024
8468391
diffuse self destruct
rachel-bousfield Mar 28, 2024
75e184d
Merge pull request #35 from OffchainLabs/stylus-dencun-self-destruct
joshuacolvin0 Apr 1, 2024
d43aafe
Merge pull request #36 from OffchainLabs/nitro-2.2.5-merge
rachel-bousfield Apr 3, 2024
874a123
Merge commit '22399a74e' into merge-nitro-v2.3.4
tsahee Apr 6, 2024
2bef373
Merge pull request #37 from OffchainLabs/merge-nitro-v2.3.4
rachel-bousfield Apr 9, 2024
3d43825
add precompile address
rachel-bousfield Apr 9, 2024
eaa41e4
Merge branch 'stylus' into init-cache
rachel-bousfield Apr 9, 2024
aca214a
add all precompiles
rachel-bousfield Apr 10, 2024
348b1fb
add block-level LRU cache
rachel-bousfield Apr 11, 2024
fd731c7
add version and debug
rachel-bousfield Apr 11, 2024
143049b
address review comments
rachel-bousfield Apr 11, 2024
64277ff
Merge pull request #38 from OffchainLabs/init-cache
rachel-bousfield Apr 15, 2024
75b0626
bump dev version
rachel-bousfield Apr 19, 2024
527c512
Merge pull request #39 from OffchainLabs/stylus-arbos-30
rachel-bousfield Apr 19, 2024
33ee405
deny wasms until ArbOS 30
rachel-bousfield Apr 19, 2024
abf6d0e
Merge pull request #40 from OffchainLabs/stylus-arbos-30
rachel-bousfield Apr 19, 2024
a21f648
separate Nitro and Stylus precompiles rules and lists
rachel-bousfield Apr 19, 2024
e4dc411
Merge pull request #41 from OffchainLabs/stylus-arbos-30
rachel-bousfield Apr 19, 2024
8a11f72
Merge remote-tracking branch 'nitro/master' into stylus-nitro-conflicts
rachel-bousfield May 3, 2024
ca535b8
add interface stub
rachel-bousfield May 3, 2024
4fefb83
remove ordDatabase stubs
rachel-bousfield May 3, 2024
6bf2598
skip cmd color test
rachel-bousfield May 3, 2024
72f81da
Merge pull request #43 from OffchainLabs/stylus-nitro-conflicts
rachel-bousfield May 3, 2024
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
7 changes: 7 additions & 0 deletions arbitrum/recordingdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func (db *RecordingKV) Get(key []byte) ([]byte, error) {
// Retrieving code
copy(hash[:], key[len(rawdb.CodePrefix):])
res, err = db.diskDb.Get(key)
} else if ok, _ := rawdb.IsActivatedAsmKey(key); ok {
// Arbitrum: the asm is non-consensus
return db.diskDb.Get(key)
} else if ok, _ := rawdb.IsActivatedModuleKey(key); ok {
// Arbitrum: the module is non-consensus (only its hash is)
return db.diskDb.Get(key)
} else {
err = fmt.Errorf("recording KV attempted to access non-hash key %v", hex.EncodeToString(key))
}
Expand Down Expand Up @@ -278,6 +284,7 @@ func (r *RecordingDatabase) PrepareRecording(ctx context.Context, lastBlockHeade
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to create recordingStateDb: %w", err)
}
recordingStateDb.StartRecording()
var recordingChainContext *RecordingChainContext
if lastBlockHeader != nil {
if !lastBlockHeader.Number.IsUint64() {
Expand Down
2 changes: 2 additions & 0 deletions cmd/evm/internal/t8ntool/tracewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ func (t *traceWriter) CaptureArbitrumTransfer(env *vm.EVM, from, to *common.Addr

func (t *traceWriter) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (t *traceWriter) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}

func (t *traceWriter) CaptureStylusHostio(name string, args, outs []byte, startInk, endInk uint64) {}
2 changes: 2 additions & 0 deletions cmd/geth/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func censor(input string, start, end int) string {
}

func TestLogging(t *testing.T) {
t.Skip("Skipping cmd logging test due to color stripping")

t.Parallel()
testConsoleLogging(t, "terminal", 6, 24)
testConsoleLogging(t, "logfmt", 2, 26)
Expand Down
22 changes: 22 additions & 0 deletions common/lru/basiclru_arbitrum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2022 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// Package lru implements generically-typed LRU caches.
package lru

func (c *BasicLRU[K, V]) Capacity() int {
return c.cap
}
64 changes: 64 additions & 0 deletions common/types_arbitrum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package common

type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Integer interface {
Signed | Unsigned
}

type Float interface {
~float32 | ~float64
}

// Ordered is anything that implements comparison operators such as `<` and `>`.
// Unfortunately, that doesn't include big ints.
type Ordered interface {
Integer | Float
}

// MinInt the minimum of two ints
func MinInt[T Ordered](value, ceiling T) T {
if value > ceiling {
return ceiling
}
return value
}

// MaxInt the maximum of two ints
func MaxInt[T Ordered](value, floor T) T {
if value < floor {
return floor
}
return value
}

// SaturatingUAdd add two integers without overflow
func SaturatingUAdd[T Unsigned](a, b T) T {
sum := a + b
if sum < a || sum < b {
sum = ^T(0)
}
return sum
}
36 changes: 36 additions & 0 deletions core/rawdb/accessors_state_arbitrum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package rawdb

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
)

// Stores the activated asm and module for a given codeHash
func WriteActivation(db ethdb.KeyValueWriter, moduleHash common.Hash, asm, module []byte) {
key := ActivatedAsmKey(moduleHash)
if err := db.Put(key[:], asm); err != nil {
log.Crit("Failed to store activated wasm asm", "err", err)
}

key = ActivatedModuleKey(moduleHash)
if err := db.Put(key[:], module); err != nil {
log.Crit("Failed to store activated wasm module", "err", err)
}
}
4 changes: 2 additions & 2 deletions core/rawdb/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

//go:build !js
// +build !js
//go:build !wasm
// +build !wasm

package rawdb

Expand Down
4 changes: 2 additions & 2 deletions core/rawdb/fileutil_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

//go:build js
// +build js
//go:build wasm
// +build wasm

package rawdb

Expand Down
66 changes: 66 additions & 0 deletions core/rawdb/schema_arbitrum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// Package rawdb contains a collection of low level database accessors.

package rawdb

import (
"bytes"

"github.com/ethereum/go-ethereum/common"
)

var (
activatedAsmPrefix = []byte{0x00, 'w', 'a'} // (prefix, moduleHash) -> stylus asm
activatedModulePrefix = []byte{0x00, 'w', 'm'} // (prefix, moduleHash) -> stylus module
)

// WasmKeyLen = CompiledWasmCodePrefix + moduleHash
const WasmKeyLen = 3 + 32

type WasmKey = [WasmKeyLen]byte

func ActivatedAsmKey(moduleHash common.Hash) WasmKey {
return newWasmKey(activatedAsmPrefix, moduleHash)
}

func ActivatedModuleKey(moduleHash common.Hash) WasmKey {
return newWasmKey(activatedModulePrefix, moduleHash)
}

// key = prefix + moduleHash
func newWasmKey(prefix []byte, moduleHash common.Hash) WasmKey {
var key WasmKey
copy(key[:3], prefix)
copy(key[3:], moduleHash[:])
return key
}

func IsActivatedAsmKey(key []byte) (bool, common.Hash) {
return extractWasmKey(activatedAsmPrefix, key)
}

func IsActivatedModuleKey(key []byte) (bool, common.Hash) {
return extractWasmKey(activatedModulePrefix, key)
}

func extractWasmKey(prefix, key []byte) (bool, common.Hash) {
if !bytes.HasPrefix(key, prefix) || len(key) != WasmKeyLen {
return false, common.Hash{}
}
return true, common.BytesToHash(key[len(prefix):])
}
19 changes: 19 additions & 0 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import (
)

const (
// Arbitrum: Cache size granted for caching clean compiled wasm code.
activatedWasmCacheSize = 64 * 1024 * 1024

// Number of codehash->size associations to keep.
codeSizeCacheSize = 100000

Expand All @@ -48,6 +51,10 @@ const (

// Database wraps access to tries and contract code.
type Database interface {
// Arbitrum: Read activated Stylus contracts
ActivatedAsm(moduleHash common.Hash) (asm []byte, err error)
ActivatedModule(moduleHash common.Hash) (module []byte, err error)

// OpenTrie opens the main account trie.
OpenTrie(root common.Hash) (Trie, error)

Expand Down Expand Up @@ -152,6 +159,10 @@ func NewDatabase(db ethdb.Database) Database {
// large memory cache.
func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
cdb := &cachingDB{
// Arbitrum only
activatedAsmCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
activatedModuleCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),

disk: db,
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
Expand All @@ -163,6 +174,10 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
// NewDatabaseWithNodeDB creates a state database with an already initialized node database.
func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database {
cdb := &cachingDB{
// Arbitrum only
activatedAsmCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),
activatedModuleCache: lru.NewSizeConstrainedCache[common.Hash, []byte](activatedWasmCacheSize),

disk: db,
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
Expand All @@ -172,6 +187,10 @@ func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database {
}

type cachingDB struct {
// Arbitrum
activatedAsmCache *lru.SizeConstrainedCache[common.Hash, []byte]
activatedModuleCache *lru.SizeConstrainedCache[common.Hash, []byte]

disk ethdb.KeyValueStore
codeSizeCache *lru.Cache[common.Hash, int]
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
Expand Down
40 changes: 40 additions & 0 deletions core/state/database_arbitrum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package state

import (
"errors"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
)

func (db *cachingDB) ActivatedAsm(moduleHash common.Hash) ([]byte, error) {
if asm, _ := db.activatedAsmCache.Get(moduleHash); len(asm) > 0 {
return asm, nil
}
wasmKey := rawdb.ActivatedAsmKey(moduleHash)
asm, err := db.disk.Get(wasmKey[:])
if err != nil {
return nil, err
}
if len(asm) > 0 {
db.activatedAsmCache.Add(moduleHash, asm)
return asm, nil
}
return nil, errors.New("not found")
}

func (db *cachingDB) ActivatedModule(moduleHash common.Hash) ([]byte, error) {
if module, _ := db.activatedModuleCache.Get(moduleHash); len(module) > 0 {
return module, nil
}
wasmKey := rawdb.ActivatedModuleKey(moduleHash)
module, err := db.disk.Get(wasmKey[:])
if err != nil {
return nil, err
}
if len(module) > 0 {
db.activatedModuleCache.Add(moduleHash, module)
return module, nil
}
return nil, errors.New("not found")
}
50 changes: 50 additions & 0 deletions core/state/journal_arbitrum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package state

import (
"github.com/ethereum/go-ethereum/common"
)

type wasmActivation struct {
moduleHash common.Hash
}

func (ch wasmActivation) revert(s *StateDB) {
delete(s.arbExtraData.activatedWasms, ch.moduleHash)
}

func (ch wasmActivation) dirtied() *common.Address {
return nil
}

// Updates the Rust-side recent program cache
var CacheWasmRust func(asm []byte, moduleHash common.Hash, version uint16, debug bool) = func([]byte, common.Hash, uint16, bool) {}
var EvictWasmRust func(moduleHash common.Hash, version uint16, debug bool) = func(common.Hash, uint16, bool) {}

type CacheWasm struct {
ModuleHash common.Hash
Version uint16
Debug bool
}

func (ch CacheWasm) revert(s *StateDB) {
EvictWasmRust(ch.ModuleHash, ch.Version, ch.Debug)
}

func (ch CacheWasm) dirtied() *common.Address {
return nil
}

type EvictWasm struct {
ModuleHash common.Hash
Version uint16
Debug bool
}

func (ch EvictWasm) revert(s *StateDB) {
asm := s.GetActivatedAsm(ch.ModuleHash) // only happens in native mode
CacheWasmRust(asm, ch.ModuleHash, ch.Version, ch.Debug)
}

func (ch EvictWasm) dirtied() *common.Address {
return nil
}
Loading
Loading