Skip to content

Commit

Permalink
add pruning tests
Browse files Browse the repository at this point in the history
  • Loading branch information
srene committed Sep 3, 2024
1 parent 901cc34 commit 920e68f
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 1 deletion.
77 changes: 77 additions & 0 deletions indexers/blockindexer/kv/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/types"
"golang.org/x/exp/rand"

blockidxkv "github.com/dymensionxyz/dymint/indexers/blockindexer/kv"
"github.com/dymensionxyz/dymint/store"
Expand Down Expand Up @@ -144,3 +145,79 @@ func TestBlockIndexer(t *testing.T) {
})
}
}

func TestBlockIndexerPruning(t *testing.T) {

// init the block indexer
prefixStore := store.NewPrefixKV(store.NewDefaultInMemoryKVStore(), []byte("block_events"))
indexer := blockidxkv.New(prefixStore)
numBlocks := 100

// index block event data
for i := 1; i <= numBlocks; i++ {
indexer.Index(types.EventDataNewBlockHeader{
Header: types.Header{Height: int64(i)},
ResultBeginBlock: getBeginBlock(),
ResultEndBlock: getEndBlock(),
})
}

// query all blocks and receive events for all block heights
queryString := fmt.Sprintf("block.height <= %d", numBlocks)
q := query.MustParse(queryString)
results, err := indexer.Search(context.Background(), q)
require.NoError(t, err)
require.Equal(t, numBlocks, len(results))

// prune indexer for all heights
pruned, err := indexer.Prune(1, int64(numBlocks+1))
require.NoError(t, err)
require.Equal(t, uint64(numBlocks), pruned)

// check the query returns empty
results, err = indexer.Search(context.Background(), q)
require.NoError(t, err)
require.Equal(t, 0, len(results))
}

func getBeginBlock() abci.ResponseBeginBlock {
if rand.Intn(2) == 1 {
return abci.ResponseBeginBlock{
Events: []abci.Event{
{
Type: "begin_event",
Attributes: []abci.EventAttribute{
{
Key: []byte("proposer"),
Value: []byte("FCAA001"),
Index: true,
},
},
},
},
}
} else {
return abci.ResponseBeginBlock{}
}
}

func getEndBlock() abci.ResponseEndBlock {
if rand.Intn(2) == 1 {
return abci.ResponseEndBlock{
Events: []abci.Event{
{
Type: "end_event",
Attributes: []abci.EventAttribute{
{
Key: []byte("foo"),
Value: []byte("value"),
Index: true,
},
},
},
},
}
} else {
return abci.ResponseEndBlock{}
}
}
2 changes: 1 addition & 1 deletion indexers/txindex/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func startKey(fields ...interface{}) []byte {
}

func prefixForHeight(height int64) []byte {
return []byte(fmt.Sprintf("%s/%d",
return []byte(fmt.Sprintf("%s/%d/",
tmtypes.TxHeightKey,
height,
))
Expand Down
81 changes: 81 additions & 0 deletions indexers/txindex/kv/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/rand"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/pubsub/query"
Expand All @@ -19,6 +20,8 @@ import (
"github.com/dymensionxyz/dymint/store"
)

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func TestTxIndex(t *testing.T) {
indexer := NewTxIndex(store.NewDefaultInMemoryKVStore())

Expand Down Expand Up @@ -312,6 +315,43 @@ func TestTxSearchMultipleTxs(t *testing.T) {
require.Len(t, results, 3)
}

func TestTxIndexerPruning(t *testing.T) {

// init the block indexer
indexer := NewTxIndex(store.NewDefaultInMemoryKVStore())
numBlocks := 100

txsWithEvents := 0
eventCounter := 0
// index tx event data
for i := 1; i <= numBlocks; i++ {
events := getRandomEvent(rand.Intn(10))
txResult := getRandomTxResult(int64(i), events)
err := indexer.Index(txResult)
require.NoError(t, err)
if len(events) > 0 {
txsWithEvents++
}
eventCounter += len(events)
}

// query all blocks and receive events for all txs
results, err := indexer.Search(context.Background(), query.MustParse("account.number = 1"))
require.NoError(t, err)
require.Equal(t, txsWithEvents, len(results))

// prune indexer for all heights
pruned, err := indexer.Prune(1, int64(numBlocks+1))
require.NoError(t, err)
require.Equal(t, uint64(numBlocks), pruned)

// check the query returns empty
results, err = indexer.Search(context.Background(), query.MustParse("account.number = 1"))
require.NoError(t, err)
require.Equal(t, 0, len(results))

}

func txResultWithEvents(events []abci.Event) *abci.TxResult {
tx := types.Tx("HELLO WORLD")
return &abci.TxResult{
Expand All @@ -326,6 +366,47 @@ func txResultWithEvents(events []abci.Event) *abci.TxResult {
},
}
}
func getRandomTxResult(height int64, events []abci.Event) *abci.TxResult {
tx := types.Tx(randStringBytesRmndr(60))
fmt.Println(tx.Hash())
return &abci.TxResult{
Height: height,
Index: 0,
Tx: tx,
Result: abci.ResponseDeliverTx{
Data: []byte{0},
Code: abci.CodeTypeOK,
Log: "",
Events: events,
},
}
}

func getRandomEvent(n int) []abci.Event {
events := []abci.Event{}
for i := 0; i < n; i++ {
event := abci.Event{
Type: "account",
Attributes: []abci.EventAttribute{
{
Key: []byte("number"),
Value: []byte("1"),
Index: true,
},
},
}
events = append(events, event)
}
return events
}

func randStringBytesRmndr(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}

func benchmarkTxIndex(txsCount int64, b *testing.B) {
dir, err := os.MkdirTemp("", "tx_index_db")
Expand Down

0 comments on commit 920e68f

Please sign in to comment.