mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-05 15:47:07 +00:00
abci: VoteInfo, ValidatorUpdate. See ADR-018
This commit is contained in:
+17
-24
@@ -1,7 +1,6 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
@@ -56,7 +55,7 @@ func (tm2pb) Header(header *Header) abci.Header {
|
||||
}
|
||||
}
|
||||
|
||||
func (tm2pb) ValidatorWithoutPubKey(val *Validator) abci.Validator {
|
||||
func (tm2pb) Validator(val *Validator) abci.Validator {
|
||||
return abci.Validator{
|
||||
Address: val.PubKey.Address(),
|
||||
Power: val.VotingPower,
|
||||
@@ -78,11 +77,10 @@ func (tm2pb) PartSetHeader(header PartSetHeader) abci.PartSetHeader {
|
||||
}
|
||||
|
||||
// XXX: panics on unknown pubkey type
|
||||
func (tm2pb) Validator(val *Validator) abci.Validator {
|
||||
return abci.Validator{
|
||||
Address: val.PubKey.Address(),
|
||||
PubKey: TM2PB.PubKey(val.PubKey),
|
||||
Power: val.VotingPower,
|
||||
func (tm2pb) ValidatorUpdate(val *Validator) abci.ValidatorUpdate {
|
||||
return abci.ValidatorUpdate{
|
||||
PubKey: TM2PB.PubKey(val.PubKey),
|
||||
Power: val.VotingPower,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,10 +104,10 @@ func (tm2pb) PubKey(pubKey crypto.PubKey) abci.PubKey {
|
||||
}
|
||||
|
||||
// XXX: panics on nil or unknown pubkey type
|
||||
func (tm2pb) Validators(vals *ValidatorSet) []abci.Validator {
|
||||
validators := make([]abci.Validator, vals.Size())
|
||||
func (tm2pb) ValidatorUpdates(vals *ValidatorSet) []abci.ValidatorUpdate {
|
||||
validators := make([]abci.ValidatorUpdate, vals.Size())
|
||||
for i, val := range vals.Validators {
|
||||
validators[i] = TM2PB.Validator(val)
|
||||
validators[i] = TM2PB.ValidatorUpdate(val)
|
||||
}
|
||||
return validators
|
||||
}
|
||||
@@ -156,7 +154,7 @@ func (tm2pb) Evidence(ev Evidence, valSet *ValidatorSet, evTime time.Time) abci.
|
||||
|
||||
return abci.Evidence{
|
||||
Type: evType,
|
||||
Validator: TM2PB.ValidatorWithoutPubKey(val),
|
||||
Validator: TM2PB.Validator(val),
|
||||
Height: ev.Height(),
|
||||
Time: evTime,
|
||||
TotalVotingPower: valSet.TotalVotingPower(),
|
||||
@@ -164,12 +162,11 @@ func (tm2pb) Evidence(ev Evidence, valSet *ValidatorSet, evTime time.Time) abci.
|
||||
}
|
||||
|
||||
// XXX: panics on nil or unknown pubkey type
|
||||
func (tm2pb) ValidatorFromPubKeyAndPower(pubkey crypto.PubKey, power int64) abci.Validator {
|
||||
func (tm2pb) NewValidatorUpdate(pubkey crypto.PubKey, power int64) abci.ValidatorUpdate {
|
||||
pubkeyABCI := TM2PB.PubKey(pubkey)
|
||||
return abci.Validator{
|
||||
Address: pubkey.Address(),
|
||||
PubKey: pubkeyABCI,
|
||||
Power: power,
|
||||
return abci.ValidatorUpdate{
|
||||
PubKey: pubkeyABCI,
|
||||
Power: power,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +202,7 @@ func (pb2tm) PubKey(pubKey abci.PubKey) (crypto.PubKey, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (pb2tm) Validators(vals []abci.Validator) ([]*Validator, error) {
|
||||
func (pb2tm) ValidatorUpdates(vals []abci.ValidatorUpdate) ([]*Validator, error) {
|
||||
tmVals := make([]*Validator, len(vals))
|
||||
for i, v := range vals {
|
||||
pub, err := PB2TM.PubKey(v.PubKey)
|
||||
@@ -214,17 +211,13 @@ func (pb2tm) Validators(vals []abci.Validator) ([]*Validator, error) {
|
||||
}
|
||||
// If the app provided an address too, it must match.
|
||||
// This is just a sanity check.
|
||||
if len(v.Address) > 0 {
|
||||
/*if len(v.Address) > 0 {
|
||||
if !bytes.Equal(pub.Address(), v.Address) {
|
||||
return nil, fmt.Errorf("Validator.Address (%X) does not match PubKey.Address (%X)",
|
||||
v.Address, pub.Address())
|
||||
}
|
||||
}
|
||||
tmVals[i] = &Validator{
|
||||
Address: pub.Address(),
|
||||
PubKey: pub,
|
||||
VotingPower: v.Power,
|
||||
}
|
||||
}*/
|
||||
tmVals[i] = NewValidator(pub, v.Power)
|
||||
}
|
||||
return tmVals, nil
|
||||
}
|
||||
|
||||
+14
-17
@@ -41,26 +41,26 @@ func TestABCIValidators(t *testing.T) {
|
||||
VotingPower: 10,
|
||||
}
|
||||
|
||||
abciVal := TM2PB.Validator(tmVal)
|
||||
tmVals, err := PB2TM.Validators([]abci.Validator{abciVal})
|
||||
abciVal := TM2PB.ValidatorUpdate(tmVal)
|
||||
tmVals, err := PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, tmValExpected, tmVals[0])
|
||||
|
||||
abciVals := TM2PB.Validators(NewValidatorSet(tmVals))
|
||||
assert.Equal(t, []abci.Validator{abciVal}, abciVals)
|
||||
abciVals := TM2PB.ValidatorUpdates(NewValidatorSet(tmVals))
|
||||
assert.Equal(t, []abci.ValidatorUpdate{abciVal}, abciVals)
|
||||
|
||||
// val with address
|
||||
tmVal.Address = pkEd.Address()
|
||||
|
||||
abciVal = TM2PB.Validator(tmVal)
|
||||
tmVals, err = PB2TM.Validators([]abci.Validator{abciVal})
|
||||
abciVal = TM2PB.ValidatorUpdate(tmVal)
|
||||
tmVals, err = PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, tmValExpected, tmVals[0])
|
||||
|
||||
// val with incorrect address
|
||||
abciVal = TM2PB.Validator(tmVal)
|
||||
abciVal.Address = []byte("incorrect!")
|
||||
tmVals, err = PB2TM.Validators([]abci.Validator{abciVal})
|
||||
// val with incorrect pubkey daya
|
||||
abciVal = TM2PB.ValidatorUpdate(tmVal)
|
||||
abciVal.PubKey.Data = []byte("incorrect!")
|
||||
tmVals, err = PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal})
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, tmVals)
|
||||
}
|
||||
@@ -104,9 +104,6 @@ func TestABCIEvidence(t *testing.T) {
|
||||
)
|
||||
|
||||
assert.Equal(t, "duplicate/vote", abciEv.Type)
|
||||
|
||||
// test we do not send pubkeys
|
||||
assert.Empty(t, abciEv.Validator.PubKey)
|
||||
}
|
||||
|
||||
type pubKeyEddie struct{}
|
||||
@@ -119,17 +116,17 @@ func (pubKeyEddie) Equals(crypto.PubKey) bool { return false }
|
||||
func TestABCIValidatorFromPubKeyAndPower(t *testing.T) {
|
||||
pubkey := ed25519.GenPrivKey().PubKey()
|
||||
|
||||
abciVal := TM2PB.ValidatorFromPubKeyAndPower(pubkey, 10)
|
||||
abciVal := TM2PB.NewValidatorUpdate(pubkey, 10)
|
||||
assert.Equal(t, int64(10), abciVal.Power)
|
||||
|
||||
assert.Panics(t, func() { TM2PB.ValidatorFromPubKeyAndPower(nil, 10) })
|
||||
assert.Panics(t, func() { TM2PB.ValidatorFromPubKeyAndPower(pubKeyEddie{}, 10) })
|
||||
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()
|
||||
|
||||
abciVal := TM2PB.ValidatorWithoutPubKey(&Validator{
|
||||
abciVal := TM2PB.Validator(&Validator{
|
||||
Address: pkEd.Address(),
|
||||
PubKey: pkEd,
|
||||
VotingPower: 10,
|
||||
|
||||
Reference in New Issue
Block a user