Skip to content

Commit

Permalink
Extend kvstore example add with with key types (#1876)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5633886)

# Conflicts:
#	abci/example/kvstore/kvstore.go
  • Loading branch information
sergio-mena authored and mergify[bot] committed Jan 5, 2024
1 parent 907a3c1 commit 70dd54b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
5 changes: 3 additions & 2 deletions abci/example/kvstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ The app has no replay protection (other than what the mempool provides).
Validator set changes are effected using the following transaction format:

```md
"val:pubkey1!power1,pubkey2!power2,pubkey3!power3"
"val:pubkeytype1!pubkey1!power1,pubkeytype2!pubkey2!power2,pubkeytype3!pubkey3!power3"
```

where `pubkeyN` is a base64-encoded 32-byte ed25519 key and `powerN` is a new voting power for the validator with `pubkeyN` (possibly a new one).
where `pubkeyN` is a base64-encoded 32-byte key, `pubkeytypeN` is a string representing the key type,
and `powerN` is a new voting power for the validator with `pubkeyN` (possibly a new one).
To remove a validator from the validator set, set power to `0`.
There is no sybil protection against new validators joining.
3 changes: 2 additions & 1 deletion abci/example/kvstore/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ func MakeValSetChangeTx(pubkey crypto.PublicKey, power int64) []byte {
panic(err)
}
pubStr := base64.StdEncoding.EncodeToString(pk.Bytes())
return []byte(fmt.Sprintf("%s%s!%d", ValidatorPrefix, pubStr, power))
pubTypeStr := pk.Type()
return []byte(fmt.Sprintf("%s%s!%s!%d", ValidatorPrefix, pubTypeStr, pubStr, power))
}
28 changes: 17 additions & 11 deletions abci/example/kvstore/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,14 @@ func (app *Application) InitChain(_ context.Context, req *types.RequestInitChain
func (app *Application) CheckTx(_ context.Context, req *types.RequestCheckTx) (*types.ResponseCheckTx, error) {
// If it is a validator update transaction, check that it is correctly formatted
if isValidatorTx(req.Tx) {
<<<<<<< HEAD

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected statement, found '<<'

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (arm, linux)

syntax error: unexpected <<, expected }

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (amd64, linux)

syntax error: unexpected <<, expected }

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected <<, expected }

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected <<, expected }

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (04)

syntax error: unexpected <<, expected }

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected <<, expected }

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected <<, expected }

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected <<, expected }

Check failure on line 133 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (05)

syntax error: unexpected <<, expected }
if _, _, err := parseValidatorTx(req.Tx); err != nil {
return &types.ResponseCheckTx{Code: CodeTypeInvalidTxFormat}, nil
=======

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected statement, found '=='

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (arm, linux)

syntax error: unexpected ==, expected }

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (amd64, linux)

syntax error: unexpected ==, expected }

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected ==, expected }

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected ==, expected }

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (04)

syntax error: unexpected ==, expected }

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected ==, expected }

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected ==, expected }

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected ==, expected }

Check failure on line 136 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (05)

syntax error: unexpected ==, expected }
if _, _, _, err := parseValidatorTx(req.Tx); err != nil {
//nolint:nilerr
return &types.CheckTxResponse{Code: CodeTypeInvalidTxFormat}, nil
>>>>>>> 5633886a7 (Extend kvstore example add with with key types (#1876))

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected statement, found '>>'

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / govulncheck

illegal character U+0023 '#'

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (arm, linux)

syntax error: unexpected >>, expected }

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (arm, linux)

invalid character U+0023 '#'

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (amd64, linux)

syntax error: unexpected >>, expected }

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (amd64, linux)

invalid character U+0023 '#'

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected >>, expected }

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (01)

invalid character U+0023 '#'

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected >>, expected }

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (03)

invalid character U+0023 '#'

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (04)

syntax error: unexpected >>, expected }

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (04)

invalid character U+0023 '#'

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected >>, expected }

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (00)

invalid character U+0023 '#'

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected >>, expected }

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / golangci-lint

invalid character U+0023 '#'

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected >>, expected }

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (05)

syntax error: unexpected >>, expected }

Check failure on line 140 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (05)

invalid character U+0023 '#'
}
} else if !isValidTx(req.Tx) {

Check failure on line 142 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (arm, linux)

syntax error: non-declaration statement outside function body

Check failure on line 142 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / Build (amd64, linux)

syntax error: non-declaration statement outside function body

Check failure on line 142 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: non-declaration statement outside function body

Check failure on line 142 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: non-declaration statement outside function body

Check failure on line 142 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (04)

syntax error: non-declaration statement outside function body

Check failure on line 142 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: non-declaration statement outside function body

Check failure on line 142 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: non-declaration statement outside function body) (typecheck)

Check failure on line 142 in abci/example/kvstore/kvstore.go

View workflow job for this annotation

GitHub Actions / tests (05)

syntax error: non-declaration statement outside function body
return &types.ResponseCheckTx{Code: CodeTypeInvalidTxFormat}, nil
Expand Down Expand Up @@ -217,11 +223,11 @@ func (app *Application) FinalizeBlock(_ context.Context, req *types.RequestFinal
respTxs := make([]*types.ExecTxResult, len(req.Txs))
for i, tx := range req.Txs {
if isValidatorTx(tx) {
pubKey, power, err := parseValidatorTx(tx)
keyType, pubKey, power, err := parseValidatorTx(tx)
if err != nil {
panic(err)
}
app.valUpdates = append(app.valUpdates, types.UpdateValidator(pubKey, power, ""))
app.valUpdates = append(app.valUpdates, types.UpdateValidator(pubKey, power, keyType))
} else {
app.stagedTxs = append(app.stagedTxs, tx)
}
Expand Down Expand Up @@ -413,33 +419,33 @@ func isValidatorTx(tx []byte) bool {
return strings.HasPrefix(string(tx), ValidatorPrefix)
}

func parseValidatorTx(tx []byte) ([]byte, int64, error) {
func parseValidatorTx(tx []byte) (string, []byte, int64, error) {
tx = tx[len(ValidatorPrefix):]

// 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)
typeKeyAndPower := strings.Split(string(tx), "!")
if len(typeKeyAndPower) != 3 {
return "", nil, 0, fmt.Errorf("expected 'pubkeytype!pubkey!power'. Got %v", typeKeyAndPower)
}
pubkeyS, powerS := pubKeyAndPower[0], pubKeyAndPower[1]
keytype, pubkeyS, powerS := typeKeyAndPower[0], typeKeyAndPower[1], typeKeyAndPower[2]

// 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
return keytype, pubkey, power, nil
}

// add, update, or remove a validator
Expand Down

0 comments on commit 70dd54b

Please sign in to comment.