Updates to support blockupdates plugin

This makes several updates to support the blockupdates plugin.

I had to update several hooks that were using the wrong types, and
provide a way to get event.Feed objects into plugins without importing
event.Feed (which I did by having the plugin loader make them
available).
This commit is contained in:
Austin Roberts 2021-09-16 15:32:25 -05:00
parent 25af69b8e2
commit f7307d527d
4 changed files with 67 additions and 32 deletions

View File

@ -1,22 +1,24 @@
package core package core
import ( import (
"encoding/json"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/plugins" "github.com/ethereum/go-ethereum/plugins"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/openrelayxyz/plugeth-utils/core"
) )
func PluginPreProcessBlock(pl *plugins.PluginLoader, block *types.Block) { func PluginPreProcessBlock(pl *plugins.PluginLoader, block *types.Block) {
fnList := pl.Lookup("PreProcessBlock", func(item interface{}) bool { fnList := pl.Lookup("PreProcessBlock", func(item interface{}) bool {
_, ok := item.(func([]byte)) _, ok := item.(func(core.Hash, uint64, []byte))
return ok return ok
}) })
encoded, _ := rlp.EncodeToBytes(block) encoded, _ := rlp.EncodeToBytes(block)
for _, fni := range fnList { for _, fni := range fnList {
if fn, ok := fni.(func([]byte)); ok { if fn, ok := fni.(func(core.Hash, uint64, []byte)); ok {
fn(encoded) fn(core.Hash(block.Hash()), block.NumberU64(), encoded)
} }
} }
} }
@ -29,14 +31,13 @@ func pluginPreProcessBlock(block *types.Block) {
} }
func PluginPreProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int) { func PluginPreProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int) {
fnList := pl.Lookup("PreProcessTransaction", func(item interface{}) bool { fnList := pl.Lookup("PreProcessTransaction", func(item interface{}) bool {
_, ok := item.(func([]byte, []byte, int)) _, ok := item.(func([]byte, core.Hash, core.Hash, int))
return ok return ok
}) })
txBytes, _ := tx.MarshalBinary() txBytes, _ := tx.MarshalBinary()
blockBytes, _ := rlp.EncodeToBytes(block)
for _, fni := range fnList { for _, fni := range fnList {
if fn, ok := fni.(func([]byte, []byte, int)); ok { if fn, ok := fni.(func([]byte, core.Hash, core.Hash, int)); ok {
fn(txBytes, blockBytes, i) fn(txBytes, core.Hash(tx.Hash()), core.Hash(block.Hash()), i)
} }
} }
} }
@ -49,12 +50,12 @@ func pluginPreProcessTransaction(tx *types.Transaction, block *types.Block, i in
} }
func PluginBlockProcessingError(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, err error) { func PluginBlockProcessingError(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, err error) {
fnList := pl.Lookup("BlockProcessingError", func(item interface{}) bool { fnList := pl.Lookup("BlockProcessingError", func(item interface{}) bool {
_, ok := item.(func(*types.Transaction, *types.Block, error)) _, ok := item.(func(core.Hash, core.Hash, error))
return ok return ok
}) })
for _, fni := range fnList { for _, fni := range fnList {
if fn, ok := fni.(func(*types.Transaction, *types.Block, error)); ok { if fn, ok := fni.(func(core.Hash, core.Hash, error)); ok {
fn(tx, block, err) fn(core.Hash(tx.Hash()), core.Hash(block.Hash()), err)
} }
} }
} }
@ -67,12 +68,13 @@ func pluginBlockProcessingError(tx *types.Transaction, block *types.Block, err e
} }
func PluginPostProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) { func PluginPostProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
fnList := pl.Lookup("PostProcessTransaction", func(item interface{}) bool { fnList := pl.Lookup("PostProcessTransaction", func(item interface{}) bool {
_, ok := item.(func(*types.Transaction, *types.Block, int, *types.Receipt)) _, ok := item.(func(core.Hash, core.Hash, int, []byte))
return ok return ok
}) })
receiptBytes, _ := json.Marshal(receipt)
for _, fni := range fnList { for _, fni := range fnList {
if fn, ok := fni.(func(*types.Transaction, *types.Block, int, *types.Receipt)); ok { if fn, ok := fni.(func(core.Hash, core.Hash, int, []byte)); ok {
fn(tx, block, i, receipt) fn(core.Hash(tx.Hash()), core.Hash(block.Hash()), i, receiptBytes)
} }
} }
} }
@ -85,12 +87,12 @@ func pluginPostProcessTransaction(tx *types.Transaction, block *types.Block, i i
} }
func PluginPostProcessBlock(pl *plugins.PluginLoader, block *types.Block) { func PluginPostProcessBlock(pl *plugins.PluginLoader, block *types.Block) {
fnList := pl.Lookup("PostProcessBlock", func(item interface{}) bool { fnList := pl.Lookup("PostProcessBlock", func(item interface{}) bool {
_, ok := item.(func(*types.Block)) _, ok := item.(func(core.Hash))
return ok return ok
}) })
for _, fni := range fnList { for _, fni := range fnList {
if fn, ok := fni.(func(*types.Block)); ok { if fn, ok := fni.(func(core.Hash)); ok {
fn(block) fn(core.Hash(block.Hash()))
} }
} }
} }
@ -105,12 +107,17 @@ func pluginPostProcessBlock(block *types.Block) {
func PluginNewHead(pl *plugins.PluginLoader, block *types.Block, hash common.Hash, logs []*types.Log) { func PluginNewHead(pl *plugins.PluginLoader, block *types.Block, hash common.Hash, logs []*types.Log) {
fnList := pl.Lookup("NewHead", func(item interface{}) bool { fnList := pl.Lookup("NewHead", func(item interface{}) bool {
_, ok := item.(func(*types.Block, common.Hash, []*types.Log)) _, ok := item.(func([]byte, core.Hash, [][]byte))
return ok 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 { for _, fni := range fnList {
if fn, ok := fni.(func(*types.Block, common.Hash, []*types.Log)); ok { if fn, ok := fni.(func([]byte, core.Hash, [][]byte)); ok {
fn(block, hash, logs) fn(blockBytes, core.Hash(hash), logBytes)
} }
} }
} }
@ -124,12 +131,17 @@ func pluginNewHead(block *types.Block, hash common.Hash, logs []*types.Log) {
func PluginNewSideBlock(pl *plugins.PluginLoader, block *types.Block, hash common.Hash, logs []*types.Log) { func PluginNewSideBlock(pl *plugins.PluginLoader, block *types.Block, hash common.Hash, logs []*types.Log) {
fnList := pl.Lookup("NewSideBlock", func(item interface{}) bool { fnList := pl.Lookup("NewSideBlock", func(item interface{}) bool {
_, ok := item.(func(*types.Block, common.Hash, []*types.Log)) _, ok := item.(func([]byte, core.Hash, [][]byte))
return ok 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 { for _, fni := range fnList {
if fn, ok := fni.(func(*types.Block, common.Hash, []*types.Log)); ok { if fn, ok := fni.(func([]byte, core.Hash, [][]byte)); ok {
fn(block, hash, logs) fn(blockBytes, core.Hash(hash), logBytes)
} }
} }
} }
@ -143,12 +155,20 @@ func pluginNewSideBlock(block *types.Block, hash common.Hash, logs []*types.Log)
func PluginReorg(pl *plugins.PluginLoader, commonBlock *types.Block, oldChain, newChain types.Blocks) { func PluginReorg(pl *plugins.PluginLoader, commonBlock *types.Block, oldChain, newChain types.Blocks) {
fnList := pl.Lookup("Reorg", func(item interface{}) bool { fnList := pl.Lookup("Reorg", func(item interface{}) bool {
_, ok := item.(func(common *types.Block, oldChain, newChain types.Blocks)) _, ok := item.(func(core.Hash, []core.Hash, []core.Hash))
return ok 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 { for _, fni := range fnList {
if fn, ok := fni.(func(common *types.Block, oldChain, newChain types.Blocks)); ok { if fn, ok := fni.(func(core.Hash, []core.Hash, []core.Hash)); ok {
fn(commonBlock, oldChain, newChain) fn(core.Hash(commonBlock.Hash()), oldChainHashes, newChainHashes)
} }
} }
} }

View File

@ -11,7 +11,7 @@ import (
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 common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.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
}) })
coreDestructs := make(map[core.Hash]struct{}) coreDestructs := make(map[core.Hash]struct{})

View File

@ -12,6 +12,7 @@ import (
"strings" "strings"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/event"
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
@ -156,3 +157,20 @@ func ParseFlags(args []string) bool {
} }
return DefaultPluginLoader.ParseFlags(args) return DefaultPluginLoader.ParseFlags(args)
} }
type feedWrapper struct {
feed *event.Feed
}
func (f *feedWrapper) Send(item interface{}) int {
return f.feed.Send(item)
}
func (f *feedWrapper) Subscribe(ch interface{}) core.Subscription {
return f.feed.Subscribe(ch)
}
func (pl *PluginLoader) GetFeed() core.Feed {
return &feedWrapper{&event.Feed{}}
}

View File

@ -104,7 +104,9 @@ func callbackifyChanPubSub(receiver, fn reflect.Value) *callback {
if !recvOK { if !recvOK {
return return
} }
notifier.Notify(rpcSub.ID, val.Interface()) if err := notifier.Notify(rpcSub.ID, val.Interface()); err != nil {
log.Warn("Subscription notification failed", "id", rpcSub.ID, "err", err)
}
case 1: case 1:
cancel() cancel()
return return
@ -126,15 +128,10 @@ func pluginExtendedCallbacks(callbacks map[string]*callback, receiver reflect.Va
if method.PkgPath != "" { if method.PkgPath != "" {
continue // method not exported continue // method not exported
} }
if method.Name == "Timer" {
methodType := method.Func.Type()
log.Info("Timer method", "in", methodType.NumIn(), "out", methodType.NumOut(), "contextType", isContextType(methodType.In(1)), "chanType", isChanType(methodType.Out(0)), "chandir", methodType.Out(0).ChanDir() & reflect.RecvDir == reflect.RecvDir, "errorType", isErrorType(methodType.Out(1)))
}
if isChanPubsub(method.Type) { if isChanPubsub(method.Type) {
cb := callbackifyChanPubSub(receiver, method.Func) cb := callbackifyChanPubSub(receiver, method.Func)
name := formatName(method.Name) name := formatName(method.Name)
callbacks[name] = cb callbacks[name] = cb
log.Info("Added chanPubsub", "name", name, "args", cb.argTypes)
} }
} }
} }