Skip to content

Commit

Permalink
chore: add genesis fetch tests
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <norman@samourai.coop>
  • Loading branch information
n0izn0iz committed Jul 24, 2024
1 parent 05af58a commit 763c68a
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
115 changes: 115 additions & 0 deletions fetch/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"testing"
"time"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
core_types "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
"github.com/gnolang/gno/tm2/pkg/bft/state"
"github.com/gnolang/gno/tm2/pkg/bft/types"

Check failure on line 15 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

dupImport: package is imported 2 times under different aliases on lines 15 and 16 (gocritic)

Check failure on line 15 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

ST1019: package "github.com/gnolang/gno/tm2/pkg/bft/types" is being imported more than once (stylecheck)
bft_types "github.com/gnolang/gno/tm2/pkg/bft/types"

Check failure on line 16 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

dupImport: package is imported 2 times under different aliases on lines 15 and 16 (gocritic)

Check failure on line 16 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

ST1019(related information): other import of "github.com/gnolang/gno/tm2/pkg/bft/types" (stylecheck)
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -903,6 +905,119 @@ func TestFetcher_InvalidBlocks(t *testing.T) {
assert.Len(t, capturedEvents, 0)
}

func TestFetcher_Genesis(t *testing.T) {
t.Parallel()

var (
txCount = 21
txs = generateTransactions(t, txCount)
savedBlocks = map[int64]*types.Block{}
savedTxs = map[string]*types.TxResult{}

capturedEvents = make([]*indexerTypes.NewBlock, 0)

mockEvents = &mockEvents{
signalEventFn: func(e events.Event) {
blockEvent, ok := e.(*indexerTypes.NewBlock)
require.True(t, ok)

capturedEvents = append(capturedEvents, blockEvent)
},
}

mockStorage = &mock.Storage{
GetLatestSavedHeightFn: func() (uint64, error) {
return 0, storageErrors.ErrNotFound
},
GetBlockFn: func(height uint64) (*types.Block, error) {
block, ok := savedBlocks[int64(height)]
require.True(t, ok)
return block, nil

Check failure on line 935 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

return with no blank line before (nlreturn)
},
GetTxFn: func(height uint64, index uint32) (*types.TxResult, error) {
tx, ok := savedTxs[fmt.Sprintf("%d-%d", height, index)]
require.True(t, ok)
return tx, nil

Check failure on line 940 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

return with no blank line before (nlreturn)
},
GetWriteBatchFn: func() storage.Batch {
return &mock.WriteBatch{
SetBlockFn: func(block *types.Block) error {
_, ok := savedBlocks[block.Height]
require.False(t, ok)
savedBlocks[block.Height] = block
return nil

Check failure on line 948 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

return with no blank line before (nlreturn)
},
SetTxFn: func(tx *types.TxResult) error {
savedTxs[fmt.Sprintf("%d-%d", tx.Height, tx.Index)] = tx
return nil

Check failure on line 952 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

return with no blank line before (nlreturn)
},
}
},
}

mockClient = &mockClient{
getLatestBlockNumberFn: func() (uint64, error) {
return 0, nil
},
getGenesisFn: func() (*core_types.ResultGenesis, error) {
localTxs := make([]std.Tx, len(txs))
for i, tx := range txs {
localTxs[i] = *tx
}
return &core_types.ResultGenesis{Genesis: &bft_types.GenesisDoc{AppState: gnoland.GnoGenesisState{

Check failure on line 967 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

return with no blank line before (nlreturn)
Txs: localTxs,
}}}, nil
},
getBlockResultsFn: func(num uint64) (*core_types.ResultBlockResults, error) {

Check failure on line 971 in fetch/fetch_test.go

View workflow job for this annotation

GitHub Actions / Go Linter / lint

unused-parameter: parameter 'num' seems to be unused, consider removing or renaming it as _ (revive)
return &core_types.ResultBlockResults{
Results: &state.ABCIResponses{
DeliverTxs: make([]abci.ResponseDeliverTx, len(txs)),
},
}, nil
},
}
)

f := New(mockStorage, mockClient, mockEvents)

require.NoError(t, f.FetchGenesisData())

_, err := mockStorage.GetBlock(0)
require.NoError(t, err)

for i := uint32(0); i < uint32(len(txs)); i++ {
tx, err := mockStorage.GetTx(0, i)
require.NoError(t, err)
expected := &types.TxResult{
Height: 0,
Index: i,
Tx: amino.MustMarshalJSON(txs[i]),
Response: abci.ResponseDeliverTx{},
}
require.Equal(t, expected, tx)
}
}

func TestFetcher_GenesisAlreadyFetched(t *testing.T) {
t.Parallel()

var (
mockEvents = &mockEvents{}

mockStorage = &mock.Storage{
GetLatestSavedHeightFn: func() (uint64, error) {
return 0, nil
},
}

mockClient = &mockClient{}
)

f := New(mockStorage, mockClient, mockEvents)

require.NoError(t, f.FetchGenesisData())
}

// generateTransactions generates dummy transactions
func generateTransactions(t *testing.T, count int) []*std.Tx {
t.Helper()
Expand Down
6 changes: 6 additions & 0 deletions fetch/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type (
getLatestBlockNumberDelegate func() (uint64, error)
getBlockDelegate func(uint64) (*core_types.ResultBlock, error)
getBlockResultsDelegate func(uint64) (*core_types.ResultBlockResults, error)
getGenesisDelegate func() (*core_types.ResultGenesis, error)

createBatchDelegate func() clientTypes.Batch
)
Expand All @@ -23,6 +24,7 @@ type mockClient struct {
getLatestBlockNumberFn getLatestBlockNumberDelegate
getBlockFn getBlockDelegate
getBlockResultsFn getBlockResultsDelegate
getGenesisFn getGenesisDelegate

createBatchFn createBatchDelegate
}
Expand All @@ -44,6 +46,10 @@ func (m *mockClient) GetBlock(blockNum uint64) (*core_types.ResultBlock, error)
}

func (m *mockClient) GetGenesis() (*core_types.ResultGenesis, error) {
if m.getGenesisFn != nil {
return m.getGenesisFn()
}

return &core_types.ResultGenesis{Genesis: &bft_types.GenesisDoc{AppState: gnoland.GnoGenesisState{}}}, nil
}

Expand Down

0 comments on commit 763c68a

Please sign in to comment.