Modified hooks for plugeth-utils functionality

This commit is contained in:
philip-morlier 2021-09-01 13:34:03 -07:00
parent 8291edc416
commit f615e3813d
No known key found for this signature in database
GPG Key ID: 0323A143B7B6F663
2 changed files with 80 additions and 79 deletions

View File

@ -1,29 +1,30 @@
package state package state
import ( import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/plugins" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/plugins"
"github.com/opoenrelayxyz/plugeth-utils/core"
) )
// TODO (philip): change common.Hash to core.Hash, // TODO (philip): change common.Hash to core.Hash,
func PluginStateUpdate(pl *plugins.PluginLoader, blockRoot, parentRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) { func PluginStateUpdate(pl *plugins.PluginLoader, blockRoot, parentRoot core.Hash, destructs map[core.Hash]struct{}, accounts map[core.Hash][]byte, storage map[core.Hash]map[core.Hash][]byte) {
fnList := pl.Lookup("StateUpdate", func(item interface{}) bool { fnList := pl.Lookup("StateUpdate", func(item interface{}) bool {
_, ok := item.(func(common.Hash, common.Hash, map[common.Hash]struct{}, map[common.Hash][]byte, map[common.Hash]map[common.Hash][]byte)) _, ok := item.(func(core.Hash, core.Hash, map[core.Hash]struct{}, map[core.Hash][]byte, map[core.Hash]map[core.Hash][]byte))
return ok return ok
}) })
for _, fni := range fnList { for _, fni := range fnList {
if fn, ok := fni.(func(common.Hash, common.Hash, map[common.Hash]struct{}, map[common.Hash][]byte, map[common.Hash]map[common.Hash][]byte)); ok { if fn, ok := fni.(func(core.Hash, core.Hash, map[core.Hash]struct{}, map[core.Hash][]byte, map[core.Hash]map[core.Hash][]byte)); ok {
fn(blockRoot, parentRoot, destructs, accounts, storage) fn(blockRoot, parentRoot, destructs, accounts, storage)
} }
} }
} }
func pluginStateUpdate(blockRoot, parentRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) { func pluginStateUpdate(blockRoot, parentRoot core.Hash, destructs map[core.Hash]struct{}, accounts map[core.Hash][]byte, storage map[core.Hash]map[core.Hash][]byte) {
if plugins.DefaultPluginLoader == nil { if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting StateUpdate, but default PluginLoader has not been initialized") log.Warn("Attempting StateUpdate, but default PluginLoader has not been initialized")
return return
} }
PluginStateUpdate(plugins.DefaultPluginLoader, blockRoot, parentRoot, destructs, accounts, storage) PluginStateUpdate(plugins.DefaultPluginLoader, blockRoot, parentRoot, destructs, accounts, storage)
} }

View File

@ -1,19 +1,20 @@
package eth package eth
import ( import (
"math/big" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/plugins" "github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/eth/ethconfig" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/plugins"
"github.com/ethereum/go-ethereum/log" "github.com/opoenrelayxyz/plugeth-utils/core"
"reflect" "math/big"
"time" "reflect"
"time"
) )
// func PluginCreateConsensusEngine(pl *plugins.PluginLoader, stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine { // func PluginCreateConsensusEngine(pl *plugins.PluginLoader, stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
@ -37,71 +38,70 @@ import (
// return PluginCreateConsensusEngine(plugins.DefaultPluginLoader, stack, chainConfig, config, notify, noverify, db) // return PluginCreateConsensusEngine(plugins.DefaultPluginLoader, stack, chainConfig, config, notify, noverify, db)
// } // }
// TODO (philip): Translate to core.Tracer instead of vm.Tracer, with appropriate type adjustments (let me know if this one is too hard) // TODO (philip): Translate to core.Tracer instead of core.Tracer, with appropriate type adjustments (let me know if this one is too hard)
type metaTracer struct{ type metaTracer struct {
tracers []vm.Tracer tracers []core.Tracer
} }
func (mt *metaTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) { func (mt *metaTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
for _, tracer := range mt.tracers { for _, tracer := range mt.tracers {
tracer.CaptureStart(env, from, to, create, input, gas, value) tracer.CaptureStart(env, from, to, create, input, gas, value)
} }
} }
func (mt *metaTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) { func (mt *metaTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
for _, tracer := range mt.tracers { for _, tracer := range mt.tracers {
tracer.CaptureState(env, pc, op, gas, cost, scope, rData, depth, err) tracer.CaptureState(env, pc, op, gas, cost, scope, rData, depth, err)
} }
} }
func (mt *metaTracer) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) { func (mt *metaTracer) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
for _, tracer := range mt.tracers { for _, tracer := range mt.tracers {
tracer.CaptureFault(env, pc, op, gas, cost, scope, depth, err) tracer.CaptureFault(env, pc, op, gas, cost, scope, depth, err)
} }
} }
func (mt *metaTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) { func (mt *metaTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {
for _, tracer := range mt.tracers { for _, tracer := range mt.tracers {
tracer.CaptureEnd(output, gasUsed, t, err) tracer.CaptureEnd(output, gasUsed, t, err)
} }
} }
func PluginUpdateBlockchainVMConfig(pl *plugins.PluginLoader, cfg *vm.Config) { func PluginUpdateBlockchainVMConfig(pl *plugins.PluginLoader, cfg *vm.Config) {
tracerList := plugins.Lookup("LiveTracer", func(item interface{}) bool { tracerList := plugins.Lookup("LiveTracer", func(item interface{}) bool {
_, ok := item.(*vm.Tracer) _, ok := item.(*core.Tracer)
log.Info("Item is LiveTracer", "ok", ok, "type", reflect.TypeOf(item)) log.Info("Item is LiveTracer", "ok", ok, "type", reflect.TypeOf(item))
return ok return ok
}) })
if len(tracerList) > 0 { if len(tracerList) > 0 {
mt := &metaTracer{tracers: []vm.Tracer{}} mt := &metaTracer{tracers: []core.Tracer{}}
for _, tracer := range(tracerList) { for _, tracer := range tracerList {
if v, ok := tracer.(*vm.Tracer); ok { if v, ok := tracer.(*core.Tracer); ok {
log.Info("LiveTracer registered") log.Info("LiveTracer registered")
mt.tracers = append(mt.tracers, *v) mt.tracers = append(mt.tracers, *v)
} else { } else {
log.Info("Item is not tracer") log.Info("Item is not tracer")
} }
} }
cfg.Debug = true cfg.Debug = true
cfg.Tracer = mt cfg.Tracer = mt
} else { } else {
log.Warn("Module is not tracer") log.Warn("Module is not tracer")
} }
fnList := plugins.Lookup("UpdateBlockchainVMConfig", func(item interface{}) bool { fnList := plugins.Lookup("UpdateBlockchainVMConfig", func(item interface{}) bool {
_, ok := item.(func(*vm.Config)) _, ok := item.(func(*vm.Config))
return ok return ok
}) })
for _, fni := range fnList { for _, fni := range fnList {
if fn, ok := fni.(func(*vm.Config)); ok { if fn, ok := fni.(func(*vm.Config)); ok {
fn(cfg) fn(cfg)
return return
} }
} }
} }
func pluginUpdateBlockchainVMConfig(cfg *vm.Config) { func pluginUpdateBlockchainVMConfig(cfg *vm.Config) {
if plugins.DefaultPluginLoader == nil { if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting CreateConsensusEngine, but default PluginLoader has not been initialized") log.Warn("Attempting CreateConsensusEngine, but default PluginLoader has not been initialized")
return return
} }
PluginUpdateBlockchainVMConfig(plugins.DefaultPluginLoader, cfg) PluginUpdateBlockchainVMConfig(plugins.DefaultPluginLoader, cfg)
} }