update fbft id

pull/3374/head
Rongjian Lan 4 years ago
parent 864ccb0d61
commit 8885eb45e0
  1. 13
      consensus/fbft_log.go
  2. 26
      consensus/fbft_log_test.go

@ -62,10 +62,11 @@ func (m *FBFTMessage) String() string {
const ( const (
idTypeBytes = 4 idTypeBytes = 4
idViewIDBytes = 8
idHashBytes = common.HashLength idHashBytes = common.HashLength
idSenderBytes = bls.PublicKeySizeInBytes idSenderBytes = bls.PublicKeySizeInBytes
idBytes = idTypeBytes + idHashBytes + idSenderBytes idBytes = idTypeBytes + idViewIDBytes + idHashBytes + idSenderBytes
) )
type ( type (
@ -78,9 +79,13 @@ type (
func (m *FBFTMessage) id() fbftMsgID { func (m *FBFTMessage) id() fbftMsgID {
var id fbftMsgID var id fbftMsgID
binary.LittleEndian.PutUint32(id[:], uint32(m.MessageType)) binary.LittleEndian.PutUint32(id[:], uint32(m.MessageType))
copy(id[idTypeBytes:], m.BlockHash[:]) binary.LittleEndian.PutUint64(id[idTypeBytes:], m.ViewID)
if m.SenderPubkey != nil { copy(id[idTypeBytes+idViewIDBytes:], m.BlockHash[:])
copy(id[idTypeBytes+idHashBytes:], m.SenderPubkey.Bytes[:])
if len(m.SenderPubkeys) == 1 {
copy(id[idTypeBytes+idViewIDBytes+idHashBytes:], m.SenderPubkeys[0].Bytes[:])
} else {
copy(id[idTypeBytes+idViewIDBytes+idHashBytes:], m.SenderPubkeyBitmap[:])
} }
return id return id
} }

@ -13,14 +13,18 @@ func TestFBFTLog_id(t *testing.T) {
tests := []FBFTMessage{ tests := []FBFTMessage{
{ {
MessageType: msg_pb.MessageType_ANNOUNCE, MessageType: msg_pb.MessageType_ANNOUNCE,
ViewID: 4,
BlockHash: [32]byte{01, 02}, BlockHash: [32]byte{01, 02},
SenderPubkey: &bls.PublicKeyWrapper{ SenderPubkeys: []*bls.PublicKeyWrapper{&bls.PublicKeyWrapper{
Bytes: bls.SerializedPublicKey{0x01, 0x02}, Bytes: bls.SerializedPublicKey{0x01, 0x02},
}, }},
SenderPubkeyBitmap: []byte{05, 07},
}, },
{ {
MessageType: msg_pb.MessageType_COMMIT, MessageType: msg_pb.MessageType_COMMIT,
BlockHash: [32]byte{02, 03}, ViewID: 4,
BlockHash: [32]byte{02, 03},
SenderPubkeyBitmap: []byte{05, 07},
}, },
} }
for _, msg := range tests { for _, msg := range tests {
@ -29,16 +33,18 @@ func TestFBFTLog_id(t *testing.T) {
if uint32(msg.MessageType) != binary.LittleEndian.Uint32(id[:]) { if uint32(msg.MessageType) != binary.LittleEndian.Uint32(id[:]) {
t.Errorf("message type not expected") t.Errorf("message type not expected")
} }
if !bytes.Equal(id[idTypeBytes:idTypeBytes+idHashBytes], msg.BlockHash[:]) { if msg.ViewID != binary.LittleEndian.Uint64(id[idTypeBytes:]) {
t.Errorf("view id not expected")
}
if !bytes.Equal(id[idTypeBytes+idViewIDBytes:idTypeBytes+idViewIDBytes+idHashBytes], msg.BlockHash[:]) {
t.Errorf("block hash not expected") t.Errorf("block hash not expected")
} }
if msg.SenderPubkey == nil { if len(msg.SenderPubkeys) != 1 {
var emptyKey bls.SerializedPublicKey if !bytes.Equal(id[idTypeBytes+idViewIDBytes+idHashBytes:idTypeBytes+idViewIDBytes+idHashBytes+len(msg.SenderPubkeyBitmap)], msg.SenderPubkeyBitmap[:]) {
if !bytes.Equal(id[idTypeBytes+idHashBytes:], emptyKey[:]) { t.Errorf("sender key expected to be the bitmap when key list is not size 1")
t.Errorf("sender key not expected when empty")
} }
} else { } else {
if !bytes.Equal(id[idTypeBytes+idHashBytes:], msg.SenderPubkey.Bytes[:]) { if !bytes.Equal(id[idTypeBytes+idViewIDBytes+idHashBytes:], msg.SenderPubkeys[0].Bytes[:]) {
t.Errorf("sender key not expected") t.Errorf("sender key not expected")
} }
} }

Loading…
Cancel
Save