Browse Source

types: cleanup protobuf.go (#6023)

## Description

- remove unused functions
- remove a function used in tests.

Closes: #XXX
pull/6029/head
Marko 4 years ago
committed by GitHub
parent
commit
2a2279e010
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 94 deletions
  1. +2
    -0
      CHANGELOG_PENDING.md
  2. +16
    -3
      state/helpers_test.go
  3. +4
    -4
      state/state_test.go
  4. +10
    -0
      types/params.go
  5. +0
    -68
      types/protobuf.go
  6. +0
    -19
      types/protobuf_test.go

+ 2
- 0
CHANGELOG_PENDING.md View File

@ -30,6 +30,8 @@ Friendly reminder, we have a [bug bounty program](https://hackerone.com/tendermi
- [libs/os] Kill() and {Must,}{Read,Write}File() functions have been removed. (@alessio)
- [store] \#5848 Remove block store state in favor of using the db iterators directly (@cmwaters)
- [state] \#5864 Use an iterator when pruning state (@cmwaters)
- [types] \#6023 Remove `tm2pb.Header`, `tm2pb.BlockID`, `tm2pb.PartSetHeader` and `tm2pb.NewValidatorUpdate`.
- Each of the above types has a `ToProto` and `FromProto` method or function which replaced this logic.
- [rpc/client/http] \#6022 Change `timeout` type to `time.Duration` in `NewWithTimeout`
- Blockchain Protocol


+ 16
- 3
state/helpers_test.go View File

@ -10,6 +10,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@ -163,10 +164,18 @@ func makeHeaderPartsResponsesValPubKeyChange(
// If the pubkey is new, remove the old and add the new.
_, val := state.NextValidators.GetByIndex(0)
if !bytes.Equal(pubkey.Bytes(), val.PubKey.Bytes()) {
vPbPk, err := cryptoenc.PubKeyToProto(val.PubKey)
if err != nil {
panic(err)
}
pbPk, err := cryptoenc.PubKeyToProto(pubkey)
if err != nil {
panic(err)
}
abciResponses.EndBlock = &abci.ResponseEndBlock{
ValidatorUpdates: []abci.ValidatorUpdate{
types.TM2PB.NewValidatorUpdate(val.PubKey, 0),
types.TM2PB.NewValidatorUpdate(pubkey, 10),
{PubKey: vPbPk, Power: 0},
{PubKey: pbPk, Power: 10},
},
}
}
@ -188,9 +197,13 @@ func makeHeaderPartsResponsesValPowerChange(
// If the pubkey is new, remove the old and add the new.
_, val := state.NextValidators.GetByIndex(0)
if val.VotingPower != power {
vPbPk, err := cryptoenc.PubKeyToProto(val.PubKey)
if err != nil {
panic(err)
}
abciResponses.EndBlock = &abci.ResponseEndBlock{
ValidatorUpdates: []abci.ValidatorUpdate{
types.TM2PB.NewValidatorUpdate(val.PubKey, power),
{PubKey: vPbPk, Power: power},
},
}
}


+ 4
- 4
state/state_test.go View File

@ -108,11 +108,11 @@ func TestABCIResponsesSaveLoad1(t *testing.T) {
abciResponses.DeliverTxs[0] = &abci.ResponseDeliverTx{Data: []byte("foo"), Events: nil}
abciResponses.DeliverTxs[1] = &abci.ResponseDeliverTx{Data: []byte("bar"), Log: "ok", Events: nil}
abciResponses.EndBlock = &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{
types.TM2PB.NewValidatorUpdate(ed25519.GenPrivKey().PubKey(), 10),
}}
pbpk, err := cryptoenc.PubKeyToProto(ed25519.GenPrivKey().PubKey())
require.NoError(t, err)
abciResponses.EndBlock = &abci.ResponseEndBlock{ValidatorUpdates: []abci.ValidatorUpdate{{PubKey: pbpk, Power: 10}}}
err := stateStore.SaveABCIResponses(block.Height, abciResponses)
err = stateStore.SaveABCIResponses(block.Height, abciResponses)
require.NoError(t, err)
loadedABCIResponses, err := stateStore.LoadABCIResponses(block.Height)
assert.Nil(err)


+ 10
- 0
types/params.go View File

@ -5,6 +5,8 @@ import (
"fmt"
"time"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/tmhash"
tmstrings "github.com/tendermint/tendermint/libs/strings"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@ -19,8 +21,16 @@ const (
// MaxBlockPartsCount is the maximum number of block parts.
MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1
ABCIPubKeyTypeEd25519 = ed25519.KeyType
ABCIPubKeyTypeSecp256k1 = secp256k1.KeyType
)
var ABCIPubKeyTypesToNames = map[string]string{
ABCIPubKeyTypeEd25519: ed25519.PubKeyName,
ABCIPubKeyTypeSecp256k1: secp256k1.PubKeyName,
}
// ConsensusParams contains consensus critical parameters that determine the
// validity of blocks.
type ConsensusParams struct {


+ 0
- 68
types/protobuf.go View File

@ -2,28 +2,9 @@ package types
import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/crypto/secp256k1"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
//-------------------------------------------------------
// Use strings to distinguish types in ABCI messages
const (
ABCIPubKeyTypeEd25519 = ed25519.KeyType
ABCIPubKeyTypeSecp256k1 = secp256k1.KeyType
)
// TODO: Make non-global by allowing for registration of more pubkey types
var ABCIPubKeyTypesToNames = map[string]string{
ABCIPubKeyTypeEd25519: ed25519.PubKeyName,
ABCIPubKeyTypeSecp256k1: secp256k1.PubKeyName,
}
//-------------------------------------------------------
// TM2PB is used for converting Tendermint ABCI to protobuf ABCI.
@ -32,29 +13,6 @@ var TM2PB = tm2pb{}
type tm2pb struct{}
func (tm2pb) Header(header *Header) tmproto.Header {
return tmproto.Header{
Version: header.Version.ToProto(),
ChainID: header.ChainID,
Height: header.Height,
Time: header.Time,
LastBlockId: header.LastBlockID.ToProto(),
LastCommitHash: header.LastCommitHash,
DataHash: header.DataHash,
ValidatorsHash: header.ValidatorsHash,
NextValidatorsHash: header.NextValidatorsHash,
ConsensusHash: header.ConsensusHash,
AppHash: header.AppHash,
LastResultsHash: header.LastResultsHash,
EvidenceHash: header.EvidenceHash,
ProposerAddress: header.ProposerAddress,
}
}
func (tm2pb) Validator(val *Validator) abci.Validator {
return abci.Validator{
Address: val.PubKey.Address(),
@ -62,20 +20,6 @@ func (tm2pb) Validator(val *Validator) abci.Validator {
}
}
func (tm2pb) BlockID(blockID BlockID) tmproto.BlockID {
return tmproto.BlockID{
Hash: blockID.Hash,
PartSetHeader: TM2PB.PartSetHeader(blockID.PartSetHeader),
}
}
func (tm2pb) PartSetHeader(header PartSetHeader) tmproto.PartSetHeader {
return tmproto.PartSetHeader{
Total: header.Total,
Hash: header.Hash,
}
}
// XXX: panics on unknown pubkey type
func (tm2pb) ValidatorUpdate(val *Validator) abci.ValidatorUpdate {
pk, err := cryptoenc.PubKeyToProto(val.PubKey)
@ -97,18 +41,6 @@ func (tm2pb) ValidatorUpdates(vals *ValidatorSet) []abci.ValidatorUpdate {
return validators
}
// XXX: panics on nil or unknown pubkey type
func (tm2pb) NewValidatorUpdate(pubkey crypto.PubKey, power int64) abci.ValidatorUpdate {
pubkeyABCI, err := cryptoenc.PubKeyToProto(pubkey)
if err != nil {
panic(err)
}
return abci.ValidatorUpdate{
PubKey: pubkeyABCI,
Power: power,
}
}
//----------------------------------------------------------------------------
// PB2TM is used for converting protobuf ABCI to Tendermint ABCI.


+ 0
- 19
types/protobuf_test.go View File

@ -52,25 +52,6 @@ func TestABCIValidators(t *testing.T) {
assert.Equal(t, tmValExpected, tmVals[0])
}
type pubKeyEddie struct{}
func (pubKeyEddie) Address() Address { return []byte{} }
func (pubKeyEddie) Bytes() []byte { return []byte{} }
func (pubKeyEddie) VerifySignature(msg []byte, sig []byte) bool { return false }
func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
func (pubKeyEddie) String() string { return "" }
func (pubKeyEddie) Type() string { return "pubKeyEddie" }
func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
pubkey := ed25519.GenPrivKey().PubKey()
abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
assert.Equal(t, int64(10), abciVal.Power)
assert.Panics(t, func() { TM2PB.NewValidatorUpdate(nil, 10) })
assert.Panics(t, func() { TM2PB.NewValidatorUpdate(pubKeyEddie{}, 10) })
}
func TestABCIValidatorWithoutPubKey(t *testing.T) {
pkEd := ed25519.GenPrivKey().PubKey()


Loading…
Cancel
Save