forked from cerc-io/plugeth
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:
+2
-3
@@ -40,7 +40,6 @@ import (
|
||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/plugins"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
)
|
||||
@@ -801,7 +800,7 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *txTrac
|
||||
}
|
||||
}
|
||||
// Get the tracer from the plugin loader
|
||||
if tr, ok := plugins.GetTracer(*config.Tracer); ok {
|
||||
if tr, ok := getPluginTracer(*config.Tracer); ok {
|
||||
tracer = tr(statedb)
|
||||
} else {
|
||||
// Constuct the JavaScript tracer to execute with
|
||||
@@ -851,7 +850,7 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *txTrac
|
||||
StructLogs: ethapi.FormatLogs(tracer.StructLogs()),
|
||||
}, nil
|
||||
|
||||
case plugins.TracerResult:
|
||||
case TracerResult:
|
||||
return tracer.GetResult()
|
||||
|
||||
case *Tracer:
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package tracers
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/plugins"
|
||||
"github.com/ethereum/go-ethereum/core/vm"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
)
|
||||
|
||||
|
||||
type TracerResult interface {
|
||||
vm.Tracer
|
||||
GetResult() (interface{}, error)
|
||||
}
|
||||
|
||||
func getPluginTracer(name string) (func(*state.StateDB)TracerResult, bool) {
|
||||
tracers := plugins.Lookup("Tracers", func(item interface{}) bool {
|
||||
_, ok := item.(map[string]func(*state.StateDB)TracerResult)
|
||||
return ok
|
||||
})
|
||||
for _, tmap := range tracers {
|
||||
if tracerMap, ok := tmap.(map[string]func(*state.StateDB)TracerResult); ok {
|
||||
if tracer, ok := tracerMap[name]; ok {
|
||||
return tracer, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
Reference in New Issue
Block a user