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

IOTEX-349 Reenable unit tests of ReadActiveBlockProducersByHeight and ReadCommitteeBlockProducersByHeight #770

Merged
merged 2 commits into from
Mar 19, 2019
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
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