2020-06-11 05:15:46 +00:00
|
|
|
package store_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2021-01-29 20:01:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
2020-06-11 05:15:46 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/gen"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types/mock"
|
|
|
|
datastore "github.com/ipfs/go-datastore"
|
|
|
|
syncds "github.com/ipfs/go-datastore/sync"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIndexSeeks(t *testing.T) {
|
|
|
|
cg, err := gen.NewGenerator()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gencar, err := cg.GenesisCar()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gen := cg.Genesis()
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
nbs := blockstore.NewMemorySync()
|
2021-07-27 13:30:23 +00:00
|
|
|
cs := store.NewChainStore(nbs, nbs, syncds.MutexWrap(datastore.NewMapDatastore()), nil)
|
2020-11-16 22:22:08 +00:00
|
|
|
defer cs.Close() //nolint:errcheck
|
2020-06-11 05:15:46 +00:00
|
|
|
|
|
|
|
_, err = cs.Import(bytes.NewReader(gencar))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cur := mock.TipSet(gen)
|
|
|
|
if err := cs.PutTipSet(ctx, mock.TipSet(gen)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-20 04:49:10 +00:00
|
|
|
assert.NoError(t, cs.SetGenesis(gen))
|
2020-06-11 05:15:46 +00:00
|
|
|
|
2020-06-11 12:49:48 +00:00
|
|
|
// Put 113 blocks from genesis
|
|
|
|
for i := 0; i < 113; i++ {
|
2020-06-11 05:15:46 +00:00
|
|
|
nextts := mock.TipSet(mock.MkBlock(cur, 1, 1))
|
|
|
|
|
|
|
|
if err := cs.PutTipSet(ctx, nextts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cur = nextts
|
|
|
|
}
|
|
|
|
|
2020-06-11 12:49:48 +00:00
|
|
|
// Put 50 null epochs + 1 block
|
2020-06-11 05:15:46 +00:00
|
|
|
skip := mock.MkBlock(cur, 1, 1)
|
|
|
|
skip.Height += 50
|
|
|
|
|
|
|
|
skipts := mock.TipSet(skip)
|
|
|
|
|
|
|
|
if err := cs.PutTipSet(ctx, skipts); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := cs.GetTipsetByHeight(ctx, skip.Height-10, skipts, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-11 12:49:48 +00:00
|
|
|
assert.Equal(t, abi.ChainEpoch(164), ts.Height())
|
2020-06-11 05:15:46 +00:00
|
|
|
|
2020-06-11 12:49:48 +00:00
|
|
|
for i := 0; i <= 113; i++ {
|
2020-06-11 05:17:29 +00:00
|
|
|
ts3, err := cs.GetTipsetByHeight(ctx, abi.ChainEpoch(i), skipts, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, abi.ChainEpoch(i), ts3.Height())
|
2020-06-11 05:15:46 +00:00
|
|
|
}
|
|
|
|
}
|