eth/tracers/plugin_hooks, tracers/plugin_hooks, and wrappers utils refactor modifications

This commit is contained in:
philip-morlier 2021-09-08 14:38:17 -07:00
parent c36c999383
commit b36e63f184
No known key found for this signature in database
GPG Key ID: 0323A143B7B6F663
3 changed files with 176 additions and 65 deletions

View File

@ -1,20 +1,16 @@
package eth package eth
import ( import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/plugins"
"github.com/opoenrelayxyz/plugeth-utils/core"
"math/big" "math/big"
"reflect" "reflect"
"time" "time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/plugins"
"github.com/ethereum/go-ethereum/plugins/wrappers"
"github.com/openrelayxyz/plugeth-utils/core"
) )
// 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 {
@ -38,24 +34,24 @@ 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 core.Tracer, with appropriate type adjustments (let me know if this one is too hard) // TODO (philip): Translate to core.TracerResult instead of vm.Tracer, with appropriate type adjustments (let me know if this one is too hard)
type metaTracer struct { type metaTracer struct {
tracers []core.Tracer tracers []core.TracerResult
} }
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(core.Address(from), core.Address(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(pc, core.OpCode(op), gas, cost, wrappers.NewWrappedScopeContext(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(pc, core.OpCode(op), gas, cost, wrappers.NewWrappedScopeContext(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) {
@ -66,22 +62,22 @@ func (mt *metaTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration,
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.(*core.Tracer) _, ok := item.(*vm.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: []core.Tracer{}} mt := &metaTracer{tracers: []core.TracerResult{}}
for _, tracer := range tracerList { for _, tracer := range tracerList {
if v, ok := tracer.(*core.Tracer); ok { if v, ok := tracer.(core.TracerResult); 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 //I think this means we will need a vm.config wrapper although confugure doesnt sound very passive
} else { } else {
log.Warn("Module is not tracer") log.Warn("Module is not tracer")
} }

View File

@ -1,24 +1,31 @@
package tracers package tracers
import ( import (
"github.com/ethereum/go-ethereum/plugins" "reflect"
"github.com/ethereum/go-ethereum/plugins/interfaces"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"reflect" "github.com/ethereum/go-ethereum/plugins"
"github.com/ethereum/go-ethereum/plugins/interfaces"
"github.com/ethereum/go-ethereum/plugins/wrappers"
"github.com/openrelayxyz/plugeth-utils/core"
) )
// TODO (philip): Translate to use core.Tracer instead of vm.Tracer. This will require a wrapper
func GetPluginTracer(pl *plugins.PluginLoader, name string) (func(*state.StateDB) interfaces.TracerResult, bool) { func GetPluginTracer(pl *plugins.PluginLoader, name string) (func(*state.StateDB) interfaces.TracerResult, bool) {
tracers := pl.Lookup("Tracers", func(item interface{}) bool { tracers := pl.Lookup("Tracers", func(item interface{}) bool {
_, ok := item.(*map[string]func(*state.StateDB) interfaces.TracerResult) _, ok := item.(*map[string]func(*state.StateDB) interfaces.TracerResult)
if !ok { log.Warn("Found tracer that did not match type", "tracer", reflect.TypeOf(item) ) } if !ok {
log.Warn("Found tracer that did not match type", "tracer", reflect.TypeOf(item))
}
return ok return ok
}) })
for _, tmap := range tracers { for _, tmap := range tracers {
if tracerMap, ok := tmap.(*map[string]func(*state.StateDB)interfaces.TracerResult); ok { if tracerMap, ok := tmap.(*map[string]func(core.StateDB) core.TracerResult); ok {
if tracer, ok := (*tracerMap)[name]; ok { if tracer, ok := (*tracerMap)[name]; ok {
return tracer, true return func(sdb *state.StateDB) interfaces.TracerResult {
return wrappers.NewWrappedTracer(tracer(wrappers.NewWrappedStateDB(sdb)))
}, true
} }
} }
} }

View File

@ -5,10 +5,12 @@ import (
"encoding/json" "encoding/json"
"math/big" "math/big"
"sync" "sync"
"time"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
gcore "github.com/ethereum/go-ethereum/core" gcore "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/downloader"
@ -22,55 +24,161 @@ import (
"github.com/openrelayxyz/plugeth-utils/restricted" "github.com/openrelayxyz/plugeth-utils/restricted"
) )
type WrapedScopeContext struct { type WrappedScopeContext struct {
s *vm.ScopeContext s *vm.ScopeContext
} }
func (w *WrapedScopeContext) Memory() core.Memory { func NewWrappedScopeContext(s *vm.ScopeContext) *WrappedScopeContext {
return &WrappedScopeContext{s}
}
func (w *WrappedScopeContext) Memory() core.Memory {
return w.s.Memory return w.s.Memory
} }
func (w *WrapedScopeContext) Stack() core.Stack { func (w *WrappedScopeContext) Stack() core.Stack {
return w.s.Stack return w.s.Stack
} }
// type Contract interface { <= this is the core.Contract func (w *WrappedScopeContext) Contract() core.Contract {
// AsDelegate() Contract return &WrappedContract{w.s.Contract}
// GetOp(n uint64) OpCode }
// GetByte(n uint64) byte
// Caller() Address
// Address() Address
// Value() *big.Int
// }
type WrappedContract struct { type WrappedContract struct {
c *vm.Contract c *vm.Contract
} }
func (w WrappedContract) AsDelegate() core.Contract { func (w *WrappedContract) AsDelegate() core.Contract {
return WrappedContract{w.c.AsDelegate()} return &WrappedContract{w.c.AsDelegate()}
} }
func (w WrappedContract) GetOp(n uint64) core.OpCode { func (w *WrappedContract) GetOp(n uint64) core.OpCode {
return core.OpCode(w.c.GetOp(n)) return core.OpCode(w.c.GetOp(n))
} }
func (w WrappedContract) GetByte(n uint64) byte { func (w *WrappedContract) GetByte(n uint64) byte {
return w.c.GetByte(n) return w.c.GetByte(n)
} }
func (w WrappedContract) Caller() core.Address { func (w *WrappedContract) Caller() core.Address {
return core.Address(w.c.Caller()) return core.Address(w.c.Caller())
} }
func (w WrappedContract) Address() core.Address { func (w *WrappedContract) Address() core.Address {
return core.Address(w.c.Address()) return core.Address(w.c.Address())
} }
func (w WrappedContract) Value() *big.Int { func (w *WrappedContract) Value() *big.Int {
return w.c.Value() return w.c.Value()
} }
// added UseGas bc compiler compained without it. Should investigate if the false return with effect performance.
// take this out of core.interface
func (w *WrappedContract) UseGas(gas uint64) (ok bool) {
return false
}
type WrappedTracer struct {
r core.TracerResult
}
func NewWrappedTracer(r core.TracerResult) *WrappedTracer {
return &WrappedTracer{r}
}
func (w WrappedTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
w.r.CaptureStart(core.Address(from), core.Address(to), create, input, gas, value)
}
func (w WrappedTracer) CaptureState(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
w.r.CaptureState(pc, core.OpCode(op), gas, cost, &WrappedScopeContext{scope}, rData, depth, err)
}
func (w WrappedTracer) CaptureFault(env *vm.EVM, pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
w.r.CaptureFault(pc, core.OpCode(op), gas, cost, &WrappedScopeContext{scope}, depth, err)
}
func (w WrappedTracer) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) {
w.r.CaptureEnd(output, gasUsed, t, err)
}
func (w WrappedTracer) GetResult() (interface{}, error) {
return w.r.Result()
}
type WrappedStateDB struct {
s *state.StateDB
}
func NewWrappedStateDB(d *state.StateDB) *WrappedStateDB {
return &WrappedStateDB{d}
}
// GetBalance(Address) *big.Int
func (w *WrappedStateDB) GetBalance(addr core.Address) *big.Int {
return w.s.GetBalance(common.Address(addr))
}
// GetNonce(Address) uint64
func (w *WrappedStateDB) GetNonce(addr core.Address) uint64 {
return w.s.GetNonce(common.Address(addr))
}
// GetCodeHash(Address) Hash
func (w *WrappedStateDB) GetCodeHash(addr core.Address) core.Hash {
return core.Hash(w.s.GetCodeHash(common.Address(addr)))
} // sort this out
// GetCode(Address) []byte
func (w *WrappedStateDB) GetCode(addr core.Address) []byte {
return w.s.GetCode(common.Address(addr))
}
// GetCodeSize(Address) int
func (w *WrappedStateDB) GetCodeSize(addr core.Address) int {
return w.s.GetCodeSize(common.Address(addr))
}
//GetRefund() uint64
func (w *WrappedStateDB) GetRefund() uint64 { //are we sure we want to include this? getting a refund seems like changing state
return w.s.GetRefund()
}
// GetCommittedState(Address, Hash) Hash
func (w *WrappedStateDB) GetCommittedState(addr core.Address, hsh core.Hash) core.Hash {
return core.Hash(w.s.GetCommittedState(common.Address(addr), common.Hash(hsh)))
}
// GetState(Address, Hash) Hash
func (w *WrappedStateDB) GetState(addr core.Address, hsh core.Hash) core.Hash {
return core.Hash(w.s.GetState(common.Address(addr), common.Hash(hsh)))
}
// HasSuicided(Address) bool
func (w *WrappedStateDB) HasSuicided(addr core.Address) bool { // I figured we'd skip some of the future labor and update the name now
return w.s.HasSuicided(common.Address(addr))
}
// // Exist reports whether the given account exists in state.
// // Notably this should also return true for suicided accounts.
// Exist(Address) bool
func (w *WrappedStateDB) Exist(addr core.Address) bool {
return w.s.Exist(common.Address(addr))
}
// // Empty returns whether the given account is empty. Empty
// // is defined according to EIP161 (balance = nonce = code = 0).
// Empty(Address) bool
func (w *WrappedStateDB) Empty(addr core.Address) bool {
return w.s.Empty(common.Address(addr))
}
// AddressInAccessList(addr Address) bool
func (w *WrappedStateDB) AddressInAccessList(addr core.Address) bool {
return w.s.AddressInAccessList(common.Address(addr))
}
// SlotInAccessList(addr Address, slot Hash) (addressOk bool, slotOk bool)
func (w *WrappedStateDB) SlotInAccessList(addr core.Address, slot core.Hash) (addressOK, slotOk bool) {
return w.s.SlotInAccessList(common.Address(addr), common.Hash(slot))
}
type Node struct { type Node struct {
n *node.Node n *node.Node
} }