-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_util.go
135 lines (112 loc) · 3.35 KB
/
test_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Common testing types and utility functions and methods to be used in unit and
integration testing of the evidence module.
*/
// DONTCOVER
package types
import (
"bytes"
"errors"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zenchainprotocol/zenchain-node/x/evidence/exported"
"gopkg.in/yaml.v2"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/tmhash"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
)
var (
_ exported.Evidence = (*TestEquivocationEvidence)(nil)
TestingCdc = codec.New()
)
const (
TestEvidenceRouteEquivocation = "TestEquivocationEvidence"
TestEvidenceTypeEquivocation = "equivocation"
)
type (
TestVote struct {
Height int64
Round int64
Timestamp time.Time
ValidatorAddress tmbytes.HexBytes
Signature []byte
}
TestCanonicalVote struct {
Height int64
Round int64
Timestamp time.Time
ChainID string
}
TestEquivocationEvidence struct {
Power int64
TotalPower int64
PubKey crypto.PubKey
VoteA TestVote
VoteB TestVote
}
)
func init() {
RegisterCodec(TestingCdc)
codec.RegisterCrypto(TestingCdc)
TestingCdc.RegisterConcrete(TestEquivocationEvidence{}, "test/TestEquivocationEvidence", nil)
}
func (e TestEquivocationEvidence) Route() string { return TestEvidenceRouteEquivocation }
func (e TestEquivocationEvidence) Type() string { return TestEvidenceTypeEquivocation }
func (e TestEquivocationEvidence) GetHeight() int64 { return e.VoteA.Height }
func (e TestEquivocationEvidence) GetValidatorPower() int64 { return e.Power }
func (e TestEquivocationEvidence) GetTotalPower() int64 { return e.TotalPower }
func (e TestEquivocationEvidence) String() string {
bz, _ := yaml.Marshal(e)
return string(bz)
}
func (e TestEquivocationEvidence) GetConsensusAddress() sdk.ConsAddress {
return sdk.ConsAddress(e.PubKey.Address())
}
func (e TestEquivocationEvidence) ValidateBasic() error {
if e.VoteA.Height != e.VoteB.Height ||
e.VoteA.Round != e.VoteB.Round {
return fmt.Errorf("H/R/S does not match (got %v and %v)", e.VoteA, e.VoteB)
}
if !bytes.Equal(e.VoteA.ValidatorAddress, e.VoteB.ValidatorAddress) {
return fmt.Errorf(
"validator addresses do not match (got %X and %X)",
e.VoteA.ValidatorAddress,
e.VoteB.ValidatorAddress,
)
}
return nil
}
func (e TestEquivocationEvidence) Hash() tmbytes.HexBytes {
return tmhash.Sum(TestingCdc.MustMarshalBinaryBare(e))
}
func (v TestVote) SignBytes(chainID string) []byte {
scv := TestCanonicalVote{
Height: v.Height,
Round: v.Round,
Timestamp: v.Timestamp,
ChainID: chainID,
}
bz, _ := TestingCdc.MarshalBinaryLengthPrefixed(scv)
return bz
}
func TestEquivocationHandler(k interface{}) Handler {
return func(ctx sdk.Context, e exported.Evidence) error {
if err := e.ValidateBasic(); err != nil {
return err
}
ee, ok := e.(TestEquivocationEvidence)
if !ok {
return fmt.Errorf("unexpected evidence type: %T", e)
}
if !ee.PubKey.VerifyBytes(ee.VoteA.SignBytes(ctx.ChainID()), ee.VoteA.Signature) {
return errors.New("failed to verify vote A signature")
}
if !ee.PubKey.VerifyBytes(ee.VoteB.SignBytes(ctx.ChainID()), ee.VoteB.Signature) {
return errors.New("failed to verify vote B signature")
}
// TODO: Slashing!
return nil
}
}