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

Remove array of runtimes #859

Merged
merged 5 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 0 additions & 3 deletions consensus/ibft/fork/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
stakingHelper "github.com/0xPolygon/polygon-edge/helper/staking"
"github.com/0xPolygon/polygon-edge/state"
itrie "github.com/0xPolygon/polygon-edge/state/immutable-trie"
"github.com/0xPolygon/polygon-edge/state/runtime/evm"
"github.com/0xPolygon/polygon-edge/types"
"github.com/0xPolygon/polygon-edge/validators"
"github.com/0xPolygon/polygon-edge/validators/store"
Expand Down Expand Up @@ -283,8 +282,6 @@ func newTestTransition(
}, st, hclog.NewNullLogger())

rootHash := ex.WriteGenesis(nil)

ex.SetRuntime(evm.NewEVM())
ex.GetHash = func(h *types.Header) state.GetHashByNumber {
return func(i uint64) types.Hash {
return rootHash
Expand Down
4 changes: 0 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/0xPolygon/polygon-edge/state"
itrie "github.com/0xPolygon/polygon-edge/state/immutable-trie"
"github.com/0xPolygon/polygon-edge/state/runtime"
"github.com/0xPolygon/polygon-edge/state/runtime/evm"
"github.com/0xPolygon/polygon-edge/state/runtime/precompiled"
"github.com/0xPolygon/polygon-edge/txpool"
"github.com/0xPolygon/polygon-edge/types"
"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -188,8 +186,6 @@ func NewServer(config *Config) (*Server, error) {
m.state = st

m.executor = state.NewExecutor(config.Chain.Params, st, logger)
m.executor.SetRuntime(precompiled.NewPrecompiled())
m.executor.SetRuntime(evm.NewEVM())

// compute the genesis root state
genesisRoot := m.executor.WriteGenesis(config.Chain.Genesis.Alloc)
Expand Down
69 changes: 35 additions & 34 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"math"
"math/big"

"github.com/0xPolygon/polygon-edge/state/runtime/evm"

"github.com/hashicorp/go-hclog"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/state/runtime"
"github.com/0xPolygon/polygon-edge/state/runtime/evm"
"github.com/0xPolygon/polygon-edge/state/runtime/precompiled"
"github.com/0xPolygon/polygon-edge/types"
)

Expand All @@ -32,22 +32,20 @@ type GetHashByNumberHelper = func(*types.Header) GetHashByNumber

// Executor is the main entity
type Executor struct {
logger hclog.Logger
config *chain.Params
runtimes []runtime.Runtime
state State
GetHash GetHashByNumberHelper
logger hclog.Logger
config *chain.Params
state State
GetHash GetHashByNumberHelper

PostHook func(txn *Transition)
}

// NewExecutor creates a new executor
func NewExecutor(config *chain.Params, s State, logger hclog.Logger) *Executor {
return &Executor{
logger: logger,
config: config,
runtimes: []runtime.Runtime{},
state: s,
logger: logger,
config: config,
state: s,
}
}

Expand Down Expand Up @@ -79,11 +77,6 @@ func (e *Executor) WriteGenesis(alloc map[types.Address]*chain.GenesisAccount) t
return types.BytesToHash(root)
}

// SetRuntime adds a runtime to the runtime set
func (e *Executor) SetRuntime(r runtime.Runtime) {
e.runtimes = append(e.runtimes, r)
}

type BlockResult struct {
Root types.Hash
Receipts []*types.Receipt
Expand Down Expand Up @@ -160,7 +153,6 @@ func (e *Executor) BeginTxn(

txn := &Transition{
logger: e.logger,
r: e,
ctx: env2,
state: newTxn,
getHash: e.GetHash(header),
Expand All @@ -170,6 +162,10 @@ func (e *Executor) BeginTxn(

receipts: []*types.Receipt{},
totalGas: 0,

evm: evm.NewEVM(),
precompiles: precompiled.NewPrecompiled(),
PostHook: e.PostHook,
}

return txn, nil
Expand All @@ -184,7 +180,6 @@ type Transition struct {
// the current block being processed
block *types.Block

r *Executor
config chain.ForksInTime
state *Txn
getHash GetHashByNumber
Expand All @@ -194,17 +189,20 @@ type Transition struct {
// result
receipts []*types.Receipt
totalGas uint64

PostHook func(t *Transition)

// runtimes
evm *evm.EVM
precompiles *precompiled.Precompiled
}

func NewTransition(config chain.ForksInTime, radix *Txn) *Transition {
return &Transition{
config: config,
state: radix,
r: &Executor{
runtimes: []runtime.Runtime{
evm.NewEVM(),
},
},
config: config,
state: radix,
evm: evm.NewEVM(),
precompiles: precompiled.NewPrecompiled(),
}
}

Expand All @@ -219,7 +217,7 @@ func (t *Transition) Receipts() []*types.Receipt {
var emptyFrom = types.Address{}

func (t *Transition) WriteFailedReceipt(txn *types.Transaction) error {
signer := crypto.NewSigner(t.config, uint64(t.r.config.ChainID))
signer := crypto.NewSigner(t.config, uint64(t.ctx.ChainID))

if txn.From == emptyFrom {
// Decrypt the from address
Expand Down Expand Up @@ -250,7 +248,7 @@ func (t *Transition) WriteFailedReceipt(txn *types.Transaction) error {

// Write writes another transaction to the executor
func (t *Transition) Write(txn *types.Transaction) error {
signer := crypto.NewSigner(t.config, uint64(t.r.config.ChainID))
signer := crypto.NewSigner(t.config, uint64(t.ctx.ChainID))

var err error
if txn.From == emptyFrom {
Expand Down Expand Up @@ -356,8 +354,8 @@ func (t *Transition) Apply(msg *types.Transaction) (*runtime.ExecutionResult, er
t.state.RevertToSnapshot(s)
}

if t.r.PostHook != nil {
t.r.PostHook(t)
if t.PostHook != nil {
t.PostHook(t)
}

return result, err
Expand Down Expand Up @@ -535,14 +533,17 @@ func (t *Transition) Call2(
}

func (t *Transition) run(contract *runtime.Contract, host runtime.Host) *runtime.ExecutionResult {
for _, r := range t.r.runtimes {
if r.CanRun(contract, host, &t.config) {
return r.Run(contract, host, &t.config)
}
// check the precompiles
if t.precompiles.CanRun(contract, host, &t.config) {
return t.precompiles.Run(contract, host, &t.config)
}
// check the evm
if t.evm.CanRun(contract, host, &t.config) {
return t.evm.Run(contract, host, &t.config)
}

return &runtime.ExecutionResult{
Err: fmt.Errorf("not found"),
Err: fmt.Errorf("runtime not found"),
}
}

Expand Down
4 changes: 0 additions & 4 deletions tests/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/state"
"github.com/0xPolygon/polygon-edge/state/runtime/evm"
"github.com/0xPolygon/polygon-edge/state/runtime/precompiled"
"github.com/0xPolygon/polygon-edge/types"
"github.com/hashicorp/go-hclog"
)
Expand Down Expand Up @@ -51,8 +49,6 @@ func RunSpecificTest(t *testing.T, file string, c stateCase, name, fork string,
forks := config.At(uint64(env.Number))

xxx := state.NewExecutor(&chain.Params{Forks: config, ChainID: 1}, s, hclog.NewNullLogger())
xxx.SetRuntime(precompiled.NewPrecompiled())
xxx.SetRuntime(evm.NewEVM())

xxx.PostHook = func(t *state.Transition) {
if name == "failed_tx_xcf416c53" {
Expand Down
2 changes: 0 additions & 2 deletions validators/store/contract/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
testHelper "github.com/0xPolygon/polygon-edge/helper/tests"
"github.com/0xPolygon/polygon-edge/state"
itrie "github.com/0xPolygon/polygon-edge/state/immutable-trie"
"github.com/0xPolygon/polygon-edge/state/runtime/evm"
"github.com/0xPolygon/polygon-edge/types"
"github.com/0xPolygon/polygon-edge/validators"
"github.com/0xPolygon/polygon-edge/validators/store"
Expand Down Expand Up @@ -87,7 +86,6 @@ func newTestTransition(

rootHash := ex.WriteGenesis(nil)

ex.SetRuntime(evm.NewEVM())
ex.GetHash = func(h *types.Header) state.GetHashByNumber {
return func(i uint64) types.Hash {
return rootHash
Expand Down