From 6d86ee1b5cf47072b6b58099bff89a9c27f52c72 Mon Sep 17 00:00:00 2001 From: tbjump Date: Thu, 8 Jun 2023 20:37:29 +0000 Subject: [PATCH] node/node: Add DB as a GuardianOption --- node/cmd/guardiand/node.go | 2 +- node/pkg/node/node.go | 27 ++++++++++++++++++++------- node/pkg/node/node_test.go | 3 ++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/node/cmd/guardiand/node.go b/node/cmd/guardiand/node.go index 757968419..f1b88e26c 100644 --- a/node/cmd/guardiand/node.go +++ b/node/cmd/guardiand/node.go @@ -1360,12 +1360,12 @@ func runNode(cmd *cobra.Command, args []string) { guardianNode := node.NewGuardianNode( env, - db, gk, wormchainConn, ) guardianOptions := []*node.GuardianOption{ + node.GuardianOptionDatabase(db), node.GuardianOptionWatchers(watcherConfigs, ibcWatcherConfig), node.GuardianOptionAccountant(*accountantContract, *accountantWS, *accountantCheckEnabled), node.GuardianOptionGovernor(*chainGovernorEnabled), diff --git a/node/pkg/node/node.go b/node/pkg/node/node.go index 5426e7dac..1ae4283d0 100644 --- a/node/pkg/node/node.go +++ b/node/pkg/node/node.go @@ -89,7 +89,8 @@ func GuardianOptionP2P(p2pKey libp2p_crypto.PrivKey, networkId string, bootstrap // Requires: wormchainConn func GuardianOptionAccountant(contract string, websocket string, enforcing bool) *GuardianOption { return &GuardianOption{ - name: "accountant", + name: "accountant", + dependencies: []string{"db"}, f: func(ctx context.Context, logger *zap.Logger, g *G) error { // Set up the accountant. If the accountant smart contract is configured, we will instantiate the accountant and VAAs // will be passed to it for processing. It will forward all token bridge transfers to the accountant contract. @@ -133,7 +134,8 @@ func GuardianOptionAccountant(contract string, websocket string, enforcing bool) func GuardianOptionGovernor(governorEnabled bool) *GuardianOption { return &GuardianOption{ - name: "governor", + name: "governor", + dependencies: []string{"db"}, f: func(ctx context.Context, logger *zap.Logger, g *G) error { if governorEnabled { logger.Info("chain governor is enabled") @@ -318,7 +320,7 @@ func GuardianOptionWatchers(watcherConfigs []watchers.WatcherConfig, ibcWatcherC func GuardianOptionAdminService(socketPath string, ethRpc *string, ethContract *string, rpcMap map[string]string) *GuardianOption { return &GuardianOption{ name: "admin-service", - dependencies: []string{"governor"}, + dependencies: []string{"governor", "db"}, f: func(ctx context.Context, logger *zap.Logger, g *G) error { adminService, err := adminServiceRunnable( logger, @@ -346,7 +348,7 @@ func GuardianOptionAdminService(socketPath string, ethRpc *string, ethContract * func GuardianOptionPublicRpcSocket(publicGRPCSocketPath string, publicRpcLogDetail common.GrpcLogDetail) *GuardianOption { return &GuardianOption{ name: "publicrpcsocket", - dependencies: []string{"governor"}, + dependencies: []string{"governor", "db"}, f: func(ctx context.Context, logger *zap.Logger, g *G) error { // local public grpc service socket publicrpcUnixService, publicrpcServer, err := publicrpcUnixServiceRunnable(logger, publicGRPCSocketPath, publicRpcLogDetail, g.db, g.gst, g.gov) @@ -363,7 +365,7 @@ func GuardianOptionPublicRpcSocket(publicGRPCSocketPath string, publicRpcLogDeta func GuardianOptionPublicrpcTcpService(publicRpc string, publicRpcLogDetail common.GrpcLogDetail) *GuardianOption { return &GuardianOption{ name: "publicrpc", - dependencies: []string{"governor", "publicrpcsocket"}, + dependencies: []string{"governor", "publicrpcsocket", "db"}, f: func(ctx context.Context, logger *zap.Logger, g *G) error { publicrpcService, err := publicrpcTcpServiceRunnable(logger, publicRpc, publicRpcLogDetail, g.db, g.gst, g.gov) if err != nil { @@ -395,6 +397,15 @@ func GuardianOptionBigTablePersistence(config *reporter.BigTableConnectionConfig }} } +func GuardianOptionDatabase(db *db.Database) *GuardianOption { + return &GuardianOption{ + name: "db", + f: func(ctx context.Context, logger *zap.Logger, g *G) error { + g.db = db + return nil + }} +} + type G struct { // rootCtxCancel is a context.CancelFunc. It MUST be a root context for any context that is passed to any member function of G. // It can be used by components to shut down the entire node if they encounter an unrecoverable state. @@ -440,13 +451,11 @@ type G struct { func NewGuardianNode( env common.Environment, - db *db.Database, gk *ecdsa.PrivateKey, wormchainConn *wormconn.ClientConn, // TODO does this need to be here? ) *G { g := G{ env: env, - db: db, gk: gk, wormchainConn: wormchainConn, } @@ -542,6 +551,10 @@ func (g *G) Run(rootCtxCancel context.CancelFunc, options ...*GuardianOption) su } } + if g.db == nil { + logger.Fatal("no database configured, cannot start guardian.") + } + logger.Info("Starting processor") if err := supervisor.Run(ctx, "processor", processor.NewProcessor(ctx, g.db, diff --git a/node/pkg/node/node_test.go b/node/pkg/node/node_test.go index 82370fd56..8ac7f7966 100644 --- a/node/pkg/node/node_test.go +++ b/node/pkg/node/node_test.go @@ -153,6 +153,7 @@ func mockGuardianRunnable(gs []*mockGuardian, mockGuardianIndex uint, obsDb mock // assemble all the options guardianOptions := []*GuardianOption{ + GuardianOptionDatabase(db), GuardianOptionWatchers(watcherConfigs, nil), GuardianOptionAccountant("", "", false), // effectively disable accountant GuardianOptionGovernor(false), // disable governor @@ -165,7 +166,6 @@ func mockGuardianRunnable(gs []*mockGuardian, mockGuardianIndex uint, obsDb mock guardianNode := NewGuardianNode( env, - db, gs[mockGuardianIndex].gk, nil, ) @@ -351,6 +351,7 @@ func testStatusServer(ctx context.Context, logger *zap.Logger, statusAddr string func TestMain(m *testing.M) { readiness.NoPanic = true // otherwise we'd panic when running multiple guardians + os.Exit(m.Run()) } // TestInvalidWatcherConfig tries to instantiate a guardian with various invlid []watchers.WatcherConfig and asserts that it errors