clean up mempool tests

This commit is contained in:
Callum Waters
2022-09-29 11:48:39 +02:00
parent 2dfcbcc310
commit e9ce605aa3
6 changed files with 88 additions and 67 deletions
+10 -3
View File
@@ -49,18 +49,25 @@ func NewTx(key, value string) []byte {
return []byte(strings.Join([]string{key, value}, "="))
}
func NewRandomTx() []byte {
return NewTx(rand.Str(5), rand.Str(10))
func NewRandomTx(size int) []byte {
if size < 4 {
panic("random tx size must be greater than 3")
}
return NewTx(rand.Str(2), rand.Str(size - 3))
}
func NewRandomTxs(n int) [][]byte {
txs := make([][]byte, n)
for i := 0; i < n; i++ {
txs[i] = NewRandomTx()
txs[i] = NewRandomTx(10)
}
return txs
}
func NewTxFromId(i int) []byte {
return []byte(fmt.Sprintf("%d=%d", i))
}
// Create a transaction to add/remove/update a validator
// To remove, set power to 0.
func MakeValSetChangeTx(pubkey crypto.PublicKey, power int64) []byte {
+2 -4
View File
@@ -119,15 +119,13 @@ func (app *Application) InitChain(_ context.Context, req *types.RequestInitChain
// As this is called frequently, it's preferably to keep the check as stateless and as quick as possible.
// Here we check that the transaction has the correctly key=value format.
func (app *Application) CheckTx(_ context.Context, req *types.RequestCheckTx) (*types.ResponseCheckTx, error) {
if !isValidTx(req.Tx) {
return &types.ResponseCheckTx{Code: CodeTypeInvalidTxFormat}, nil
}
// If it is a validator update transaction, check that it is correctly formatted
if isValidatorTx(req.Tx) {
if _, _, err := parseValidatorTx(req.Tx); err != nil {
return &types.ResponseCheckTx{Code: CodeTypeInvalidTxFormat}, nil
}
} else if !isValidTx(req.Tx) {
return &types.ResponseCheckTx{Code: CodeTypeInvalidTxFormat}, nil
}
return &types.ResponseCheckTx{Code: CodeTypeOK, GasWanted: 1}, nil
+11
View File
@@ -188,6 +188,8 @@ func TestCheckTx(t *testing.T) {
defer cancel()
kvstore := NewInMemoryApplication()
val := RandVal(1)
testCases := []struct {
expCode uint32
tx []byte
@@ -198,11 +200,15 @@ func TestCheckTx(t *testing.T) {
{CodeTypeInvalidTxFormat, []byte("=hello")},
{CodeTypeInvalidTxFormat, []byte("hello=")},
{CodeTypeOK, []byte("a=b")},
{CodeTypeInvalidTxFormat, []byte("val=hello")},
{CodeTypeInvalidTxFormat, []byte("val=hi!5")},
{CodeTypeOK, MakeValSetChangeTx(val.PubKey, 10)},
}
for idx, tc := range testCases {
resp, err := kvstore.CheckTx(ctx, &types.RequestCheckTx{Tx: tc.tx})
require.NoError(t, err, idx)
fmt.Println(string(tc.tx))
require.Equal(t, tc.expCode, resp.Code, idx)
}
}
@@ -336,3 +342,8 @@ func runClientTests(ctx context.Context, t *testing.T, client abcicli.Client) {
tx = []byte(testKey + "=" + testValue)
testKVStore(ctx, t, client, tx, testKey, testValue)
}
func TestTxGeneration(t *testing.T) {
require.Len(t, NewRandomTx(20), 20)
require.Len(t, NewRandomTxs(10), 10)
}