Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmwaters committed Mar 22, 2024
1 parent 5f5a8cc commit 591caaa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
5 changes: 3 additions & 2 deletions app/test/big_blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/user"
"github.com/celestiaorg/celestia-app/test/util/testfactory"
"github.com/celestiaorg/celestia-app/test/util/testnode"
Expand Down Expand Up @@ -79,8 +80,8 @@ func (s *BigBlobSuite) TestErrBlobsTooLarge() {
s.Run(tc.name, func() {
subCtx, cancel := context.WithTimeout(s.cctx.GoContext(), 30*time.Second)
defer cancel()
res, err := signer.SubmitPayForBlob(subCtx, []*blob.Blob{tc.blob}, user.SetGasLimit(1e9), user.SetFee(2000000))
require.NoError(t, err)
res, err := signer.SubmitPayForBlob(subCtx, []*blob.Blob{tc.blob}, user.SetGasLimitAndFee(1e9, appconsts.DefaultGlobalMinGasPrice))
require.Error(t, err)
require.NotNil(t, res)
require.Equal(t, tc.want, res.Code, res.Logs)
})
Expand Down
54 changes: 30 additions & 24 deletions app/test/priority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app_test
import (
"encoding/hex"
"sort"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -71,41 +72,46 @@ func (s *PriorityTestSuite) TestPriorityByGasPrice() {
t := s.T()

// quickly submit blobs with a random fee
hashes := make([]string, 0, len(s.signers))

hashes := make(chan string, len(s.signers))
blobSize := uint32(100)
gasLimit := blobtypes.DefaultEstimateGas([]uint32{blobSize})
wg := &sync.WaitGroup{}
for _, signer := range s.signers {
blobSize := uint32(100)
gasLimit := blobtypes.DefaultEstimateGas([]uint32{blobSize})
gasPrice := s.rand.Float64()
resp, err := signer.SubmitPayForBlob(
s.cctx.GoContext(),
blobfactory.ManyBlobs(
s.rand,
[]namespace.Namespace{namespace.RandomBlobNamespace()},
[]int{100}),
user.SetGasLimitAndFee(gasLimit, gasPrice),
)
require.NoError(t, err)
require.Equal(t, abci.CodeTypeOK, resp.Code, resp.RawLog)
hashes = append(hashes, resp.TxHash)
wg.Add(1)
go func() {
defer wg.Done()
gasPrice := float64(s.rand.Intn(1000)+1) / 1000
resp, err := signer.SubmitPayForBlob(
s.cctx.GoContext(),
blobfactory.ManyBlobs(
s.rand,
[]namespace.Namespace{namespace.RandomBlobNamespace()},
[]int{100}),
user.SetGasLimitAndFee(gasLimit, gasPrice),
)
require.NoError(t, err)
require.Equal(t, abci.CodeTypeOK, resp.Code, resp.RawLog)
hashes <- resp.TxHash
}()
}

wg.Wait()
close(hashes)

err := s.cctx.WaitForNextBlock()
require.NoError(t, err)

// get the responses for each tx for analysis and sort by height
// note: use rpc types because they contain the tx index
heightMap := make(map[int64][]*rpctypes.ResultTx)
for _, hash := range hashes {
resp, err := s.signers[0].ConfirmTx(s.cctx.GoContext(), hash)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, abci.CodeTypeOK, resp.Code)
for hash := range hashes {
// use the core rpc type because it contains the tx index
hash, err := hex.DecodeString(hash)
require.NoError(t, err)
coreRes, err := s.cctx.Client.Tx(s.cctx.GoContext(), hash, false)
require.NoError(t, err)
heightMap[resp.Height] = append(heightMap[resp.Height], coreRes)
heightMap[coreRes.Height] = append(heightMap[coreRes.Height], coreRes)
}
require.GreaterOrEqual(t, len(heightMap), 1)

Expand All @@ -122,7 +128,7 @@ func (s *PriorityTestSuite) TestPriorityByGasPrice() {

// check that there was at least one block with more than three transactions
// in it. This is more of a sanity check than a test.
require.True(t, highestNumOfTxsPerBlock > 3)
require.Greater(t, highestNumOfTxsPerBlock, 3)
}

func sortByIndex(txs []*rpctypes.ResultTx) []*rpctypes.ResultTx {
Expand All @@ -134,14 +140,14 @@ func sortByIndex(txs []*rpctypes.ResultTx) []*rpctypes.ResultTx {

func isSortedByFee(t *testing.T, ecfg encoding.Config, responses []*rpctypes.ResultTx) bool {
for i := 0; i < len(responses)-1; i++ {
if gasPrice(t, ecfg, responses[i]) <= gasPrice(t, ecfg, responses[i+1]) {
if getGasPrice(t, ecfg, responses[i]) <= getGasPrice(t, ecfg, responses[i+1]) {
return false
}
}
return true
}

func gasPrice(t *testing.T, ecfg encoding.Config, resp *rpctypes.ResultTx) float64 {
func getGasPrice(t *testing.T, ecfg encoding.Config, resp *rpctypes.ResultTx) float64 {
sdkTx, err := ecfg.TxConfig.TxDecoder()(resp.Tx)
require.NoError(t, err)
feeTx := sdkTx.(sdk.FeeTx)
Expand Down
23 changes: 10 additions & 13 deletions pkg/user/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
abci "github.com/tendermint/tendermint/abci/types"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -164,7 +165,7 @@ func (s *Signer) SubmitTx(ctx context.Context, msgs []sdktypes.Msg, opts ...TxOp
func (s *Signer) SubmitPayForBlob(ctx context.Context, blobs []*blob.Blob, opts ...TxOption) (*sdktypes.TxResponse, error) {
resp, err := s.broadcastPayForBlob(ctx, blobs, opts...)
if err != nil {
return nil, err
return resp, err
}

return s.ConfirmTx(ctx, resp.TxHash)
Expand All @@ -178,14 +179,7 @@ func (s *Signer) broadcastPayForBlob(ctx context.Context, blobs []*blob.Blob, op
return nil, err
}

resp, err := s.broadcastTx(ctx, txBytes, seqNum)
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
return s.broadcastTx(ctx, txBytes, seqNum)
}

// CreateTx forms a transaction from the provided messages and signs it. TxOptions may be optionally
Expand Down Expand Up @@ -305,9 +299,12 @@ func (s *Signer) broadcastTx(ctx context.Context, txBytes []byte, sequence uint6
s.localSequence = nextSequence
return s.retryBroadcastingTx(ctx, txBytes, nextSequence)
}
s.outboundSequences[sequence] = struct{}{}
s.reverseTxHashSequenceMap[resp.TxResponse.TxHash] = sequence
return resp.TxResponse, nil
if resp.TxResponse.Code == abci.CodeTypeOK {
s.outboundSequences[sequence] = struct{}{}
s.reverseTxHashSequenceMap[resp.TxResponse.TxHash] = sequence
return resp.TxResponse, nil
}
return resp.TxResponse, fmt.Errorf("tx failed with code %d: %s", resp.TxResponse.Code, resp.TxResponse.RawLog)
}

// retryBroadcastingTx creates a new transaction by copying over an existing transaction but creates a new signature with the
Expand Down Expand Up @@ -375,7 +372,7 @@ func (s *Signer) ConfirmTx(ctx context.Context, txHash string) (*sdktypes.TxResp
if err == nil {
if resp.TxResponse.Code != 0 {
s.updateNetworkSequence(txHash, false)
return resp.TxResponse, fmt.Errorf("tx failed with code %d: %s", resp.TxResponse.Code, resp.TxResponse.RawLog)
return resp.TxResponse, fmt.Errorf("tx was included but failed with code %d: %s", resp.TxResponse.Code, resp.TxResponse.RawLog)
}
s.updateNetworkSequence(txHash, true)
return resp.TxResponse, nil
Expand Down

0 comments on commit 591caaa

Please sign in to comment.