Skip to content

Commit

Permalink
fix cometbft tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Nov 7, 2024
1 parent 7089eab commit bae9ee7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
28 changes: 17 additions & 11 deletions server/v2/cometbft/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

var (
sum = sha256.Sum256([]byte("test-hash"))
emptyHash = sha256.Sum256([]byte(""))
DefaulConsensusParams = &v1.ConsensusParams{
Block: &v1.BlockParams{
MaxGas: 5000000,
Expand Down Expand Up @@ -124,6 +125,7 @@ func TestConsensus_InitChain_Without_UpdateParam(t *testing.T) {
_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{
Time: time.Now(),
Height: 1,
Hash: emptyHash[:],
})
require.NoError(t, err)
assertStoreLatestVersion(t, mockStore, 1)
Expand All @@ -144,6 +146,7 @@ func TestConsensus_InitChain_With_UpdateParam(t *testing.T) {
_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{
Time: time.Now(),
Height: 1,
Hash: emptyHash[:],
})
require.NoError(t, err)

Expand All @@ -159,15 +162,16 @@ func TestConsensus_InitChain_Invalid_Height(t *testing.T) {
InitialHeight: 2,
})
require.NoError(t, err)
assertStoreLatestVersion(t, mockStore, 0)
assertStoreLatestVersion(t, mockStore, 1)

// Shouldn't be able to commit genesis block 2
// Shouldn't be able to commit genesis block 3
_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{
Time: time.Now(),
Height: 2,
Height: 3,
Hash: emptyHash[:],
})
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), "unable to commit the changeset"))
require.True(t, strings.Contains(err.Error(), "invalid height"))
}

func TestConsensus_FinalizeBlock_Invalid_Height(t *testing.T) {
Expand All @@ -182,12 +186,14 @@ func TestConsensus_FinalizeBlock_Invalid_Height(t *testing.T) {
_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{
Time: time.Now(),
Height: 1,
Hash: emptyHash[:],
})
require.NoError(t, err)

_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{
Time: time.Now(),
Height: 3,
Hash: emptyHash[:],
})
require.Error(t, err)
}
Expand All @@ -206,6 +212,7 @@ func TestConsensus_FinalizeBlock_NoTxs(t *testing.T) {
_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{
Time: time.Now(),
Height: 1,
Hash: emptyHash[:],
})
require.NoError(t, err)

Expand Down Expand Up @@ -236,6 +243,7 @@ func TestConsensus_FinalizeBlock_MultiTxs_OutOfGas(t *testing.T) {
_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{
Time: time.Now(),
Height: 1,
Hash: emptyHash[:],
})
require.NoError(t, err)

Expand Down Expand Up @@ -267,6 +275,7 @@ func TestConsensus_FinalizeBlock_MultiTxs(t *testing.T) {
_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{
Time: time.Now(),
Height: 1,
Hash: emptyHash[:],
})
require.NoError(t, err)

Expand Down Expand Up @@ -554,6 +563,7 @@ func TestConsensus_Info(t *testing.T) {
_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{
Time: time.Now(),
Height: 1,
Hash: emptyHash[:],
})
require.NoError(t, err)

Expand Down Expand Up @@ -598,6 +608,7 @@ func TestConsensus_Query(t *testing.T) {
Time: time.Now(),
Height: 1,
Txs: [][]byte{mockTx.Bytes()},
Hash: emptyHash[:],
})
require.NoError(t, err)

Expand Down Expand Up @@ -695,17 +706,12 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.

// Check target version same with store's latest version
// And should have commit info of target version
// If block 0, commitInfo returned should be nil
func assertStoreLatestVersion(t *testing.T, store types.Store, target uint64) {
t.Helper()
version, err := store.GetLatestVersion()
require.NoError(t, err)
require.Equal(t, version, target)
require.Equal(t, target, version)
commitInfo, err := store.GetStateCommitment().GetCommitInfo(version)
require.NoError(t, err)
if target != 0 {
require.Equal(t, commitInfo.Version, target)
} else {
require.Nil(t, commitInfo)
}
require.Equal(t, target, commitInfo.Version)
}
4 changes: 1 addition & 3 deletions server/v2/cometbft/internal/mock/mock_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (s *MockStore) StateLatest() (uint64, corestore.ReaderMap, error) {
}

func (s *MockStore) Commit(changeset *corestore.Changeset) (corestore.Hash, error) {
v, _, _ := s.StateLatest()
err := s.Storage.ApplyChangeset(changeset)
if err != nil {
return []byte{}, err
Expand All @@ -70,8 +69,7 @@ func (s *MockStore) Commit(changeset *corestore.Changeset) (corestore.Hash, erro
return []byte{}, err
}

commitInfo, err := s.Committer.Commit(v + 1)
fmt.Println("commitInfo", commitInfo, err)
_, err = s.Committer.Commit(changeset.Version)
return []byte{}, err
}

Expand Down

0 comments on commit bae9ee7

Please sign in to comment.