From 34c8216fcea9aa83b10df37304bfe6d3d011436d Mon Sep 17 00:00:00 2001 From: Hoon <48665813+sh-cha@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:51:25 +0900 Subject: [PATCH] Feat/fetch output submitter info (#34) * make nodes use l1 brige config instead of l2 bridge info * use bech32 prefix from config & fetch keyrecord info from keyring config * add bech32prefix to challenge node configs * small safe guard * delete keyringconfig from broadcaster config --- challenger/challenger.go | 17 ++++--- challenger/child/child.go | 8 ++-- challenger/host/host.go | 9 ++-- challenger/types/config.go | 10 ++-- executor/batch/batch.go | 12 ++--- executor/celestia/celestia.go | 9 ++-- executor/child/child.go | 8 ++-- executor/executor.go | 79 ++++++++++++++++++++------------ executor/host/host.go | 17 +++---- executor/types/config.go | 53 ++++++++++----------- node/broadcaster/broadcaster.go | 17 ++++--- node/broadcaster/types/config.go | 22 ++++----- node/node.go | 5 +- node/types/config.go | 7 +++ provider/child/child.go | 14 +++--- provider/host/host.go | 15 +++--- 16 files changed, 163 insertions(+), 139 deletions(-) diff --git a/challenger/challenger.go b/challenger/challenger.go index de83923..5e4fd30 100644 --- a/challenger/challenger.go +++ b/challenger/challenger.go @@ -57,12 +57,12 @@ func NewChallenger(cfg *challengertypes.Config, db types.DB, sv *server.Server, host: host.NewHostV1( cfg.L1NodeConfig(homePath), db.WithPrefix([]byte(types.HostName)), - logger.Named(types.HostName), cfg.L1Node.Bech32Prefix, + logger.Named(types.HostName), ), child: child.NewChildV1( cfg.L2NodeConfig(homePath), db.WithPrefix([]byte(types.ChildName)), - logger.Named(types.ChildName), cfg.L2Node.Bech32Prefix, + logger.Named(types.ChildName), ), cfg: cfg, @@ -83,14 +83,19 @@ func NewChallenger(cfg *challengertypes.Config, db types.DB, sv *server.Server, } func (c *Challenger) Initialize(ctx context.Context) error { - bridgeInfo, err := c.child.QueryBridgeInfo(ctx) + childBridgeInfo, err := c.child.QueryBridgeInfo(ctx) if err != nil { return err } - if bridgeInfo.BridgeId == 0 { + if childBridgeInfo.BridgeId == 0 { return errors.New("bridge info is not set") } + bridgeInfo, err := c.host.QueryBridgeConfig(ctx, childBridgeInfo.BridgeId) + if err != nil { + return err + } + c.logger.Info( "bridge info", zap.Uint64("id", bridgeInfo.BridgeId), @@ -103,7 +108,7 @@ func (c *Challenger) Initialize(ctx context.Context) error { } var initialBlockTime time.Time - hostInitialBlockTime, err := c.host.Initialize(ctx, hostProcessedHeight, c.child, bridgeInfo, c) + hostInitialBlockTime, err := c.host.Initialize(ctx, hostProcessedHeight, c.child, *bridgeInfo, c) if err != nil { return err } @@ -111,7 +116,7 @@ func (c *Challenger) Initialize(ctx context.Context) error { initialBlockTime = hostInitialBlockTime } - childInitialBlockTime, err := c.child.Initialize(ctx, childProcessedHeight, processedOutputIndex+1, c.host, bridgeInfo, c) + childInitialBlockTime, err := c.child.Initialize(ctx, childProcessedHeight, processedOutputIndex+1, c.host, *bridgeInfo, c) if err != nil { return err } diff --git a/challenger/child/child.go b/challenger/child/child.go index 1d8cd6d..473e8b5 100644 --- a/challenger/child/child.go +++ b/challenger/child/child.go @@ -47,17 +47,17 @@ type Child struct { func NewChildV1( cfg nodetypes.NodeConfig, - db types.DB, logger *zap.Logger, bech32Prefix string, + db types.DB, logger *zap.Logger, ) *Child { return &Child{ - BaseChild: childprovider.NewBaseChildV1(cfg, db, logger, bech32Prefix), + BaseChild: childprovider.NewBaseChildV1(cfg, db, logger), eventHandler: eventhandler.NewChallengeEventHandler(db, logger), eventQueue: make([]challengertypes.ChallengeEvent, 0), } } -func (ch *Child) Initialize(ctx context.Context, processedHeight int64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) (time.Time, error) { - _, err := ch.BaseChild.Initialize(ctx, processedHeight, startOutputIndex, bridgeInfo) +func (ch *Child) Initialize(ctx context.Context, processedHeight int64, startOutputIndex uint64, host hostNode, bridgeInfo ophosttypes.QueryBridgeResponse, challenger challenger) (time.Time, error) { + _, err := ch.BaseChild.Initialize(ctx, processedHeight, startOutputIndex, bridgeInfo, nil) if err != nil { return time.Time{}, err } diff --git a/challenger/host/host.go b/challenger/host/host.go index d34f3d1..f948631 100644 --- a/challenger/host/host.go +++ b/challenger/host/host.go @@ -6,7 +6,6 @@ import ( "go.uber.org/zap" - opchildtypes "github.com/initia-labs/OPinit/x/opchild/types" ophosttypes "github.com/initia-labs/OPinit/x/ophost/types" nodetypes "github.com/initia-labs/opinit-bots/node/types" @@ -45,18 +44,18 @@ type Host struct { func NewHostV1( cfg nodetypes.NodeConfig, - db types.DB, logger *zap.Logger, bech32Prefix string, + db types.DB, logger *zap.Logger, ) *Host { return &Host{ - BaseHost: hostprovider.NewBaseHostV1(cfg, db, logger, bech32Prefix), + BaseHost: hostprovider.NewBaseHostV1(cfg, db, logger), eventHandler: eventhandler.NewChallengeEventHandler(db, logger), eventQueue: make([]challengertypes.ChallengeEvent, 0), outputPendingEventQueue: make([]challengertypes.ChallengeEvent, 0), } } -func (h *Host) Initialize(ctx context.Context, processedHeight int64, child childNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) (time.Time, error) { - err := h.BaseHost.Initialize(ctx, processedHeight, bridgeInfo) +func (h *Host) Initialize(ctx context.Context, processedHeight int64, child childNode, bridgeInfo ophosttypes.QueryBridgeResponse, challenger challenger) (time.Time, error) { + err := h.BaseHost.Initialize(ctx, processedHeight, bridgeInfo, nil) if err != nil { return time.Time{}, err } diff --git a/challenger/types/config.go b/challenger/types/config.go index 414f1b2..4865fc9 100644 --- a/challenger/types/config.go +++ b/challenger/types/config.go @@ -106,16 +106,18 @@ func (cfg Config) Validate() error { func (cfg Config) L1NodeConfig(homePath string) nodetypes.NodeConfig { nc := nodetypes.NodeConfig{ - RPC: cfg.L1Node.RPCAddress, - ProcessType: nodetypes.PROCESS_TYPE_DEFAULT, + RPC: cfg.L1Node.RPCAddress, + ProcessType: nodetypes.PROCESS_TYPE_DEFAULT, + Bech32Prefix: cfg.L1Node.Bech32Prefix, } return nc } func (cfg Config) L2NodeConfig(homePath string) nodetypes.NodeConfig { nc := nodetypes.NodeConfig{ - RPC: cfg.L2Node.RPCAddress, - ProcessType: nodetypes.PROCESS_TYPE_DEFAULT, + RPC: cfg.L2Node.RPCAddress, + ProcessType: nodetypes.PROCESS_TYPE_DEFAULT, + Bech32Prefix: cfg.L2Node.Bech32Prefix, } return nc } diff --git a/executor/batch/batch.go b/executor/batch/batch.go index d5f4f5e..dd05045 100644 --- a/executor/batch/batch.go +++ b/executor/batch/batch.go @@ -31,7 +31,7 @@ type BatchSubmitter struct { host hostNode da executortypes.DANode - bridgeInfo opchildtypes.BridgeInfo + bridgeInfo ophosttypes.QueryBridgeResponse cfg nodetypes.NodeConfig batchCfg executortypes.BatchConfig @@ -59,9 +59,9 @@ func NewBatchSubmitterV1( cfg nodetypes.NodeConfig, batchCfg executortypes.BatchConfig, db types.DB, logger *zap.Logger, - chainID, homePath, bech32Prefix string, + chainID, homePath string, ) *BatchSubmitter { - appCodec, txConfig, err := childprovider.GetCodec(bech32Prefix) + appCodec, txConfig, err := childprovider.GetCodec(cfg.Bech32Prefix) if err != nil { panic(err) } @@ -96,8 +96,8 @@ func NewBatchSubmitterV1( return ch } -func (bs *BatchSubmitter) Initialize(ctx context.Context, processedHeight int64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error { - err := bs.node.Initialize(ctx, processedHeight) +func (bs *BatchSubmitter) Initialize(ctx context.Context, processedHeight int64, host hostNode, bridgeInfo ophosttypes.QueryBridgeResponse) error { + err := bs.node.Initialize(ctx, processedHeight, nil) if err != nil { return err } @@ -172,7 +172,7 @@ func (bs *BatchSubmitter) Close() { } } -func (bs *BatchSubmitter) SetBridgeInfo(bridgeInfo opchildtypes.BridgeInfo) { +func (bs *BatchSubmitter) SetBridgeInfo(bridgeInfo ophosttypes.QueryBridgeResponse) { bs.bridgeInfo = bridgeInfo } diff --git a/executor/celestia/celestia.go b/executor/celestia/celestia.go index 004a123..0343c6d 100644 --- a/executor/celestia/celestia.go +++ b/executor/celestia/celestia.go @@ -52,7 +52,7 @@ type Celestia struct { func NewDACelestia( version uint8, cfg nodetypes.NodeConfig, - db types.DB, logger *zap.Logger, bech32Prefix, batchSubmitter string, + db types.DB, logger *zap.Logger, ) *Celestia { c := &Celestia{ version: version, @@ -65,13 +65,12 @@ func NewDACelestia( msgQueue: make([]sdk.Msg, 0), } - appCodec, txConfig, err := createCodec(bech32Prefix) + appCodec, txConfig, err := createCodec(cfg.Bech32Prefix) if err != nil { panic(err) } if cfg.BroadcasterConfig != nil { - cfg.BroadcasterConfig.KeyringConfig.Address = batchSubmitter cfg.BroadcasterConfig.BuildTxWithMessages = c.BuildTxWithMessages cfg.BroadcasterConfig.PendingTxToProcessedMsgs = c.PendingTxToProcessedMsgs } @@ -95,8 +94,8 @@ func createCodec(bech32Prefix string) (codec.Codec, client.TxConfig, error) { }) } -func (c *Celestia) Initialize(ctx context.Context, batch batchNode, bridgeId uint64) error { - err := c.node.Initialize(ctx, 0) +func (c *Celestia) Initialize(ctx context.Context, batch batchNode, bridgeId uint64, keyringConfig *btypes.KeyringConfig) error { + err := c.node.Initialize(ctx, 0, keyringConfig) if err != nil { return err } diff --git a/executor/child/child.go b/executor/child/child.go index 5504b20..fc146b8 100644 --- a/executor/child/child.go +++ b/executor/child/child.go @@ -46,15 +46,15 @@ type Child struct { func NewChildV1( cfg nodetypes.NodeConfig, - db types.DB, logger *zap.Logger, bech32Prefix string, + db types.DB, logger *zap.Logger, ) *Child { return &Child{ - BaseChild: childprovider.NewBaseChildV1(cfg, db, logger, bech32Prefix), + BaseChild: childprovider.NewBaseChildV1(cfg, db, logger), } } -func (ch *Child) Initialize(ctx context.Context, processedHeight int64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error { - l2Sequence, err := ch.BaseChild.Initialize(ctx, processedHeight, startOutputIndex, bridgeInfo) +func (ch *Child) Initialize(ctx context.Context, processedHeight int64, startOutputIndex uint64, host hostNode, bridgeInfo ophosttypes.QueryBridgeResponse, keyringConfig *btypes.KeyringConfig) error { + l2Sequence, err := ch.BaseChild.Initialize(ctx, processedHeight, startOutputIndex, bridgeInfo, keyringConfig) if err != nil { return err } diff --git a/executor/executor.go b/executor/executor.go index 6d7d192..a5f4fe5 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -16,8 +16,8 @@ import ( bottypes "github.com/initia-labs/opinit-bots/bot/types" executortypes "github.com/initia-labs/opinit-bots/executor/types" + btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types" - opchildtypes "github.com/initia-labs/OPinit/x/opchild/types" ophosttypes "github.com/initia-labs/OPinit/x/ophost/types" "github.com/initia-labs/opinit-bots/types" "go.uber.org/zap" @@ -51,18 +51,17 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg host: host.NewHostV1( cfg.L1NodeConfig(homePath), db.WithPrefix([]byte(types.HostName)), - logger.Named(types.HostName), cfg.L1Node.Bech32Prefix, "", + logger.Named(types.HostName), ), child: child.NewChildV1( cfg.L2NodeConfig(homePath), db.WithPrefix([]byte(types.ChildName)), - logger.Named(types.ChildName), cfg.L2Node.Bech32Prefix, + logger.Named(types.ChildName), ), batch: batch.NewBatchSubmitterV1( cfg.L2NodeConfig(homePath), cfg.BatchConfig(), db.WithPrefix([]byte(types.BatchName)), logger.Named(types.BatchName), cfg.L2Node.ChainID, homePath, - cfg.L2Node.Bech32Prefix, ), cfg: cfg, @@ -75,14 +74,19 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg } func (ex *Executor) Initialize(ctx context.Context) error { - bridgeInfo, err := ex.child.QueryBridgeInfo(ctx) + childBridgeInfo, err := ex.child.QueryBridgeInfo(ctx) if err != nil { return err } - if bridgeInfo.BridgeId == 0 { + if childBridgeInfo.BridgeId == 0 { return errors.New("bridge info is not set") } + bridgeInfo, err := ex.host.QueryBridgeConfig(ctx, childBridgeInfo.BridgeId) + if err != nil { + return err + } + ex.logger.Info( "bridge info", zap.Uint64("id", bridgeInfo.BridgeId), @@ -94,20 +98,22 @@ func (ex *Executor) Initialize(ctx context.Context) error { return err } - err = ex.host.Initialize(ctx, hostProcessedHeight, ex.child, ex.batch, bridgeInfo) + hostKeyringConfig, childKeyringConfig, daKeyringConfig := ex.getKeyringConfigs(*bridgeInfo) + + err = ex.host.Initialize(ctx, hostProcessedHeight, ex.child, ex.batch, *bridgeInfo, hostKeyringConfig) if err != nil { return err } - err = ex.child.Initialize(ctx, childProcessedHeight, processedOutputIndex+1, ex.host, bridgeInfo) + err = ex.child.Initialize(ctx, childProcessedHeight, processedOutputIndex+1, ex.host, *bridgeInfo, childKeyringConfig) if err != nil { return err } - err = ex.batch.Initialize(ctx, batchProcessedHeight, ex.host, bridgeInfo) + err = ex.batch.Initialize(ctx, batchProcessedHeight, ex.host, *bridgeInfo) if err != nil { return err } - da, err := ex.makeDANode(ctx, bridgeInfo) + da, err := ex.makeDANode(ctx, *bridgeInfo, daKeyringConfig) if err != nil { return err } @@ -165,44 +171,38 @@ func (ex *Executor) RegisterQuerier() { }) } -func (ex *Executor) makeDANode(ctx context.Context, bridgeInfo opchildtypes.BridgeInfo) (executortypes.DANode, error) { +func (ex *Executor) makeDANode(ctx context.Context, bridgeInfo ophosttypes.QueryBridgeResponse, daKeyringConfig *btypes.KeyringConfig) (executortypes.DANode, error) { if !ex.cfg.EnableBatchSubmitter { return batch.NewNoopDA(), nil } batchInfo := ex.batch.BatchInfo() + if batchInfo == nil { + return nil, errors.New("batch info is not set") + } switch batchInfo.BatchInfo.ChainType { case ophosttypes.BatchInfo_CHAIN_TYPE_INITIA: - hostda := host.NewHostV1( - ex.cfg.DANodeConfig(ex.homePath), - ex.db.WithPrefix([]byte(types.DAHostName)), - ex.logger.Named(types.DAHostName), - ex.cfg.DANode.Bech32Prefix, batchInfo.BatchInfo.Submitter, - ) - - // should exist - daAddr, err := hostda.GetAddress() - if err != nil { - return nil, err - } - // might not exist - hostAddr, err := ex.host.GetAddress() + hostAddrStr, err := ex.host.GetAddressStr() if err != nil && !errors.Is(err, types.ErrKeyNotSet) { return nil, err - } else if err == nil && hostAddr.Equals(daAddr) { + } else if err == nil && hostAddrStr == batchInfo.BatchInfo.Submitter { return ex.host, nil } - err = hostda.InitializeDA(ctx, bridgeInfo) + hostda := host.NewHostV1( + ex.cfg.DANodeConfig(ex.homePath), + ex.db.WithPrefix([]byte(types.DAHostName)), + ex.logger.Named(types.DAHostName), + ) + err = hostda.InitializeDA(ctx, bridgeInfo, daKeyringConfig) return hostda, err case ophosttypes.BatchInfo_CHAIN_TYPE_CELESTIA: celestiada := celestia.NewDACelestia(ex.cfg.Version, ex.cfg.DANodeConfig(ex.homePath), ex.db.WithPrefix([]byte(types.DACelestiaName)), ex.logger.Named(types.DACelestiaName), - ex.cfg.DANode.Bech32Prefix, batchInfo.BatchInfo.Submitter, ) - err := celestiada.Initialize(ctx, ex.batch, bridgeInfo.BridgeId) + err := celestiada.Initialize(ctx, ex.batch, bridgeInfo.BridgeId, daKeyringConfig) if err != nil { return nil, err } @@ -269,3 +269,24 @@ func (ex *Executor) getProcessedHeights(ctx context.Context, bridgeId uint64) (l } return l1ProcessedHeight, l2ProcessedHeight, processedOutputIndex, batchProcessedHeight, err } + +func (ex *Executor) getKeyringConfigs(bridgeInfo ophosttypes.QueryBridgeResponse) (hostKeyringConfig *btypes.KeyringConfig, childKeyringConfig *btypes.KeyringConfig, daKeyringConfig *btypes.KeyringConfig) { + if ex.cfg.EnableOutputSubmitter { + hostKeyringConfig = &btypes.KeyringConfig{ + Address: bridgeInfo.BridgeConfig.Proposer, + } + } + + if ex.cfg.BridgeExecutor != "" { + childKeyringConfig = &btypes.KeyringConfig{ + Name: ex.cfg.BridgeExecutor, + } + } + + if ex.cfg.EnableBatchSubmitter { + daKeyringConfig = &btypes.KeyringConfig{ + Address: bridgeInfo.BridgeConfig.BatchInfo.Submitter, + } + } + return +} diff --git a/executor/host/host.go b/executor/host/host.go index ee5ac20..b7b78a9 100644 --- a/executor/host/host.go +++ b/executor/host/host.go @@ -7,7 +7,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" - opchildtypes "github.com/initia-labs/OPinit/x/opchild/types" ophosttypes "github.com/initia-labs/OPinit/x/ophost/types" executortypes "github.com/initia-labs/opinit-bots/executor/types" @@ -50,19 +49,15 @@ type Host struct { func NewHostV1( cfg nodetypes.NodeConfig, - db types.DB, logger *zap.Logger, bech32Prefix, batchSubmitter string, + db types.DB, logger *zap.Logger, ) *Host { - if cfg.BroadcasterConfig != nil && batchSubmitter != "" { - cfg.BroadcasterConfig.Bech32Prefix = bech32Prefix - cfg.BroadcasterConfig.KeyringConfig.Address = batchSubmitter - } return &Host{ - BaseHost: hostprovider.NewBaseHostV1(cfg, db, logger, bech32Prefix), + BaseHost: hostprovider.NewBaseHostV1(cfg, db, logger), } } -func (h *Host) Initialize(ctx context.Context, processedHeight int64, child childNode, batch batchNode, bridgeInfo opchildtypes.BridgeInfo) error { - err := h.BaseHost.Initialize(ctx, processedHeight, bridgeInfo) +func (h *Host) Initialize(ctx context.Context, processedHeight int64, child childNode, batch batchNode, bridgeInfo ophosttypes.QueryBridgeResponse, keyringConfig *btypes.KeyringConfig) error { + err := h.BaseHost.Initialize(ctx, processedHeight, bridgeInfo, keyringConfig) if err != nil { return err } @@ -76,8 +71,8 @@ func (h *Host) Initialize(ctx context.Context, processedHeight int64, child chil return nil } -func (h *Host) InitializeDA(ctx context.Context, bridgeInfo opchildtypes.BridgeInfo) error { - err := h.BaseHost.Initialize(ctx, 0, bridgeInfo) +func (h *Host) InitializeDA(ctx context.Context, bridgeInfo ophosttypes.QueryBridgeResponse, keyringConfig *btypes.KeyringConfig) error { + err := h.BaseHost.Initialize(ctx, 0, bridgeInfo, keyringConfig) if err != nil { return err } diff --git a/executor/types/config.go b/executor/types/config.go index b362067..b529f91 100644 --- a/executor/types/config.go +++ b/executor/types/config.go @@ -44,18 +44,16 @@ type Config struct { // DANode is the configuration for the data availability node. DANode NodeConfig `json:"da_node"` - // OutputSubmitter is the key name in the keyring for the output submitter, - // which is used to relay the output transaction from l2 to l1. - // - // If you don't want to use the output submitter feature, you can leave it empty. - OutputSubmitter string `json:"output_submitter"` - // BridgeExecutor is the key name in the keyring for the bridge executor, // which is used to relay initiate token bridge transaction from l1 to l2. // // If you don't want to use the bridge executor feature, you can leave it empty. BridgeExecutor string `json:"bridge_executor"` + // EnableOutputSubmitter is the flag to enable the output submitter. + // If it is false, the output submitter will not be started. + EnableOutputSubmitter bool `json:"enable_output_submitter"` + // EnableBatchSubmitter is the flag to enable the batch submitter. // If it is false, the batch submitter will not be started. EnableBatchSubmitter bool `json:"enable_batch_submitter"` @@ -116,9 +114,9 @@ func DefaultConfig() *Config { TxTimeout: 60, }, - OutputSubmitter: "", - BridgeExecutor: "", - EnableBatchSubmitter: true, + BridgeExecutor: "", + EnableOutputSubmitter: true, + EnableBatchSubmitter: true, MaxChunks: 5000, MaxChunkSize: 300000, // 300KB @@ -184,21 +182,19 @@ func (cfg Config) Validate() error { func (cfg Config) L1NodeConfig(homePath string) nodetypes.NodeConfig { nc := nodetypes.NodeConfig{ - RPC: cfg.L1Node.RPCAddress, - ProcessType: nodetypes.PROCESS_TYPE_DEFAULT, + RPC: cfg.L1Node.RPCAddress, + ProcessType: nodetypes.PROCESS_TYPE_DEFAULT, + Bech32Prefix: cfg.L1Node.Bech32Prefix, } - if cfg.OutputSubmitter != "" { + if cfg.EnableOutputSubmitter { nc.BroadcasterConfig = &btypes.BroadcasterConfig{ ChainID: cfg.L1Node.ChainID, GasPrice: cfg.L1Node.GasPrice, GasAdjustment: cfg.L1Node.GasAdjustment, - Bech32Prefix: cfg.L1Node.Bech32Prefix, TxTimeout: time.Duration(cfg.L1Node.TxTimeout) * time.Second, - KeyringConfig: btypes.KeyringConfig{ - Name: cfg.OutputSubmitter, - HomePath: homePath, - }, + Bech32Prefix: cfg.L1Node.Bech32Prefix, + HomePath: homePath, } } @@ -207,8 +203,9 @@ func (cfg Config) L1NodeConfig(homePath string) nodetypes.NodeConfig { func (cfg Config) L2NodeConfig(homePath string) nodetypes.NodeConfig { nc := nodetypes.NodeConfig{ - RPC: cfg.L2Node.RPCAddress, - ProcessType: nodetypes.PROCESS_TYPE_DEFAULT, + RPC: cfg.L2Node.RPCAddress, + ProcessType: nodetypes.PROCESS_TYPE_DEFAULT, + Bech32Prefix: cfg.L2Node.Bech32Prefix, } if cfg.BridgeExecutor != "" { @@ -216,12 +213,9 @@ func (cfg Config) L2NodeConfig(homePath string) nodetypes.NodeConfig { ChainID: cfg.L2Node.ChainID, GasPrice: cfg.L2Node.GasPrice, GasAdjustment: cfg.L2Node.GasAdjustment, - Bech32Prefix: cfg.L2Node.Bech32Prefix, TxTimeout: time.Duration(cfg.L2Node.TxTimeout) * time.Second, - KeyringConfig: btypes.KeyringConfig{ - Name: cfg.BridgeExecutor, - HomePath: homePath, - }, + Bech32Prefix: cfg.L2Node.Bech32Prefix, + HomePath: homePath, } } @@ -230,8 +224,9 @@ func (cfg Config) L2NodeConfig(homePath string) nodetypes.NodeConfig { func (cfg Config) DANodeConfig(homePath string) nodetypes.NodeConfig { nc := nodetypes.NodeConfig{ - RPC: cfg.DANode.RPCAddress, - ProcessType: nodetypes.PROCESS_TYPE_ONLY_BROADCAST, + RPC: cfg.DANode.RPCAddress, + ProcessType: nodetypes.PROCESS_TYPE_ONLY_BROADCAST, + Bech32Prefix: cfg.DANode.Bech32Prefix, } if cfg.EnableBatchSubmitter { @@ -239,11 +234,9 @@ func (cfg Config) DANodeConfig(homePath string) nodetypes.NodeConfig { ChainID: cfg.DANode.ChainID, GasPrice: cfg.DANode.GasPrice, GasAdjustment: cfg.DANode.GasAdjustment, - Bech32Prefix: cfg.DANode.Bech32Prefix, TxTimeout: time.Duration(cfg.DANode.TxTimeout) * time.Second, - KeyringConfig: btypes.KeyringConfig{ - HomePath: homePath, - }, + Bech32Prefix: cfg.DANode.Bech32Prefix, + HomePath: homePath, } } return nc diff --git a/node/broadcaster/broadcaster.go b/node/broadcaster/broadcaster.go index bbff333..114aa93 100644 --- a/node/broadcaster/broadcaster.go +++ b/node/broadcaster/broadcaster.go @@ -95,23 +95,28 @@ func NewBroadcaster( if rpcClient == nil { return nil, errors.New("rpc client is nil") } + return b, nil +} + +func (b *Broadcaster) Initialize(ctx context.Context, status *rpccoretypes.ResultStatus, keyringConfig *btypes.KeyringConfig) error { + err := keyringConfig.Validate() + if err != nil { + return err + } // setup keyring - keyBase, keyringRecord, err := cfg.GetKeyringRecord(cdc, cfg.ChainID) + keyBase, keyringRecord, err := b.cfg.GetKeyringRecord(b.cdc, keyringConfig) if err != nil { - return nil, err + return err } b.keyBase = keyBase addr, err := keyringRecord.GetAddress() if err != nil { - return nil, err + return err } b.keyAddress = addr b.keyName = keyringRecord.Name - return b, nil -} -func (b *Broadcaster) Initialize(ctx context.Context, status *rpccoretypes.ResultStatus) error { // prepare broadcaster return b.prepareBroadcaster(ctx, status.SyncInfo.LatestBlockTime) } diff --git a/node/broadcaster/types/config.go b/node/broadcaster/types/config.go index c8206fd..45ec3e9 100644 --- a/node/broadcaster/types/config.go +++ b/node/broadcaster/types/config.go @@ -37,8 +37,8 @@ type BroadcasterConfig struct { // PendingTxToProcessedMsgs is the function to convert pending tx to processed messages. PendingTxToProcessedMsgs PendingTxToProcessedMsgsFn - // KeyringConfig is the keyring configuration. - KeyringConfig KeyringConfig + // HomePath is the path to the keyring. + HomePath string } func (bc *BroadcasterConfig) WithPendingTxToProcessedMsgsFn(fn PendingTxToProcessedMsgsFn) { @@ -79,18 +79,22 @@ func (bc BroadcasterConfig) Validate() error { return fmt.Errorf("tx timeout is zero") } - return bc.KeyringConfig.Validate() + return nil } -func (bc BroadcasterConfig) GetKeyringRecord(cdc codec.Codec, chainID string) (keyring.Keyring, *keyring.Record, error) { - keyBase, err := keys.GetKeyBase(chainID, bc.KeyringConfig.HomePath, cdc, nil) +func (bc BroadcasterConfig) GetKeyringRecord(cdc codec.Codec, keyringConfig *KeyringConfig) (keyring.Keyring, *keyring.Record, error) { + if keyringConfig == nil { + return nil, nil, fmt.Errorf("keyring config cannot be nil") + } + + keyBase, err := keys.GetKeyBase(bc.ChainID, bc.HomePath, cdc, nil) if err != nil { return nil, nil, err } else if keyBase == nil { return nil, nil, fmt.Errorf("failed to get key base") } - keyringRecord, err := bc.KeyringConfig.GetKeyRecord(keyBase, bc.Bech32Prefix) + keyringRecord, err := keyringConfig.GetKeyRecord(keyBase, bc.Bech32Prefix) if err != nil { return nil, nil, err } else if keyringRecord == nil { @@ -101,17 +105,11 @@ func (bc BroadcasterConfig) GetKeyringRecord(cdc codec.Codec, chainID string) (k } type KeyringConfig struct { - // HomePath is the path to the keyring. - HomePath string `json:"home_path"` - // Name of key in keyring Name string `json:"name"` // Address of key in keyring Address string `json:"address"` - - // Mnemonic of key in keyring - // Mnemonic string `json:"mnemonic"` } func (kc KeyringConfig) GetKeyRecord(keyBase keyring.Keyring, bech32Prefix string) (*keyring.Record, error) { diff --git a/node/node.go b/node/node.go index bd6c2d0..c1065ed 100644 --- a/node/node.go +++ b/node/node.go @@ -8,6 +8,7 @@ import ( "cosmossdk.io/core/address" "github.com/initia-labs/opinit-bots/node/broadcaster" + btypes "github.com/initia-labs/opinit-bots/node/broadcaster/types" "github.com/initia-labs/opinit-bots/node/rpcclient" nodetypes "github.com/initia-labs/opinit-bots/node/types" "github.com/initia-labs/opinit-bots/types" @@ -85,7 +86,7 @@ func NewNode(cfg nodetypes.NodeConfig, db types.DB, logger *zap.Logger, cdc code // StartHeight is the height to start processing. // If it is 0, the latest height is used. // If the latest height exists in the database, this is ignored. -func (n *Node) Initialize(ctx context.Context, processedHeight int64) (err error) { +func (n *Node) Initialize(ctx context.Context, processedHeight int64, keyringConfig *btypes.KeyringConfig) (err error) { // check if node is catching up status, err := n.rpcClient.Status(ctx) if err != nil { @@ -95,7 +96,7 @@ func (n *Node) Initialize(ctx context.Context, processedHeight int64) (err error return errors.New("node is catching up") } if n.broadcaster != nil { - err = n.broadcaster.Initialize(ctx, status) + err = n.broadcaster.Initialize(ctx, status, keyringConfig) if err != nil { return err } diff --git a/node/types/config.go b/node/types/config.go index adcd0e1..0af26fe 100644 --- a/node/types/config.go +++ b/node/types/config.go @@ -20,6 +20,9 @@ type NodeConfig struct { // BlockProcessType is the type of block process. ProcessType BlockProcessType + // Bech32Prefix is the Bech32 prefix of the chain. + Bech32Prefix string + // You can leave it empty, then the bot will skip the transaction submission. BroadcasterConfig *btypes.BroadcasterConfig } @@ -33,6 +36,10 @@ func (nc NodeConfig) Validate() error { return fmt.Errorf("invalid process type") } + if nc.Bech32Prefix == "" { + return fmt.Errorf("bech32 prefix is empty") + } + // Validated in broadcaster // // if nc.BroadcasterConfig != nil { diff --git a/provider/child/child.go b/provider/child/child.go index ec1b01d..00facdb 100644 --- a/provider/child/child.go +++ b/provider/child/child.go @@ -28,7 +28,7 @@ type BaseChild struct { node *node.Node mk *merkle.Merkle - bridgeInfo opchildtypes.BridgeInfo + bridgeInfo ophosttypes.QueryBridgeResponse initializeTreeFn func(int64) (bool, error) @@ -44,9 +44,9 @@ type BaseChild struct { func NewBaseChildV1( cfg nodetypes.NodeConfig, - db types.DB, logger *zap.Logger, bech32Prefix string, + db types.DB, logger *zap.Logger, ) *BaseChild { - appCodec, txConfig, err := GetCodec(bech32Prefix) + appCodec, txConfig, err := GetCodec(cfg.Bech32Prefix) if err != nil { panic(err) } @@ -89,8 +89,8 @@ func GetCodec(bech32Prefix string) (codec.Codec, client.TxConfig, error) { }) } -func (b *BaseChild) Initialize(ctx context.Context, processedHeight int64, startOutputIndex uint64, bridgeInfo opchildtypes.BridgeInfo) (uint64, error) { - err := b.node.Initialize(ctx, processedHeight) +func (b *BaseChild) Initialize(ctx context.Context, processedHeight int64, startOutputIndex uint64, bridgeInfo ophosttypes.QueryBridgeResponse, keyringConfig *btypes.KeyringConfig) (uint64, error) { + err := b.node.Initialize(ctx, processedHeight, keyringConfig) if err != nil { return 0, err } @@ -162,11 +162,11 @@ func (b BaseChild) HasKey() bool { return b.node.HasBroadcaster() } -func (b *BaseChild) SetBridgeInfo(bridgeInfo opchildtypes.BridgeInfo) { +func (b *BaseChild) SetBridgeInfo(bridgeInfo ophosttypes.QueryBridgeResponse) { b.bridgeInfo = bridgeInfo } -func (b BaseChild) BridgeInfo() opchildtypes.BridgeInfo { +func (b BaseChild) BridgeInfo() ophosttypes.QueryBridgeResponse { return b.bridgeInfo } diff --git a/provider/host/host.go b/provider/host/host.go index 6b3e26e..4bf2277 100644 --- a/provider/host/host.go +++ b/provider/host/host.go @@ -10,7 +10,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" - opchildtypes "github.com/initia-labs/OPinit/x/opchild/types" "github.com/initia-labs/OPinit/x/ophost" ophosttypes "github.com/initia-labs/OPinit/x/ophost/types" @@ -26,7 +25,7 @@ type BaseHost struct { node *node.Node - bridgeInfo opchildtypes.BridgeInfo + bridgeInfo ophosttypes.QueryBridgeResponse cfg nodetypes.NodeConfig db types.DB @@ -39,9 +38,9 @@ type BaseHost struct { } func NewBaseHostV1(cfg nodetypes.NodeConfig, - db types.DB, logger *zap.Logger, bech32Prefix string, + db types.DB, logger *zap.Logger, ) *BaseHost { - appCodec, txConfig, err := GetCodec(bech32Prefix) + appCodec, txConfig, err := GetCodec(cfg.Bech32Prefix) if err != nil { panic(err) } @@ -79,8 +78,8 @@ func GetCodec(bech32Prefix string) (codec.Codec, client.TxConfig, error) { }) } -func (b *BaseHost) Initialize(ctx context.Context, processedHeight int64, bridgeInfo opchildtypes.BridgeInfo) error { - err := b.node.Initialize(ctx, processedHeight) +func (b *BaseHost) Initialize(ctx context.Context, processedHeight int64, bridgeInfo ophosttypes.QueryBridgeResponse, keyringConfig *btypes.KeyringConfig) error { + err := b.node.Initialize(ctx, processedHeight, keyringConfig) if err != nil { return err } @@ -121,11 +120,11 @@ func (b BaseHost) OracleEnabled() bool { return b.bridgeInfo.BridgeConfig.OracleEnabled } -func (b *BaseHost) SetBridgeInfo(bridgeInfo opchildtypes.BridgeInfo) { +func (b *BaseHost) SetBridgeInfo(bridgeInfo ophosttypes.QueryBridgeResponse) { b.bridgeInfo = bridgeInfo } -func (b BaseHost) BridgeInfo() opchildtypes.BridgeInfo { +func (b BaseHost) BridgeInfo() ophosttypes.QueryBridgeResponse { return b.bridgeInfo }