-
Notifications
You must be signed in to change notification settings - Fork 321
/
endorsement.go
204 lines (179 loc) · 5.77 KB
/
endorsement.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package endorsement
import (
"encoding/hex"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/keypair"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/protogen/iotextypes"
)
// ConsensusVoteTopic defines the topic of an consensus vote
type ConsensusVoteTopic uint8
const (
// PROPOSAL stands for an consensus vote to endorse a block proposal
PROPOSAL ConsensusVoteTopic = 0
// LOCK stands for an consensus vote to endorse a lock on a proposed block
LOCK ConsensusVoteTopic = 1
// COMMIT stands for an consensus vote to endorse a block commit
COMMIT ConsensusVoteTopic = 2
)
// ConsensusVote is a vote on a given topic for a block on a specific height
type ConsensusVote struct {
BlkHash []byte
Height uint64
Round uint32
Topic ConsensusVoteTopic
}
// NewConsensusVote creates a consensus vote
func NewConsensusVote(blkHash []byte, height uint64, round uint32, topic ConsensusVoteTopic) *ConsensusVote {
return &ConsensusVote{
blkHash,
height,
round,
topic,
}
}
// Hash returns a Hash256 for the consensus vote
func (en *ConsensusVote) Hash() hash.Hash256 {
stream := byteutil.Uint64ToBytes(en.Height)
stream = append(stream, uint8(en.Topic))
stream = append(stream, byteutil.Uint32ToBytes(en.Round)...)
stream = append(stream, en.BlkHash...)
return hash.Hash256b(stream)
}
// Endorsement is a stamp on a consensus vote
type Endorsement struct {
object *ConsensusVote
endorser string
endorserPubkey keypair.PublicKey
signature []byte
}
// NewEndorsement creates an Endorsement for an consensus vote
func NewEndorsement(object *ConsensusVote, endorserPriKey keypair.PrivateKey, endorserAddr string) *Endorsement {
hash := object.Hash()
sig, err := endorserPriKey.Sign(hash[:])
if err != nil {
log.L().Error("Failed to sign endorsement.")
return nil
}
return &Endorsement{
object: object,
endorser: endorserAddr,
endorserPubkey: endorserPriKey.PublicKey(),
signature: sig,
}
}
// ConsensusVote returns the Object of the endorse for signature
func (en *Endorsement) ConsensusVote() *ConsensusVote {
return en.object
}
// Endorser returns the endorser of this endorsement
func (en *Endorsement) Endorser() string {
return en.endorser
}
// EndorserPublicKey returns the public key of the endorser of this endorsement
func (en *Endorsement) EndorserPublicKey() keypair.PublicKey {
return en.endorserPubkey
}
// Signature returns the signature of this endorsement
func (en *Endorsement) Signature() []byte {
return en.signature
}
// VerifySignature verifies that the endorse with pubkey
func (en *Endorsement) VerifySignature() bool {
hash := en.object.Hash()
return en.endorserPubkey.Verify(hash[:], en.signature)
}
// ToProtoMsg converts an endorsement to endorse proto
func (en *Endorsement) ToProtoMsg() *iotextypes.Endorsement {
vote := en.ConsensusVote()
var topic iotextypes.Endorsement_ConsensusVoteTopic
switch vote.Topic {
case PROPOSAL:
topic = iotextypes.Endorsement_PROPOSAL
case LOCK:
topic = iotextypes.Endorsement_LOCK
case COMMIT:
topic = iotextypes.Endorsement_COMMIT
default:
log.L().Error("Endorsement object is of the wrong topic.")
return nil
}
pubkey := en.EndorserPublicKey()
return &iotextypes.Endorsement{
Height: vote.Height,
Round: vote.Round,
BlockHash: vote.BlkHash,
Topic: topic,
Endorser: en.Endorser(),
EndorserPubKey: pubkey.Bytes(),
Decision: true,
Signature: en.Signature(),
}
}
// Serialize converts an endorsement to bytes
func (en *Endorsement) Serialize() ([]byte, error) {
pb := en.ToProtoMsg()
if pb == nil {
return nil, errors.New("error when converting to protobuf")
}
return proto.Marshal(pb)
}
// FromProtoMsg creates an endorsement from endorsePb
func (en *Endorsement) FromProtoMsg(endorsePb *iotextypes.Endorsement) error {
var topic ConsensusVoteTopic
switch endorsePb.Topic {
case iotextypes.Endorsement_PROPOSAL:
topic = PROPOSAL
case iotextypes.Endorsement_LOCK:
topic = LOCK
case iotextypes.Endorsement_COMMIT:
topic = COMMIT
default:
return errors.New("Invalid topic")
}
vote := NewConsensusVote(
endorsePb.BlockHash,
endorsePb.Height,
endorsePb.Round,
topic,
)
pubKey, err := keypair.BytesToPublicKey(endorsePb.EndorserPubKey)
if err != nil {
log.L().Error("Error when constructing endorse from proto message.",
zap.Error(err),
log.Hex("endorserPubKey", endorsePb.EndorserPubKey))
return err
}
en.object = vote
en.endorser = endorsePb.Endorser
en.endorserPubkey = pubKey
en.signature = endorsePb.Signature
return nil
}
// Deserialize converts a byte array to endorsement
func (en *Endorsement) Deserialize(bs []byte) error {
pb := iotextypes.Endorsement{}
if err := proto.Unmarshal(bs, &pb); err != nil {
return err
}
return en.FromProtoMsg(&pb)
}
// MarshalLogObject marshals the endorsement to a zap object
func (en *Endorsement) MarshalLogObject(oe zapcore.ObjectEncoder) error {
oe.AddUint8("topic", uint8(en.object.Topic))
oe.AddString("warrantee", hex.EncodeToString(en.object.BlkHash))
oe.AddUint64("height", en.object.Height)
oe.AddUint32("round", en.object.Round)
oe.AddString("endorser", en.endorser)
return nil
}