Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Aug 6, 2024
1 parent 8c36887 commit a62d73d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 48 deletions.
9 changes: 6 additions & 3 deletions executor/batch/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions executor/celestia/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions executor/child/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
40 changes: 6 additions & 34 deletions node/broadcaster/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
) (
Expand All @@ -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
}
Expand Down
6 changes: 1 addition & 5 deletions node/broadcaster/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
39 changes: 39 additions & 0 deletions txutils/utils.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a62d73d

Please sign in to comment.