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

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

Loading…
Cancel
Save