-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct.go
executable file
·178 lines (162 loc) · 5.51 KB
/
construct.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
package consensus
import (
"bytes"
"errors"
protobuf "github.com/golang/protobuf/proto"
"github.com/zennittians/intelchain/crypto/bls"
bls_core "github.com/zennittians/bls/ffi/go/bls"
"github.com/zennittians/intelchain/api/proto"
msg_pb "github.com/zennittians/intelchain/api/proto/message"
"github.com/zennittians/intelchain/consensus/quorum"
"github.com/zennittians/intelchain/internal/utils"
)
// NetworkMessage is a message intended to be
// created only for distribution to
// all the other quorum members.
type NetworkMessage struct {
MessageType msg_pb.MessageType
Bytes []byte
FBFTMsg *FBFTMessage
OptionalAggregateSignature *bls_core.Sign
}
// Populates the common basic fields for all consensus message.
func (consensus *Consensus) populateMessageFields(
request *msg_pb.ConsensusRequest, blockHash []byte,
) *msg_pb.ConsensusRequest {
request.ViewId = consensus.getCurBlockViewID()
request.BlockNum = consensus.getBlockNum()
request.ShardId = consensus.ShardID
// 32 byte block hash
request.BlockHash = blockHash
return request
}
// Populates the common basic fields for the consensus message and senders bitmap.
func (consensus *Consensus) populateMessageFieldsAndSendersBitmap(
request *msg_pb.ConsensusRequest, blockHash []byte, bitmap []byte,
) *msg_pb.ConsensusRequest {
consensus.populateMessageFields(request, blockHash)
// sender address
request.SenderPubkeyBitmap = bitmap
return request
}
// Populates the common basic fields for the consensus message and single sender.
func (consensus *Consensus) populateMessageFieldsAndSender(
request *msg_pb.ConsensusRequest, blockHash []byte, pubKey bls.SerializedPublicKey,
) *msg_pb.ConsensusRequest {
consensus.populateMessageFields(request, blockHash)
// sender address
request.SenderPubkey = pubKey[:]
return request
}
// construct is the single creation point of messages intended for the wire.
func (consensus *Consensus) construct(
p msg_pb.MessageType, payloadForSign []byte, priKeys []*bls.PrivateKeyWrapper,
) (*NetworkMessage, error) {
if len(priKeys) == 0 {
return nil, errors.New("no elected bls keys provided")
}
message := &msg_pb.Message{
ServiceType: msg_pb.ServiceType_CONSENSUS,
Type: p,
Request: &msg_pb.Message_Consensus{
Consensus: &msg_pb.ConsensusRequest{},
},
}
var (
consensusMsg *msg_pb.ConsensusRequest
aggSig *bls_core.Sign
)
if len(priKeys) == 1 {
consensusMsg = consensus.populateMessageFieldsAndSender(
message.GetConsensus(), consensus.blockHash[:], priKeys[0].Pub.Bytes,
)
} else {
// TODO: use a persistent bitmap to report bitmap
mask := bls.NewMask(consensus.decider.Participants())
for _, key := range priKeys {
mask.SetKey(key.Pub.Bytes, true)
}
consensusMsg = consensus.populateMessageFieldsAndSendersBitmap(
message.GetConsensus(), consensus.blockHash[:], mask.Bitmap,
)
}
// Do the signing, 96 byte of bls signature
needMsgSig := true
switch p {
case msg_pb.MessageType_ANNOUNCE:
consensusMsg.Block = consensus.block
consensusMsg.Payload = consensus.blockHash[:]
case msg_pb.MessageType_PREPARE:
needMsgSig = false
sig := bls_core.Sign{}
for _, priKey := range priKeys {
if s := priKey.Pri.SignHash(consensusMsg.BlockHash); s != nil {
sig.Add(s)
}
}
consensusMsg.Payload = sig.Serialize()
case msg_pb.MessageType_COMMIT:
needMsgSig = false
sig := bls_core.Sign{}
for _, priKey := range priKeys {
if s := priKey.Pri.SignHash(payloadForSign); s != nil {
sig.Add(s)
}
}
consensusMsg.Payload = sig.Serialize()
case msg_pb.MessageType_PREPARED:
consensusMsg.Block = consensus.block
consensusMsg.Payload = consensus.constructQuorumSigAndBitmap(quorum.Prepare)
case msg_pb.MessageType_COMMITTED:
consensusMsg.Payload = consensus.constructQuorumSigAndBitmap(quorum.Commit)
}
var marshaledMessage []byte
var err error
if needMsgSig {
// The message that needs signing only needs to be signed with a single key
marshaledMessage, err = consensus.signAndMarshalConsensusMessage(message, priKeys[0].Pri)
} else {
// Skip message (potentially multi-sig) signing for validator consensus messages (prepare and commit)
// as signature is already signed on the block data.
marshaledMessage, err = protobuf.Marshal(message)
}
if err != nil {
utils.Logger().Error().Err(err).
Str("phase", p.String()).
Msg("Failed to sign and marshal consensus message")
return nil, err
}
FBFTMsg, err2 := consensus.parseFBFTMessage(message)
if err2 != nil {
utils.Logger().Error().Err(err).
Str("phase", p.String()).
Msg("failed to deal with the FBFT message")
return nil, err
}
return &NetworkMessage{
MessageType: p,
Bytes: proto.ConstructConsensusMessage(marshaledMessage),
FBFTMsg: FBFTMsg,
OptionalAggregateSignature: aggSig,
}, nil
}
// constructQuorumSigAndBitmap constructs the aggregated sig and bitmap as
// a byte slice in format of: [[aggregated sig], [sig bitmap]]
func (consensus *Consensus) constructQuorumSigAndBitmap(p quorum.Phase) []byte {
buffer := bytes.Buffer{}
// 96 bytes aggregated signature
aggSig := consensus.decider.AggregateVotes(p)
buffer.Write(aggSig.Serialize())
// Bitmap
if p == quorum.Prepare {
buffer.Write(consensus.prepareBitmap.Bitmap)
} else if p == quorum.Commit {
buffer.Write(consensus.commitBitmap.Bitmap)
} else {
utils.Logger().Error().
Str("phase", p.String()).
Msg("[constructQuorumSigAndBitmap] Invalid phase is supplied.")
return []byte{}
}
return buffer.Bytes()
}