Refactor plugin system

When the plugin loader itself had to know the types in the arguments
and return values of the plugin functions, it was very difficult to
avoid import loops, given that the types were often defined in the
same package that needed to invoke the plugins.

Under this model, the plugin loader has much less knowledge of the
plugins themselves, and within each package we define functions to
interact with the plugins.
This commit is contained in:
Austin Roberts
2021-06-25 22:46:17 -05:00
parent 091a2f4884
commit 03808de29a
10 changed files with 283 additions and 256 deletions
+62
View File
@@ -0,0 +1,62 @@
package core
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/plugins"
)
func pluginPreProcessBlock(block *types.Block) {
fnList := plugins.Lookup("ProcessBlock", func(item interface{}) bool {
_, ok := item.(func(*types.Block))
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(*types.Block)); ok {
fn(block)
}
}
}
func pluginPreProcessTransaction(tx *types.Transaction, block *types.Block, i int) {
fnList := plugins.Lookup("ProcessTransaction", func(item interface{}) bool {
_, ok := item.(func(*types.Transaction, *types.Block, int))
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(*types.Transaction, *types.Block, int)); ok {
fn(tx, block, i)
}
}
}
func pluginBlockProcessingError(tx *types.Transaction, block *types.Block, err error) {
fnList := plugins.Lookup("ProcessingError", func(item interface{}) bool {
_, ok := item.(func(*types.Transaction, *types.Block, error))
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(*types.Transaction, *types.Block, error)); ok {
fn(tx, block, err)
}
}
}
func pluginPostProcessTransaction(tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
fnList := plugins.Lookup("ProcessTransaction", func(item interface{}) bool {
_, ok := item.(func(*types.Transaction, *types.Block, int, *types.Receipt))
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(*types.Transaction, *types.Block, int, *types.Receipt)); ok {
fn(tx, block, i, receipt)
}
}
}
func pluginPostProcessBlock(block *types.Block) {
fnList := plugins.Lookup("ProcessBlock", func(item interface{}) bool {
_, ok := item.(func(*types.Block))
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(*types.Block)); ok {
fn(block)
}
}
}
+6 -7
View File
@@ -27,7 +27,6 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/plugins"
)
// StateProcessor is a basic Processor, which takes care of transitioning
@@ -71,27 +70,27 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
blockContext := NewEVMBlockContext(header, p.bc, nil)
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
// Iterate over and process the individual transactions
plugins.PreProcessBlock(block)
pluginPreProcessBlock(block)
for i, tx := range block.Transactions() {
msg, err := tx.AsMessage(types.MakeSigner(p.config, header.Number), header.BaseFee)
if err != nil {
plugins.BlockProcessingError(tx, block, err)
pluginBlockProcessingError(tx, block, err)
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
statedb.Prepare(tx.Hash(), block.Hash(), i)
plugins.PreProcessTransaction(tx, block, i)
pluginPreProcessTransaction(tx, block, i)
receipt, err := applyTransaction(msg, p.config, p.bc, nil, gp, statedb, header, tx, usedGas, vmenv)
if err != nil {
plugins.BlockProcessingError(tx, block, err)
pluginBlockProcessingError(tx, block, err)
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
}
plugins.PostProcessTransaction(tx, block, i, receipt)
pluginPostProcessTransaction(tx, block, i, receipt)
receipts = append(receipts, receipt)
allLogs = append(allLogs, receipt.Logs...)
}
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles())
plugins.PostProcessBlock(block)
pluginPostProcessBlock(block)
return receipts, allLogs, *usedGas, nil
}