From 4128c0990947d4c69944979626e6399a29ec6624 Mon Sep 17 00:00:00 2001 From: tony Date: Fri, 8 Apr 2022 19:51:15 +0800 Subject: [PATCH] use param max block proposers to pick authority --- builtin/authority/authority_test.go | 8 ++++---- packer/packer.go | 13 ++++++++++++- poa/candidates.go | 11 ++++++++++- poa/sched.go | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/builtin/authority/authority_test.go b/builtin/authority/authority_test.go index 70bcc7022..b4517c89a 100644 --- a/builtin/authority/authority_test.go +++ b/builtin/authority/authority_test.go @@ -40,13 +40,13 @@ func TestAuthority(t *testing.T) { {M(aut.Get(p1)), M(true, p1, thor.Bytes32{}, true, nil)}, {M(aut.Add(p2, p2, thor.Bytes32{})), M(true, nil)}, {M(aut.Add(p3, p3, thor.Bytes32{})), M(true, nil)}, - {M(aut.Candidates(big.NewInt(10), thor.MaxBlockProposers)), M( + {M(aut.Candidates(big.NewInt(10), thor.InitialMaxBlockProposers)), M( []*Candidate{{p1, p1, thor.Bytes32{}, true}, {p2, p2, thor.Bytes32{}, true}, {p3, p3, thor.Bytes32{}, true}}, nil, )}, - {M(aut.Candidates(big.NewInt(20), thor.MaxBlockProposers)), M( + {M(aut.Candidates(big.NewInt(20), thor.InitialMaxBlockProposers)), M( []*Candidate{{p2, p2, thor.Bytes32{}, true}, {p3, p3, thor.Bytes32{}, true}}, nil, )}, - {M(aut.Candidates(big.NewInt(30), thor.MaxBlockProposers)), M( + {M(aut.Candidates(big.NewInt(30), thor.InitialMaxBlockProposers)), M( []*Candidate{{p3, p3, thor.Bytes32{}, true}}, nil, )}, {M(aut.Candidates(big.NewInt(10), 2)), M( @@ -59,7 +59,7 @@ func TestAuthority(t *testing.T) { {M(aut.Get(p1)), M(true, p1, thor.Bytes32{}, true, nil)}, {M(aut.Revoke(p1)), M(true, nil)}, {M(aut.Get(p1)), M(false, p1, thor.Bytes32{}, false, nil)}, - {M(aut.Candidates(&big.Int{}, thor.MaxBlockProposers)), M( + {M(aut.Candidates(&big.Int{}, thor.InitialMaxBlockProposers)), M( []*Candidate{{p2, p2, thor.Bytes32{}, true}, {p3, p3, thor.Bytes32{}, true}}, nil, )}, } diff --git a/packer/packer.go b/packer/packer.go index eb2ceb966..ff1c1a74a 100644 --- a/packer/packer.go +++ b/packer/packer.go @@ -62,7 +62,18 @@ func (p *Packer) Schedule(parent *chain.BlockSummary, nowTimestamp uint64) (flow if err != nil { return nil, err } - candidates, err := authority.Candidates(endorsement, thor.MaxBlockProposers) + + mbp, err := builtin.Params.Native(state).Get(thor.KeyMaxBlockProposers) + if err != nil { + return nil, err + } + + maxBlockProposers := mbp.Uint64() + if maxBlockProposers == 0 || maxBlockProposers > thor.InitialMaxBlockProposers { + maxBlockProposers = thor.InitialMaxBlockProposers + } + + candidates, err := authority.Candidates(endorsement, maxBlockProposers) if err != nil { return nil, err } diff --git a/poa/candidates.go b/poa/candidates.go index e050d2a07..9cc9234f6 100644 --- a/poa/candidates.go +++ b/poa/candidates.go @@ -53,8 +53,17 @@ func (c *Candidates) Pick(state *state.State) ([]Proposer, error) { return nil, err } + mbp, err := builtin.Params.Native(state).Get(thor.KeyMaxBlockProposers) + if err != nil { + return nil, err + } + maxBlockProposers := mbp.Uint64() + if maxBlockProposers == 0 || maxBlockProposers > thor.InitialMaxBlockProposers { + maxBlockProposers = thor.InitialMaxBlockProposers + } + satisfied = make([]int, 0, len(c.list)) - for i := 0; i < len(c.list) && uint64(len(satisfied)) < thor.MaxBlockProposers; i++ { + for i := 0; i < len(c.list) && uint64(len(satisfied)) < maxBlockProposers; i++ { bal, err := state.GetBalance(c.list[i].Endorsor) if err != nil { return nil, err diff --git a/poa/sched.go b/poa/sched.go index 584321c49..f7401978a 100644 --- a/poa/sched.go +++ b/poa/sched.go @@ -112,7 +112,7 @@ func (s *SchedulerV1) Updates(newBlockTime uint64) (updates []Proposer, score ui toDeactivate := make(map[thor.Address]Proposer) t := newBlockTime - thor.BlockInterval - for i := uint64(0); i < thor.MaxBlockProposers && t > s.parentBlockTime; i++ { + for i := uint64(0); i < thor.InitialMaxBlockProposers && t > s.parentBlockTime; i++ { p := s.whoseTurn(t) if p.Address != s.proposer.Address { toDeactivate[p.Address] = p