-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from OffchainLabs/stylus
Arbitrum Stylus
- Loading branch information
Showing
50 changed files
with
1,028 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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):]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.