Skip to content

Commit

Permalink
Fix codecov for `rpc: index block events to support block event queri…
Browse files Browse the repository at this point in the history
…es (bp #6226) (#6261)`
  • Loading branch information
tnasu committed Dec 20, 2021
1 parent a76c5f8 commit df3f303
Show file tree
Hide file tree
Showing 4 changed files with 492 additions and 0 deletions.
58 changes: 58 additions & 0 deletions light/rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,64 @@ func TestABCIQuery(t *testing.T) {
assert.NotNil(t, res)
}

func TestTxSearch(t *testing.T) {

query := "query/test"
prove := false
page := 0
perPage := 1
orderBy := ""

next := &rpcmock.Client{}
next.On(
"TxSearch",
context.Background(),
query,
prove,
&page,
&perPage,
orderBy,
).Return(&ctypes.ResultTxSearch{
Txs: nil,
TotalCount: 0,
}, nil)

lc := &lcmock.LightClient{}

c := NewClient(next, lc)
res, err := c.TxSearch(context.Background(), query, prove, &page, &perPage, orderBy)
require.NoError(t, err)
assert.NotNil(t, res)
}

func TestBlockSearch(t *testing.T) {

query := "query/test"
page := 0
perPage := 1
orderBy := ""

next := &rpcmock.Client{}
next.On(
"BlockSearch",
context.Background(),
query,
&page,
&perPage,
orderBy,
).Return(&ctypes.ResultBlockSearch{
Blocks: nil,
TotalCount: 0,
}, nil)

lc := &lcmock.LightClient{}

c := NewClient(next, lc)
res, err := c.BlockSearch(context.Background(), query, &page, &perPage, orderBy)
require.NoError(t, err)
assert.NotNil(t, res)
}

type testOp struct {
Spec *ics23.ProofSpec
Key []byte
Expand Down
36 changes: 36 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,42 @@ func TestNodeNewNodeCustomReactors(t *testing.T) {
assert.Equal(t, customBlockchainReactor, n.Switch().Reactor("BLOCKCHAIN"))
}

func TestNodeNewNodeTxIndexIndexer(t *testing.T) {
config := cfg.ResetTestRoot("node_new_node_tx_index_indexer_test")
defer os.RemoveAll(config.RootDir)

doTest := func(doProvider func(ctx *DBContext) (dbm.DB, error)) (*Node, error) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
require.NoError(t, err)

pvKey, _ := privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(),
config.PrivValidatorKeyType())
return NewNode(config,
pvKey,
nodeKey,
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()),
DefaultGenesisDocProviderFunc(config),
doProvider,
DefaultMetricsProvider(config.Instrumentation),
log.TestingLogger(),
)
}

{
// Change to panic-provider for test
n, err := doTest(func(ctx *DBContext) (dbm.DB, error) { return nil, fmt.Errorf("test error") })
require.Error(t, err)
require.Nil(t, n)
}
{
// Change to non-default-value for test
config.TxIndex.Indexer = ""
n, err := doTest(DefaultDBProvider)
require.NoError(t, err)
require.NotNil(t, n)
}
}

func state(nVals int, height int64) (sm.State, dbm.DB, []types.PrivValidator) {
privVals := make([]types.PrivValidator, nVals)
vals := make([]types.GenesisValidator, nVals)
Expand Down
204 changes: 204 additions & 0 deletions rpc/core/blocks_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package core

import (
errors "errors"
"fmt"
"os"
"testing"
"time"

txidxkv "github.com/line/ostracon/state/txindex/kv"

cfg "github.com/line/ostracon/config"
"github.com/line/ostracon/crypto"
tmrand "github.com/line/ostracon/libs/rand"
blockidxkv "github.com/line/ostracon/state/indexer/block/kv"
blockidxnull "github.com/line/ostracon/state/indexer/block/null"
"github.com/line/ostracon/store"

"github.com/line/tm-db/v2/memdb"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -114,6 +126,198 @@ func TestBlockResults(t *testing.T) {
}
}

func TestBlockSearchByBlockHeightQuery(t *testing.T) {
height := int64(1)
ctx := &rpctypes.Context{}

q := fmt.Sprintf("%s=%d", types.BlockHeightKey, height)
page := 1
perPage := 10
orderBy := TestOrderByDefault

state, cleanup := makeTestState()
defer cleanup()

{
// Get by block.height (not search/range)
res, err := BlockSearch(ctx, q, &page, &perPage, orderBy)

require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 0, res.TotalCount) // Don't have height in db
require.Equal(t, 0, len(res.Blocks))
}

numToMakeBlocks := 1
numToGet := 1
// Save blocks
storeTestBlocks(height, int64(numToMakeBlocks), 0, state, time.Now())

{
// Get by block.height (not search/range)
res, err := BlockSearch(ctx, q, &page, &perPage, orderBy)

require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, numToMakeBlocks, res.TotalCount) // Get
require.Equal(t, numToGet, len(res.Blocks))
}
}

func TestBlockSearchByRangeQuery(t *testing.T) {
height := int64(1)
ctx := &rpctypes.Context{}

q := fmt.Sprintf("%s>=%d", types.BlockHeightKey, height)
page := 1
perPage := 10
orderBy := TestOrderByDefault

state, cleanup := makeTestState()
defer cleanup()

numToMakeBlocks := 15
numToGet := perPage
// Save blocks
storeTestBlocks(height, int64(numToMakeBlocks), 0, state, time.Now())

{
// Search blocks by range query with desc (default)
res, err := BlockSearch(ctx, q, &page, &perPage, orderBy)

require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, numToMakeBlocks, res.TotalCount)
require.Equal(t, numToGet, len(res.Blocks))
require.Equal(t, int64(numToMakeBlocks), res.Blocks[0].Block.Height)
require.Equal(t, height+int64(numToMakeBlocks-numToGet), res.Blocks[numToGet-1].Block.Height)
}
{
orderBy = TestOrderByAsc
// Search blocks by range query with asc
res, err := BlockSearch(ctx, q, &page, &perPage, orderBy)

require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, numToMakeBlocks, res.TotalCount)
require.Equal(t, numToGet, len(res.Blocks))
require.Equal(t, height, res.Blocks[0].Block.Height)
require.Equal(t, int64(numToGet), res.Blocks[numToGet-1].Block.Height)
}
}

func TestBlockSearch_errors(t *testing.T) {
ctx := &rpctypes.Context{}

q := ""
page := 0
perPage := 1
orderBy := "error"

{
// error: env.BlockIndexer.(*blockidxnull.BlockerIndexer)
env = &Environment{}
env.BlockIndexer = &blockidxnull.BlockerIndexer{}

res, err := BlockSearch(ctx, q, &page, &perPage, orderBy)

require.Error(t, err)
require.Equal(t, errors.New("block indexing is disabled"), err)
require.Nil(t, res)
}
{
// error: tmquery.New(query)
env = &Environment{}

res, err := BlockSearch(ctx, q, &page, &perPage, orderBy)

require.Error(t, err)
require.Equal(t,
"\nparse error near Unknown (line 1 symbol 1 - line 1 symbol 1):\n\"\"\n",
err.Error())
require.Nil(t, res)
}
{
// error: switch orderBy
env = &Environment{}
env.BlockIndexer = blockidxkv.New(memdb.NewDB())
q = fmt.Sprintf("%s>%d", types.BlockHeightKey, 1)

res, err := BlockSearch(ctx, q, &page, &perPage, orderBy)

require.Error(t, err)
require.Equal(t,
"expected order_by to be either `asc` or `desc` or empty",
err.Error())
require.Nil(t, res)
}
{
// error: validatePage(pagePtr, perPage, totalCount)
env = &Environment{}
env.BlockIndexer = blockidxkv.New(memdb.NewDB())
q = fmt.Sprintf("%s>%d", types.BlockHeightKey, 1)
orderBy = TestOrderByDesc

res, err := BlockSearch(ctx, q, &page, &perPage, orderBy)

require.Error(t, err)
require.Equal(t,
"page should be within [1, 1] range, given 0",
err.Error())
require.Nil(t, res)
}
}

func makeTestState() (sm.State, func()) {
config := cfg.ResetTestRoot("rpc_core_test")
env = &Environment{}
env.StateStore = sm.NewStore(memdb.NewDB())
env.BlockStore = store.NewBlockStore(memdb.NewDB())
env.BlockIndexer = blockidxkv.New(memdb.NewDB())
env.TxIndexer = txidxkv.NewTxIndex(memdb.NewDB())

state, _ := env.StateStore.LoadFromDBOrGenesisFile(config.GenesisFile())
return state, func() { os.RemoveAll(config.RootDir) }
}

func storeTestBlocks(startHeight, numToMakeBlocks, numToMakeTxs int64, state sm.State, timestamp time.Time) {
for i := int64(0); i < numToMakeBlocks; i++ {
commitSigs := []types.CommitSig{{
BlockIDFlag: types.BlockIDFlagCommit,
ValidatorAddress: tmrand.Bytes(crypto.AddressSize),
Timestamp: timestamp,
Signature: []byte("Signature"),
}}
height := startHeight + i
lastHeight := startHeight - 1
round := int32(0)
hash := []byte("")
partSize := uint32(2)
blockID := types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: partSize}}
proposer := state.Validators.SelectProposer(state.LastProofHash, startHeight, round)
txs := make([]types.Tx, numToMakeTxs)
for txIndex := int64(0); txIndex < numToMakeTxs; txIndex++ {
tx := []byte{byte(height), byte(txIndex)}
txs[txIndex] = tx
// Indexing
env.TxIndexer.Index(&abci.TxResult{Height: height, Index: uint32(txIndex), Tx: tx}) // nolint:errcheck
}
lastCommit := types.NewCommit(lastHeight, round, blockID, commitSigs)
block, _ := state.MakeBlock(height, txs, lastCommit, nil, proposer.Address, round, nil)
blockPart := block.MakePartSet(partSize)
// Indexing
env.BlockIndexer.Index(types.EventDataNewBlockHeader{Header: block.Header}) // nolint:errcheck
// Save
env.BlockStore.SaveBlock(block, blockPart, lastCommit)
}
}

const (
TestOrderByDefault = ""
TestOrderByDesc = "desc"
TestOrderByAsc = "asc"
)

type mockBlockStore struct {
height int64
}
Expand Down
Loading

0 comments on commit df3f303

Please sign in to comment.