Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/fetch output submitter info #34

Merged
merged 6 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions challenger/challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -103,15 +108,15 @@ 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
}
if initialBlockTime.Before(hostInitialBlockTime) {
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
}
Expand Down
8 changes: 4 additions & 4 deletions challenger/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions challenger/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions challenger/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions executor/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type BatchSubmitter struct {
host hostNode
da executortypes.DANode

bridgeInfo opchildtypes.BridgeInfo
bridgeInfo ophosttypes.QueryBridgeResponse

cfg nodetypes.NodeConfig
batchCfg executortypes.BatchConfig
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 4 additions & 5 deletions executor/celestia/celestia.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions executor/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
79 changes: 50 additions & 29 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
sh-cha marked this conversation as resolved.
Show resolved Hide resolved
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
}
Expand Down Expand Up @@ -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
}
sh-cha marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading