8000 Extend kvstore example add with with key types (backport #1876) by mergify[bot] · Pull Request #1966 · cometbft/cometbft · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Extend kvstore example add with with key types (backport #1876) #1966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
25 changes: 13 additions & 12 deletions abci/example/kvstore/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ 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) {
if _, _, err := parseValidatorTx(req.Tx); err != nil {
if _, _, _, err := parseValidatorTx(req.Tx); err != nil {
//nolint:nilerr
return &types.ResponseCheckTx{Code: CodeTypeInvalidTxFormat}, nil
}
} else if !isValidTx(req.Tx) {
Expand Down Expand Up @@ -217,11 +218,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 +414,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