77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package store_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/filecoin-project/lotus/chain/gen"
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
"github.com/filecoin-project/lotus/chain/types/mock"
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
datastore "github.com/ipfs/go-datastore"
|
|
syncds "github.com/ipfs/go-datastore/sync"
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
|
"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()
|
|
|
|
nbs := blockstore.NewBlockstore(syncds.MutexWrap(datastore.NewMapDatastore()))
|
|
cs := store.NewChainStore(nbs, syncds.MutexWrap(datastore.NewMapDatastore()), nil)
|
|
|
|
_, 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)
|
|
}
|
|
cs.SetGenesis(gen)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
nextts := mock.TipSet(mock.MkBlock(cur, 1, 1))
|
|
|
|
if err := cs.PutTipSet(ctx, nextts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cur = nextts
|
|
}
|
|
|
|
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)
|
|
}
|
|
assert.Equal(t, abi.ChainEpoch(151), ts.Height())
|
|
|
|
ts2, err := cs.GetTipsetByHeight(ctx, 90, skipts, false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assert.Equal(t, abi.ChainEpoch(90), ts2.Height())
|
|
}
|