Skip to content

Commit

Permalink
Consensus Mask. Removed unnecessary argument. (#4466)
Browse files Browse the repository at this point in the history
* Removed unnecessary argument.
* Fixed comment.
  • Loading branch information
Frozen authored Jul 21, 2023
1 parent 8656c7a commit 0e4b629
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 75 deletions.
13 changes: 5 additions & 8 deletions consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ func (consensus *Consensus) updateBitmaps() {
Str("MessageType", consensus.phase.String()).
Msg("[UpdateBitmaps] Updating consensus bitmaps")
members := consensus.Decider.Participants()
prepareBitmap, _ := bls_cosi.NewMask(members, nil)
commitBitmap, _ := bls_cosi.NewMask(members, nil)
multiSigBitmap, _ := bls_cosi.NewMask(members, nil)
prepareBitmap := bls_cosi.NewMask(members)
commitBitmap := bls_cosi.NewMask(members)
multiSigBitmap := bls_cosi.NewMask(members)
consensus.prepareBitmap = prepareBitmap
consensus.commitBitmap = commitBitmap
consensus.multiSigBitmap = multiSigBitmap
Expand Down Expand Up @@ -627,11 +627,8 @@ func (consensus *Consensus) NumSignaturesIncludedInBlock(block *types.Block) uin
count := uint32(0)
members := consensus.Decider.Participants()
// TODO(audit): do not reconstruct the Mask
mask, err := bls.NewMask(members, nil)
if err != nil {
return count
}
err = mask.SetMask(block.Header().LastCommitBitmap())
mask := bls.NewMask(members)
err := mask.SetMask(block.Header().LastCommitBitmap())
if err != nil {
return count
}
Expand Down
6 changes: 1 addition & 5 deletions consensus/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ func (consensus *Consensus) construct(
)
} else {
// TODO: use a persistent bitmap to report bitmap
mask, err := bls.NewMask(consensus.Decider.Participants(), nil)
if err != nil {
utils.Logger().Warn().Err(err).Msg("unable to setup mask for multi-sig message")
return nil, err
}
mask := bls.NewMask(consensus.Decider.Participants())
for _, key := range priKeys {
mask.SetKey(key.Pub.Bytes, true)
}
Expand Down
20 changes: 10 additions & 10 deletions consensus/view_change_construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ func (vc *viewChange) AddViewIDKeyIfNotExist(viewID uint64, members multibls.Pub
vc.viewIDSigs[viewID] = map[string]*bls_core.Sign{}
}
if _, ok := vc.bhpBitmap[viewID]; !ok {
bhpBitmap, _ := bls_cosi.NewMask(members, nil)
bhpBitmap := bls_cosi.NewMask(members)
vc.bhpBitmap[viewID] = bhpBitmap
}
if _, ok := vc.nilBitmap[viewID]; !ok {
nilBitmap, _ := bls_cosi.NewMask(members, nil)
nilBitmap := bls_cosi.NewMask(members)
vc.nilBitmap[viewID] = nilBitmap
}
if _, ok := vc.viewIDBitmap[viewID]; !ok {
viewIDBitmap, _ := bls_cosi.NewMask(members, nil)
viewIDBitmap := bls_cosi.NewMask(members)
vc.viewIDBitmap[viewID] = viewIDBitmap
}
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func (vc *viewChange) ProcessViewChangeMsg(
}
vc.bhpSigs[recvMsg.ViewID][senderKeyStr] = recvMsg.ViewchangeSig
if _, ok := vc.bhpBitmap[recvMsg.ViewID]; !ok {
bhpBitmap, _ := bls_cosi.NewMask(decider.Participants(), nil)
bhpBitmap := bls_cosi.NewMask(decider.Participants())
vc.bhpBitmap[recvMsg.ViewID] = bhpBitmap
}
vc.bhpBitmap[recvMsg.ViewID].SetKey(senderKey.Bytes, true) // Set the bitmap indicating that this validator signed.
Expand All @@ -305,7 +305,7 @@ func (vc *viewChange) ProcessViewChangeMsg(
vc.viewIDSigs[recvMsg.ViewID][senderKeyStr] = recvMsg.ViewidSig

if _, ok := vc.viewIDBitmap[recvMsg.ViewID]; !ok {
viewIDBitmap, _ := bls_cosi.NewMask(decider.Participants(), nil)
viewIDBitmap := bls_cosi.NewMask(decider.Participants())
vc.viewIDBitmap[recvMsg.ViewID] = viewIDBitmap
}
// Set the bitmap indicating that this validator signed.
Expand Down Expand Up @@ -350,7 +350,7 @@ func (vc *viewChange) ProcessViewChangeMsg(
vc.nilSigs[recvMsg.ViewID][senderKeyStr] = recvMsg.ViewchangeSig

if _, ok := vc.nilBitmap[recvMsg.ViewID]; !ok {
nilBitmap, _ := bls_cosi.NewMask(decider.Participants(), nil)
nilBitmap := bls_cosi.NewMask(decider.Participants())
vc.nilBitmap[recvMsg.ViewID] = nilBitmap
}
vc.nilBitmap[recvMsg.ViewID].SetKey(senderKey.Bytes, true) // Set the bitmap indicating that this validator signed.
Expand All @@ -366,7 +366,7 @@ func (vc *viewChange) ProcessViewChangeMsg(

// Set the bitmap indicating that this validator signed.
if _, ok := vc.viewIDBitmap[recvMsg.ViewID]; !ok {
viewIDBitmap, _ := bls_cosi.NewMask(decider.Participants(), nil)
viewIDBitmap := bls_cosi.NewMask(decider.Participants())
vc.viewIDBitmap[recvMsg.ViewID] = viewIDBitmap
}
vc.viewIDBitmap[recvMsg.ViewID].SetKey(senderKey.Bytes, true)
Expand Down Expand Up @@ -411,7 +411,7 @@ func (vc *viewChange) InitPayload(
for _, key := range privKeys {
// update the dictionary key if the viewID is first time received
if _, ok := vc.bhpBitmap[viewID]; !ok {
bhpBitmap, _ := bls_cosi.NewMask(members, nil)
bhpBitmap := bls_cosi.NewMask(members)
vc.bhpBitmap[viewID] = bhpBitmap
}
if err := vc.bhpBitmap[viewID].SetKey(key.Pub.Bytes, true); err != nil {
Expand All @@ -435,7 +435,7 @@ func (vc *viewChange) InitPayload(
vc.getLogger().Info().Uint64("viewID", viewID).Uint64("blockNum", blockNum).Msg("[InitPayload] add my M2 (NIL) type messaage")
for _, key := range privKeys {
if _, ok := vc.nilBitmap[viewID]; !ok {
nilBitmap, _ := bls_cosi.NewMask(members, nil)
nilBitmap := bls_cosi.NewMask(members)
vc.nilBitmap[viewID] = nilBitmap
}
if err := vc.nilBitmap[viewID].SetKey(key.Pub.Bytes, true); err != nil {
Expand Down Expand Up @@ -467,7 +467,7 @@ func (vc *viewChange) InitPayload(
vc.getLogger().Info().Uint64("viewID", viewID).Uint64("blockNum", blockNum).Msg("[InitPayload] add my M3 (ViewID) type messaage")
for _, key := range privKeys {
if _, ok := vc.viewIDBitmap[viewID]; !ok {
viewIDBitmap, _ := bls_cosi.NewMask(members, nil)
viewIDBitmap := bls_cosi.NewMask(members)
vc.viewIDBitmap[viewID] = viewIDBitmap
}
if err := vc.viewIDBitmap[viewID].SetKey(key.Pub.Bytes, true); err != nil {
Expand Down
10 changes: 2 additions & 8 deletions consensus/view_change_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,7 @@ func ParseNewViewMessage(msg *msg_pb.Message, members multibls.PublicKeys) (*FBF
if err != nil {
return nil, err
}
m3mask, err := bls_cosi.NewMask(members, nil)
if err != nil {
return nil, err
}
m3mask := bls_cosi.NewMask(members)
m3mask.SetMask(vcMsg.M3Bitmap)
FBFTMsg.M3AggSig = &m3Sig
FBFTMsg.M3Bitmap = m3mask
Expand All @@ -237,10 +234,7 @@ func ParseNewViewMessage(msg *msg_pb.Message, members multibls.PublicKeys) (*FBF
if err != nil {
return nil, err
}
m2mask, err := bls_cosi.NewMask(members, nil)
if err != nil {
return nil, err
}
m2mask := bls_cosi.NewMask(members)
m2mask.SetMask(vcMsg.M2Bitmap)
FBFTMsg.M2AggSig = &m2Sig
FBFTMsg.M2Bitmap = m2mask
Expand Down
3 changes: 2 additions & 1 deletion core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ func (v validatorWrapperChange) dirtied() *common.Address {
}

// revert undoes the changes introduced by this journal entry.
func (v validatorWrapperChange) revert(*DB) {
func (v validatorWrapperChange) revert(s *DB) {
s.stateValidators[*(v.address)] = v.prev
}

func (ch createObjectChange) revert(s *DB) {
Expand Down
18 changes: 3 additions & 15 deletions crypto/bls/mask.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ type Mask struct {
}

// NewMask returns a new participation bitmask for cosigning where all
// cosigners are disabled by default. If a public key is given it verifies that
// it is present in the list of keys and sets the corresponding index in the
// bitmask to 1 (enabled).
func NewMask(publics []PublicKeyWrapper, myKey *PublicKeyWrapper) (*Mask, error) {
// cosigners are disabled by default.
func NewMask(publics []PublicKeyWrapper) *Mask {
index := map[SerializedPublicKey]int{}
publicKeys := make([]*PublicKeyWrapper, len(publics))
for i, key := range publics {
Expand All @@ -88,17 +86,7 @@ func NewMask(publics []PublicKeyWrapper, myKey *PublicKeyWrapper) (*Mask, error)
}
m.Bitmap = make([]byte, m.Len())
m.AggregatePublic = &bls.PublicKey{}
if myKey != nil {
i, found := m.PublicsIndex[myKey.Bytes]
if found {
m.SetBit(i, true)
found = true
}
if !found {
return nil, errors.New("key not found")
}
}
return m, nil
return m
}

// Clear clears the existing bits and aggregate public keys.
Expand Down
34 changes: 19 additions & 15 deletions crypto/bls/mask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func TestNewMask(test *testing.T) {
pubKey1.Bytes.FromLibBLSPublicKey(pubKey1.Object)
pubKey2.Bytes.FromLibBLSPublicKey(pubKey2.Object)
pubKey3.Bytes.FromLibBLSPublicKey(pubKey3.Object)
mask, err := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3}, &pubKey1)
mask := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3})
err := mask.SetKey(pubKey1.Bytes, true)

if err != nil {
test.Errorf("Failed to create a new Mask: %s", err)
Expand Down Expand Up @@ -51,16 +52,12 @@ func TestNewMaskWithAbsentPublicKey(test *testing.T) {
pubKey3.Bytes.FromLibBLSPublicKey(pubKey3.Object)
pubKey4.Bytes.FromLibBLSPublicKey(pubKey4.Object)

mask, err := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3}, &pubKey4)
mask := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3})
err := mask.SetKey(pubKey4.Bytes, true)

if err == nil {
test.Errorf("Failed to create a new Mask: %s", err)
}

if mask != nil {
test.Errorf("Expected failure to create a new mask")
test.Errorf("Failed to set a key: %s", err)
}

}

func TestThreshHoldPolicy(test *testing.T) {
Expand All @@ -71,7 +68,8 @@ func TestThreshHoldPolicy(test *testing.T) {
pubKey1.Bytes.FromLibBLSPublicKey(pubKey1.Object)
pubKey2.Bytes.FromLibBLSPublicKey(pubKey2.Object)
pubKey3.Bytes.FromLibBLSPublicKey(pubKey3.Object)
mask, err := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3}, &pubKey1)
mask := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3})
err := mask.SetKey(pubKey1.Bytes, true)

if err != nil {
test.Errorf("Failed to create a new Mask: %s", err)
Expand Down Expand Up @@ -110,7 +108,8 @@ func TestCompletePolicy(test *testing.T) {
pubKey1.Bytes.FromLibBLSPublicKey(pubKey1.Object)
pubKey2.Bytes.FromLibBLSPublicKey(pubKey2.Object)
pubKey3.Bytes.FromLibBLSPublicKey(pubKey3.Object)
mask, err := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3}, &pubKey1)
mask := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3})
err := mask.SetKey(pubKey1.Bytes, true)

if err != nil {
test.Errorf("Failed to create a new Mask: %s", err)
Expand Down Expand Up @@ -184,7 +183,8 @@ func TestEnableKeyFunctions(test *testing.T) {
pubKey2.Bytes.FromLibBLSPublicKey(pubKey2.Object)
pubKey3.Bytes.FromLibBLSPublicKey(pubKey3.Object)
pubKey4.Bytes.FromLibBLSPublicKey(pubKey4.Object)
mask, err := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3}, &pubKey1)
mask := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3})
err := mask.SetKey(pubKey1.Bytes, true)

if err != nil {
test.Errorf("Failed to create a new Mask: %s", err)
Expand Down Expand Up @@ -238,7 +238,8 @@ func TestGetSignedPubKeysFromBitmap(test *testing.T) {
pubKey2.Bytes.FromLibBLSPublicKey(pubKey2.Object)
pubKey3.Bytes.FromLibBLSPublicKey(pubKey3.Object)
pubKey4.Bytes.FromLibBLSPublicKey(pubKey4.Object)
mask, err := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3}, &pubKey1)
mask := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3})
err := mask.SetKey(pubKey1.Bytes, true)

if err != nil {
test.Errorf("Failed to create a new Mask: %s", err)
Expand Down Expand Up @@ -273,7 +274,8 @@ func TestSetKeyAtomic(test *testing.T) {
pubKey2.Bytes.FromLibBLSPublicKey(pubKey2.Object)
pubKey3.Bytes.FromLibBLSPublicKey(pubKey3.Object)
pubKey4.Bytes.FromLibBLSPublicKey(pubKey4.Object)
mask, err := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3}, &pubKey1)
mask := NewMask([]PublicKeyWrapper{pubKey1, pubKey2, pubKey3})
err := mask.SetKey(pubKey1.Bytes, true)

if err != nil {
test.Errorf("Failed to create a new Mask: %s", err)
Expand Down Expand Up @@ -315,7 +317,8 @@ func TestCopyParticipatingMask(test *testing.T) {

pubKey1.Bytes.FromLibBLSPublicKey(pubKey1.Object)
pubKey2.Bytes.FromLibBLSPublicKey(pubKey2.Object)
mask, _ := NewMask([]PublicKeyWrapper{pubKey1, pubKey2}, &pubKey1)
mask := NewMask([]PublicKeyWrapper{pubKey1, pubKey2})
_ = mask.SetKey(pubKey1.Bytes, true)

clonedMask := mask.Mask()

Expand All @@ -331,7 +334,8 @@ func TestSetMask(test *testing.T) {

pubKey1.Bytes.FromLibBLSPublicKey(pubKey1.Object)
pubKey2.Bytes.FromLibBLSPublicKey(pubKey2.Object)
mask, _ := NewMask([]PublicKeyWrapper{pubKey1, pubKey2}, &pubKey1)
mask := NewMask([]PublicKeyWrapper{pubKey1, pubKey2})
_ = mask.SetKey(pubKey1.Bytes, true)

_ = mask
maskBytes := []byte{3}
Expand Down
5 changes: 1 addition & 4 deletions hmy/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ func (hmy *Harmony) GetBlockSigners(
Object: key,
}
}
mask, err := internal_bls.NewMask(pubKeys, nil)
if err != nil {
return nil, nil, err
}
mask := internal_bls.NewMask(pubKeys)
err = mask.SetMask(blockWithSigners.Header().LastCommitBitmap())
if err != nil {
return nil, nil, err
Expand Down
6 changes: 1 addition & 5 deletions internal/chain/sig.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ func DecodeSigBitmap(sigBytes bls.SerializedSignature, bitmap []byte, pubKeys []
if err != nil {
return nil, nil, errors.New("unable to deserialize multi-signature from payload")
}
mask, err := bls.NewMask(pubKeys, nil)
if err != nil {
utils.Logger().Warn().Err(err).Msg("onNewView unable to setup mask for prepared message")
return nil, nil, errors.New("unable to setup mask from payload")
}
mask := bls.NewMask(pubKeys)
if err := mask.SetMask(bitmap); err != nil {
utils.Logger().Warn().Err(err).Msg("mask.SetMask failed")
return nil, nil, errors.New("mask.SetMask failed")
Expand Down
5 changes: 1 addition & 4 deletions staking/availability/measure.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ func BlockSigners(
if err != nil {
return nil, nil, err
}
mask, err := bls.NewMask(committerKeys, nil)
if err != nil {
return nil, nil, err
}
mask := bls.NewMask(committerKeys)
if err := mask.SetMask(bitmap); err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 0e4b629

Please sign in to comment.