2021-06-26 03:38:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2021-11-06 00:26:53 +00:00
|
|
|
"encoding/json"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"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/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/openrelayxyz/plugeth-utils/core"
|
2021-06-26 03:38:04 +00:00
|
|
|
)
|
|
|
|
|
2021-06-26 04:02:25 +00:00
|
|
|
func PluginPreProcessBlock(pl *plugins.PluginLoader, block *types.Block) {
|
2021-11-06 00:26:53 +00:00
|
|
|
fnList := pl.Lookup("PreProcessBlock", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func(core.Hash, uint64, []byte))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
encoded, _ := rlp.EncodeToBytes(block)
|
|
|
|
for _, fni := range fnList {
|
|
|
|
if fn, ok := fni.(func(core.Hash, uint64, []byte)); ok {
|
|
|
|
fn(core.Hash(block.Hash()), block.NumberU64(), encoded)
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 03:38:04 +00:00
|
|
|
}
|
2021-06-26 04:02:25 +00:00
|
|
|
func pluginPreProcessBlock(block *types.Block) {
|
2021-11-06 00:26:53 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
2021-06-26 04:02:25 +00:00
|
|
|
log.Warn("Attempting PreProcessBlock, but default PluginLoader has not been initialized")
|
2021-11-06 00:26:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginPreProcessBlock(plugins.DefaultPluginLoader, block) // TODO
|
2021-06-26 04:02:25 +00:00
|
|
|
}
|
|
|
|
func PluginPreProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int) {
|
2021-11-06 00:26:53 +00:00
|
|
|
fnList := pl.Lookup("PreProcessTransaction", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func([]byte, core.Hash, core.Hash, int))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
txBytes, _ := tx.MarshalBinary()
|
|
|
|
for _, fni := range fnList {
|
|
|
|
if fn, ok := fni.(func([]byte, core.Hash, core.Hash, int)); ok {
|
|
|
|
fn(txBytes, core.Hash(tx.Hash()), core.Hash(block.Hash()), i)
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 03:38:04 +00:00
|
|
|
}
|
2021-06-26 04:02:25 +00:00
|
|
|
func pluginPreProcessTransaction(tx *types.Transaction, block *types.Block, i int) {
|
2021-11-06 00:26:53 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
2021-06-26 04:02:25 +00:00
|
|
|
log.Warn("Attempting PreProcessTransaction, but default PluginLoader has not been initialized")
|
2021-11-06 00:26:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginPreProcessTransaction(plugins.DefaultPluginLoader, tx, block, i)
|
2021-06-26 04:02:25 +00:00
|
|
|
}
|
|
|
|
func PluginBlockProcessingError(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, err error) {
|
2021-11-06 00:26:53 +00:00
|
|
|
fnList := pl.Lookup("BlockProcessingError", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func(core.Hash, core.Hash, error))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
for _, fni := range fnList {
|
|
|
|
if fn, ok := fni.(func(core.Hash, core.Hash, error)); ok {
|
|
|
|
fn(core.Hash(tx.Hash()), core.Hash(block.Hash()), err)
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 03:38:04 +00:00
|
|
|
}
|
2021-06-26 04:02:25 +00:00
|
|
|
func pluginBlockProcessingError(tx *types.Transaction, block *types.Block, err error) {
|
2021-11-06 00:26:53 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
2021-06-26 04:02:25 +00:00
|
|
|
log.Warn("Attempting BlockProcessingError, but default PluginLoader has not been initialized")
|
2021-11-06 00:26:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginBlockProcessingError(plugins.DefaultPluginLoader, tx, block, err)
|
2021-06-26 04:02:25 +00:00
|
|
|
}
|
|
|
|
func PluginPostProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
|
2021-11-06 00:26:53 +00:00
|
|
|
fnList := pl.Lookup("PostProcessTransaction", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func(core.Hash, core.Hash, int, []byte))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
receiptBytes, _ := json.Marshal(receipt)
|
|
|
|
for _, fni := range fnList {
|
|
|
|
if fn, ok := fni.(func(core.Hash, core.Hash, int, []byte)); ok {
|
|
|
|
fn(core.Hash(tx.Hash()), core.Hash(block.Hash()), i, receiptBytes)
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 03:38:04 +00:00
|
|
|
}
|
2021-06-26 04:02:25 +00:00
|
|
|
func pluginPostProcessTransaction(tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
|
2021-11-06 00:26:53 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
2021-06-26 04:02:25 +00:00
|
|
|
log.Warn("Attempting PostProcessTransaction, but default PluginLoader has not been initialized")
|
2021-11-06 00:26:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginPostProcessTransaction(plugins.DefaultPluginLoader, tx, block, i, receipt)
|
2021-06-26 04:02:25 +00:00
|
|
|
}
|
|
|
|
func PluginPostProcessBlock(pl *plugins.PluginLoader, block *types.Block) {
|
2021-11-06 00:26:53 +00:00
|
|
|
fnList := pl.Lookup("PostProcessBlock", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func(core.Hash))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
for _, fni := range fnList {
|
|
|
|
if fn, ok := fni.(func(core.Hash)); ok {
|
|
|
|
fn(core.Hash(block.Hash()))
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 03:38:04 +00:00
|
|
|
}
|
2021-06-26 04:02:25 +00:00
|
|
|
func pluginPostProcessBlock(block *types.Block) {
|
2021-11-06 00:26:53 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
2021-06-26 04:02:25 +00:00
|
|
|
log.Warn("Attempting PostProcessBlock, but default PluginLoader has not been initialized")
|
2021-11-06 00:26:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginPostProcessBlock(plugins.DefaultPluginLoader, block)
|
2021-06-26 04:02:25 +00:00
|
|
|
}
|
2021-09-20 16:23:37 +00:00
|
|
|
func PluginNewHead(pl *plugins.PluginLoader, block *types.Block, hash common.Hash, logs []*types.Log, td *big.Int) {
|
2021-11-06 00:26:53 +00:00
|
|
|
fnList := pl.Lookup("NewHead", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func([]byte, core.Hash, [][]byte, *big.Int))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
blockBytes, _ := rlp.EncodeToBytes(block)
|
|
|
|
logBytes := make([][]byte, len(logs))
|
|
|
|
for i, l := range logs {
|
|
|
|
logBytes[i], _ = rlp.EncodeToBytes(l)
|
|
|
|
}
|
|
|
|
for _, fni := range fnList {
|
|
|
|
if fn, ok := fni.(func([]byte, core.Hash, [][]byte, *big.Int)); ok {
|
|
|
|
fn(blockBytes, core.Hash(hash), logBytes, td)
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 16:38:52 +00:00
|
|
|
}
|
2021-09-20 16:23:37 +00:00
|
|
|
func pluginNewHead(block *types.Block, hash common.Hash, logs []*types.Log, td *big.Int) {
|
2021-11-06 00:26:53 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
2021-07-12 16:38:52 +00:00
|
|
|
log.Warn("Attempting NewHead, but default PluginLoader has not been initialized")
|
2021-11-06 00:26:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginNewHead(plugins.DefaultPluginLoader, block, hash, logs, td)
|
2021-07-12 16:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PluginNewSideBlock(pl *plugins.PluginLoader, block *types.Block, hash common.Hash, logs []*types.Log) {
|
2021-11-06 00:26:53 +00:00
|
|
|
fnList := pl.Lookup("NewSideBlock", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func([]byte, core.Hash, [][]byte))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
blockBytes, _ := rlp.EncodeToBytes(block)
|
|
|
|
logBytes := make([][]byte, len(logs))
|
|
|
|
for i, l := range logs {
|
|
|
|
logBytes[i], _ = rlp.EncodeToBytes(l)
|
|
|
|
}
|
|
|
|
for _, fni := range fnList {
|
|
|
|
if fn, ok := fni.(func([]byte, core.Hash, [][]byte)); ok {
|
|
|
|
fn(blockBytes, core.Hash(hash), logBytes)
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 16:38:52 +00:00
|
|
|
}
|
|
|
|
func pluginNewSideBlock(block *types.Block, hash common.Hash, logs []*types.Log) {
|
2021-11-06 00:26:53 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
2021-07-12 16:38:52 +00:00
|
|
|
log.Warn("Attempting NewSideBlock, but default PluginLoader has not been initialized")
|
2021-11-06 00:26:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginNewSideBlock(plugins.DefaultPluginLoader, block, hash, logs)
|
2021-07-12 16:38:52 +00:00
|
|
|
}
|
2021-07-12 20:46:50 +00:00
|
|
|
|
|
|
|
func PluginReorg(pl *plugins.PluginLoader, commonBlock *types.Block, oldChain, newChain types.Blocks) {
|
2021-11-06 00:26:53 +00:00
|
|
|
fnList := pl.Lookup("Reorg", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func(core.Hash, []core.Hash, []core.Hash))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
oldChainHashes := make([]core.Hash, len(oldChain))
|
|
|
|
for i, block := range oldChain {
|
|
|
|
oldChainHashes[i] = core.Hash(block.Hash())
|
|
|
|
}
|
|
|
|
newChainHashes := make([]core.Hash, len(newChain))
|
|
|
|
for i, block := range newChain {
|
|
|
|
newChainHashes[i] = core.Hash(block.Hash())
|
|
|
|
}
|
|
|
|
for _, fni := range fnList {
|
|
|
|
if fn, ok := fni.(func(core.Hash, []core.Hash, []core.Hash)); ok {
|
|
|
|
fn(core.Hash(commonBlock.Hash()), oldChainHashes, newChainHashes)
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 20:46:50 +00:00
|
|
|
}
|
|
|
|
func pluginReorg(commonBlock *types.Block, oldChain, newChain types.Blocks) {
|
2021-11-06 00:26:53 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
2021-07-12 20:46:50 +00:00
|
|
|
log.Warn("Attempting Reorg, but default PluginLoader has not been initialized")
|
2021-11-06 00:26:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginReorg(plugins.DefaultPluginLoader, commonBlock, oldChain, newChain)
|
|
|
|
}
|
|
|
|
|
2022-02-16 00:56:50 +00:00
|
|
|
type PreTracer interface {
|
|
|
|
CapturePreStart(from common.Address, to *common.Address, input []byte, gas uint64, value *big.Int)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-06 00:26:53 +00:00
|
|
|
type metaTracer struct {
|
|
|
|
tracers []core.BlockTracer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *metaTracer) PreProcessBlock(block *types.Block) {
|
2022-01-07 17:34:07 +00:00
|
|
|
if len(mt.tracers) == 0 { return }
|
2021-11-06 00:26:53 +00:00
|
|
|
blockHash := core.Hash(block.Hash())
|
|
|
|
blockNumber := block.NumberU64()
|
|
|
|
encoded, _ := rlp.EncodeToBytes(block)
|
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
tracer.PreProcessBlock(blockHash, blockNumber, encoded)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (mt *metaTracer) PreProcessTransaction(tx *types.Transaction, block *types.Block, i int) {
|
2022-01-07 17:34:07 +00:00
|
|
|
if len(mt.tracers) == 0 { return }
|
2021-11-06 00:26:53 +00:00
|
|
|
blockHash := core.Hash(block.Hash())
|
|
|
|
transactionHash := core.Hash(tx.Hash())
|
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
tracer.PreProcessTransaction(transactionHash, blockHash, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (mt *metaTracer) BlockProcessingError(tx *types.Transaction, block *types.Block, err error) {
|
2022-01-07 17:34:07 +00:00
|
|
|
if len(mt.tracers) == 0 { return }
|
2021-11-06 00:26:53 +00:00
|
|
|
blockHash := core.Hash(block.Hash())
|
|
|
|
transactionHash := core.Hash(tx.Hash())
|
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
tracer.BlockProcessingError(transactionHash, blockHash, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (mt *metaTracer) PostProcessTransaction(tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
|
2022-01-07 17:34:07 +00:00
|
|
|
if len(mt.tracers) == 0 { return }
|
2021-11-06 00:26:53 +00:00
|
|
|
blockHash := core.Hash(block.Hash())
|
|
|
|
transactionHash := core.Hash(tx.Hash())
|
|
|
|
receiptBytes, _ := json.Marshal(receipt)
|
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
tracer.PostProcessTransaction(transactionHash, blockHash, i, receiptBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (mt *metaTracer) PostProcessBlock(block *types.Block) {
|
2022-01-07 17:34:07 +00:00
|
|
|
if len(mt.tracers) == 0 { return }
|
2021-11-06 00:26:53 +00:00
|
|
|
blockHash := core.Hash(block.Hash())
|
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
tracer.PostProcessBlock(blockHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
tracer.CaptureStart(core.Address(from), core.Address(to), create, input, gas, value)
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 16:41:24 +00:00
|
|
|
func (mt *metaTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
|
2021-11-06 00:26:53 +00:00
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
tracer.CaptureState(pc, core.OpCode(op), gas, cost, wrappers.NewWrappedScopeContext(scope), rData, depth, err)
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 16:41:24 +00:00
|
|
|
func (mt *metaTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
|
2021-11-06 00:26:53 +00:00
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
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) {
|
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
tracer.CaptureEnd(output, gasUsed, t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *metaTracer) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
|
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
tracer.CaptureEnter(core.OpCode(typ), core.Address(from), core.Address(to), input, gas, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *metaTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
|
|
|
|
for _, tracer := range mt.tracers {
|
|
|
|
tracer.CaptureExit(output, gasUsed, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 17:34:07 +00:00
|
|
|
func PluginGetBlockTracer(pl *plugins.PluginLoader, hash common.Hash, statedb *state.StateDB) (*metaTracer, bool) {
|
2021-11-06 00:26:53 +00:00
|
|
|
//look for a function that takes whatever the ctx provides and statedb and returns a core.blocktracer append into meta tracer
|
|
|
|
tracerList := plugins.Lookup("GetLiveTracer", func(item interface{}) bool {
|
|
|
|
_, ok := item.(func(core.Hash, core.StateDB) core.BlockTracer)
|
|
|
|
log.Info("Item is LiveTracer", "ok", ok, "type", reflect.TypeOf(item))
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
mt := &metaTracer{tracers: []core.BlockTracer{}}
|
|
|
|
for _, tracer := range tracerList {
|
|
|
|
if v, ok := tracer.(func(core.Hash, core.StateDB) core.BlockTracer); ok {
|
|
|
|
bt := v(core.Hash(hash), wrappers.NewWrappedStateDB(statedb))
|
|
|
|
if bt != nil {
|
|
|
|
mt.tracers = append(mt.tracers, bt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 17:34:07 +00:00
|
|
|
return mt, (len(mt.tracers) > 0)
|
2021-11-06 00:26:53 +00:00
|
|
|
}
|
2022-01-07 17:34:07 +00:00
|
|
|
func pluginGetBlockTracer(hash common.Hash, statedb *state.StateDB) (*metaTracer, bool) {
|
2021-11-06 00:26:53 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting GetBlockTracer, but default PluginLoader has not been initialized")
|
2022-01-07 18:05:18 +00:00
|
|
|
return &metaTracer{}, false
|
2021-11-06 00:26:53 +00:00
|
|
|
}
|
|
|
|
return PluginGetBlockTracer(plugins.DefaultPluginLoader, hash, statedb)
|
2021-07-12 20:46:50 +00:00
|
|
|
}
|