Skip to content

Commit

Permalink
define WasmPrefix type as [3]byte
Browse files Browse the repository at this point in the history
  • Loading branch information
magicxyyz committed Aug 16, 2024
1 parent abc46c6 commit 575062f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
6 changes: 3 additions & 3 deletions core/rawdb/accessors_state_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func LocalTarget() Target {
return TargetHost
}

func (t Target) keyPrefix() ([]byte, error) {
var prefix []byte
func (t Target) keyPrefix() (WasmPrefix, error) {
var prefix WasmPrefix
switch t {
case TargetWavm:
prefix = activatedAsmWavmPrefix
Expand All @@ -58,7 +58,7 @@ func (t Target) keyPrefix() ([]byte, error) {
case TargetHost:
prefix = activatedAsmHostPrefix
default:
return nil, fmt.Errorf("invalid target: %v", t)
return WasmPrefix{}, fmt.Errorf("invalid target: %v", t)
}
return prefix, nil
}
Expand Down
27 changes: 15 additions & 12 deletions core/rawdb/schema_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@ import (

const WasmSchemaVersion byte = 0x01

const WasmPrefixLen = 3

// WasmKeyLen = CompiledWasmCodePrefix + moduleHash
const WasmKeyLen = WasmPrefixLen + common.HashLength

type WasmPrefix = [WasmPrefixLen]byte
type WasmKey = [WasmKeyLen]byte

var (
wasmSchemaVersionKey = []byte("WasmSchemaVersion")

// 0x00 prefix to avoid conflicts when wasmdb is not separate database
activatedAsmWavmPrefix = []byte{0x00, 'w', 'w'} // (prefix, moduleHash) -> stylus module (wavm)
activatedAsmArmPrefix = []byte{0x00, 'w', 'r'} // (prefix, moduleHash) -> stylus asm for ARM system
activatedAsmX86Prefix = []byte{0x00, 'w', 'x'} // (prefix, moduleHash) -> stylus asm for x86 system
activatedAsmHostPrefix = []byte{0x00, 'w', 'h'} // (prefix, moduleHash) -> stylus asm for system other then ARM and x86
activatedAsmWavmPrefix = WasmPrefix{0x00, 'w', 'w'} // (prefix, moduleHash) -> stylus module (wavm)
activatedAsmArmPrefix = WasmPrefix{0x00, 'w', 'r'} // (prefix, moduleHash) -> stylus asm for ARM system
activatedAsmX86Prefix = WasmPrefix{0x00, 'w', 'x'} // (prefix, moduleHash) -> stylus asm for x86 system
activatedAsmHostPrefix = WasmPrefix{0x00, 'w', 'h'} // (prefix, moduleHash) -> stylus asm for system other then ARM and x86
)

func DeprecatedPrefixesV0() (keyPrefixes [][]byte, keyLength int) {
Expand All @@ -42,15 +50,10 @@ func DeprecatedPrefixesV0() (keyPrefixes [][]byte, keyLength int) {
}, 3 + 32
}

// WasmKeyLen = CompiledWasmCodePrefix + moduleHash
const WasmKeyLen = 3 + common.HashLength

type WasmKey = [WasmKeyLen]byte

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

0 comments on commit 575062f

Please sign in to comment.