Skip to content

Commit

Permalink
app,bridge,helper: fix account sequence mismatch b/w bridge and DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Raneet10 committed Jun 7, 2024
1 parent 4b09573 commit b437234
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 71 deletions.
18 changes: 11 additions & 7 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
dbm "github.com/tendermint/tm-db"

authTypes "github.com/maticnetwork/heimdall/auth/types"
"github.com/maticnetwork/heimdall/helper"

hmTypes "github.com/maticnetwork/heimdall/types"
)

// Setup initializes a new App. A Nop logger is set in App.
func Setup(isCheckTx bool) *HeimdallApp {
func Setup(isCheckTx bool, testOpts ...*helper.TestOpts) *HeimdallApp {
db := dbm.NewMemDB()
app := NewHeimdallApp(log.NewNopLogger(), db)

Expand All @@ -26,12 +27,15 @@ func Setup(isCheckTx bool) *HeimdallApp {
}

// Initialize the chain
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)
req := abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
}

if len(testOpts) > 0 && testOpts[0] != nil {
req.ChainId = testOpts[0].GetChainId()
}
app.InitChain(req)
}

return app
Expand Down
14 changes: 7 additions & 7 deletions bridge/setu/broadcaster/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewTxBroadcaster(cdc *codec.Codec) *TxBroadcaster {
}

// BroadcastToHeimdall broadcast to heimdall
func (tb *TxBroadcaster) BroadcastToHeimdall(msg sdk.Msg, event interface{}) error {
func (tb *TxBroadcaster) BroadcastToHeimdall(msg sdk.Msg, event interface{}, testOpts ...*helper.TestOpts) (sdk.TxResponse, error) {
tb.heimdallMutex.Lock()
defer tb.heimdallMutex.Unlock()
defer util.LogElapsedTimeForStateSyncedEvent(event, "BroadcastToHeimdall", time.Now())
Expand All @@ -75,9 +75,9 @@ func (tb *TxBroadcaster) BroadcastToHeimdall(msg sdk.Msg, event interface{}) err
WithSequence(tb.lastSeqNo).
WithChainID(chainID)

txResponse, err := helper.BuildAndBroadcastMsgs(tb.CliCtx, txBldr, []sdk.Msg{msg})
if err != nil {
tb.logger.Error("Error while broadcasting the heimdall transaction", "error", err)
txResponse, err := helper.BuildAndBroadcastMsgs(tb.CliCtx, txBldr, []sdk.Msg{msg}, testOpts...)
if err != nil || txResponse.Code != uint32(sdk.CodeOK) {
tb.logger.Error("Error while broadcasting the heimdall transaction", "error", err, "txResponse", txResponse.Code)

// current address
address := hmTypes.BytesToHeimdallAddress(helper.GetAddress())
Expand All @@ -86,13 +86,13 @@ func (tb *TxBroadcaster) BroadcastToHeimdall(msg sdk.Msg, event interface{}) err
account, errAcc := util.GetAccount(tb.CliCtx, address)
if errAcc != nil {
tb.logger.Error("Error fetching account from rest-api", "url", helper.GetHeimdallServerEndpoint(fmt.Sprintf(util.AccountDetailsURL, helper.GetAddress())))
return errAcc
return txResponse, errAcc
}

// update seqNo for safety
tb.lastSeqNo = account.GetSequence()

return err
return txResponse, err
}

txHash := txResponse.TxHash
Expand All @@ -102,7 +102,7 @@ func (tb *TxBroadcaster) BroadcastToHeimdall(msg sdk.Msg, event interface{}) err
// increment account sequence
tb.lastSeqNo += 1

return nil
return txResponse, nil
}

// BroadcastToMatic broadcast to matic
Expand Down
Loading

0 comments on commit b437234

Please sign in to comment.