Skip to content

Commit

Permalink
fix: unit test for splitstore
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZexiao committed Dec 29, 2023
1 parent b7ca555 commit 9623d93
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ gengen
dockerfile
coverage_unit.txt
coverage_venus_shared.txt
venus-shared/blockstore/splitstore/test_data/base_583_bafy2bzaceazuutcexhvwkyyesszohrkjjzk2zgknasgs7bb7zgfnwghtnu5w2.db/LOCK
109 changes: 80 additions & 29 deletions venus-shared/blockstore/splitstore/splitstore_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,61 @@
package splitstore

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
"testing"

blocks "github.com/ipfs/go-block-format"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/venus/venus-shared/types"
cid "github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
)

func TestNewSplitstore(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
tempBlocks := []blocks.Block{
newBlock("b1"),
newBlock("b2"),
newBlock("b3"),
newBlock("b4"),
newBlock("b4"),
mockTipset := []*types.TipSet{
newTipSet(100),
newTipSet(200),
newTipSet(300),
newTipSet(400),
newTipSet(500),
}

for i, b := range tempBlocks {
storePath := fmt.Sprintf("base_%d_%s.db", 10+i, b.Cid())
storePath = filepath.Join(tempDir, storePath)
err := os.MkdirAll(storePath, 0777)
var tskCids []cid.Cid
for i, ts := range mockTipset {
tsk := ts.Key()
tskCid, err := tsk.Cid()
require.NoError(t, err)
tskCids = append(tskCids, tskCid)

Check failure on line 35 in venus-shared/blockstore/splitstore/splitstore_test.go

View workflow job for this annotation

GitHub Actions / check

SA4010: this result of append is never used, except maybe in other appends (staticcheck)

s, err := newLayer(tempDir, int64(100+i*100), tskCid)
require.NoError(t, err)

rawBlk, err := ts.Blocks()[0].ToStorageBlock()
require.NoError(t, err)
err = s.Put(ctx, rawBlk)
require.NoError(t, err)

rawTsk, err := tsk.ToStorageBlock()
require.NoError(t, err)
err = s.Put(ctx, rawTsk)
require.NoError(t, err)

if s, ok := s.Blockstore.(Closer); ok {
err := s.Close()
require.NoError(t, err)
}
}

ss, err := NewSplitstore(tempDir, nil)
require.NoError(t, err)
require.Len(t, ss.layers, ss.maxLayerCount)
require.Equal(t, 5, ss.layers.Len())
}

func TestSethead(t *testing.T) {
Expand Down Expand Up @@ -60,42 +83,53 @@ func TestSethead(t *testing.T) {
}

func TestScan(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
tempBlocks := []blocks.Block{
newBlock("b1"),
newBlock("b2"),
newBlock("b3"),
mockTipset := []*types.TipSet{
newTipSet(100),
newTipSet(200),
newTipSet(300),
}

for i, b := range tempBlocks {
s, err := newLayer(tempDir, int64(10+i), b.Cid())
var tskCids []cid.Cid
for i, ts := range mockTipset {
tsk := ts.Key()
tskCid, err := tsk.Cid()
require.NoError(t, err)
tskCids = append(tskCids, tskCid)

s, err := newLayer(tempDir, int64(100+i*100), tskCid)
require.NoError(t, err)

rawBlk, err := ts.Blocks()[0].ToStorageBlock()
require.NoError(t, err)
err = s.Put(ctx, rawBlk)
require.NoError(t, err)

rawTsk, err := tsk.ToStorageBlock()
require.NoError(t, err)
err = s.Put(ctx, rawTsk)
require.NoError(t, err)

if s, ok := s.Blockstore.(Closer); ok {
err := s.Close()
require.NoError(t, err)
}
}

// base_init.db(place holder)
_, err := newLayer(tempDir, int64(0), cid.Undef)
require.NoError(t, err)

// any.db will not be scanned in
err = os.MkdirAll(filepath.Join(tempDir, "any.db"), 0777)
err := os.MkdirAll(filepath.Join(tempDir, "any.db"), 0777)
require.NoError(t, err)

bs, err := scan(tempDir)
require.NoError(t, err)

t.Run("scan in", func(t *testing.T) {
require.Len(t, bs, len(tempBlocks)+1)
require.Len(t, bs, len(mockTipset))

for i, b := range tempBlocks {
require.Equal(t, b.Cid(), bs[i+1].Base())
for i, c := range tskCids {
require.Equal(t, c, bs[i].Base())
}

// store from place holder should be empty
require.Nil(t, bs[0].Blockstore)
})

t.Run("clean up", func(t *testing.T) {
Expand Down Expand Up @@ -125,3 +159,20 @@ func TestExtractHeightAndCid(t *testing.T) {
_, _, err = extractHeightAndCid("base_10_bafy2bzacedyokdqa4mnkercuk5hcufi52w5q2xannm567ij2njiqovgwiicx6.db.del")
require.Error(t, err)
}

const blkRaw = `{"Miner":"t038057","Ticket":{"VRFProof":"kfggWR2GcEbfTuJ20hkAFNRbF7xusDuAQR7XwTjJ2/gc1rwIDmaXbSVxXe4j1njcCBoMhmlYIn9D/BLqQuIOayMHPYvDmOJGc9M27Hwg1UZkiuJmXji+iM/JBNYaOA61"},"ElectionProof":{"WinCount":1,"VRFProof":"tI7cWWM9sGsKc69N9DjN41glaO5Hg7r742H56FPzg7szbhTrxj8kw0OsiJzcPJdiAa6D5jZ1S2WKoLK7lwg2R5zYvCRwwWLGDiExqbqsvqmH5z/e6YGpaD7ghTPRH1SR"},"BeaconEntries":[{"Round":2118576,"Data":"rintMKcvVAslYpn9DcshDBmlPN6hUR+wjvVQSkkVUK5klx1kOSpcDvzODSc2wXFQA7BVbEcXJW/5KLoL0KHx2alLUWDOwxhsIQnAydXdZqG8G76nTIgogthfIMgSGdB2"}],"WinPoStProof":[{"PoStProof":3,"ProofBytes":"t0ZgPHFv0ao9fVZJ/fxbBrzATmOiIv9/IueSyAjtcqEpxqWViqchaaxnz1afwzAbhahpfZsGiGWyc408WYh7Q8u0Aa52KGPmUNtf3pAvxWfsUDMz9QUfhLZVg/p8/PUVC/O/E7RBNq4YPrRK5b6Q8PVwzIOxGOS14ge6ys8Htq+LfNJbcqY676qOYF4lzMuMtQIe3CxMSAEaEBfNpHhAEs83dO6vll9MZKzcXYpNWeqmMIz4xSdF18StQq9vL/Lo"}],"Parents":[{"/":"bafy2bzacecf4wtqz3kgumeowhdulejk3xbfzgibfyhs42x4vx2guqgudem2hg"},{"/":"bafy2bzacebkpxh2k63xreigl6a3ggdr2adwk67b4zw5dddckhqex2tmha6hee"},{"/":"bafy2bzacecor3xq4ykmhhrgq55rdo5w7up65elc4qwx5uwjy25ffynidskbxw"},{"/":"bafy2bzacedr2mztmef65fodqzvyjcdnsgpcjthstseinll4maqg24avnv7ljo"}],"ParentWeight":"21779626255","Height":1164251,"ParentStateRoot":{"/":"bafy2bzacecypgutbewmyop2wfuafvxt7dm7ew4u3ssy2p4rn457f6ynrj2i6a"},"ParentMessageReceipts":{"/":"bafy2bzaceaflsspsxuxew2y4g6o72wp5i2ewp3fcolga6n2plw3gycam7s4lg"},"Messages":{"/":"bafy2bzaceanux5ivzlxzvhqxtwc5vkktcfqepubwtwgv26dowzbl3rtgqk54k"},"BLSAggregate":{"Type":2,"Data":"lQg9jBfYhY2vvjB/RPlWg6i+MBTlH1u0lmdasiab5BigsKAuZSeLNlTGbdoVZhAsDUT59ZdGsMmueHjafygDUN2KLhZoChFf6LQHH42PTSXFlkRVHvmKVz9DDU03FLMB"},"Timestamp":1658988330,"BlockSig":{"Type":2,"Data":"rMOv2tXKqV5VDOq5IQ35cP0cCAzGmaugVr/g5JTrilhAn4LYK0h6ByPL5cX5ONzlDTx9+zYZFteIzaenirZhw7G510Lh0J8lbTLP5X2EX251rEA8dpkPZPcNylzN0r8X"},"ForkSignaling":0,"ParentBaseFee":"100"}`

func newTipSet(height abi.ChainEpoch) *types.TipSet {
var blk types.BlockHeader
err := json.Unmarshal([]byte(blkRaw), &blk)
if err != nil {
panic(err)
}
blk.Height = height

ts, err := types.NewTipSet([]*types.BlockHeader{&blk})
if err != nil {
panic(err)
}
return ts
}

0 comments on commit 9623d93

Please sign in to comment.