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

Leader rotation by VRF. Unpredictable leader schedule. #4665

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions consensus/consensus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ func (consensus *Consensus) commitBlock(blk *types.Block, committedMsg *FBFTMess

// rotateLeader rotates the leader to the next leader in the committee.
// This function must be called with enabled leader rotation.
func (consensus *Consensus) rotateLeader(epoch *big.Int, defaultKey *bls.PublicKeyWrapper) *bls.PublicKeyWrapper {
func (consensus *Consensus) rotateLeader(epoch *big.Int, vrf []byte, defaultKey *bls.PublicKeyWrapper) *bls.PublicKeyWrapper {
var (
bc = consensus.Blockchain()
leader = consensus.getLeaderPubKey()
Expand Down Expand Up @@ -740,7 +740,9 @@ func (consensus *Consensus) rotateLeader(epoch *big.Int, defaultKey *bls.PublicK
)

for i := 0; i < len(committee.Slots); i++ {
if bc.Config().IsLeaderRotationExternalValidatorsAllowed(epoch) {
if bc.Config().IsLeaderRotationVRF(epoch) {
wasFound, next = consensus.decider.NthNextVRF(committee.Slots, leader, vrf)
} else if bc.Config().IsLeaderRotationExternalValidatorsAllowed(epoch) {
wasFound, next = consensus.decider.NthNextValidator(committee.Slots, leader, offset)
} else {
wasFound, next = consensus.decider.NthNextHmy(shard.Schedule.InstanceForEpoch(epoch), leader, offset)
Expand Down Expand Up @@ -801,7 +803,7 @@ func (consensus *Consensus) setupForNewConsensus(blk *types.Block, committedMsg
epoch = blk.Epoch()
}
if consensus.Blockchain().Config().IsLeaderRotationInternalValidators(epoch) {
if next := consensus.rotateLeader(epoch, committedMsg.SenderPubkeys[0]); next != nil {
if next := consensus.rotateLeader(epoch, blk.Vrf(), committedMsg.SenderPubkeys[0]); next != nil {
prev := consensus.getLeaderPubKey()
consensus.setLeaderPubKey(next)
if consensus.isLeader() {
Expand Down
57 changes: 55 additions & 2 deletions consensus/quorum/quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"math/big"
"sort"

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

"github.com/ethereum/go-ethereum/common"
bls_core "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/consensus/votepower"
"github.com/harmony-one/harmony/crypto/bls"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
"github.com/harmony-one/harmony/internal/utils"
Expand Down Expand Up @@ -77,6 +76,9 @@ type ParticipantTracker interface {
ParticipantsCount() int64
// 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)

NthNextVRF(slotList shard.SlotList, pubKey *bls.PublicKeyWrapper, vrf []byte) (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
Expand Down Expand Up @@ -254,6 +256,57 @@ func (s *cIdentities) NthNextValidator(slotList shard.SlotList, pubKey *bls.Publ
}
}

func VrfRandomness(vrf []byte) []byte {
return vrf[:32]
}

func (s *cIdentities) NthNextVRF(slotList shard.SlotList, pubKey *bls.PublicKeyWrapper, vrf []byte) (bool, *bls.PublicKeyWrapper) {

if len(s.publicKeys) == 0 {
return false, pubKey
}

var (
bts = VrfRandomness(vrf)
val = new(big.Int).SetBytes(bts)
mod = val.Mod(val, big.NewInt(int64(len(s.publicKeys))))
// absolute value
next = int(mod.Abs(mod).Int64())
found = false
)
// sanity check to avoid out of bound access
if next < 0 {
return false, pubKey
}
fmt.Println("NthNextVRF", next)
utils.Logger().Debug().Int("next", next).Msg("NthNextVRF")

//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
//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
6 changes: 6 additions & 0 deletions consensus/quorum/thread_safe_decider.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func (a threadSafeDeciderImpl) NthNextValidator(slotList shard.SlotList, pubKey
return a.decider.NthNextValidator(slotList, pubKey, next)
}

func (a threadSafeDeciderImpl) NthNextVRF(slotList shard.SlotList, pubKey *bls.PublicKeyWrapper, vrf []byte) (bool, *bls.PublicKeyWrapper) {
a.mu.Lock()
defer a.mu.Unlock()
return a.decider.NthNextVRF(slotList, pubKey, vrf)
}

func (a threadSafeDeciderImpl) NthNextHmy(instance shardingconfig.Instance, pubkey *bls.PublicKeyWrapper, next int) (bool, *bls.PublicKeyWrapper) {
a.mu.Lock()
defer a.mu.Unlock()
Expand Down
8 changes: 8 additions & 0 deletions internal/params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ var (
AllowlistEpoch: EpochTBD,
LeaderRotationInternalValidatorsEpoch: big.NewInt(5),
LeaderRotationExternalValidatorsEpoch: big.NewInt(6),
LeaderRotationVRF: big.NewInt(6),
FeeCollectEpoch: big.NewInt(2),
ValidatorCodeFixEpoch: big.NewInt(2),
HIP30Epoch: EpochTBD,
Expand Down Expand Up @@ -350,6 +351,7 @@ var (
big.NewInt(1), // CrossShardXferPrecompileEpoch
big.NewInt(0), // AllowlistEpoch
big.NewInt(1), // LeaderRotationExternalNonBeaconLeaders
big.NewInt(1), // VRF
big.NewInt(1), // LeaderRotationExternalBeaconLeaders
big.NewInt(0), // FeeCollectEpoch
big.NewInt(0), // ValidatorCodeFixEpoch
Expand Down Expand Up @@ -398,6 +400,7 @@ var (
big.NewInt(1), // CrossShardXferPrecompileEpoch
big.NewInt(0), // AllowlistEpoch
big.NewInt(1), // LeaderRotationExternalNonBeaconLeaders
big.NewInt(1), // VRF
big.NewInt(1), // LeaderRotationExternalBeaconLeaders
big.NewInt(0), // FeeCollectEpoch
big.NewInt(0), // ValidatorCodeFixEpoch
Expand Down Expand Up @@ -547,6 +550,8 @@ type ChainConfig struct {

LeaderRotationInternalValidatorsEpoch *big.Int `json:"leader-rotation-internal-validators,omitempty"`

LeaderRotationVRF *big.Int `json:"leader-rotation-vrf,omitempty"`

LeaderRotationExternalValidatorsEpoch *big.Int `json:"leader-rotation-external-validators,omitempty"`

// FeeCollectEpoch is the first epoch that enables txn fees to be collected into the community-managed account.
Expand Down Expand Up @@ -823,6 +828,9 @@ func (c *ChainConfig) IsBlockGas30M(epoch *big.Int) bool {
func (c *ChainConfig) IsLeaderRotationExternalValidatorsAllowed(epoch *big.Int) bool {
return isForked(c.LeaderRotationExternalValidatorsEpoch, epoch)
}
func (c *ChainConfig) IsLeaderRotationVRF(epoch *big.Int) bool {
return isForked(c.LeaderRotationVRF, epoch)
}

// IsFeeCollectEpoch determines whether Txn Fees will be collected into the community-managed account.
func (c *ChainConfig) IsFeeCollectEpoch(epoch *big.Int) bool {
Expand Down