2021-06-26 03:38:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2021-09-16 20:32:25 +00:00
|
|
|
"encoding/json"
|
2021-09-20 16:23:37 +00:00
|
|
|
"math/big"
|
2021-06-26 03:38:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2021-07-12 16:38:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-06-26 03:38:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/plugins"
|
2021-06-26 04:02:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2021-08-31 20:51:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2021-09-16 20:32:25 +00:00
|
|
|
"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-07-12 20:46:50 +00:00
|
|
|
fnList := pl.Lookup("PreProcessBlock", func(item interface{}) bool {
|
2021-09-16 20:32:25 +00:00
|
|
|
_, ok := item.(func(core.Hash, uint64, []byte))
|
2021-06-26 03:38:04 +00:00
|
|
|
return ok
|
|
|
|
})
|
2021-09-14 22:44:25 +00:00
|
|
|
encoded, _ := rlp.EncodeToBytes(block)
|
2021-06-26 03:38:04 +00:00
|
|
|
for _, fni := range fnList {
|
2021-09-16 20:32:25 +00:00
|
|
|
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) {
|
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting PreProcessBlock, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginPreProcessBlock(plugins.DefaultPluginLoader, block) // TODO
|
|
|
|
}
|
|
|
|
func PluginPreProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int) {
|
2021-07-12 20:46:50 +00:00
|
|
|
fnList := pl.Lookup("PreProcessTransaction", func(item interface{}) bool {
|
2021-09-16 20:32:25 +00:00
|
|
|
_, ok := item.(func([]byte, core.Hash, core.Hash, int))
|
2021-06-26 03:38:04 +00:00
|
|
|
return ok
|
|
|
|
})
|
2021-08-31 20:51:41 +00:00
|
|
|
txBytes, _ := tx.MarshalBinary()
|
2021-06-26 03:38:04 +00:00
|
|
|
for _, fni := range fnList {
|
2021-09-16 20:32:25 +00:00
|
|
|
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) {
|
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting PreProcessTransaction, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginPreProcessTransaction(plugins.DefaultPluginLoader, tx, block, i)
|
|
|
|
}
|
|
|
|
func PluginBlockProcessingError(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, err error) {
|
2021-07-12 20:46:50 +00:00
|
|
|
fnList := pl.Lookup("BlockProcessingError", func(item interface{}) bool {
|
2021-09-16 20:32:25 +00:00
|
|
|
_, ok := item.(func(core.Hash, core.Hash, error))
|
2021-06-26 03:38:04 +00:00
|
|
|
return ok
|
|
|
|
})
|
|
|
|
for _, fni := range fnList {
|
2021-09-16 20:32:25 +00:00
|
|
|
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) {
|
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting BlockProcessingError, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginBlockProcessingError(plugins.DefaultPluginLoader, tx, block, err)
|
|
|
|
}
|
|
|
|
func PluginPostProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
|
2021-07-12 20:46:50 +00:00
|
|
|
fnList := pl.Lookup("PostProcessTransaction", func(item interface{}) bool {
|
2021-09-16 20:32:25 +00:00
|
|
|
_, ok := item.(func(core.Hash, core.Hash, int, []byte))
|
2021-06-26 03:38:04 +00:00
|
|
|
return ok
|
|
|
|
})
|
2021-09-16 20:32:25 +00:00
|
|
|
receiptBytes, _ := json.Marshal(receipt)
|
2021-06-26 03:38:04 +00:00
|
|
|
for _, fni := range fnList {
|
2021-09-16 20:32:25 +00:00
|
|
|
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) {
|
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting PostProcessTransaction, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginPostProcessTransaction(plugins.DefaultPluginLoader, tx, block, i, receipt)
|
|
|
|
}
|
|
|
|
func PluginPostProcessBlock(pl *plugins.PluginLoader, block *types.Block) {
|
2021-07-12 20:46:50 +00:00
|
|
|
fnList := pl.Lookup("PostProcessBlock", func(item interface{}) bool {
|
2021-09-16 20:32:25 +00:00
|
|
|
_, ok := item.(func(core.Hash))
|
2021-06-26 03:38:04 +00:00
|
|
|
return ok
|
|
|
|
})
|
|
|
|
for _, fni := range fnList {
|
2021-09-16 20:32:25 +00:00
|
|
|
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) {
|
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting PostProcessBlock, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginPostProcessBlock(plugins.DefaultPluginLoader, block)
|
|
|
|
}
|
2021-07-12 16:38:52 +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-07-12 16:38:52 +00:00
|
|
|
fnList := pl.Lookup("NewHead", func(item interface{}) bool {
|
2021-09-20 16:23:37 +00:00
|
|
|
_, ok := item.(func([]byte, core.Hash, [][]byte, *big.Int))
|
2021-07-12 16:38:52 +00:00
|
|
|
return ok
|
|
|
|
})
|
2021-09-16 20:32:25 +00:00
|
|
|
blockBytes, _ := rlp.EncodeToBytes(block)
|
|
|
|
logBytes := make([][]byte, len(logs))
|
|
|
|
for i, l := range logs {
|
|
|
|
logBytes[i], _ = rlp.EncodeToBytes(l)
|
|
|
|
}
|
2021-07-12 16:38:52 +00:00
|
|
|
for _, fni := range fnList {
|
2021-09-20 16:23:37 +00:00
|
|
|
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-07-12 16:38:52 +00:00
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting NewHead, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
2021-09-20 16:23:37 +00:00
|
|
|
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) {
|
|
|
|
fnList := pl.Lookup("NewSideBlock", func(item interface{}) bool {
|
2021-09-16 20:32:25 +00:00
|
|
|
_, ok := item.(func([]byte, core.Hash, [][]byte))
|
2021-07-12 16:38:52 +00:00
|
|
|
return ok
|
|
|
|
})
|
2021-09-16 20:32:25 +00:00
|
|
|
blockBytes, _ := rlp.EncodeToBytes(block)
|
|
|
|
logBytes := make([][]byte, len(logs))
|
|
|
|
for i, l := range logs {
|
|
|
|
logBytes[i], _ = rlp.EncodeToBytes(l)
|
|
|
|
}
|
2021-07-12 16:38:52 +00:00
|
|
|
for _, fni := range fnList {
|
2021-09-16 20:32:25 +00:00
|
|
|
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) {
|
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting NewSideBlock, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginNewSideBlock(plugins.DefaultPluginLoader, block, hash, logs)
|
|
|
|
}
|
2021-07-12 20:46:50 +00:00
|
|
|
|
|
|
|
func PluginReorg(pl *plugins.PluginLoader, commonBlock *types.Block, oldChain, newChain types.Blocks) {
|
|
|
|
fnList := pl.Lookup("Reorg", func(item interface{}) bool {
|
2021-09-16 20:32:25 +00:00
|
|
|
_, ok := item.(func(core.Hash, []core.Hash, []core.Hash))
|
2021-07-12 20:46:50 +00:00
|
|
|
return ok
|
|
|
|
})
|
2021-09-16 20:32:25 +00:00
|
|
|
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())
|
|
|
|
}
|
2021-07-12 20:46:50 +00:00
|
|
|
for _, fni := range fnList {
|
2021-09-16 20:32:25 +00:00
|
|
|
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) {
|
|
|
|
if plugins.DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting Reorg, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
PluginReorg(plugins.DefaultPluginLoader, commonBlock, oldChain, newChain)
|
|
|
|
}
|