diff options
author | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-03-27 18:49:38 +0800 |
---|---|---|
committer | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-05-06 10:50:26 +0800 |
commit | e715414cae9b4654b4784dfb924880a0787d1d55 (patch) | |
tree | a0772b2a0ef3be4ac03fc608a21ee0b7ccbd167a /les | |
parent | 996310cbd484b5ff1ea76068578314d71973770f (diff) | |
download | dexon-e715414cae9b4654b4784dfb924880a0787d1d55.tar.gz dexon-e715414cae9b4654b4784dfb924880a0787d1d55.tar.zst dexon-e715414cae9b4654b4784dfb924880a0787d1d55.zip |
core: vm: refactor vm config and context
To support multiple VMs, there must be a shared execution environment for each VM,
so this pull request moved some shared component to vm.Context and
implemented the vm.ExecPack to hold the list of VM, list of VM configures,
context and some shared resources.
The adjustment includes:
* Move NoRecursion, Depth, ReadOnly, RandCallIndex, IntPool and CallGasTemp to Context.
* Adjust VM enumeration from byte to uint8, and the VMList from map to
array.
* Register VM constructor in each VM package's init function.
* Initialize all VM instance in NewExecPack.
* Remove EVMImplement, and modify EVM, such that EVM can do the same
functions with EVMImplement.
Diffstat (limited to 'les')
-rw-r--r-- | les/api_backend.go | 12 | ||||
-rw-r--r-- | les/helper_test.go | 5 | ||||
-rw-r--r-- | les/odr_test.go | 19 |
3 files changed, 24 insertions, 12 deletions
diff --git a/les/api_backend.go b/les/api_backend.go index 2ca62c8a4..94661df4b 100644 --- a/les/api_backend.go +++ b/les/api_backend.go @@ -28,7 +28,8 @@ import ( "github.com/dexon-foundation/dexon/core/rawdb" "github.com/dexon-foundation/dexon/core/state" "github.com/dexon-foundation/dexon/core/types" - vm "github.com/dexon-foundation/dexon/core/vm/evm" + "github.com/dexon-foundation/dexon/core/vm" + "github.com/dexon-foundation/dexon/core/vm/evm" "github.com/dexon-foundation/dexon/eth/gasprice" "github.com/dexon-foundation/dexon/ethdb" "github.com/dexon-foundation/dexon/event" @@ -105,10 +106,13 @@ func (b *LesApiBackend) GetTd(hash common.Hash) *big.Int { return b.eth.blockchain.GetTdByHash(hash) } -func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*vm.EVM, func() error, error) { +func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header) (*evm.EVM, func() error, error) { state.SetBalance(msg.From(), math.MaxBig256) - context := core.NewEVMContext(msg, header, b.eth.blockchain, nil) - return vm.NewEVM(context, state, b.eth.chainConfig, vm.Config{}), state.Error, nil + context := core.NewVMContext(msg, header, b.eth.blockchain, nil) + vmConfig := [vm.NUMS]interface{}{} + vmConfig[vm.EVM] = evm.Config{} + pack := vm.NewExecPack(context, state, b.eth.chainConfig, vmConfig) + return pack.VMList[vm.EVM].(*evm.EVM), state.Error, nil } func (b *LesApiBackend) SendTx(ctx context.Context, signedTx *types.Transaction) error { diff --git a/les/helper_test.go b/les/helper_test.go index 136c4e33e..804a0ac7d 100644 --- a/les/helper_test.go +++ b/les/helper_test.go @@ -30,6 +30,7 @@ import ( "github.com/dexon-foundation/dexon/consensus/ethash" "github.com/dexon-foundation/dexon/core" "github.com/dexon-foundation/dexon/core/types" + "github.com/dexon-foundation/dexon/core/vm" "github.com/dexon-foundation/dexon/core/vm/evm" "github.com/dexon-foundation/dexon/core/vm/tools" "github.com/dexon-foundation/dexon/crypto" @@ -169,7 +170,9 @@ func newTestProtocolManager(lightSync bool, blocks int, generator func(int, *cor if lightSync { chain, _ = light.NewLightChain(odr, gspec.Config, engine) } else { - blockchain, _ := core.NewBlockChain(db, nil, gspec.Config, engine, evm.Config{}, nil) + vmConfig := [vm.NUMS]interface{}{} + vmConfig[vm.EVM] = evm.Config{} + blockchain, _ := core.NewBlockChain(db, nil, gspec.Config, engine, vmConfig, nil) gchain, _ := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, blocks, generator) if _, err := blockchain.InsertChain(gchain); err != nil { panic(err) diff --git a/les/odr_test.go b/les/odr_test.go index f6e6e873b..3c049b9a5 100644 --- a/les/odr_test.go +++ b/les/odr_test.go @@ -29,7 +29,8 @@ import ( "github.com/dexon-foundation/dexon/core/rawdb" "github.com/dexon-foundation/dexon/core/state" "github.com/dexon-foundation/dexon/core/types" - vm "github.com/dexon-foundation/dexon/core/vm/evm" + "github.com/dexon-foundation/dexon/core/vm" + "github.com/dexon-foundation/dexon/core/vm/evm" "github.com/dexon-foundation/dexon/ethdb" "github.com/dexon-foundation/dexon/light" "github.com/dexon-foundation/dexon/params" @@ -134,12 +135,14 @@ func odrContractCall(ctx context.Context, db ethdb.Database, config *params.Chai msg := callmsg{types.NewMessage(from.Address(), &testContractAddr, 0, new(big.Int), 100000, new(big.Int), data, false)} - context := core.NewEVMContext(msg, header, bc, nil) - vmenv := vm.NewEVM(context, statedb, config, vm.Config{}) + context := core.NewVMContext(msg, header, bc, nil) + vmConfig := [vm.NUMS]interface{}{} + vmConfig[vm.EVM] = evm.Config{} + pack := vm.NewExecPack(context, statedb, config, vmConfig) //vmenv := core.NewEnv(statedb, config, bc, msg, header, vm.Config{}) gp := new(core.GasPool).AddGas(math.MaxUint64) - ret, _, _, _ := core.ApplyMessage(vmenv, msg, gp) + ret, _, _, _ := core.ApplyMessage(&pack, msg, gp) res = append(res, ret...) } } else { @@ -147,10 +150,12 @@ func odrContractCall(ctx context.Context, db ethdb.Database, config *params.Chai state := light.NewState(ctx, header, lc.Odr()) state.SetBalance(testBankAddress, math.MaxBig256) msg := callmsg{types.NewMessage(testBankAddress, &testContractAddr, 0, new(big.Int), 100000, new(big.Int), data, false)} - context := core.NewEVMContext(msg, header, lc, nil) - vmenv := vm.NewEVM(context, state, config, vm.Config{}) + context := core.NewVMContext(msg, header, lc, nil) + vmConfig := [vm.NUMS]interface{}{} + vmConfig[vm.EVM] = evm.Config{} + pack := vm.NewExecPack(context, state, config, vmConfig) gp := new(core.GasPool).AddGas(math.MaxUint64) - ret, _, _, _ := core.ApplyMessage(vmenv, msg, gp) + ret, _, _, _ := core.ApplyMessage(&pack, msg, gp) if state.Error() == nil { res = append(res, ret...) } |