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

Minor: removed unused method NthNextHmyExt #4670

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 0 additions & 46 deletions consensus/quorum/quorom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
bls_core "github.com/harmony-one/bls/ffi/go/bls"
harmony_bls "github.com/harmony-one/harmony/crypto/bls"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -550,51 +549,6 @@ func TestInvalidAggregateSig(test *testing.T) {
}
}

func TestNthNextHmyExt(test *testing.T) {
numHmyNodes := 10
numAllExtNodes := 10
numAllowlistExtNodes := numAllExtNodes / 2
allowlist := shardingconfig.Allowlist{MaxLimitPerShard: numAllowlistExtNodes - 1}
blsKeys := []harmony_bls.PublicKeyWrapper{}
for i := 0; i < numHmyNodes+numAllExtNodes; i++ {
blsKey := harmony_bls.RandPrivateKey()
wrapper := harmony_bls.PublicKeyWrapper{Object: blsKey.GetPublicKey()}
wrapper.Bytes.FromLibBLSPublicKey(wrapper.Object)
blsKeys = append(blsKeys, wrapper)
}
allowlistLeaders := blsKeys[len(blsKeys)-allowlist.MaxLimitPerShard:]
allLeaders := append(blsKeys[:numHmyNodes], allowlistLeaders...)

decider := NewDecider(SuperMajorityVote, shard.BeaconChainShardID)
fakeInstance := shardingconfig.MustNewInstance(2, 20, numHmyNodes, 0, numeric.OneDec(), nil, nil, allowlist, nil, numeric.ZeroDec(), common.Address{}, nil, 0)

decider.UpdateParticipants(blsKeys, allowlistLeaders)
for i := 0; i < len(allLeaders); i++ {
leader := allLeaders[i]
for j := 0; j < len(allLeaders)*2; j++ {
expectNextLeader := allLeaders[(i+j)%len(allLeaders)]
found, nextLeader := decider.NthNextHmyExt(fakeInstance, &leader, j)
if !found {
test.Fatal("next leader not found")
}
if expectNextLeader.Bytes != nextLeader.Bytes {
test.Fatal("next leader is not expected")
}

preJ := -j
preIndex := (i + len(allLeaders) + preJ%len(allLeaders)) % len(allLeaders)
expectPreLeader := allLeaders[preIndex]
found, preLeader := decider.NthNextHmyExt(fakeInstance, &leader, preJ)
if !found {
test.Fatal("previous leader not found")
}
if expectPreLeader.Bytes != preLeader.Bytes {
test.Fatal("previous leader is not expected")
}
}
}
}

func TestCIdentities_NthNextValidatorHmy(t *testing.T) {
address := []common.Address{
common.HexToAddress("0x1"),
Expand Down
49 changes: 2 additions & 47 deletions consensus/quorum/quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package quorum
import (
"fmt"
"math/big"
"sort"

"github.com/harmony-one/harmony/crypto/bls"

Expand Down Expand Up @@ -78,7 +77,6 @@ type ParticipantTracker interface {
// NthNextValidator returns key for next validator. It assumes external validators and leader rotation.
NthNextValidator(slotList shard.SlotList, pubKey *bls.PublicKeyWrapper, next int) (bool, *bls.PublicKeyWrapper)
NthNextHmy(instance shardingconfig.Instance, pubkey *bls.PublicKeyWrapper, next int) (bool, *bls.PublicKeyWrapper)
NthNextHmyExt(shardingconfig.Instance, *bls.PublicKeyWrapper, int) (bool, *bls.PublicKeyWrapper)
FirstParticipant(shardingconfig.Instance) *bls.PublicKeyWrapper
UpdateParticipants(pubKeys, allowlist []bls.PublicKeyWrapper)
}
Expand Down Expand Up @@ -151,10 +149,8 @@ type cIdentities struct {
// Public keys of the committee including leader and validators
publicKeys []bls.PublicKeyWrapper
keyIndexMap map[bls.SerializedPublicKey]int
// every element is a index of publickKeys
allowlistIndex []int
prepare *votepower.Round
commit *votepower.Round
prepare *votepower.Round
commit *votepower.Round
// viewIDSigs: every validator
// sign on |viewID|blockHash| in view changing message
viewChange *votepower.Round
Expand Down Expand Up @@ -274,41 +270,6 @@ func (s *cIdentities) NthNextHmy(instance shardingconfig.Instance, pubKey *bls.P
return found, &s.publicKeys[idx]
}

// NthNextHmyExt return the Nth next pubkey of Harmony + allowlist nodes, next can be negative number
func (s *cIdentities) NthNextHmyExt(instance shardingconfig.Instance, pubKey *bls.PublicKeyWrapper, next int) (bool, *bls.PublicKeyWrapper) {
found := false

idx := s.IndexOf(pubKey.Bytes)
if idx != -1 {
found = true
}
numHmyNodes := instance.NumHarmonyOperatedNodesPerShard()
// sanity check to avoid out of bound access
if numHmyNodes <= 0 || numHmyNodes > len(s.publicKeys) {
numHmyNodes = len(s.publicKeys)
}
nth := idx
if idx >= numHmyNodes {
nth = sort.SearchInts(s.allowlistIndex, idx) + numHmyNodes
}

numExtNodes := instance.ExternalAllowlistLimit()
if numExtNodes > len(s.allowlistIndex) {
numExtNodes = len(s.allowlistIndex)
}

totalNodes := numHmyNodes + numExtNodes
// (totalNodes + next%totalNodes) can convert negitive 'next' to positive
nth = (nth + totalNodes + next%totalNodes) % totalNodes
if nth < numHmyNodes {
idx = nth
} else {
// find index of external slot key
idx = s.allowlistIndex[nth-numHmyNodes]
}
return found, &s.publicKeys[idx]
}

// FirstParticipant returns the first participant of the shard
func (s *cIdentities) FirstParticipant(instance shardingconfig.Instance) *bls.PublicKeyWrapper {
return &s.publicKeys[0]
Expand All @@ -323,12 +284,6 @@ func (s *cIdentities) UpdateParticipants(pubKeys, allowlist []bls.PublicKeyWrapp
for i := range pubKeys {
keyIndexMap[pubKeys[i].Bytes] = i
}
for _, key := range allowlist {
if i, exist := keyIndexMap[key.Bytes]; exist {
s.allowlistIndex = append(s.allowlistIndex, i)
}
}
sort.Ints(s.allowlistIndex)
s.publicKeys = pubKeys
s.keyIndexMap = keyIndexMap
}
Expand Down
6 changes: 0 additions & 6 deletions consensus/quorum/thread_safe_decider.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ func (a threadSafeDeciderImpl) NthNextHmy(instance shardingconfig.Instance, pubk
return a.decider.NthNextHmy(instance, pubkey, next)
}

func (a threadSafeDeciderImpl) NthNextHmyExt(instance shardingconfig.Instance, wrapper *bls.PublicKeyWrapper, i int) (bool, *bls.PublicKeyWrapper) {
a.mu.Lock()
defer a.mu.Unlock()
return a.decider.NthNextHmyExt(instance, wrapper, i)
}

func (a threadSafeDeciderImpl) FirstParticipant(instance shardingconfig.Instance) *bls.PublicKeyWrapper {
a.mu.Lock()
defer a.mu.Unlock()
Expand Down