continue wading through tests

This commit is contained in:
Callum Waters
2022-09-29 15:56:33 +02:00
parent 178ea47b56
commit b5fa9b11d2
11 changed files with 122 additions and 126 deletions
+3 -4
View File
@@ -8,7 +8,6 @@ import (
"github.com/tendermint/tendermint/abci/types"
cryptoencoding "github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/libs/rand"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/proto/tendermint/crypto"
)
@@ -53,7 +52,7 @@ 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))
return NewTx(tmrand.Str(2), tmrand.Str(size-3))
}
func NewRandomTxs(n int) [][]byte {
@@ -64,8 +63,8 @@ func NewRandomTxs(n int) [][]byte {
return txs
}
func NewTxFromId(i int) []byte {
return []byte(fmt.Sprintf("%d=%d", i))
func NewTxFromID(i int) []byte {
return []byte(fmt.Sprintf("%d=%d", i, i))
}
// Create a transaction to add/remove/update a validator
+10 -11
View File
@@ -172,16 +172,15 @@ func (app *Application) substPrepareTx(blockData [][]byte) [][]byte {
// - if key is `val` that the validator update transaction is also valid
func (app *Application) ProcessProposal(_ context.Context, req *types.RequestProcessProposal) (*types.ResponseProcessProposal, error) {
for _, tx := range req.Txs {
if len(tx) < 3 ||
bytes.Count(tx, []byte("=")) != 1 ||
bytes.HasPrefix(tx, []byte("=")) ||
bytes.HasSuffix(tx, []byte("=")) {
return &types.ResponseProcessProposal{Status: types.ResponseProcessProposal_REJECT}, nil
}
if isValidatorTx(tx) {
if _, _, err := parseValidatorTx(tx); err != nil {
return &types.ResponseProcessProposal{Status: types.ResponseProcessProposal_REJECT}, nil
}
} else if len(tx) < 3 ||
bytes.Count(tx, []byte("=")) != 1 ||
bytes.HasPrefix(tx, []byte("=")) ||
bytes.HasSuffix(tx, []byte("=")) {
return &types.ResponseProcessProposal{Status: types.ResponseProcessProposal_REJECT}, nil
}
}
return &types.ResponseProcessProposal{Status: types.ResponseProcessProposal_ACCEPT}, nil
@@ -345,24 +344,24 @@ func parseValidatorTx(tx []byte) ([]byte, int64, error) {
// get the pubkey and power
pubKeyAndPower := strings.Split(string(tx), "!")
if len(pubKeyAndPower) != 2 {
return nil, 0, fmt.Errorf("Expected 'pubkey!power'. Got %v", pubKeyAndPower)
return nil, 0, fmt.Errorf("expected 'pubkey!power'. Got %v", pubKeyAndPower)
}
pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1]
// decode the pubkey
pubkey, err := base64.StdEncoding.DecodeString(pubkeyS)
if err != nil {
return nil, 0, fmt.Errorf("Pubkey (%s) is invalid base64", pubkeyS)
return nil, 0, fmt.Errorf("pubkey (%s) is invalid base64", pubkeyS)
}
// decode the power
power, err := strconv.ParseInt(powerS, 10, 64)
if err != nil {
return nil, 0, fmt.Errorf("Power (%s) is not an int", powerS)
return nil, 0, fmt.Errorf("power (%s) is not an int", powerS)
}
if power < 0 {
return nil, 0, fmt.Errorf("Power can not be less than 0, got %d", power)
return nil, 0, fmt.Errorf("power can not be less than 0, got %d", power)
}
return pubkey, power, nil
@@ -464,7 +463,7 @@ func saveState(state State) {
// Hash returns the hash of the application state. This is computed
// as the size or number of transactions processed within the state. Note that this isn't
// a strong gaurantee of state machine replication because states could
// a strong guarantee of state machine replication because states could
// have different kv values but still have the same size.
func (s State) Hash() []byte {
appHash := make([]byte, 8)
+3 -2
View File
@@ -38,6 +38,7 @@ func testKVStore(ctx context.Context, t *testing.T, app types.Application, tx []
require.Equal(t, uint32(0), checkTxResp.Code)
ppResp, err := app.PrepareProposal(ctx, &types.RequestPrepareProposal{Txs: [][]byte{tx}})
require.NoError(t, err)
require.Len(t, ppResp.Txs, 1)
req := &types.RequestFinalizeBlock{Height: 1, Txs: ppResp.Txs}
ar, err := app.FinalizeBlock(ctx, req)
@@ -343,7 +344,7 @@ func runClientTests(ctx context.Context, t *testing.T, client abcicli.Client) {
testKVStore(ctx, t, client, tx, testKey, testValue)
}
func TestTxGeneration(t *testing.T) {
func TestTxGeneration(t *testing.T) {
require.Len(t, NewRandomTx(20), 20)
require.Len(t, NewRandomTxs(10), 10)
}
}