Skip to content

Commit

Permalink
refactor: tidy up evm subrealm access
Browse files Browse the repository at this point in the history
  • Loading branch information
dessaya committed Jul 13, 2023
1 parent ee44f4f commit 9fee54d
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 69 deletions.
11 changes: 2 additions & 9 deletions packages/evm/jsonrpc/evmchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ func blockchainDB(chainState state.State) *emulator.BlockchainDB {
gasFeePolicy := governance.MustGetGasFeePolicy(govPartition)
blockKeepAmount := governance.GetBlockKeepAmount(govPartition)
return emulator.NewBlockchainDB(
buffered.NewBufferedKVStore(evmStateSubrealmR(chainState)),
buffered.NewBufferedKVStore(evm.EmulatorStateSubrealmR(evm.EVMPartitionR(chainState))),
gas.EVMBlockGasLimit(gasLimits, &gasFeePolicy.EVMGasRatio),
blockKeepAmount,
)
Expand All @@ -708,18 +708,11 @@ func blockchainDB(chainState state.State) *emulator.BlockchainDB {
func stateDB(chainState state.State) *emulator.StateDB {
accountsPartition := subrealm.NewReadOnly(chainState, kv.Key(accounts.Contract.Hname().Bytes()))
return emulator.NewStateDB(
buffered.NewBufferedKVStore(evmStateSubrealmR(chainState)),
buffered.NewBufferedKVStore(evm.EmulatorStateSubrealmR(evm.EVMPartitionR(chainState))),
newL2Balance(accountsPartition),
)
}

func evmStateSubrealmR(chainState state.State) kv.KVStoreReader {
return subrealm.NewReadOnly(
chainState,
kv.Key(evm.Contract.Hname().Bytes())+evm.KeyEVMState,
)
}

type l2BalanceR struct {
accounts kv.KVStoreReader
}
Expand Down
9 changes: 5 additions & 4 deletions packages/vm/core/evm/evmimpl/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import (
"github.com/ethereum/go-ethereum/common"

"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/vm/core/evm"
"github.com/iotaledger/wasp/packages/vm/core/evm/emulator"
)

func Nonce(state kv.KVStoreReader, addr common.Address) uint64 {
evmState := evmStateSubrealmR(state)
func Nonce(evmPartition kv.KVStoreReader, addr common.Address) uint64 {
evmState := evm.EmulatorStateSubrealmR(evmPartition)
stateDBStore := emulator.StateDBSubrealmR(evmState)
return emulator.GetNonce(stateDBStore, addr)
}

func CheckNonce(state kv.KVStore, addr common.Address, nonce uint64) error {
expected := Nonce(state, addr)
func CheckNonce(evmPartition kv.KVStore, addr common.Address, nonce uint64) error {
expected := Nonce(evmPartition, addr)
if nonce != expected {
return fmt.Errorf("Invalid nonce, expected %d, got %d", expected, nonce)
}
Expand Down
10 changes: 5 additions & 5 deletions packages/vm/core/evm/evmimpl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var Processor = evm.Contract.Processor(nil,
evm.FuncGetChainID.WithHandler(getChainID),
)

func SetInitialState(state kv.KVStore, evmChainID uint16) {
func SetInitialState(evmPartition kv.KVStore, evmChainID uint16) {
// add the standard ISC contract at arbitrary address 0x1074...
genesisAlloc := core.GenesisAlloc{}
deployMagicContractOnGenesis(genesisAlloc)
Expand All @@ -54,21 +54,21 @@ func SetInitialState(state kv.KVStore, evmChainID uint16) {
Storage: map[common.Hash]common.Hash{},
Balance: nil,
}
addToPrivileged(state, iscmagic.ERC20BaseTokensAddress)
addToPrivileged(evmPartition, iscmagic.ERC20BaseTokensAddress)

// add the standard ERC721 contract
genesisAlloc[iscmagic.ERC721NFTsAddress] = core.GenesisAccount{
Code: iscmagic.ERC721NFTsRuntimeBytecode,
Storage: map[common.Hash]common.Hash{},
Balance: nil,
}
addToPrivileged(state, iscmagic.ERC721NFTsAddress)
addToPrivileged(evmPartition, iscmagic.ERC721NFTsAddress)

// chain always starts with default gas fee & limits configuration
gasLimits := gas.LimitsDefault
gasRatio := gas.DefaultFeePolicy().EVMGasRatio
emulator.Init(
evmStateSubrealm(state),
evm.EmulatorStateSubrealm(evmPartition),
evmChainID,
emulator.GasLimits{
Block: gas.EVMBlockGasLimit(gasLimits, &gasRatio),
Expand Down Expand Up @@ -359,7 +359,7 @@ func registerERC721NFTCollection(ctx isc.Sandbox) dict.Dict {
func getChainID(ctx isc.SandboxView) dict.Dict {
chainID := emulator.GetChainIDFromBlockChainDBState(
emulator.BlockchainDBSubrealmR(
evmStateSubrealmR(ctx.StateR()),
evm.EmulatorStateSubrealmR(ctx.StateR()),
),
)
return result(codec.EncodeUint16(chainID))
Expand Down
16 changes: 8 additions & 8 deletions packages/vm/core/evm/evmimpl/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
// MintBlock "mints" the Ethereum block after all requests in the ISC
// block have been processed.
// IMPORTANT: Must only be called from the ISC VM
func MintBlock(state kv.KVStore, chainInfo *isc.ChainInfo, blockTimestamp time.Time) {
createBlockchainDB(state, chainInfo).MintBlock(timestamp(blockTimestamp))
func MintBlock(evmPartition kv.KVStore, chainInfo *isc.ChainInfo, blockTimestamp time.Time) {
createBlockchainDB(evmPartition, chainInfo).MintBlock(timestamp(blockTimestamp))
}

func getTracer(ctx isc.Sandbox) tracers.Tracer {
Expand All @@ -42,7 +42,7 @@ func getTracer(ctx isc.Sandbox) tracers.Tracer {
func createEmulator(ctx isc.Sandbox) *emulator.EVMEmulator {
chainInfo := ctx.ChainInfo()
return emulator.NewEVMEmulator(
evmStateSubrealm(ctx.State()),
evm.EmulatorStateSubrealm(ctx.State()),
timestamp(ctx.Timestamp()),
gasLimits(chainInfo),
chainInfo.BlockKeepAmount,
Expand All @@ -51,13 +51,13 @@ func createEmulator(ctx isc.Sandbox) *emulator.EVMEmulator {
)
}

func createBlockchainDB(state kv.KVStore, chainInfo *isc.ChainInfo) *emulator.BlockchainDB {
return emulator.NewBlockchainDB(evmStateSubrealm(state), gasLimits(chainInfo).Block, chainInfo.BlockKeepAmount)
func createBlockchainDB(evmPartition kv.KVStore, chainInfo *isc.ChainInfo) *emulator.BlockchainDB {
return emulator.NewBlockchainDB(evm.EmulatorStateSubrealm(evmPartition), gasLimits(chainInfo).Block, chainInfo.BlockKeepAmount)
}

// IMPORTANT: Must only be called from the ISC VM (when the request is done executing)
func AddFailedTx(
state kv.KVStore,
evmPartition kv.KVStore,
chainInfo *isc.ChainInfo,
tx *types.Transaction,
receipt *types.Receipt,
Expand All @@ -68,9 +68,9 @@ func AddFailedTx(
if receipt == nil {
panic("nil receipt")
}
createBlockchainDB(state, chainInfo).AddTransaction(tx, receipt)
createBlockchainDB(evmPartition, chainInfo).AddTransaction(tx, receipt)
// we must also increment the nonce manually since the original request was reverted
emulator.NewStateDB(evmStateSubrealm(state), nil).IncNonce(evmutil.MustGetSender(tx))
emulator.NewStateDB(evm.EmulatorStateSubrealm(evmPartition), nil).IncNonce(evmutil.MustGetSender(tx))
}

func gasLimits(chainInfo *isc.ChainInfo) emulator.GasLimits {
Expand Down
15 changes: 8 additions & 7 deletions packages/vm/core/evm/evmimpl/iscmagic_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/vm/core/errors/coreerrors"
"github.com/iotaledger/wasp/packages/vm/core/evm"
"github.com/iotaledger/wasp/packages/vm/core/evm/iscmagic"
)

Expand All @@ -30,12 +31,12 @@ func keyPrivileged(addr common.Address) kv.Key {
}

func isCallerPrivileged(ctx isc.SandboxBase, addr common.Address) bool {
state := iscMagicSubrealmR(ctx.StateR())
state := evm.ISCMagicSubrealmR(ctx.StateR())
return state.Has(keyPrivileged(addr))
}

func addToPrivileged(s kv.KVStore, addr common.Address) {
state := iscMagicSubrealm(s)
state := evm.ISCMagicSubrealm(s)
state.Set(keyPrivileged(addr), []byte{1})
}

Expand All @@ -45,7 +46,7 @@ func keyAllowance(from, to common.Address) kv.Key {
}

func getAllowance(ctx isc.SandboxBase, from, to common.Address) *isc.Assets {
state := iscMagicSubrealmR(ctx.StateR())
state := evm.ISCMagicSubrealmR(ctx.StateR())
key := keyAllowance(from, to)
return isc.MustAssetsFromBytes(state.Get(key))
}
Expand Down Expand Up @@ -84,7 +85,7 @@ func addToAllowance(ctx isc.Sandbox, from, to common.Address, add *isc.Assets) {
}

func withAllowance(ctx isc.Sandbox, from, to common.Address, f func(*isc.Assets)) {
state := iscMagicSubrealm(ctx.State())
state := evm.ISCMagicSubrealm(ctx.State())
key := keyAllowance(from, to)
allowance := isc.MustAssetsFromBytes(state.Get(key))
f(allowance)
Expand All @@ -94,7 +95,7 @@ func withAllowance(ctx isc.Sandbox, from, to common.Address, f func(*isc.Assets)
var errFundsNotAllowed = coreerrors.Register("remaining allowance insufficient").Create()

func subtractFromAllowance(ctx isc.Sandbox, from, to common.Address, taken *isc.Assets) *isc.Assets {
state := iscMagicSubrealm(ctx.State())
state := evm.ISCMagicSubrealm(ctx.State())
key := keyAllowance(from, to)

remaining := isc.MustAssetsFromBytes(state.Get(key))
Expand All @@ -121,12 +122,12 @@ func keyERC20ExternalNativeTokensAddress(nativeTokenID iotago.NativeTokenID) kv.
}

func addERC20ExternalNativeTokensAddress(ctx isc.Sandbox, nativeTokenID iotago.NativeTokenID, addr common.Address) {
state := iscMagicSubrealm(ctx.State())
state := evm.ISCMagicSubrealm(ctx.State())
state.Set(keyERC20ExternalNativeTokensAddress(nativeTokenID), addr.Bytes())
}

func getERC20ExternalNativeTokensAddress(ctx isc.SandboxBase, nativeTokenID iotago.NativeTokenID) (ret common.Address, ok bool) {
state := iscMagicSubrealmR(ctx.StateR())
state := evm.ISCMagicSubrealmR(ctx.StateR())
b := state.Get(keyERC20ExternalNativeTokensAddress(nativeTokenID))
if b == nil {
return ret, false
Expand Down
26 changes: 0 additions & 26 deletions packages/vm/core/evm/evmimpl/state.go

This file was deleted.

8 changes: 3 additions & 5 deletions packages/vm/core/evm/evmimpl/stateaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import (
"github.com/ethereum/go-ethereum/common"

"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/kv/subrealm"
"github.com/iotaledger/wasp/packages/vm/core/evm"
)

type StateAccess struct {
state kv.KVStoreReader
evmPartition kv.KVStoreReader
}

func NewStateAccess(store kv.KVStoreReader) *StateAccess {
contractState := subrealm.NewReadOnly(store, kv.Key(evm.Contract.Hname().Bytes()))
return &StateAccess{state: contractState}
return &StateAccess{evmPartition: evm.EVMPartitionR(store)}
}

func (sa *StateAccess) Nonce(addr common.Address) uint64 {
return Nonce(sa.state, addr)
return Nonce(sa.evmPartition, addr)
}
8 changes: 4 additions & 4 deletions packages/vm/core/evm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ const (
var GasPrice = big.NewInt(0)

const (
// KeyEVMState is the subrealm prefix for the EVM state, used by the emulator
KeyEVMState = "s"
// keyEmulatorState is the subrealm prefix for the data stored by the emulator (StateDB + BlockchainDB)
keyEmulatorState = "s"

// KeyISCMagic is the subrealm prefix for the ISC magic contract
KeyISCMagic = "m"
// keyISCMagic is the subrealm prefix for the ISC magic contract
keyISCMagic = "m"
)
33 changes: 33 additions & 0 deletions packages/vm/core/evm/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

package evm

import (
"github.com/iotaledger/wasp/packages/kv"
"github.com/iotaledger/wasp/packages/kv/subrealm"
)

func EVMPartition(chainState kv.KVStore) kv.KVStore {

Check warning on line 11 in packages/vm/core/evm/state.go

View workflow job for this annotation

GitHub Actions / Lint

exported: func name will be used as evm.EVMPartition by other packages, and that stutters; consider calling this Partition (revive)
return subrealm.New(chainState, kv.Key(Contract.Hname().Bytes()))
}

func EVMPartitionR(chainState kv.KVStoreReader) kv.KVStoreReader {

Check warning on line 15 in packages/vm/core/evm/state.go

View workflow job for this annotation

GitHub Actions / Lint

exported: func name will be used as evm.EVMPartitionR by other packages, and that stutters; consider calling this PartitionR (revive)
return subrealm.NewReadOnly(chainState, kv.Key(Contract.Hname().Bytes()))
}

func EmulatorStateSubrealm(evmPartition kv.KVStore) kv.KVStore {
return subrealm.New(evmPartition, keyEmulatorState)
}

func EmulatorStateSubrealmR(evmPartition kv.KVStoreReader) kv.KVStoreReader {
return subrealm.NewReadOnly(evmPartition, keyEmulatorState)
}

func ISCMagicSubrealm(evmPartition kv.KVStore) kv.KVStore {
return subrealm.New(evmPartition, keyISCMagic)
}

func ISCMagicSubrealmR(evmPartition kv.KVStoreReader) kv.KVStoreReader {
return subrealm.NewReadOnly(evmPartition, keyISCMagic)
}
1 change: 0 additions & 1 deletion packages/vm/vmimpl/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (reqctx *requestContext) GasBurn(burnCode gas.BurnCode, par ...uint64) {

if reqctx.gas.burned > reqctx.gas.budgetAdjusted {
reqctx.gas.burned = reqctx.gas.budgetAdjusted // do not charge more than the limit set by the request
// debug.PrintStack()
panic(vm.ErrGasBudgetExceeded)
}

Expand Down

0 comments on commit 9fee54d

Please sign in to comment.