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

refactor: Imporve signer confirm tx #2970

Merged
merged 13 commits into from
Jan 10, 2024
42 changes: 21 additions & 21 deletions pkg/user/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,31 +211,31 @@ func (s *Signer) BroadcastTx(ctx context.Context, txBytes []byte) (*sdktypes.TxR
// is encountered.
func (s *Signer) ConfirmTx(ctx context.Context, txHash string) (*sdktypes.TxResponse, error) {
txClient := tx.NewServiceClient(s.grpc)
timer := time.NewTimer(0)
defer timer.Stop()

s.mtx.RLock()
pollTime := s.pollTime
s.mtx.RUnlock()
rootulp marked this conversation as resolved.
Show resolved Hide resolved

pollTicker := time.NewTicker(pollTime)
htiennv marked this conversation as resolved.
Show resolved Hide resolved
defer pollTicker.Stop()

for {
resp, err := txClient.GetTx(ctx, &tx.GetTxRequest{Hash: txHash})
if err == nil {
if resp.TxResponse.Code != 0 {
return resp.TxResponse, fmt.Errorf("tx failed with code %d: %s", resp.TxResponse.Code, resp.TxResponse.RawLog)
}
return resp.TxResponse, nil
}
if !strings.Contains(err.Error(), "not found") {
return &sdktypes.TxResponse{}, err
}
htiennv marked this conversation as resolved.
Show resolved Hide resolved

// Wait for the next round.
select {
case <-ctx.Done():
return &sdktypes.TxResponse{}, ctx.Err()
case <-timer.C:
resp, err := txClient.GetTx(
ctx,
&tx.GetTxRequest{
Hash: txHash,
},
)
if err == nil {
if resp.TxResponse.Code != 0 {
return resp.TxResponse, fmt.Errorf("tx failed with code %d: %s", resp.TxResponse.Code, resp.TxResponse.RawLog)
}
return resp.TxResponse, nil
}

if !strings.Contains(err.Error(), "not found") {
return &sdktypes.TxResponse{}, err
}

timer.Reset(s.pollTime)
case <-pollTicker.C:
}
}
}
Expand Down
59 changes: 54 additions & 5 deletions pkg/user/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package user_test

import (
"context"
"fmt"
"testing"
"time"

Expand All @@ -11,6 +12,7 @@ import (
"github.com/celestiaorg/celestia-app/test/util/blobfactory"
"github.com/celestiaorg/celestia-app/test/util/testnode"
sdk "github.com/cosmos/cosmos-sdk/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -69,12 +71,43 @@ func (s *SignerTestSuite) TestSubmitTx() {
require.EqualValues(t, 0, resp.Code)
}

func (s *SignerTestSuite) ConfirmTxTimeout() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
func (s *SignerTestSuite) TestConfirmTx() {
rootulp marked this conversation as resolved.
Show resolved Hide resolved
t := s.T()

// 1. ConfirmTx returns an error when the context times out
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := s.signer.ConfirmTx(ctx, "E32BD15CAF57AF15D17B0D63CF4E63A9835DD1CEBB059C335C79586BC3013728")
assert.Error(t, err)
assert.Contains(t, err.Error(), context.DeadlineExceeded.Error())

// 2. ConfirmTx returns an error when tx is not found
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := s.signer.ConfirmTx(ctx, string("E32BD15CAF57AF15D17B0D63CF4E63A9835DD1CEBB059C335C79586BC3013728"))
require.Error(s.T(), err)
require.Equal(s.T(), err, context.DeadlineExceeded)
_, err = s.signer.ConfirmTx(ctx, "not found tx")
assert.Error(t, err)

// 3. ConfirmTx returns no error if tx is found immediately
fee := user.SetFee(1e6)
gas := user.SetGasLimit(1e6)
msg := bank.NewMsgSend(s.signer.Address(), testnode.RandomAddress().(sdk.AccAddress), sdk.NewCoins(sdk.NewInt64Coin(app.BondDenom, 10)))
resp, err := s.submitTxWithoutConfirm([]sdk.Msg{msg}, fee, gas)
assert.NoError(t, err)
assert.NotNil(t, resp)
resp, err = s.signer.ConfirmTx(s.ctx.GoContext(), resp.TxHash)
assert.NoError(t, err)
assert.Equal(t, abci.CodeTypeOK, resp.Code)

// 4. ConfirmTx returns error if the tx is found with a non-zero error code
balance := s.queryCurrentBalance(t)
// Create a msg send with out of balance, ensure this tx fails
msg = bank.NewMsgSend(s.signer.Address(), testnode.RandomAddress().(sdk.AccAddress), sdk.NewCoins(sdk.NewInt64Coin(app.BondDenom, 1+balance)))
resp, err = s.submitTxWithoutConfirm([]sdk.Msg{msg}, fee, gas)
assert.NoError(t, err)
assert.NotNil(t, resp)
resp, err = s.signer.ConfirmTx(s.ctx.GoContext(), resp.TxHash)
assert.Error(t, err)
assert.NotEqual(t, abci.CodeTypeOK, resp.Code)
htiennv marked this conversation as resolved.
Show resolved Hide resolved
}

// TestGasConsumption verifies that the amount deducted from a user's balance is
Expand Down Expand Up @@ -118,3 +151,19 @@ func (s *SignerTestSuite) queryCurrentBalance(t *testing.T) int64 {
require.NoError(t, err)
return balanceResp.Balances.AmountOf(app.BondDenom).Int64()
}

func (s *SignerTestSuite) submitTxWithoutConfirm(msgs []sdktypes.Msg, opts ...user.TxOption) (*sdktypes.TxResponse, error) {
txBytes, err := s.signer.CreateTx(msgs, opts...)
if err != nil {
return nil, err
}

resp, err := s.signer.BroadcastTx(s.ctx.GoContext(), txBytes)
if err != nil {
return nil, err
}
if resp.Code != 0 {
return resp, fmt.Errorf("tx failed with code %d: %s", resp.Code, resp.RawLog)
}
return resp, nil
}
Loading