Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next validator in view change. #4492

Merged
merged 4 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions consensus/quorum/quorom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/crypto/bls"
Expand Down Expand Up @@ -593,3 +594,33 @@ func TestNthNextHmyExt(test *testing.T) {
}
}
}

func TestCIdentities_NthNextValidatorHmy(t *testing.T) {
address := []common.Address{
common.HexToAddress("0x1"),
common.HexToAddress("0x2"),
common.HexToAddress("0x3"),
}
slots := shard.SlotList{}
list := []harmony_bls.PublicKeyWrapper{}
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
blsKey := harmony_bls.RandPrivateKey()
wrapper := harmony_bls.PublicKeyWrapper{Object: blsKey.GetPublicKey()}
wrapper.Bytes.FromLibBLSPublicKey(wrapper.Object)
slots = append(slots, shard.Slot{
EcdsaAddress: address[i%3],
BLSPublicKey: wrapper.Bytes,
EffectiveStake: nil,
})
list = append(list, wrapper)
}
}

c := newCIdentities()
c.UpdateParticipants(list, []bls.PublicKeyWrapper{})
found, key := c.NthNextValidatorHmy(nil, slots, &list[0], 1)
require.Equal(t, true, found)
// because we skip 3 keys of current validator
require.Equal(t, 3, c.IndexOf(key.Bytes))
}
36 changes: 34 additions & 2 deletions consensus/quorum/quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,35 @@ func (s *cIdentities) NthNext(pubKey *bls.PublicKeyWrapper, next int) (bool, *bl
return found, &s.publicKeys[idx]
}

// NthNextHmy return the Nth next pubkey of Harmony nodes, next can be negative number
// NthNextValidatorHmy return the Nth next pubkey nodes, but from another validator.
func (s *cIdentities) NthNextValidatorHmy(instance shardingconfig.Instance, slotList shard.SlotList, pubKey *bls.PublicKeyWrapper, next int) (bool, *bls.PublicKeyWrapper) {
Frozen marked this conversation as resolved.
Show resolved Hide resolved
Frozen marked this conversation as resolved.
Show resolved Hide resolved
found := false

Frozen marked this conversation as resolved.
Show resolved Hide resolved
publicToAddress := make(map[bls.SerializedPublicKey]common.Address)
for _, slot := range slotList {
publicToAddress[slot.BLSPublicKey] = slot.EcdsaAddress
}

idx := s.IndexOf(pubKey.Bytes)
if idx != -1 {
found = true
} else {
utils.Logger().Error().
Str("key", pubKey.Bytes.Hex()).
Msg("[NthNextHmy] pubKey not found")
}
for {
numNodes := len(s.publicKeys)
idx = (idx + next) % numNodes
Frozen marked this conversation as resolved.
Show resolved Hide resolved
if publicToAddress[s.publicKeys[idx].Bytes] == publicToAddress[pubKey.Bytes] {
// same validator, go next
idx++
continue
}
return found, &s.publicKeys[idx]
}
}

func (s *cIdentities) NthNextHmy(instance shardingconfig.Instance, pubKey *bls.PublicKeyWrapper, next int) (bool, *bls.PublicKeyWrapper) {
found := false

Expand Down Expand Up @@ -410,7 +438,7 @@ func (s *cIdentities) ReadAllBallots(p Phase) []*votepower.Ballot {
return ballots
}

func newBallotsBackedSignatureReader() *cIdentities {
func newCIdentities() *cIdentities {
return &cIdentities{
publicKeys: []bls.PublicKeyWrapper{},
keyIndexMap: map[bls.SerializedPublicKey]int{},
Expand All @@ -420,6 +448,10 @@ func newBallotsBackedSignatureReader() *cIdentities {
}
}

func newBallotsBackedSignatureReader() *cIdentities {
return newCIdentities()
}

// NewDecider ..
func NewDecider(p Policy, shardID uint32) Decider {
switch p {
Expand Down