/
canonical_json.go
126 lines (108 loc) · 3.71 KB
/
canonical_json.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
package types
// canonical json is go-wire's json for structs with fields in alphabetical order
import (
crypto "github.com/Gessiux/go-crypto"
)
type CanonicalJSONBlockID struct {
Hash []byte `json:"hash,omitempty"`
PartsHeader CanonicalJSONPartSetHeader `json:"parts,omitempty"`
}
type CanonicalJSONPartSetHeader struct {
Hash []byte `json:"hash"`
Total uint64 `json:"total"`
}
type CanonicalJSONProposal struct {
BlockPartsHeader CanonicalJSONPartSetHeader `json:"block_parts_header"`
Height uint64 `json:"height"`
POLBlockID CanonicalJSONBlockID `json:"pol_block_id"`
POLRound int `json:"pol_round"`
Round int `json:"round"`
Hash []byte `json:"hash"`
}
type CanonicalJSONVote struct {
BlockID CanonicalJSONBlockID `json:"block_id"`
Height uint64 `json:"height"`
Round uint64 `json:"round"`
Type byte `json:"type"`
}
type CanonicalJSONSignAggr struct {
Height uint64 `json:"height"`
Round int `json:"round"`
Type byte `json:"type"`
NumValidators int `json:"NumValidators"`
BlockID CanonicalJSONBlockID `json:"block_id"`
Maj23 CanonicalJSONBlockID `json:"maj23"`
Sum int64 `json:"sum"`
}
//------------------------------------
// Messages including a "chain id" can only be applied to one chain, hence "Once"
type CanonicalJSONOnceProposal struct {
ChainID string `json:"chain_id"`
Proposal CanonicalJSONProposal `json:"proposal"`
}
type CanonicalJSONOnceVote struct {
ChainID string `json:"chain_id"`
Vote CanonicalJSONVote `json:"vote"`
}
type CanonicalJSONOnceSignAggr struct {
ChainID string `json:"chain_id"`
SignAggr CanonicalJSONSignAggr `json:"sign_aggr"`
}
//-----------------------------
//author@liaoyd
type CanonicalJSONOnceValidatorMsg struct {
ChainID string `json:"chain_id"`
ValidatorMsg CanonicalJSONValidatorMsg `json:"validator_msg"`
}
type CanonicalJSONValidatorMsg struct {
From string `json:"from"`
Epoch int `json:"epoch"`
ValidatorIndex int `json:"validator_index"`
Key string `json:"key"`
PubKey crypto.PubKey `json:"pub_key"`
Power uint64 `json:"power"`
Action string `json:"action"`
Target string `json:"target"`
}
//-----------------------------------
// Canonicalize the structs
func CanonicalBlockID(blockID BlockID) CanonicalJSONBlockID {
return CanonicalJSONBlockID{
Hash: blockID.Hash,
PartsHeader: CanonicalPartSetHeader(blockID.PartsHeader),
}
}
func CanonicalPartSetHeader(psh PartSetHeader) CanonicalJSONPartSetHeader {
return CanonicalJSONPartSetHeader{
psh.Hash,
psh.Total,
}
}
func CanonicalProposal(proposal *Proposal) CanonicalJSONProposal {
return CanonicalJSONProposal{
BlockPartsHeader: CanonicalPartSetHeader(proposal.BlockPartsHeader),
Height: proposal.Height,
POLBlockID: CanonicalBlockID(proposal.POLBlockID),
POLRound: proposal.POLRound,
Round: proposal.Round,
}
}
func CanonicalVote(vote *Vote) CanonicalJSONVote {
return CanonicalJSONVote{
CanonicalBlockID(vote.BlockID),
vote.Height,
vote.Round,
vote.Type,
}
}
func CanonicalSignAggr(signAggr *SignAggr) CanonicalJSONSignAggr {
return CanonicalJSONSignAggr{
signAggr.Height,
signAggr.Round,
signAggr.Type,
signAggr.NumValidators,
CanonicalBlockID(signAggr.BlockID),
CanonicalBlockID(signAggr.Maj23),
signAggr.Sum,
}
}