From a62d73d50905804aa2bb8a79e4f63f3ab6067b1b Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Tue, 6 Aug 2024 13:59:14 +0900 Subject: [PATCH] fix bug --- executor/batch/utils.go | 9 ++++--- executor/celestia/node.go | 10 ++++---- executor/child/msgs.go | 8 +++++++ executor/executor.go | 4 ++-- node/broadcaster/tx.go | 40 +++++--------------------------- node/broadcaster/types/config.go | 6 +---- node/node.go | 4 ++++ txutils/utils.go | 39 +++++++++++++++++++++++++++++++ 8 files changed, 72 insertions(+), 48 deletions(-) create mode 100644 txutils/utils.go diff --git a/executor/batch/utils.go b/executor/batch/utils.go index 18c1b85..5ce9c90 100644 --- a/executor/batch/utils.go +++ b/executor/batch/utils.go @@ -6,7 +6,9 @@ import ( cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/gogoproto/proto" + opchildtypes "github.com/initia-labs/OPinit/x/opchild/types" + "github.com/initia-labs/opinit-bots-go/txutils" ) // prependLength prepends the length of the data to the data. @@ -20,7 +22,8 @@ func prependLength(data []byte) []byte { // to decrease the size of the batch. func (bs *BatchSubmitter) emptyOracleData(pbb *cmtproto.Block) ([]byte, error) { for i, txBytes := range pbb.Data.GetTxs() { - tx, err := bs.node.MustGetBroadcaster().DecodeTx(txBytes) + txConfig := bs.node.GetTxConfig() + tx, err := txutils.DecodeTx(txConfig, txBytes) if err != nil { // ignore not registered tx in codec continue @@ -33,11 +36,11 @@ func (bs *BatchSubmitter) emptyOracleData(pbb *cmtproto.Block) ([]byte, error) { if msg, ok := msgs[0].(*opchildtypes.MsgUpdateOracle); ok { msg.Data = []byte{} - tx, err := bs.node.MustGetBroadcaster().ChangeMsgsFromTx(tx, []sdk.Msg{msg}) + tx, err := txutils.ChangeMsgsFromTx(txConfig, tx, []sdk.Msg{msg}) if err != nil { return nil, err } - convertedTxBytes, err := bs.node.MustGetBroadcaster().EncodeTx(tx) + convertedTxBytes, err := txutils.EncodeTx(txConfig, tx) if err != nil { return nil, err } diff --git a/executor/celestia/node.go b/executor/celestia/node.go index a2c69c3..a75ca72 100644 --- a/executor/celestia/node.go +++ b/executor/celestia/node.go @@ -8,6 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" btypes "github.com/initia-labs/opinit-bots-go/node/broadcaster/types" + "github.com/initia-labs/opinit-bots-go/txutils" celestiatypes "github.com/initia-labs/opinit-bots-go/types/celestia" ) @@ -52,7 +53,8 @@ func (c *Celestia) BuildTxWithMessages( } tx := txb.GetTx() - txBytes, err = b.EncodeTx(tx) + txConfig := c.node.GetTxConfig() + txBytes, err = txutils.EncodeTx(txConfig, tx) if err != nil { return nil, "", err } @@ -73,11 +75,11 @@ func (c *Celestia) BuildTxWithMessages( func (c *Celestia) PendingTxToProcessedMsgs( txBytes []byte, ) ([]sdk.Msg, error) { - b := c.node.MustGetBroadcaster() + txConfig := c.node.GetTxConfig() blobTx := &celestiatypes.BlobTx{} if err := blobTx.Unmarshal(txBytes); err == nil { - pfbTx, err := b.DecodeTx(blobTx.Tx) + pfbTx, err := txutils.DecodeTx(txConfig, blobTx.Tx) if err != nil { return nil, err } @@ -91,7 +93,7 @@ func (c *Celestia) PendingTxToProcessedMsgs( }, nil } - tx, err := b.DecodeTx(txBytes) + tx, err := txutils.DecodeTx(txConfig, txBytes) if err != nil { return nil, err } diff --git a/executor/child/msgs.go b/executor/child/msgs.go index d1789af..acc74ea 100644 --- a/executor/child/msgs.go +++ b/executor/child/msgs.go @@ -15,6 +15,10 @@ func (ch Child) GetMsgFinalizeTokenDeposit( data []byte, ) (sdk.Msg, error) { sender, err := ch.node.MustGetBroadcaster().GetAddressString() + if err != nil { + return nil, err + } + msg := opchildtypes.NewMsgFinalizeTokenDeposit( sender, from, @@ -37,6 +41,10 @@ func (ch Child) GetMsgUpdateOracle( data []byte, ) (sdk.Msg, error) { sender, err := ch.node.MustGetBroadcaster().GetAddressString() + if err != nil { + return nil, err + } + msg := opchildtypes.NewMsgUpdateOracle( sender, height, diff --git a/executor/executor.go b/executor/executor.go index ae7044a..913842e 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -177,7 +177,7 @@ func (ex *Executor) makeDANode(bridgeId int64) (executortypes.DANode, error) { ex.cfg.Version, false, ex.cfg.DANodeConfig(ex.homePath), ex.db.WithPrefix([]byte(executortypes.DAHostNodeName)), ex.logger.Named(executortypes.DAHostNodeName), - ex.homePath, batchInfo.BatchInfo.Submitter, + ex.cfg.DABech32Prefix, batchInfo.BatchInfo.Submitter, ) if ex.host.GetAddress().Equals(da.GetAddress()) { return ex.host, nil @@ -189,7 +189,7 @@ func (ex *Executor) makeDANode(bridgeId int64) (executortypes.DANode, error) { da := celestia.NewDACelestia(ex.cfg.Version, ex.cfg.DANodeConfig(ex.homePath), ex.db.WithPrefix([]byte(executortypes.DACelestiaNodeName)), ex.logger.Named(executortypes.DACelestiaNodeName), - ex.homePath, batchInfo.BatchInfo.Submitter, + ex.cfg.DABech32Prefix, batchInfo.BatchInfo.Submitter, ) da.Initialize(ex.batch, bridgeId) da.RegisterDAHandlers() diff --git a/node/broadcaster/tx.go b/node/broadcaster/tx.go index b891fb2..753fbc8 100644 --- a/node/broadcaster/tx.go +++ b/node/broadcaster/tx.go @@ -19,9 +19,9 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" txtypes "github.com/cosmos/cosmos-sdk/types/tx" "github.com/cosmos/cosmos-sdk/types/tx/signing" - authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" btypes "github.com/initia-labs/opinit-bots-go/node/broadcaster/types" + "github.com/initia-labs/opinit-bots-go/txutils" opchildtypes "github.com/initia-labs/OPinit/x/opchild/types" ) @@ -226,7 +226,7 @@ func (b Broadcaster) buildSimTx(info *keyring.Record, txf tx.Factory, msgs ...sd return nil, err } - return b.EncodeTx(txb.GetTx()) + return txutils.EncodeTx(b.txConfig, txb.GetTx()) } func (b *Broadcaster) enqueueLocalPendingTx(tx btypes.PendingTxInfo) { @@ -257,36 +257,8 @@ func (b *Broadcaster) dequeueLocalPendingTx() { b.pendingTxs = b.pendingTxs[1:] } -func (b Broadcaster) EncodeTx(tx authsigning.Tx) ([]byte, error) { - txBytes, err := b.txConfig.TxEncoder()(tx) - if err != nil { - return nil, err - } - return txBytes, nil -} - -func (b Broadcaster) DecodeTx(txBytes []byte) (authsigning.Tx, error) { - tx, err := b.txConfig.TxDecoder()(txBytes) - if err != nil { - return nil, err - } - return tx.(authsigning.Tx), nil -} - -func (n Broadcaster) ChangeMsgsFromTx(tx authsigning.Tx, msgs []sdk.Msg) (authsigning.Tx, error) { - builder, err := n.txConfig.WrapTxBuilder(tx) - if err != nil { - return nil, err - } - err = builder.SetMsgs(msgs...) - if err != nil { - return nil, err - } - return builder.GetTx(), nil -} - // buildTxWithMessages creates a transaction from the given messages. -func (b Broadcaster) DefaultBuildTxWithMessages( +func (b *Broadcaster) DefaultBuildTxWithMessages( ctx context.Context, msgs []sdk.Msg, ) ( @@ -311,17 +283,17 @@ func (b Broadcaster) DefaultBuildTxWithMessages( } tx := txb.GetTx() - txBytes, err = b.EncodeTx(tx) + txBytes, err = txutils.EncodeTx(b.txConfig, tx) if err != nil { return nil, "", err } return txBytes, btypes.TxHash(txBytes), nil } -func (b Broadcaster) DefaultPendingTxToProcessedMsgs( +func (b *Broadcaster) DefaultPendingTxToProcessedMsgs( txBytes []byte, ) ([]sdk.Msg, error) { - tx, err := b.DecodeTx(txBytes) + tx, err := txutils.DecodeTx(b.txConfig, txBytes) if err != nil { return nil, err } diff --git a/node/broadcaster/types/config.go b/node/broadcaster/types/config.go index 7b63c81..9551a14 100644 --- a/node/broadcaster/types/config.go +++ b/node/broadcaster/types/config.go @@ -51,10 +51,6 @@ func (bc BroadcasterConfig) Validate() error { return fmt.Errorf("failed to parse gas price: %s", bc.GasPrice) } - if bc.GasPrice == "" { - return fmt.Errorf("gas price is empty") - } - if bc.Bech32Prefix == "" { return fmt.Errorf("bech32 prefix is empty") } @@ -78,7 +74,7 @@ func (bc BroadcasterConfig) GetKeyringRecord(cdc codec.Codec, chainID string) (k return nil, nil, fmt.Errorf("failed to get key base") } - keyringRecord, err := bc.KeyringConfig.GetKeyRecord(keyBase, "") + keyringRecord, err := bc.KeyringConfig.GetKeyRecord(keyBase, bc.Bech32Prefix) if err != nil { return nil, nil, err } else if keyringRecord == nil { diff --git a/node/node.go b/node/node.go index 445c6c9..b8701f2 100644 --- a/node/node.go +++ b/node/node.go @@ -160,6 +160,10 @@ func (n Node) GetHeight() uint64 { return n.lastProcessedBlockHeight + 1 } +func (n Node) GetTxConfig() client.TxConfig { + return n.txConfig +} + func (n Node) HasBroadcaster() bool { return n.broadcaster != nil } diff --git a/txutils/utils.go b/txutils/utils.go new file mode 100644 index 0000000..a96e5e7 --- /dev/null +++ b/txutils/utils.go @@ -0,0 +1,39 @@ +package txutils + +import ( + "github.com/cosmos/cosmos-sdk/client" + sdk "github.com/cosmos/cosmos-sdk/types" + authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" +) + +func EncodeTx(txConfig client.TxConfig, tx authsigning.Tx) ([]byte, error) { + txBytes, err := txConfig.TxEncoder()(tx) + if err != nil { + return nil, err + } + + return txBytes, nil +} + +func DecodeTx(txConfig client.TxConfig, txBytes []byte) (authsigning.Tx, error) { + tx, err := txConfig.TxDecoder()(txBytes) + if err != nil { + return nil, err + } + + return tx.(authsigning.Tx), nil +} + +func ChangeMsgsFromTx(txConfig client.TxConfig, tx authsigning.Tx, msgs []sdk.Msg) (authsigning.Tx, error) { + builder, err := txConfig.WrapTxBuilder(tx) + if err != nil { + return nil, err + } + + err = builder.SetMsgs(msgs...) + if err != nil { + return nil, err + } + + return builder.GetTx(), nil +}