-
Notifications
You must be signed in to change notification settings - Fork 2
/
view_change_msg.go
250 lines (217 loc) · 7.31 KB
/
view_change_msg.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package consensus
import (
"encoding/binary"
"github.com/PositionExchange/posichain/crypto/bls"
"github.com/ethereum/go-ethereum/rlp"
bls_core "github.com/PositionExchange/bls/ffi/go/bls"
"github.com/PositionExchange/posichain/api/proto"
msg_pb "github.com/PositionExchange/posichain/api/proto/message"
bls_cosi "github.com/PositionExchange/posichain/crypto/bls"
"github.com/PositionExchange/posichain/multibls"
"github.com/pkg/errors"
)
// construct the view change message
func (consensus *Consensus) constructViewChangeMessage(priKey *bls.PrivateKeyWrapper) []byte {
message := &msg_pb.Message{
ServiceType: msg_pb.ServiceType_CONSENSUS,
Type: msg_pb.MessageType_VIEWCHANGE,
Request: &msg_pb.Message_Viewchange{
Viewchange: &msg_pb.ViewChangeRequest{
ViewId: consensus.GetViewChangingID(),
BlockNum: consensus.BlockNum(),
ShardId: consensus.ShardID,
SenderPubkey: priKey.Pub.Bytes[:],
LeaderPubkey: consensus.LeaderPubKey.Bytes[:],
},
},
}
preparedMsgs := consensus.FBFTLog.GetMessagesByTypeSeq(
msg_pb.MessageType_PREPARED, consensus.BlockNum(),
)
preparedMsg := consensus.FBFTLog.FindMessageByMaxViewID(preparedMsgs)
var encodedBlock []byte
if preparedMsg != nil {
block := consensus.FBFTLog.GetBlockByHash(preparedMsg.BlockHash)
consensus.getLogger().Info().
Interface("Block", block).
Interface("preparedMsg", preparedMsg).
Msg("[constructViewChangeMessage] found prepared msg")
if block != nil {
if err := consensus.VerifyBlock(block); err == nil {
tmpEncoded, err := rlp.EncodeToBytes(block)
if err != nil {
consensus.getLogger().Err(err).Msg("[constructViewChangeMessage] Failed encoding block")
}
encodedBlock = tmpEncoded
} else {
consensus.getLogger().Err(err).Msg("[constructViewChangeMessage] Failed validating prepared block")
}
}
}
vcMsg := message.GetViewchange()
var msgToSign []byte
if len(encodedBlock) == 0 {
msgToSign = NIL // m2 type message
vcMsg.Payload = []byte{}
} else {
// m1 type message
msgToSign = append(preparedMsg.BlockHash[:], preparedMsg.Payload...)
vcMsg.Payload = append(msgToSign[:0:0], msgToSign...)
vcMsg.PreparedBlock = encodedBlock
}
consensus.getLogger().Info().
Hex("m1Payload", vcMsg.Payload).
Str("NextLeader", consensus.LeaderPubKey.Bytes.Hex()).
Str("SenderPubKey", priKey.Pub.Bytes.Hex()).
Msg("[constructViewChangeMessage]")
sign := priKey.Pri.SignHash(msgToSign)
if sign != nil {
vcMsg.ViewchangeSig = sign.Serialize()
} else {
consensus.getLogger().Error().Msg("unable to serialize m1/m2 view change message signature")
}
viewIDBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(viewIDBytes, consensus.GetViewChangingID())
sign1 := priKey.Pri.SignHash(viewIDBytes)
if sign1 != nil {
vcMsg.ViewidSig = sign1.Serialize()
} else {
consensus.getLogger().Error().Msg("unable to serialize viewID signature")
}
marshaledMessage, err := consensus.signAndMarshalConsensusMessage(message, priKey.Pri)
if err != nil {
consensus.getLogger().Err(err).
Msg("[constructViewChangeMessage] failed to sign and marshal the viewchange message")
}
return proto.ConstructConsensusMessage(marshaledMessage)
}
// new leader construct newview message
func (consensus *Consensus) constructNewViewMessage(viewID uint64, priKey *bls.PrivateKeyWrapper) []byte {
message := &msg_pb.Message{
ServiceType: msg_pb.ServiceType_CONSENSUS,
Type: msg_pb.MessageType_NEWVIEW,
Request: &msg_pb.Message_Viewchange{
Viewchange: &msg_pb.ViewChangeRequest{
ViewId: viewID,
BlockNum: consensus.BlockNum(),
ShardId: consensus.ShardID,
SenderPubkey: priKey.Pub.Bytes[:],
},
},
}
vcMsg := message.GetViewchange()
vcMsg.Payload, vcMsg.PreparedBlock = consensus.vc.GetPreparedBlock(consensus.FBFTLog)
vcMsg.M2Aggsigs, vcMsg.M2Bitmap = consensus.vc.GetM2Bitmap(viewID)
vcMsg.M3Aggsigs, vcMsg.M3Bitmap = consensus.vc.GetM3Bitmap(viewID)
if vcMsg.M3Bitmap == nil || vcMsg.M3Aggsigs == nil {
return nil
}
marshaledMessage, err := consensus.signAndMarshalConsensusMessage(message, priKey.Pri)
if err != nil {
consensus.getLogger().Err(err).
Msg("[constructNewViewMessage] failed to sign and marshal the new view message")
}
return proto.ConstructConsensusMessage(marshaledMessage)
}
var (
errNilMessage = errors.New("Nil protobuf message")
errIncorrectMessageType = errors.New("Incorrect message type")
)
// ParseViewChangeMessage parses view change message into FBFTMessage structure
func ParseViewChangeMessage(msg *msg_pb.Message) (*FBFTMessage, error) {
if msg == nil {
return nil, errNilMessage
}
vcMsg := msg.GetViewchange()
FBFTMsg := FBFTMessage{
BlockNum: vcMsg.BlockNum,
ViewID: vcMsg.ViewId,
MessageType: msg.GetType(),
Payload: make([]byte, len(vcMsg.Payload)),
Block: make([]byte, len(vcMsg.PreparedBlock)),
}
if FBFTMsg.MessageType != msg_pb.MessageType_VIEWCHANGE {
return nil, errIncorrectMessageType
}
copy(FBFTMsg.Block[:], vcMsg.PreparedBlock[:])
copy(FBFTMsg.Payload[:], vcMsg.Payload[:])
pubKey, err := bls_cosi.BytesToBLSPublicKey(vcMsg.SenderPubkey)
if err != nil {
return nil, err
}
leaderKey, err := bls_cosi.BytesToBLSPublicKey(vcMsg.LeaderPubkey)
if err != nil {
return nil, err
}
vcSig := bls_core.Sign{}
err = vcSig.Deserialize(vcMsg.ViewchangeSig)
if err != nil {
return nil, err
}
FBFTMsg.ViewchangeSig = &vcSig
vcSig1 := bls_core.Sign{}
err = vcSig1.Deserialize(vcMsg.ViewidSig)
if err != nil {
return nil, err
}
FBFTMsg.ViewidSig = &vcSig1
FBFTMsg.SenderPubkeys = []*bls.PublicKeyWrapper{{Object: pubKey}}
copy(FBFTMsg.SenderPubkeys[0].Bytes[:], vcMsg.SenderPubkey[:])
FBFTMsg.LeaderPubkey = &bls.PublicKeyWrapper{Object: leaderKey}
copy(FBFTMsg.LeaderPubkey.Bytes[:], vcMsg.LeaderPubkey[:])
return &FBFTMsg, nil
}
// ParseNewViewMessage parses new view message into FBFTMessage structure
func ParseNewViewMessage(msg *msg_pb.Message, members multibls.PublicKeys) (*FBFTMessage, error) {
if msg == nil {
return nil, errNilMessage
}
vcMsg := msg.GetViewchange()
FBFTMsg := FBFTMessage{
BlockNum: vcMsg.BlockNum,
ViewID: vcMsg.ViewId,
MessageType: msg.GetType(),
Payload: make([]byte, len(vcMsg.Payload)),
Block: make([]byte, len(vcMsg.PreparedBlock)),
}
if FBFTMsg.MessageType != msg_pb.MessageType_NEWVIEW {
return nil, errIncorrectMessageType
}
copy(FBFTMsg.Payload[:], vcMsg.Payload[:])
copy(FBFTMsg.Block[:], vcMsg.PreparedBlock[:])
pubKey, err := bls_cosi.BytesToBLSPublicKey(vcMsg.SenderPubkey)
if err != nil {
return nil, err
}
FBFTMsg.SenderPubkeys = []*bls.PublicKeyWrapper{{Object: pubKey}}
copy(FBFTMsg.SenderPubkeys[0].Bytes[:], vcMsg.SenderPubkey[:])
if len(vcMsg.M3Aggsigs) > 0 {
m3Sig := bls_core.Sign{}
err = m3Sig.Deserialize(vcMsg.M3Aggsigs)
if err != nil {
return nil, err
}
m3mask, err := bls_cosi.NewMask(members, nil)
if err != nil {
return nil, err
}
m3mask.SetMask(vcMsg.M3Bitmap)
FBFTMsg.M3AggSig = &m3Sig
FBFTMsg.M3Bitmap = m3mask
}
if len(vcMsg.M2Aggsigs) > 0 {
m2Sig := bls_core.Sign{}
err = m2Sig.Deserialize(vcMsg.M2Aggsigs)
if err != nil {
return nil, err
}
m2mask, err := bls_cosi.NewMask(members, nil)
if err != nil {
return nil, err
}
m2mask.SetMask(vcMsg.M2Bitmap)
FBFTMsg.M2AggSig = &m2Sig
FBFTMsg.M2Bitmap = m2mask
}
return &FBFTMsg, nil
}