Skip to content

Commit

Permalink
fix: using from instead of to (#968)
Browse files Browse the repository at this point in the history
* fix: using from instead of to

---------

Co-authored-by: otherview <[email protected]>
  • Loading branch information
darrenvechain and otherview authored Jan 30, 2025
1 parent 918b4ab commit 1b4a1b5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api/events/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func ConvertRange(chain *chain.Chain, r *Range) (*logdb.Range, error) {

toHeader := head
if r.To != nil {
toHeader, err = chain.FindBlockHeaderByTimestamp(*r.From, -1)
toHeader, err = chain.FindBlockHeaderByTimestamp(*r.To, -1)
if err != nil {
return nil, err
}
Expand Down
71 changes: 63 additions & 8 deletions api/events/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/vechain/thor/v2/logdb"
"github.com/vechain/thor/v2/test/testchain"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/tx"
)

func newRange(unit RangeType, from uint64, to uint64) *Range {
Expand All @@ -29,17 +28,53 @@ func newRange(unit RangeType, from uint64, to uint64) *Range {
func TestEventsTypes(t *testing.T) {
c := initChain(t)
for name, tt := range map[string]func(*testing.T, *testchain.Chain){
"testConvertRangeWithBlockRangeType": testConvertRangeWithBlockRangeType,
"testConvertRangeWithTimeRangeTypeLessThenGenesis": testConvertRangeWithTimeRangeTypeLessThenGenesis,
"testConvertRangeWithTimeRangeType": testConvertRangeWithTimeRangeType,
"testConvertRangeWithFromGreaterThanGenesis": testConvertRangeWithFromGreaterThanGenesis,
"testConvertRangeWithBlockRangeType": testConvertRangeWithBlockRangeType,
"testConvertRangeWithBlockRangeTypeMoreThanMaxBlockNumber": testConvertRangeWithBlockRangeTypeMoreThanMaxBlockNumber,
"testConvertRangeWithBlockRangeTypeWithSwitchedFromAndTo": testConvertRangeWithBlockRangeTypeWithSwitchedFromAndTo,
"testConvertRangeWithTimeRangeTypeLessThenGenesis": testConvertRangeWithTimeRangeTypeLessThenGenesis,
"testConvertRangeWithTimeRangeType": testConvertRangeWithTimeRangeType,
"testConvertRangeWithFromGreaterThanGenesis": testConvertRangeWithFromGreaterThanGenesis,
"testConvertRangeWithTimeRangeLessThanGenesisGreaterThanBest": testConvertRangeWithTimeRangeLessThanGenesisGreaterThanBest,
"testConvertRangeWithTimeRangeTypeWithSwitchedFromAndTo": testConvertRangeWithTimeRangeTypeWithSwitchedFromAndTo,
} {
t.Run(name, func(t *testing.T) {
tt(t, c)
})
}
}

func testConvertRangeWithTimeRangeLessThanGenesisGreaterThanBest(t *testing.T, chain *testchain.Chain) {
genesis := chain.GenesisBlock().Header()
bestBlock := chain.Repo().BestBlockSummary()

rng := newRange(TimeRangeType, genesis.Timestamp()-1_000, bestBlock.Header.Timestamp()+1_000)
expectedRange := &logdb.Range{
From: genesis.Number(),
To: bestBlock.Header.Number(),
}

convRng, err := ConvertRange(chain.Repo().NewBestChain(), rng)

assert.NoError(t, err)
assert.Equal(t, expectedRange, convRng)
}

func testConvertRangeWithTimeRangeTypeWithSwitchedFromAndTo(t *testing.T, chain *testchain.Chain) {
genesis := chain.GenesisBlock().Header()
bestBlock := chain.Repo().BestBlockSummary()

rng := newRange(TimeRangeType, bestBlock.Header.Timestamp(), genesis.Timestamp())
expectedRange := &logdb.Range{
From: bestBlock.Header.Number(),
To: genesis.Number(),
}

convRng, err := ConvertRange(chain.Repo().NewBestChain(), rng)

assert.NoError(t, err)
assert.Equal(t, expectedRange, convRng)
}

func testConvertRangeWithBlockRangeType(t *testing.T, chain *testchain.Chain) {
rng := newRange(BlockRangeType, 1, 2)

Expand All @@ -50,8 +85,27 @@ func testConvertRangeWithBlockRangeType(t *testing.T, chain *testchain.Chain) {
assert.Equal(t, uint32(*rng.To), convertedRng.To)
}

func testConvertRangeWithBlockRangeTypeMoreThanMaxBlockNumber(t *testing.T, chain *testchain.Chain) {
rng := newRange(BlockRangeType, logdb.MaxBlockNumber+1, logdb.MaxBlockNumber+2)

convertedRng, err := ConvertRange(chain.Repo().NewBestChain(), rng)

assert.NoError(t, err)
assert.Equal(t, &emptyRange, convertedRng)
}

func testConvertRangeWithBlockRangeTypeWithSwitchedFromAndTo(t *testing.T, chain *testchain.Chain) {
rng := newRange(BlockRangeType, logdb.MaxBlockNumber, 0)

convertedRng, err := ConvertRange(chain.Repo().NewBestChain(), rng)

assert.NoError(t, err)
assert.Equal(t, emptyRange.From, convertedRng.From)
assert.Equal(t, uint32(*rng.To), convertedRng.To)
}

func testConvertRangeWithTimeRangeTypeLessThenGenesis(t *testing.T, chain *testchain.Chain) {
rng := newRange(TimeRangeType, 100, 2200)
rng := newRange(TimeRangeType, chain.GenesisBlock().Header().Timestamp()-1000, chain.GenesisBlock().Header().Timestamp()-100)
expectedEmptyRange := &logdb.Range{
From: logdb.MaxBlockNumber,
To: logdb.MaxBlockNumber,
Expand Down Expand Up @@ -98,8 +152,9 @@ func initChain(t *testing.T) *testchain.Chain {
thorChain, err := testchain.NewIntegrationTestChain()
require.NoError(t, err)

require.NoError(t, thorChain.MintBlock(genesis.DevAccounts()[0], []*tx.Transaction{}...))
require.NoError(t, thorChain.MintBlock(genesis.DevAccounts()[0], []*tx.Transaction{}...))
for i := 0; i < 10; i++ {
require.NoError(t, thorChain.MintBlock(genesis.DevAccounts()[0]))
}

return thorChain
}
Expand Down

0 comments on commit 1b4a1b5

Please sign in to comment.