Skip to content

Commit

Permalink
Add tests for ordering outgoing txs by nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit committed Oct 23, 2024
1 parent 397d3b1 commit 2b6ebe2
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
58 changes: 58 additions & 0 deletions module/x/gravity/keeper/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,61 @@ func TestCancelBatchTx(t *testing.T) {
})
require.Len(t, gotUnbatchedTx, 4) // All 4 transactions should be back in the pool
}

func TestOrderBatchesByNonceAscending(t *testing.T) {
t.Run("normal case", func(t *testing.T) {
// Create test batches with different nonces
batches := []*types.BatchTx{
{BatchNonce: 3},
{BatchNonce: 1},
{BatchNonce: 4},
{BatchNonce: 2},
}

// Order the batches
orderedBatches := orderBatchesByNonceAscending(batches)

// Check if the batches are ordered correctly
assert.Equal(t, uint64(1), orderedBatches[0].BatchNonce)
assert.Equal(t, uint64(2), orderedBatches[1].BatchNonce)
assert.Equal(t, uint64(3), orderedBatches[2].BatchNonce)
assert.Equal(t, uint64(4), orderedBatches[3].BatchNonce)

// Check if the length of the slice remains the same
assert.Equal(t, len(batches), len(orderedBatches))
})

t.Run("empty slice", func(t *testing.T) {
batches := []*types.BatchTx{}
orderedBatches := orderBatchesByNonceAscending(batches)
assert.Empty(t, orderedBatches)
})

t.Run("nil slice", func(t *testing.T) {
var batches []*types.BatchTx
orderedBatches := orderBatchesByNonceAscending(batches)
assert.Nil(t, orderedBatches)
})

t.Run("single element", func(t *testing.T) {
batches := []*types.BatchTx{{BatchNonce: 1}}
orderedBatches := orderBatchesByNonceAscending(batches)
assert.Equal(t, 1, len(orderedBatches))
assert.Equal(t, uint64(1), orderedBatches[0].BatchNonce)
})

t.Run("duplicate nonces", func(t *testing.T) {
batches := []*types.BatchTx{
{BatchNonce: 2},
{BatchNonce: 1},
{BatchNonce: 2},
{BatchNonce: 1},
}
orderedBatches := orderBatchesByNonceAscending(batches)
assert.Equal(t, 4, len(orderedBatches))
assert.Equal(t, uint64(1), orderedBatches[0].BatchNonce)
assert.Equal(t, uint64(1), orderedBatches[1].BatchNonce)
assert.Equal(t, uint64(2), orderedBatches[2].BatchNonce)
assert.Equal(t, uint64(2), orderedBatches[3].BatchNonce)
})
}
58 changes: 58 additions & 0 deletions module/x/gravity/keeper/contract_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,61 @@ func TestGetUnconfirmedContractCallTxs(t *testing.T) {
require.Empty(t, gk.GetUnsignedContractCallTxs(ctx, val1))
require.Equal(t, 1, len(gk.GetUnsignedContractCallTxs(ctx, val2)))
}

func TestOrderContractCallsByNonceAscending(t *testing.T) {
t.Run("normal case", func(t *testing.T) {
// Create test contract calls with different nonces
calls := []*types.ContractCallTx{
{InvalidationNonce: 3},
{InvalidationNonce: 1},
{InvalidationNonce: 4},
{InvalidationNonce: 2},
}

// Order the contract calls
orderedCalls := orderContractCallsByNonceAscending(calls)

// Check if the contract calls are ordered correctly
assert.Equal(t, uint64(1), orderedCalls[0].InvalidationNonce)
assert.Equal(t, uint64(2), orderedCalls[1].InvalidationNonce)
assert.Equal(t, uint64(3), orderedCalls[2].InvalidationNonce)
assert.Equal(t, uint64(4), orderedCalls[3].InvalidationNonce)

// Check if the length of the slice remains the same
assert.Equal(t, len(calls), len(orderedCalls))
})

t.Run("empty slice", func(t *testing.T) {
calls := []*types.ContractCallTx{}
orderedCalls := orderContractCallsByNonceAscending(calls)
assert.Empty(t, orderedCalls)
})

t.Run("nil slice", func(t *testing.T) {
var calls []*types.ContractCallTx
orderedCalls := orderContractCallsByNonceAscending(calls)
assert.Nil(t, orderedCalls)
})

t.Run("single element", func(t *testing.T) {
calls := []*types.ContractCallTx{{InvalidationNonce: 1}}
orderedCalls := orderContractCallsByNonceAscending(calls)
assert.Equal(t, 1, len(orderedCalls))
assert.Equal(t, uint64(1), orderedCalls[0].InvalidationNonce)
})

t.Run("duplicate nonces", func(t *testing.T) {
calls := []*types.ContractCallTx{
{InvalidationNonce: 2},
{InvalidationNonce: 1},
{InvalidationNonce: 2},
{InvalidationNonce: 1},
}
orderedCalls := orderContractCallsByNonceAscending(calls)
assert.Equal(t, 4, len(orderedCalls))
assert.Equal(t, uint64(1), orderedCalls[0].InvalidationNonce)
assert.Equal(t, uint64(1), orderedCalls[1].InvalidationNonce)
assert.Equal(t, uint64(2), orderedCalls[2].InvalidationNonce)
assert.Equal(t, uint64(2), orderedCalls[3].InvalidationNonce)
})
}
58 changes: 58 additions & 0 deletions module/x/gravity/keeper/signer_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,61 @@ func TestGetUnconfirmedSignerSetTxs(t *testing.T) {
require.Empty(t, gk.GetUnsignedSignerSetTxs(ctx, val1))
require.Equal(t, 1, len(gk.GetUnsignedSignerSetTxs(ctx, val2)))
}

func TestOrderSignerSetsByNonceAscending(t *testing.T) {
t.Run("normal case", func(t *testing.T) {
// Create test signer sets with different nonces
signerSets := []*types.SignerSetTx{
{Nonce: 3},
{Nonce: 1},
{Nonce: 4},
{Nonce: 2},
}

// Order the signer sets
orderedSignerSets := orderSignerSetsByNonceAscending(signerSets)

// Check if the signer sets are ordered correctly
assert.Equal(t, uint64(1), orderedSignerSets[0].Nonce)
assert.Equal(t, uint64(2), orderedSignerSets[1].Nonce)
assert.Equal(t, uint64(3), orderedSignerSets[2].Nonce)
assert.Equal(t, uint64(4), orderedSignerSets[3].Nonce)

// Check if the length of the slice remains the same
assert.Equal(t, len(signerSets), len(orderedSignerSets))
})

t.Run("empty slice", func(t *testing.T) {
signerSets := []*types.SignerSetTx{}
orderedSignerSets := orderSignerSetsByNonceAscending(signerSets)
assert.Empty(t, orderedSignerSets)
})

t.Run("nil slice", func(t *testing.T) {
var signerSets []*types.SignerSetTx
orderedSignerSets := orderSignerSetsByNonceAscending(signerSets)
assert.Nil(t, orderedSignerSets)
})

t.Run("single element", func(t *testing.T) {
signerSets := []*types.SignerSetTx{{Nonce: 1}}
orderedSignerSets := orderSignerSetsByNonceAscending(signerSets)
assert.Equal(t, 1, len(orderedSignerSets))
assert.Equal(t, uint64(1), orderedSignerSets[0].Nonce)
})

t.Run("duplicate nonces", func(t *testing.T) {
signerSets := []*types.SignerSetTx{
{Nonce: 2},
{Nonce: 1},
{Nonce: 2},
{Nonce: 1},
}
orderedSignerSets := orderSignerSetsByNonceAscending(signerSets)
assert.Equal(t, 4, len(orderedSignerSets))
assert.Equal(t, uint64(1), orderedSignerSets[0].Nonce)
assert.Equal(t, uint64(1), orderedSignerSets[1].Nonce)
assert.Equal(t, uint64(2), orderedSignerSets[2].Nonce)
assert.Equal(t, uint64(2), orderedSignerSets[3].Nonce)
})
}

0 comments on commit 2b6ebe2

Please sign in to comment.