Skip to content

Commit

Permalink
Merge pull request #91 from bandprotocol/nathachai/yoda-fix-account-a…
Browse files Browse the repository at this point in the history
…ttempt

Yoda Fix No-Reuse Account Information
  • Loading branch information
ntchjb committed Jun 2, 2021
1 parent a8fe613 commit 4b1a8c5
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions yoda/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ var (
)

func signAndBroadcast(
c *Context, key keyring.Info, msgs []sdk.Msg, acc client.Account, gasLimit uint64, memo string,
c *Context, key keyring.Info, msgs []sdk.Msg, gasLimit uint64, memo string,
) (string, error) {
clientCtx := client.Context{
Client: c.client,
TxConfig: band.MakeEncodingConfig().TxConfig,
BroadcastMode: "async",
InterfaceRegistry: band.MakeEncodingConfig().InterfaceRegistry,
}
acc, err := queryAccount(clientCtx, key)
if err != nil {
return "", fmt.Errorf("unable to get account: %w", err)
}

txf := tx.Factory{}.
WithAccountNumber(acc.GetAccountNumber()).
Expand Down Expand Up @@ -72,6 +76,16 @@ func signAndBroadcast(
return res.TxHash, nil
}

func queryAccount(clientCtx client.Context, key keyring.Info) (client.Account, error) {
accountRetriever := authtypes.AccountRetriever{}
acc, err := accountRetriever.GetAccount(clientCtx, key.GetAddress())
if err != nil {
return nil, err
}

return acc, nil
}

func SubmitReport(c *Context, l *Logger, keyIndex int64, reports []ReportMsgWithKey) {
// Return key and update pending metric when done with SubmitReport whether successfully or not.
defer func() {
Expand Down Expand Up @@ -111,11 +125,9 @@ func SubmitReport(c *Context, l *Logger, keyIndex int64, reports []ReportMsgWith
TxConfig: band.MakeEncodingConfig().TxConfig,
InterfaceRegistry: band.MakeEncodingConfig().InterfaceRegistry,
}

accountRetriever := authtypes.AccountRetriever{}
acc, err := accountRetriever.GetAccount(clientCtx, key.GetAddress())
acc, err := queryAccount(clientCtx, key)
if err != nil {
l.Debug(":warning: Failed to query account with error: %s", err.Error())
l.Error(":warning: Failed to query account with error: %s", c, err.Error())
return
}

Expand All @@ -126,7 +138,7 @@ func SubmitReport(c *Context, l *Logger, keyIndex int64, reports []ReportMsgWith
l.Info(":e-mail: Sending report transaction attempt: (%d/%d)", sendAttempt, c.maxTry)
for broadcastTry := uint64(1); broadcastTry <= c.maxTry; broadcastTry++ {
l.Info(":writing_hand: Try to sign and broadcast report transaction(%d/%d)", broadcastTry, c.maxTry)
hash, err := signAndBroadcast(c, key, msgs, acc, gasLimit, memo)
hash, err := signAndBroadcast(c, key, msgs, gasLimit, memo)
if err != nil {
// Use info level because this error can happen and retry process can solve this error.
l.Info(":warning: %s", err.Error())
Expand Down

0 comments on commit 4b1a8c5

Please sign in to comment.