Skip to content

Commit

Permalink
remove TestFinalityGadget
Browse files Browse the repository at this point in the history
  • Loading branch information
bap2pecs committed Nov 4, 2024
1 parent a467d81 commit 3d85e2f
Showing 1 changed file with 53 additions and 90 deletions.
143 changes: 53 additions & 90 deletions finalitygadget/finalitygadget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ import (
"go.uber.org/zap"
)

type TestFinalityGadget struct {
*FinalityGadget
}

// A helper function for testing only
func (tfg *TestFinalityGadget) InsertBlocks(blocks []*types.Block) error {
return tfg.FinalityGadget.insertBlocks(blocks)
}

// TODO: add `QueryIsBlockBabylonFinalizedFromBabylon` as test fn once removed from interface

func TestFinalityGadgetDisabled(t *testing.T) {
Expand All @@ -35,12 +26,10 @@ func TestFinalityGadgetDisabled(t *testing.T) {
mockCwClient := mocks.NewMockICosmWasmClient(ctl)
mockCwClient.EXPECT().QueryIsEnabled().Return(false, nil).Times(1)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
cwClient: mockCwClient,
bbnClient: nil,
btcClient: nil,
},
mockTestFinalityGadget := &FinalityGadget{
cwClient: mockCwClient,
bbnClient: nil,
btcClient: nil,
}

// check QueryIsBlockBabylonFinalized always returns true when finality gadget is not enabled
Expand Down Expand Up @@ -341,14 +330,12 @@ func TestInsertBlock(t *testing.T) {
mockDbHandler.EXPECT().InsertBlocks(blocks).Return(nil).Times(1)
mockDbHandler.EXPECT().GetBlockByHeight(block.BlockHeight).Return(block, nil).Times(1)

mockFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// insert block
err := mockFinalityGadget.InsertBlocks(blocks)
err := mockFinalityGadget.insertBlocks(blocks)
require.NoError(t, err)

// verify block was inserted
Expand Down Expand Up @@ -446,20 +433,18 @@ func TestBatchInsertBlocks(t *testing.T) {
}

// Create finality gadget instance with mock DB and specified batch size
mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
batchSize: tc.batchSize,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
batchSize: tc.batchSize,
}

// Test batch insert
err := mockTestFinalityGadget.insertBlocks(tc.blocks)
err := mockFinalityGadget.insertBlocks(tc.blocks)
require.NoError(t, err)

// Verify each block was inserted correctly
for _, block := range tc.blocks {
retrievedBlock, err := mockTestFinalityGadget.GetBlockByHeight(block.BlockHeight)
retrievedBlock, err := mockFinalityGadget.GetBlockByHeight(block.BlockHeight)
require.NoError(t, err)
require.NotNil(t, retrievedBlock)
require.Equal(t, block.BlockHeight, retrievedBlock.BlockHeight)
Expand All @@ -485,18 +470,16 @@ func TestGetBlockByHeight(t *testing.T) {
mockDbHandler.EXPECT().InsertBlocks(blocks).Return(nil).Times(1)
mockDbHandler.EXPECT().GetBlockByHeight(block.BlockHeight).Return(block, nil).Times(1)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// insert block
err := mockTestFinalityGadget.insertBlocks(blocks)
err := mockFinalityGadget.insertBlocks(blocks)
require.NoError(t, err)

// fetch block by height
retrievedBlock, err := mockTestFinalityGadget.GetBlockByHeight(1)
retrievedBlock, err := mockFinalityGadget.GetBlockByHeight(1)
require.NoError(t, err)
require.Equal(t, block.BlockHeight, retrievedBlock.BlockHeight)
require.Equal(t, block.BlockHash, retrievedBlock.BlockHash)
Expand All @@ -509,14 +492,12 @@ func TestGetBlockByHeightForNonExistentBlock(t *testing.T) {
mockDbHandler := mocks.NewMockIDatabaseHandler(ctl)
mockDbHandler.EXPECT().GetBlockByHeight(uint64(1)).Return(nil, types.ErrBlockNotFound).Times(1)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// fetch block by height
retrievedBlock, err := mockTestFinalityGadget.GetBlockByHeight(1)
retrievedBlock, err := mockFinalityGadget.GetBlockByHeight(1)
require.Nil(t, retrievedBlock)
require.Equal(t, err, types.ErrBlockNotFound)
}
Expand All @@ -536,25 +517,23 @@ func TestGetBlockByHashWith0xPrefix(t *testing.T) {
mockDbHandler.EXPECT().InsertBlocks(blocks).Return(nil).Times(1)
mockDbHandler.EXPECT().GetBlockByHash(normalizeBlockHash(block.BlockHash)).Return(block, nil).Times(2)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// insert block
err := mockTestFinalityGadget.insertBlocks(blocks)
err := mockFinalityGadget.insertBlocks(blocks)
require.NoError(t, err)

// fetch block by hash including 0x prefix
retrievedBlock, err := mockTestFinalityGadget.GetBlockByHash(block.BlockHash)
retrievedBlock, err := mockFinalityGadget.GetBlockByHash(block.BlockHash)
require.NoError(t, err)
require.Equal(t, block.BlockHeight, retrievedBlock.BlockHeight)
require.Equal(t, block.BlockHash, retrievedBlock.BlockHash)
require.Equal(t, block.BlockTimestamp, retrievedBlock.BlockTimestamp)

// fetch block by hash excluding 0x prefix
retrievedBlock, err = mockTestFinalityGadget.GetBlockByHash(strings.TrimPrefix(block.BlockHash, "0x"))
retrievedBlock, err = mockFinalityGadget.GetBlockByHash(strings.TrimPrefix(block.BlockHash, "0x"))
require.NoError(t, err)
require.Equal(t, block.BlockHeight, retrievedBlock.BlockHeight)
require.Equal(t, block.BlockHash, retrievedBlock.BlockHash)
Expand All @@ -576,25 +555,23 @@ func TestGetBlockByHashWithout0xPrefix(t *testing.T) {
mockDbHandler.EXPECT().InsertBlocks(blocks).Return(nil).Times(1)
mockDbHandler.EXPECT().GetBlockByHash(normalizeBlockHash(block.BlockHash)).Return(block, nil).Times(2)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// insert block
err := mockTestFinalityGadget.insertBlocks(blocks)
err := mockFinalityGadget.insertBlocks(blocks)
require.NoError(t, err)

// fetch block by hash including 0x prefix
retrievedBlock, err := mockTestFinalityGadget.GetBlockByHash("0x" + block.BlockHash)
retrievedBlock, err := mockFinalityGadget.GetBlockByHash("0x" + block.BlockHash)
require.NoError(t, err)
require.Equal(t, block.BlockHeight, retrievedBlock.BlockHeight)
require.Equal(t, block.BlockHash, retrievedBlock.BlockHash)
require.Equal(t, block.BlockTimestamp, retrievedBlock.BlockTimestamp)

// fetch block by hash excluding 0x prefix
retrievedBlock, err = mockTestFinalityGadget.GetBlockByHash(block.BlockHash)
retrievedBlock, err = mockFinalityGadget.GetBlockByHash(block.BlockHash)
require.NoError(t, err)
require.Equal(t, block.BlockHeight, retrievedBlock.BlockHeight)
require.Equal(t, block.BlockHash, retrievedBlock.BlockHash)
Expand All @@ -607,14 +584,12 @@ func TestGetBlockByHashForNonExistentBlock(t *testing.T) {
mockDbHandler := mocks.NewMockIDatabaseHandler(ctl)
mockDbHandler.EXPECT().GetBlockByHash(normalizeBlockHash("123")).Return(nil, types.ErrBlockNotFound).Times(1)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// fetch block by hash
retrievedBlock, err := mockTestFinalityGadget.GetBlockByHash("123")
retrievedBlock, err := mockFinalityGadget.GetBlockByHash("123")
require.Nil(t, retrievedBlock)
require.Equal(t, err, types.ErrBlockNotFound)
}
Expand All @@ -627,14 +602,12 @@ func TestQueryIsBlockFinalizedByHeight(t *testing.T) {
mockDbHandler := mocks.NewMockIDatabaseHandler(ctl)
mockDbHandler.EXPECT().QueryIsBlockFinalizedByHeight(blockHeight).Return(true, nil).Times(1)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// fetch block status by height
isFinalized, err := mockTestFinalityGadget.QueryIsBlockFinalizedByHeight(1)
isFinalized, err := mockFinalityGadget.QueryIsBlockFinalizedByHeight(1)
require.NoError(t, err)
require.True(t, isFinalized)
}
Expand All @@ -645,14 +618,12 @@ func TestQueryIsBlockFinalizedByHeightForNonExistentBlock(t *testing.T) {
mockDbHandler := mocks.NewMockIDatabaseHandler(ctl)
mockDbHandler.EXPECT().QueryIsBlockFinalizedByHeight(uint64(1)).Return(false, types.ErrBlockNotFound).Times(1)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// fetch block status by height
isFinalized, err := mockTestFinalityGadget.QueryIsBlockFinalizedByHeight(1)
isFinalized, err := mockFinalityGadget.QueryIsBlockFinalizedByHeight(1)
require.False(t, isFinalized)
require.Equal(t, err, types.ErrBlockNotFound)
}
Expand All @@ -663,19 +634,17 @@ func TestQueryIsBlockFinalizedByHashWith0xPrefix(t *testing.T) {
mockDbHandler := mocks.NewMockIDatabaseHandler(ctl)
mockDbHandler.EXPECT().QueryIsBlockFinalizedByHash(normalizeBlockHash("0x123")).Return(true, nil).Times(2)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// fetch block status by hash including 0x prefix
isFinalized, err := mockTestFinalityGadget.QueryIsBlockFinalizedByHash("0x123")
isFinalized, err := mockFinalityGadget.QueryIsBlockFinalizedByHash("0x123")
require.NoError(t, err)
require.True(t, isFinalized)

// fetch block status by hash excluding 0x prefix
isFinalized, err = mockTestFinalityGadget.QueryIsBlockFinalizedByHash("123")
isFinalized, err = mockFinalityGadget.QueryIsBlockFinalizedByHash("123")
require.NoError(t, err)
require.True(t, isFinalized)
}
Expand All @@ -686,19 +655,17 @@ func TestQueryIsBlockFinalizedByHashWithout0xPrefix(t *testing.T) {
mockDbHandler := mocks.NewMockIDatabaseHandler(ctl)
mockDbHandler.EXPECT().QueryIsBlockFinalizedByHash(normalizeBlockHash("123")).Return(true, nil).Times(2)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// fetch block status by hash including 0x prefix
isFinalized, err := mockTestFinalityGadget.QueryIsBlockFinalizedByHash("0x123")
isFinalized, err := mockFinalityGadget.QueryIsBlockFinalizedByHash("0x123")
require.NoError(t, err)
require.True(t, isFinalized)

// fetch block status by hash excluding 0x prefix
isFinalized, err = mockTestFinalityGadget.QueryIsBlockFinalizedByHash("123")
isFinalized, err = mockFinalityGadget.QueryIsBlockFinalizedByHash("123")
require.NoError(t, err)
require.True(t, isFinalized)
}
Expand All @@ -709,14 +676,12 @@ func TestQueryIsBlockFinalizedByHashForNonExistentBlock(t *testing.T) {
mockDbHandler := mocks.NewMockIDatabaseHandler(ctl)
mockDbHandler.EXPECT().QueryIsBlockFinalizedByHash(normalizeBlockHash("123")).Return(false, types.ErrBlockNotFound).Times(1)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// fetch block status by hash
isFinalized, err := mockTestFinalityGadget.QueryIsBlockFinalizedByHash("123")
isFinalized, err := mockFinalityGadget.QueryIsBlockFinalizedByHash("123")
require.False(t, isFinalized)
require.Equal(t, err, types.ErrBlockNotFound)
}
Expand All @@ -743,18 +708,16 @@ func TestQueryLatestFinalizedBlock(t *testing.T) {
mockDbHandler.EXPECT().InsertBlocks(blocks).Return(nil).Times(1)
mockDbHandler.EXPECT().QueryLatestFinalizedBlock().Return(normalizedSecond, nil).Times(1)

mockTestFinalityGadget := &TestFinalityGadget{
FinalityGadget: &FinalityGadget{
db: mockDbHandler,
},
mockFinalityGadget := &FinalityGadget{
db: mockDbHandler,
}

// insert two blocks
err := mockTestFinalityGadget.insertBlocks(blocks)
err := mockFinalityGadget.insertBlocks(blocks)
require.NoError(t, err)

// fetch latest block
latestBlock, err := mockTestFinalityGadget.QueryLatestFinalizedBlock()
latestBlock, err := mockFinalityGadget.QueryLatestFinalizedBlock()
require.NoError(t, err)
require.Equal(t, normalizedSecond.BlockHeight, latestBlock.BlockHeight)
require.Equal(t, normalizedSecond.BlockHash, latestBlock.BlockHash)
Expand Down

0 comments on commit 3d85e2f

Please sign in to comment.