Skip to content

Commit

Permalink
IOTEX-349 Reenable unit tests of ReadActiveBlockProducersByHeight an…
Browse files Browse the repository at this point in the history
…d ReadCommitteeBlockProducersByHeight (#770)
  • Loading branch information
lizhefeng authored and zjshen14 committed Mar 20, 2019
1 parent 6feb897 commit b4695bf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
12 changes: 5 additions & 7 deletions action/protocol/poll/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (p *lifeLongDelegatesProtocol) readBlockProducers() ([]byte, error) {
}

type governanceChainCommitteeProtocol struct {
cm protocol.ChainManager
getBlockTime GetBlockTime
getEpochHeight GetEpochHeight
getEpochNum GetEpochNum
Expand All @@ -138,6 +139,7 @@ type governanceChainCommitteeProtocol struct {

// NewGovernanceChainCommitteeProtocol creates a Poll Protocol which fetch result from governance chain
func NewGovernanceChainCommitteeProtocol(
cm protocol.ChainManager,
electionCommittee committee.Committee,
initGravityChainHeight uint64,
getBlockTime GetBlockTime,
Expand All @@ -161,6 +163,7 @@ func NewGovernanceChainCommitteeProtocol(
}

return &governanceChainCommitteeProtocol{
cm: cm,
electionCommittee: electionCommittee,
initGravityChainHeight: initGravityChainHeight,
getBlockTime: getBlockTime,
Expand Down Expand Up @@ -276,14 +279,9 @@ func (p *governanceChainCommitteeProtocol) ReadState(
}

func (p *governanceChainCommitteeProtocol) readActiveBlockProducersByHeight(height uint64) ([]string, error) {
epochHeight := p.getEpochHeight(p.getEpochNum(height))
gravityHeight, err := p.getGravityHeight(epochHeight)
delegates, err := p.cm.CandidatesByHeight(height)
if err != nil {
return nil, errors.Wrap(err, "failed to get gravity chain height")
}
delegates, err := p.delegatesByGravityChainHeight(gravityHeight)
if err != nil {
return nil, errors.Wrapf(err, "failed to get delegates on height %d", height)
return nil, err
}
blockProducers := make([]string, 0)
for i, delegate := range delegates {
Expand Down
1 change: 1 addition & 0 deletions action/protocol/poll/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestInitialize(t *testing.T) {
r := types.NewElectionResultForTest(time.Now())
committee.EXPECT().ResultByHeight(uint64(123456)).Return(r, nil).Times(1)
p, err := NewGovernanceChainCommitteeProtocol(
nil,
committee,
uint64(123456),
func(uint64) (time.Time, error) { return time.Now(), nil },
Expand Down
30 changes: 23 additions & 7 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
"github.com/iotexproject/iotex-election/test/mock/mock_committee"
"github.com/iotexproject/iotex-election/types"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -42,6 +41,7 @@ import (
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/protogen/iotexapi"
"github.com/iotexproject/iotex-core/protogen/iotextypes"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/state/factory"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_blockchain"
Expand Down Expand Up @@ -790,10 +790,17 @@ func TestServer_ReadActiveBlockProducersByHeight(t *testing.T) {

ctrl := gomock.NewController(t)
defer ctrl.Finish()
mbc := mock_blockchain.NewMockBlockchain(ctrl)
committee := mock_committee.NewMockCommittee(ctrl)
r := types.NewElectionResultForTest(time.Now())
committee.EXPECT().ResultByHeight(gomock.Any()).Return(r, nil).Times(2)
committee.EXPECT().HeightByTime(gomock.Any()).Return(uint64(123456), nil).AnyTimes()
candidates := []*state.Candidate{
{
Address: "address1",
},
{
Address: "address2",
},
}
mbc.EXPECT().CandidatesByHeight(gomock.Any()).Return(candidates, nil).Times(2)

for _, test := range readActiveBlockProducersByHeightTests {
var pol poll.Protocol
Expand All @@ -802,6 +809,7 @@ func TestServer_ReadActiveBlockProducersByHeight(t *testing.T) {
pol = poll.NewLifeLongDelegatesProtocol(cfg.Genesis.Delegates)
} else {
pol, _ = poll.NewGovernanceChainCommitteeProtocol(
mbc,
committee,
uint64(123456),
func(uint64) (time.Time, error) { return time.Now(), nil },
Expand Down Expand Up @@ -833,10 +841,17 @@ func TestServer_ReadCommitteeBlockProducersByHeight(t *testing.T) {

ctrl := gomock.NewController(t)
defer ctrl.Finish()
mbc := mock_blockchain.NewMockBlockchain(ctrl)
committee := mock_committee.NewMockCommittee(ctrl)
r := types.NewElectionResultForTest(time.Now())
committee.EXPECT().ResultByHeight(gomock.Any()).Return(r, nil).Times(2)
committee.EXPECT().HeightByTime(gomock.Any()).Return(uint64(123456), nil).AnyTimes()
candidates := []*state.Candidate{
{
Address: "address1",
},
{
Address: "address2",
},
}
mbc.EXPECT().CandidatesByHeight(gomock.Any()).Return(candidates, nil).Times(2)

for _, test := range readCommitteeProducersByHeightTests {
var pol poll.Protocol
Expand All @@ -845,6 +860,7 @@ func TestServer_ReadCommitteeBlockProducersByHeight(t *testing.T) {
pol = poll.NewLifeLongDelegatesProtocol(cfg.Genesis.Delegates)
} else {
pol, _ = poll.NewGovernanceChainCommitteeProtocol(
mbc,
committee,
uint64(123456),
func(uint64) (time.Time, error) { return time.Now(), nil },
Expand Down
1 change: 1 addition & 0 deletions server/itx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func registerDefaultProtocols(cs *chainservice.ChainService, genesisConfig genes
var pollProtocol poll.Protocol
if genesisConfig.GravityChainStartHeight != 0 && electionCommittee != nil {
if pollProtocol, err = poll.NewGovernanceChainCommitteeProtocol(
cs.Blockchain(),
electionCommittee,
gravityChainStartHeight,
func(height uint64) (time.Time, error) {
Expand Down

0 comments on commit b4695bf

Please sign in to comment.