Skip to content

Commit 2e90f74

Browse files
swap version & hash, polish
1 parent 3878535 commit 2e90f74

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

core/rawdb/accessors_state_arbitrum.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323
)
2424

2525
// ReadCompiledWasmCode retrieves the compiled wasm contract code of the provided code hash.
26-
func ReadCompiledWasmCode(db ethdb.KeyValueReader, hash common.Hash, version uint32) []byte {
27-
data, _ := db.Get(compiledWasmCodeKey(hash, version))
26+
func ReadCompiledWasmCode(db ethdb.KeyValueReader, version uint32, hash common.Hash) []byte {
27+
data, _ := db.Get(compiledWasmCodeKey(version, hash))
2828
return data
2929
}
3030

3131
// WriteCompiledWasmCode writes the provided contract compiled wasm code database.
32-
func WriteCompiledWasmCode(db ethdb.KeyValueWriter, hash common.Hash, version uint32, code []byte) {
33-
if err := db.Put(compiledWasmCodeKey(hash, version), code); err != nil {
32+
func WriteCompiledWasmCode(db ethdb.KeyValueWriter, version uint32, hash common.Hash, code []byte) {
33+
if err := db.Put(compiledWasmCodeKey(version, hash), code); err != nil {
3434
log.Crit("Failed to store compiled wasm contract code", "err", err)
3535
}
3636
}

core/rawdb/schema_arbitrum.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,32 @@ package rawdb
2121
import (
2222
"bytes"
2323
"encoding/binary"
24+
2425
"github.com/ethereum/go-ethereum/common"
2526
)
2627

2728
var (
28-
CompiledWasmCodePrefix = []byte("w") // CompiledWasmCodePrefix + code hash -> account compiled wasm code
29+
CompiledWasmCodePrefix = []byte{0x00, 'w'} // (prefix, version, code_hash) -> account compiled wasm code
2930
)
3031

31-
// compiledWasmCodeKey = CompiledWasmCodePrefix + hash
32-
func compiledWasmCodeKey(hash common.Hash, version uint32) []byte {
32+
// compiledWasmCodeKey = CompiledWasmCodePrefix + version + hash
33+
func compiledWasmCodeKey(version uint32, hash common.Hash) []byte {
3334
var versionBytes [4]byte
3435
binary.BigEndian.PutUint32(versionBytes[:], version)
35-
return append(append(CompiledWasmCodePrefix, hash.Bytes()...), versionBytes[:]...)
36+
return append(append(CompiledWasmCodePrefix, versionBytes[:]...), hash.Bytes()...)
3637
}
3738

3839
// IsCompiledWasmCodeKey reports whether the given byte slice is the key of compiled wasm contract code,
3940
// if so return the raw code hash and version as well.
40-
func IsCompiledWasmCodeKey(key []byte) (bool, []byte, uint32) {
41-
if bytes.HasPrefix(key, CompiledWasmCodePrefix) && len(key) == common.HashLength+4+len(CompiledWasmCodePrefix) {
42-
endOfHashOffset := len(CompiledWasmCodePrefix) + common.HashLength
43-
codeHash := key[len(CompiledWasmCodePrefix):endOfHashOffset]
44-
version := binary.BigEndian.Uint32(key[endOfHashOffset:])
41+
func IsCompiledWasmCodeKey(key []byte) (bool, common.Hash, uint32) {
42+
43+
wasmKeyLen := len(CompiledWasmCodePrefix) + 4 + common.HashLength
44+
start := len(CompiledWasmCodePrefix)
45+
46+
if bytes.HasPrefix(key, CompiledWasmCodePrefix) && len(key) == wasmKeyLen {
47+
version := binary.BigEndian.Uint32(key[start : start+4])
48+
codeHash := common.BytesToHash(key[start+4:])
4549
return true, codeHash, version
4650
}
47-
return false, nil, 0
51+
return false, common.Hash{}, 0
4852
}

core/state/database_arbitrum.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package state
22

33
import (
44
"errors"
5+
56
"github.com/ethereum/go-ethereum/common"
67
"github.com/ethereum/go-ethereum/core/rawdb"
78
)
@@ -11,7 +12,7 @@ func (db *cachingDB) CompiledWasmContractCode(codeHash common.Hash, version uint
1112
if code := db.compiledWasmCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
1213
return code, nil
1314
}
14-
code := rawdb.ReadCompiledWasmCode(db.db.DiskDB(), codeHash, version)
15+
code := rawdb.ReadCompiledWasmCode(db.db.DiskDB(), version, codeHash)
1516
if len(code) > 0 {
1617
db.compiledWasmCache.Set(codeHash.Bytes(), code)
1718
return code, nil

core/state/state_object_arbitrum.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
// Copyright 2014 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
117
package state
218

319
import (
420
"fmt"
21+
522
"github.com/ethereum/go-ethereum/common"
623
)
724

core/state/statedb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,8 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
962962
// Arbitrum Only
963963
for version, wasm := range obj.compiledWasmCode {
964964
if wasm.dirty {
965-
rawdb.WriteCompiledWasmCode(codeWriter, common.BytesToHash(obj.CodeHash()), version, wasm.code)
965+
codeHash := common.BytesToHash(obj.CodeHash())
966+
rawdb.WriteCompiledWasmCode(codeWriter, version, codeHash, wasm.code)
966967
wasm.dirty = false
967968
}
968969
}

core/vm/evm_arbitrum.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/ethereum/go-ethereum/common"
2323
"github.com/ethereum/go-ethereum/core/types"
24+
"github.com/ethereum/go-ethereum/log"
2425
)
2526

2627
// Depth returns the current depth
@@ -96,5 +97,6 @@ func (p DefaultTxProcessor) GasPriceOp(evm *EVM) *big.Int {
9697
func (p DefaultTxProcessor) FillReceiptInfo(*types.Receipt) {}
9798

9899
func (p DefaultTxProcessor) ExecuteWASM(contract *Contract, input []byte, readOnly bool, txContext TxContext, blockContext BlockContext) ([]byte, error) {
100+
log.Crit("tried to execute WASM with default processing hook")
99101
return nil, nil
100102
}

0 commit comments

Comments
 (0)