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

use seperate interpreter (core/vm) #570

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ import (
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/eth/filters"
"github.com/ava-labs/coreth/interfaces"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -737,7 +737,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call interfaces.Cal
// about the transaction and calling mechanisms.
txContext := core.NewEVMTxContext(msg)
evmContext := core.NewEVMBlockContext(header, b.blockchain, nil)
vmEnv := vm.NewEVM(evmContext, txContext, stateDB, b.config, vm.Config{NoBaseFee: true})
vmEnv := core.NewEVM(evmContext, txContext, stateDB, b.config, vm.Config{NoBaseFee: true})
gasPool := new(core.GasPool).AddGas(math.MaxUint64)

return core.ApplyMessage(vmEnv, msg, gasPool)
Expand Down
6 changes: 3 additions & 3 deletions accounts/abi/bind/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
"sync"

"github.com/ava-labs/coreth/accounts/abi"
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/interfaces"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -307,14 +307,14 @@ func wrapNativeAssetCall(opts *TransactOpts, contract *common.Address, input []b
return nil, nil, errNativeAssetDeployContract
}
// wrap input with native asset call params
input = vm.PackNativeAssetCallInput(
input = core.PackNativeAssetCallInput(
*contract,
opts.NativeAssetCall.AssetID,
opts.NativeAssetCall.AssetAmount,
input,
)
// target addr is now precompile
contract = &vm.NativeAssetCallAddr
contract = &core.NativeAssetCallAddr
}
return contract, input, nil
}
Expand Down
6 changes: 3 additions & 3 deletions accounts/abi/bind/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import (

"github.com/ava-labs/coreth/accounts/abi"
"github.com/ava-labs/coreth/accounts/abi/bind"
"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/interfaces"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -404,8 +404,8 @@ func TestTransactNativeAssetCall(t *testing.T) {
nativeCallTx, err := bc.Transact(opts, methodName, arg1, arg2)
assert.Nil(err)
// verify transformations
assert.Equal(vm.NativeAssetCallAddr, *nativeCallTx.To())
unpackedAddr, unpackedAssetID, unpackedAssetAmount, unpackedData, err := vm.UnpackNativeAssetCallInput(nativeCallTx.Data())
assert.Equal(core.NativeAssetCallAddr, *nativeCallTx.To())
unpackedAddr, unpackedAssetID, unpackedAssetAmount, unpackedData, err := core.UnpackNativeAssetCallInput(nativeCallTx.Data())
assert.Nil(err)
assert.NotEmpty(unpackedData)
assert.Equal(unpackedData, normalCallTx.Data())
Expand Down
5 changes: 5 additions & 0 deletions accounts/abi/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,11 @@ func golangBindings(t *testing.T, overload bool) {
if out, err := replacer.CombinedOutput(); err != nil {
t.Fatalf("failed to replace binding test dependency to current source tree: %v\n%s", err, out)
}
replacer = exec.Command(gocmd, "mod", "edit", "-x", "-replace", "github.com/ethereum/go-ethereum=github.com/darioush/[email protected]")
replacer.Dir = pkg
if out, err := replacer.CombinedOutput(); err != nil {
t.Fatalf("failed to replace binding test dependency to current source tree: %v\n%s", err, out)
}
tidier := exec.Command(gocmd, "mod", "tidy", "-compat=1.21")
tidier.Dir = pkg
if out, err := tidier.CombinedOutput(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ import (
"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/params"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
)
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/state/snapshot"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/internal/version"
"github.com/ava-labs/coreth/metrics"
"github.com/ava-labs/coreth/params"
Expand All @@ -54,6 +53,7 @@ import (
"github.com/ava-labs/coreth/trie/triedb/pathdb"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/params"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/state/snapshot"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/trie"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/event"
)

Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ import (
"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/trie"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ import (
"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/params"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
)

Expand Down
4 changes: 2 additions & 2 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/state/pruner"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/eth/tracers/logger"
"github.com/ava-labs/coreth/params"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/stretchr/testify/require"
)
Expand Down
6 changes: 3 additions & 3 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ import (
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/state"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/trie"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
)

Expand Down Expand Up @@ -107,9 +107,9 @@ func (b *BlockGen) SetParentBeaconRoot(root common.Hash) {
b.header.ParentBeaconRoot = &root
var (
blockContext = NewEVMBlockContext(b.header, b.cm, &b.header.Coinbase)
vmenv = vm.NewEVM(blockContext, vm.TxContext{}, b.statedb, b.cm.config, vm.Config{})
vmenv = NewEVM(blockContext, vm.TxContext{}, b.statedb, b.cm.config, vm.Config{})
)
ProcessBeaconBlockRoot(root, vmenv, b.statedb)
ProcessBeaconBlockRoot(root, vmenv.EVM, b.statedb)
}

// addTx adds a transaction to the generated block. If no coinbase has
Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (
"github.com/ava-labs/coreth/consensus/dummy"
"github.com/ava-labs/coreth/core/rawdb"
"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/core/vm"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/trie"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
)

Expand Down
153 changes: 153 additions & 0 deletions core/contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// (c) 2019-2020, Ava Labs, Inc.
//
// This file is a derived work, based on the go-ethereum library whose original
// notices appear below.
//
// It is distributed under a license compatible with the licensing terms of the
// original code from which it is derived.
//
// Much love to the original authors for their work.
// **********
// Copyright 2014 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 core

import (
"fmt"

"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/precompile/contract"
"github.com/ava-labs/coreth/precompile/modules"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
)

// PrecompiledContract is the basic interface for native Go contracts. The implementation
// requires a deterministic gas count based on the input size of the Run method of the
// contract.
type PrecompiledContract interface {
RequiredGas(input []byte) uint64 // RequiredPrice calculates the contract gas use
Run(input []byte) ([]byte, error) // Run runs the precompiled contract
}

// PrecompiledContractsApricotPhase2 contains the default set of pre-compiled Ethereum
// contracts used in the Apricot Phase 2 release.
var PrecompiledContractsApricotPhase2 = map[common.Address]contract.StatefulPrecompiledContract{
genesisContractAddr: &deprecatedContract{},
NativeAssetBalanceAddr: &nativeAssetBalance{gasCost: params.AssetBalanceApricot},
NativeAssetCallAddr: &nativeAssetCall{gasCost: params.AssetCallApricot},
}

// PrecompiledContractsApricotPhasePre6 contains the default set of pre-compiled Ethereum
// contracts used in the PrecompiledContractsApricotPhasePre6 release.
var PrecompiledContractsApricotPhasePre6 = map[common.Address]contract.StatefulPrecompiledContract{
genesisContractAddr: &deprecatedContract{},
NativeAssetBalanceAddr: &deprecatedContract{},
NativeAssetCallAddr: &deprecatedContract{},
}

// PrecompiledContractsApricotPhase6 contains the default set of pre-compiled Ethereum
// contracts used in the Apricot Phase 6 release.
var PrecompiledContractsApricotPhase6 = map[common.Address]contract.StatefulPrecompiledContract{
genesisContractAddr: &deprecatedContract{},
NativeAssetBalanceAddr: &nativeAssetBalance{gasCost: params.AssetBalanceApricot},
NativeAssetCallAddr: &nativeAssetCall{gasCost: params.AssetCallApricot},
}

// PrecompiledContractsBanff contains the default set of pre-compiled Ethereum
// contracts used in the Banff release.
var PrecompiledContractsBanff = map[common.Address]contract.StatefulPrecompiledContract{
genesisContractAddr: &deprecatedContract{},
NativeAssetBalanceAddr: &deprecatedContract{},
NativeAssetCallAddr: &deprecatedContract{},
}

var (
PrecompiledAddressesBanff []common.Address
PrecompiledAddressesApricotPhase6 []common.Address
PrecompiledAddressesApricotPhasePre6 []common.Address
PrecompiledAddressesApricotPhase2 []common.Address
PrecompiledAddressesBLS []common.Address
PrecompileAllNativeAddresses map[common.Address]struct{}
)

func init() {
for k := range PrecompiledContractsApricotPhase2 {
PrecompiledAddressesApricotPhase2 = append(PrecompiledAddressesApricotPhase2, k)
}
for k := range PrecompiledContractsApricotPhasePre6 {
PrecompiledAddressesApricotPhasePre6 = append(PrecompiledAddressesApricotPhasePre6, k)
}
for k := range PrecompiledContractsApricotPhase6 {
PrecompiledAddressesApricotPhase6 = append(PrecompiledAddressesApricotPhase6, k)
}
for k := range PrecompiledContractsBanff {
PrecompiledAddressesBanff = append(PrecompiledAddressesBanff, k)
}
for k := range vm.PrecompiledContractsBLS {
PrecompiledAddressesBLS = append(PrecompiledAddressesBLS, k)
}

// Set of all native precompile addresses that are in use
// Note: this will repeat some addresses, but this is cheap and makes the code clearer.
PrecompileAllNativeAddresses = make(map[common.Address]struct{})
addrsList := append(vm.PrecompiledAddressesHomestead, vm.PrecompiledAddressesByzantium...)
addrsList = append(addrsList, vm.PrecompiledAddressesIstanbul...)
addrsList = append(addrsList, PrecompiledAddressesApricotPhase2...)
addrsList = append(addrsList, PrecompiledAddressesApricotPhasePre6...)
addrsList = append(addrsList, PrecompiledAddressesApricotPhase6...)
addrsList = append(addrsList, PrecompiledAddressesBanff...)
addrsList = append(addrsList, vm.PrecompiledAddressesCancun...)
addrsList = append(addrsList, PrecompiledAddressesBLS...)
for _, k := range addrsList {
PrecompileAllNativeAddresses[k] = struct{}{}
}

// Ensure that this package will panic during init if there is a conflict present with the declared
// precompile addresses.
for _, module := range modules.RegisteredModules() {
address := module.Address
if _, ok := PrecompileAllNativeAddresses[address]; ok {
panic(fmt.Errorf("precompile address collides with existing native address: %s", address))
}
}
}

// ActivePrecompiles returns the precompiles enabled with the current configuration.
func ActivePrecompiles(rules params.Rules) []common.Address {
var precompiles []common.Address
precompiles = append(precompiles, vm.ActivePrecompiles(asGethRules(rules))...)
precompiles = append(precompiles, avalanchePrecompiles(rules)...)
return precompiles
}

// avalanchePrecompiles returns the Avalanche specific precompiles enabled with
// the current configuration.
func avalanchePrecompiles(rules params.Rules) []common.Address {
switch {
case rules.IsBanff:
return PrecompiledAddressesBanff
case rules.IsApricotPhase6:
return PrecompiledAddressesApricotPhase6
case rules.IsApricotPhasePre6:
return PrecompiledAddressesApricotPhasePre6
case rules.IsApricotPhase2:
return PrecompiledAddressesApricotPhase2
default:
return nil
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package vm
package core

import (
"fmt"
Expand Down
Loading
Loading