evidence: migrate reactor to proto (#4949)

## Description

migration of evidence reactor to proto

Closes: #XXX
This commit is contained in:
Marko
2020-06-09 09:54:47 +02:00
committed by GitHub
parent 6ec58f1560
commit 99985278d4
13 changed files with 336 additions and 219 deletions

View File

@@ -10,6 +10,7 @@ import (
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"
tmjson "github.com/tendermint/tendermint/libs/json"
@@ -1257,6 +1258,59 @@ func (e ProofOfLockChange) String() string {
e.Votes[0].Round)
}
func (e *ProofOfLockChange) ToProto() (*tmproto.ProofOfLockChange, error) {
if e == nil {
return nil, errors.New("nil proof of lock change")
}
plc := new(tmproto.ProofOfLockChange)
vpb := make([]*tmproto.Vote, len(e.Votes))
for i, v := range e.Votes {
pb := v.ToProto()
if pb != nil {
vpb[i] = pb
}
}
pk, err := cryptoenc.PubKeyToProto(e.PubKey)
if err != nil {
return nil, err
}
plc.PubKey = &pk
plc.Votes = vpb
return plc, nil
}
func ProofOfLockChangeFromProto(pb *tmproto.ProofOfLockChange) (*ProofOfLockChange, error) {
if pb == nil {
return nil, errors.New("nil proof of lock change")
}
plc := new(ProofOfLockChange)
vpb := make([]Vote, len(pb.Votes))
for i, v := range pb.Votes {
vi, err := VoteFromProto(v)
if err != nil {
return nil, err
}
vpb[i] = *vi
}
if pb.PubKey == nil {
return nil, errors.New("proofOfLockChange: nil PubKey")
}
pk, err := cryptoenc.PubKeyFromProto(*pb.PubKey)
if err != nil {
return nil, err
}
plc.PubKey = pk
plc.Votes = vpb
return plc, nil
}
//-----------------------------------------------------------------
// UNSTABLE
@@ -1297,7 +1351,8 @@ func NewMockEvidence(height int64, eTime time.Time, address []byte) MockEvidence
return MockEvidence{
EvidenceHeight: height,
EvidenceTime: eTime,
EvidenceAddress: address}
EvidenceAddress: address,
}
}
func (e MockEvidence) Height() int64 { return e.EvidenceHeight }

View File

@@ -573,3 +573,42 @@ func TestEvidenceProto(t *testing.T) {
})
}
}
func TestProofOfLockChangeProtoBuf(t *testing.T) {
// -------- Votes --------
val := NewMockPV()
val2 := NewMockPV()
val3 := NewMockPV()
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
const chainID = "mychain"
v := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 1, 0x01, blockID, defaultVoteTime)
v2 := makeVote(t, val2, chainID, math.MaxInt32, math.MaxInt64, 1, 0x01, blockID, defaultVoteTime)
testCases := []struct {
msg string
polc ProofOfLockChange
expErr bool
expErr2 bool
}{
{"failure, empty key", ProofOfLockChange{Votes: []Vote{*v, *v2}}, true, true},
{"failure empty ProofOfLockChange", ProofOfLockChange{}, true, true},
{"success", ProofOfLockChange{Votes: []Vote{*v, *v2}, PubKey: val3.PrivKey.PubKey()}, false, false},
}
for _, tc := range testCases {
tc := tc
pbpolc, err := tc.polc.ToProto()
if tc.expErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
c, err := ProofOfLockChangeFromProto(pbpolc)
if !tc.expErr2 {
require.NoError(t, err, tc.msg)
require.Equal(t, &tc.polc, c, tc.msg)
} else {
require.Error(t, err, tc.msg)
}
}
}