Skip to content

Commit

Permalink
add changes to test modules
Browse files Browse the repository at this point in the history
  • Loading branch information
torao committed Oct 4, 2022
1 parent b679d60 commit 498f53d
Show file tree
Hide file tree
Showing 32 changed files with 622 additions and 153 deletions.
17 changes: 16 additions & 1 deletion abci/client/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion abci/types/mocks/application.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions blockchain/v0/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,14 @@ func newBlockchainReactor(
for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
lastCommit := types.NewCommit(blockHeight-1, 0, types.BlockID{}, nil)
if blockHeight > 1 {
lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
lastBlock := blockStore.LoadBlock(blockHeight - 1)
lastBlockMeta, err := blockStore.LoadBlockMeta(blockHeight - 1)
if err != nil {
panic(err)
}
lastBlock, err := blockStore.LoadBlock(blockHeight - 1)
if err != nil {
panic(err)
}

vote, err := types.MakeVote(
lastBlock.Header.Height,
Expand Down Expand Up @@ -183,7 +189,10 @@ func TestNoBlockResponse(t *testing.T) {
assert.Equal(t, maxBlockHeight, reactorPairs[0].reactor.store.Height())

for _, tt := range tests {
block := reactorPairs[1].reactor.store.LoadBlock(tt.height)
block, err := reactorPairs[1].reactor.store.LoadBlock(tt.height)
if err != nil {
panic(err)
}
if tt.existent {
assert.True(t, block != nil)
} else {
Expand Down
15 changes: 12 additions & 3 deletions blockchain/v1/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,14 @@ func newBlockchainReactor(
for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
lastCommit := types.NewCommit(blockHeight-1, 1, types.BlockID{}, nil)
if blockHeight > 1 {
lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
lastBlock := blockStore.LoadBlock(blockHeight - 1)
lastBlockMeta, err := blockStore.LoadBlockMeta(blockHeight - 1)
if err != nil {
panic(err)
}
lastBlock, err := blockStore.LoadBlock(blockHeight - 1)
if err != nil {
panic(err)
}

vote := makeVote(t, &lastBlock.Header, lastBlockMeta.BlockID, state.Validators, privVals[0])
lastCommit = types.NewCommit(vote.Height, vote.Round, lastBlockMeta.BlockID, []types.CommitSig{vote.CommitSig()})
Expand Down Expand Up @@ -234,7 +240,10 @@ func TestFastSyncNoBlockResponse(t *testing.T) {
assert.Equal(t, maxBlockHeight, reactorPairs[0].bcR.store.Height())

for _, tt := range tests {
block := reactorPairs[1].bcR.store.LoadBlock(tt.height)
block, err := reactorPairs[1].bcR.store.LoadBlock(tt.height)
if err != nil {
panic(err)
}
if tt.existent {
assert.True(t, block != nil, "height=%d, existent=%t", tt.height, tt.existent)
} else {
Expand Down
10 changes: 8 additions & 2 deletions blockchain/v2/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,14 @@ func newReactorStore(
for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
lastCommit := types.NewCommit(blockHeight-1, 0, types.BlockID{}, nil)
if blockHeight > 1 {
lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
lastBlock := blockStore.LoadBlock(blockHeight - 1)
lastBlockMeta, err := blockStore.LoadBlockMeta(blockHeight - 1)
if err != nil {
panic(err)
}
lastBlock, err := blockStore.LoadBlock(blockHeight - 1)
if err != nil {
panic(err)
}
vote, err := types.MakeVote(
lastBlock.Header.Height,
lastBlockMeta.BlockID,
Expand Down
15 changes: 12 additions & 3 deletions consensus/aggregate_signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ func TestAggregateSignature(t *testing.T) {
)
css := startConsensusAndMakeBlocks(t, nPeers, nVals, nValsWithComposite)
for _, state := range css {
block := state.blockStore.LoadBlock(2)
block, err := state.blockStore.LoadBlock(2)
if err != nil {
panic(err)
}

// validators are ed25519 only
for _, comsig := range block.LastCommit.Signatures {
Expand All @@ -73,7 +76,10 @@ func TestAggregateSignatureWithComposite(t *testing.T) {
css := startConsensusAndMakeBlocks(t, nPeers, nVals, nValsWithComposite)

for _, state := range css {
block := state.blockStore.LoadBlock(2)
block, err := state.blockStore.LoadBlock(2)
if err != nil {
panic(err)
}
// validators are composite only
for _, comsig := range block.LastCommit.Signatures {
require.Nil(t, comsig.Signature)
Expand All @@ -93,7 +99,10 @@ func TestAggregateSignatureWithMix(t *testing.T) {
css := startConsensusAndMakeBlocks(t, nPeers, nVals, nValsWithComposite)

for _, state := range css {
block := state.blockStore.LoadBlock(2)
block, err := state.blockStore.LoadBlock(2)
if err != nil {
panic(err)
}
// composite and ed25519 validators
cnt := 0
for _, comsig := range block.LastCommit.Signatures {
Expand Down
4 changes: 2 additions & 2 deletions consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,11 @@ OUTER_LOOP:
// If peer is lagging by more than 1, send Commit.
blockStoreBase := conR.conS.blockStore.Base()
if blockStoreBase > 0 && prs.Height != 0 && rs.Height >= prs.Height+2 && prs.Height >= blockStoreBase {
// Load the seen commit for prs.Height,
// Load the block commit for prs.Height,
// which contains precommit signatures for prs.Height.
// Originally the block commit was used, but with the addition of the BLS signature-aggregation,
// we use seen commit instead of the block commit because block commit has no individual signature.
commit, err := conR.conS.blockStore.LoadSeenCommit(prs.Height)
commit, err := conR.conS.blockStore.LoadBlockCommit(prs.Height)
if err != nil {
// BlockDB may be closed If the node is stopped during operation. For this reason, if the acquisition
// of seen commit fails and the reactor is stopped, we don't consider an error and terminate the
Expand Down
32 changes: 20 additions & 12 deletions consensus/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,12 @@ func TestSimulateValidatorsChange(t *testing.T) {
sim.Chain = make([]*types.Block, 0)
sim.Commits = make([]*types.Commit, 0)
for i := 1; i <= numBlocks; i++ {
sim.Chain = append(sim.Chain, css[0].blockStore.LoadBlock(int64(i)))
sim.Commits = append(sim.Commits, css[0].blockStore.LoadBlockCommit(int64(i)))
block, err := css[0].blockStore.LoadBlock(int64(i))
require.NoError(t, err)
sim.Chain = append(sim.Chain, block)
commit, err := css[0].blockStore.LoadBlockCommit(int64(i))
require.NoError(t, err)
sim.Commits = append(sim.Commits, commit)
}
}

Expand Down Expand Up @@ -1197,22 +1201,26 @@ func newMockBlockStore(config *cfg.Config, params tmproto.ConsensusParams) *mock
return &mockBlockStore{config, params, nil, nil, 0}
}

func (bs *mockBlockStore) Height() int64 { return int64(len(bs.chain)) }
func (bs *mockBlockStore) Base() int64 { return bs.base }
func (bs *mockBlockStore) Size() int64 { return bs.Height() - bs.Base() + 1 }
func (bs *mockBlockStore) LoadBaseMeta() *types.BlockMeta { return bs.LoadBlockMeta(bs.base) }
func (bs *mockBlockStore) LoadBlock(height int64) *types.Block { return bs.chain[height-1] }
func (bs *mockBlockStore) LoadBlockByHash(hash []byte) *types.Block {
return bs.chain[int64(len(bs.chain))-1]
func (bs *mockBlockStore) Height() int64 { return int64(len(bs.chain)) }
func (bs *mockBlockStore) Base() int64 { return bs.base }
func (bs *mockBlockStore) Size() int64 { return bs.Height() - bs.Base() + 1 }
func (bs *mockBlockStore) LoadBaseMeta() (*types.BlockMeta, error) { return bs.LoadBlockMeta(bs.base) }
func (bs *mockBlockStore) LoadBlock(height int64) (*types.Block, error) {
return bs.chain[height-1], nil
}
func (bs *mockBlockStore) LoadBlockByHash(hash []byte) (*types.Block, error) {
return bs.chain[int64(len(bs.chain))-1], nil
}
func (bs *mockBlockStore) LoadBlockMeta(height int64) *types.BlockMeta {
func (bs *mockBlockStore) LoadBlockMeta(height int64) (*types.BlockMeta, error) {
block := bs.chain[height-1]
return &types.BlockMeta{
BlockID: types.BlockID{Hash: block.Hash(), PartSetHeader: block.MakePartSet(types.BlockPartSizeBytes).Header()},
Header: block.Header,
}
}, nil
}
func (bs *mockBlockStore) LoadBlockPart(height int64, index int) (*types.Part, error) {
return nil, nil
}
func (bs *mockBlockStore) LoadBlockPart(height int64, index int) *types.Part { return nil }
func (bs *mockBlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) {
}
func (bs *mockBlockStore) LoadBlockCommit(height int64) (*types.Commit, error) {
Expand Down
41 changes: 35 additions & 6 deletions evidence/mocks/block_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions evidence/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestEvidencePoolBasic(t *testing.T) {

blockStore.On("LoadBlockMeta", mock.AnythingOfType("int64")).Return(
&types.BlockMeta{Header: types.Header{Time: defaultEvidenceTime}},
nil,
)
stateStore.On("LoadValidators", mock.AnythingOfType("int64")).Return(valSet, nil)
stateStore.On("LoadVoters", mock.AnythingOfType("int64"), state.VoterParams).Return(
Expand Down Expand Up @@ -129,7 +130,7 @@ func TestAddExpiredEvidence(t *testing.T) {
return &types.BlockMeta{Header: types.Header{Time: defaultEvidenceTime}}
}
return &types.BlockMeta{Header: types.Header{Time: expiredEvidenceTime}}
})
}, nil)

pool, err := evidence.NewPool(evidenceDB, stateStore, blockStore)
require.NoError(t, err)
Expand Down Expand Up @@ -280,10 +281,10 @@ func TestLightClientAttackEvidenceLifecycle(t *testing.T) {
trusted.ValidatorSet, ev.ConflictingBlock.VoterSet, state.VoterParams, state.LastProofHash, nil)
stateStore.On("Load").Return(state, nil)
blockStore := &mocks.BlockStore{}
blockStore.On("LoadBlockMeta", height).Return(&types.BlockMeta{Header: *trusted.Header})
blockStore.On("LoadBlockMeta", commonHeight).Return(&types.BlockMeta{Header: *common.Header})
blockStore.On("LoadBlockCommit", height).Return(trusted.Commit)
blockStore.On("LoadBlockCommit", commonHeight).Return(common.Commit)
blockStore.On("LoadBlockMeta", height).Return(&types.BlockMeta{Header: *trusted.Header}, nil)
blockStore.On("LoadBlockMeta", commonHeight).Return(&types.BlockMeta{Header: *common.Header}, nil)
blockStore.On("LoadBlockCommit", height).Return(trusted.Commit, nil)
blockStore.On("LoadBlockCommit", commonHeight).Return(common.Commit, nil)

pool, err := evidence.NewPool(dbm.NewMemDB(), stateStore, blockStore)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions evidence/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func TestReactorBroadcastEvidenceMemoryLeak(t *testing.T) {
blockStore := &mocks.BlockStore{}
blockStore.On("LoadBlockMeta", mock.AnythingOfType("int64")).Return(
&types.BlockMeta{Header: types.Header{Time: evidenceTime}},
nil,
)
val := types.NewMockPV(types.PrivKeyComposite) // TODO 🏺 need to test by all key types
stateStore := initializeValidatorState(val, 1)
Expand Down Expand Up @@ -251,6 +252,7 @@ func makeAndConnectReactorsAndPools(config *cfg.Config, stateStores []sm.Store)
blockStore := &mocks.BlockStore{}
blockStore.On("LoadBlockMeta", mock.AnythingOfType("int64")).Return(
&types.BlockMeta{Header: types.Header{Time: evidenceTime}},
nil,
)
pool, err := evidence.NewPool(evidenceDB, stateStores[i], blockStore)
if err != nil {
Expand Down
Loading

0 comments on commit 498f53d

Please sign in to comment.