plugeth-statediff/main/main.go
Roy Crihfield 9f967abfb9 initial plugeth work
refactor packages, flags, subscriptions

also DRY up builder tests

use mockgen
2023-06-23 21:21:14 +08:00

76 lines
1.9 KiB
Go

package main
import (
"strconv"
geth_flags "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/openrelayxyz/plugeth-utils/core"
"github.com/openrelayxyz/plugeth-utils/restricted"
"github.com/cerc-io/plugeth-statediff"
"github.com/cerc-io/plugeth-statediff/utils/log"
)
var (
pluginLoader core.PluginLoader
gethContext core.Context
service *statediff.Service
)
func Initialize(ctx core.Context, pl core.PluginLoader, logger core.Logger) {
log.DefaultLogger = logger
pluginLoader = pl
gethContext = ctx
log.Info("Initialized statediff plugin")
}
func InitializeNode(stack core.Node, b core.Backend) {
backend := b.(restricted.Backend)
networkid, err := strconv.ParseUint(gethContext.String(geth_flags.NetworkIdFlag.Name), 10, 64)
if err != nil {
log.Error("cannot parse network ID", "error", err)
return
}
serviceConfig := GetConfig()
service, err = statediff.NewIndexingService(serviceConfig, backend, networkid)
if err != nil {
log.Error("failed to construct service", "error", err)
return
}
if err = service.Start(); err != nil {
log.Error("failed to start service", "error", err)
return
}
log.Info("Initialized node and backend")
}
func GetAPIs(stack core.Node, backend core.Backend) []core.API {
return []core.API{
{
Namespace: statediff.APIName,
Version: statediff.APIVersion,
Service: statediff.NewPublicAPI(service),
Public: true,
},
}
}
// StateUpdate gives us updates about state changes made in each block.
// We extract contract code here, since it's not exposed by plugeth's state interfaces.
func StateUpdate(blockRoot core.Hash,
parentRoot core.Hash,
destructs map[core.Hash]struct{},
accounts map[core.Hash][]byte,
storage map[core.Hash]map[core.Hash][]byte,
codeUpdates map[core.Hash][]byte) {
if service == nil {
log.Error("StateUpdate called before InitializeNode", "root", blockRoot)
return
}
// TODO extract code
}