Skip to content

Commit

Permalink
fix: store tipsetkeys in the blockstore
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Dec 17, 2022
1 parent c9f1b18 commit 69c9eaf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 44 deletions.
4 changes: 0 additions & 4 deletions chain/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,6 @@ func (cg *ChainGen) NextTipSetFromMinersWithMessagesAndNulls(base *types.TipSet,
return nil, xerrors.Errorf("making a block for next tipset failed: %w", err)
}

if err := cg.cs.PersistBlockHeaders(context.TODO(), fblk.Header); err != nil {
return nil, xerrors.Errorf("chainstore AddBlock: %w", err)
}

blks = append(blks, fblk)
}
}
Expand Down
42 changes: 20 additions & 22 deletions chain/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,8 @@ func (cs *ChainStore) SetGenesis(ctx context.Context, b *types.BlockHeader) erro
}

func (cs *ChainStore) PutTipSet(ctx context.Context, ts *types.TipSet) error {
for _, b := range ts.Blocks() {
if err := cs.PersistBlockHeaders(ctx, b); err != nil {
return err
}
if err := cs.PersistTipset(ctx, ts); err != nil {
return xerrors.Errorf("failed to persist tipset: %w", err)
}

expanded, err := cs.expandTipset(ctx, ts.Blocks()[0])
Expand Down Expand Up @@ -971,7 +969,24 @@ func (cs *ChainStore) AddToTipSetTracker(ctx context.Context, b *types.BlockHead
return nil
}

func (cs *ChainStore) PersistBlockHeaders(ctx context.Context, b ...*types.BlockHeader) error {
func (cs *ChainStore) PersistTipset(ctx context.Context, ts *types.TipSet) error {
if err := cs.persistBlockHeaders(ctx, ts.Blocks()...); err != nil {
return xerrors.Errorf("failed to persist block headers: %w", err)
}

tsBlk, err := ts.Key().ToStorageBlock()
if err != nil {
return xerrors.Errorf("failed to get tipset key block: %w", err)
}

if err = cs.chainLocalBlockstore.Put(ctx, tsBlk); err != nil {
return xerrors.Errorf("failed to put tipset key block: %w", err)
}

return nil
}

func (cs *ChainStore) persistBlockHeaders(ctx context.Context, b ...*types.BlockHeader) error {
sbs := make([]block.Block, len(b))

for i, header := range b {
Expand Down Expand Up @@ -1039,23 +1054,6 @@ func (cs *ChainStore) expandTipset(ctx context.Context, b *types.BlockHeader) (*
return types.NewTipSet(all)
}

func (cs *ChainStore) AddBlock(ctx context.Context, b *types.BlockHeader) error {
if err := cs.PersistBlockHeaders(ctx, b); err != nil {
return err
}

ts, err := cs.expandTipset(ctx, b)
if err != nil {
return err
}

if err := cs.MaybeTakeHeavierTipSet(ctx, ts); err != nil {
return xerrors.Errorf("MaybeTakeHeavierTipSet failed: %w", err)
}

return nil
}

func (cs *ChainStore) GetGenesis(ctx context.Context) (*types.BlockHeader, error) {
data, err := cs.metadataDs.Get(ctx, dstore.NewKey("0"))
if err != nil {
Expand Down
15 changes: 6 additions & 9 deletions chain/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (syncer *Syncer) InformNewHead(from peer.ID, fts *store.FullTipSet) bool {

// TODO: IMPORTANT(GARBAGE) this needs to be put in the 'temporary' side of
// the blockstore
if err := syncer.store.PersistBlockHeaders(ctx, fts.TipSet().Blocks()...); err != nil {
if err := syncer.store.PersistTipset(ctx, fts.TipSet()); err != nil {
log.Warn("failed to persist incoming block header: ", err)
return false
}
Expand Down Expand Up @@ -1198,16 +1198,13 @@ func (syncer *Syncer) collectChain(ctx context.Context, ts *types.TipSet, hts *t

ss.SetStage(api.StagePersistHeaders)

toPersist := make([]*types.BlockHeader, 0, len(headers)*int(build.BlocksPerEpoch))
for _, ts := range headers {
toPersist = append(toPersist, ts.Blocks()...)
}
if err := syncer.store.PersistBlockHeaders(ctx, toPersist...); err != nil {
err = xerrors.Errorf("failed to persist synced blocks to the chainstore: %w", err)
ss.Error(err)
return err
if err := syncer.store.PersistTipset(ctx, ts); err != nil {
err = xerrors.Errorf("failed to persist synced tipset to the chainstore: %w", err)
ss.Error(err)
return err
}
}
toPersist = nil

ss.SetStage(api.StageMessages)

Expand Down
10 changes: 5 additions & 5 deletions chain/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,13 @@ func (tu *syncTestUtil) addSourceNode(gen int) {
require.NoError(tu.t, err)
tu.t.Cleanup(func() { _ = stop(context.Background()) })

lastTs := blocks[len(blocks)-1].Blocks
for _, lastB := range lastTs {
cs := out.(*impl.FullNodeAPI).ChainAPI.Chain
lastTs := blocks[len(blocks)-1]
cs := out.(*impl.FullNodeAPI).ChainAPI.Chain
for _, lastB := range lastTs.Blocks {
require.NoError(tu.t, cs.AddToTipSetTracker(context.Background(), lastB.Header))
err = cs.AddBlock(tu.ctx, lastB.Header)
require.NoError(tu.t, err)
}
err = cs.PutTipSet(tu.ctx, lastTs.TipSet())
require.NoError(tu.t, err)

tu.genesis = genesis
tu.blocks = blocks
Expand Down
11 changes: 7 additions & 4 deletions cmd/lotus-sim/simulation/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ func (sim *Simulation) makeTipSet(ctx context.Context, messages []*types.Message
Timestamp: uts,
ElectionProof: &types.ElectionProof{WinCount: 1},
}}
err = sim.Node.Chainstore.PersistBlockHeaders(ctx, blks...)
if err != nil {
return nil, xerrors.Errorf("failed to persist block headers: %w", err)
}

newTipSet, err := types.NewTipSet(blks)
if err != nil {
return nil, xerrors.Errorf("failed to create new tipset: %w", err)
}

err = sim.Node.Chainstore.PersistTipset(ctx, newTipSet)
if err != nil {
return nil, xerrors.Errorf("failed to persist block headers: %w", err)
}

now := time.Now()
_, _, err = sim.StateManager.TipSetState(ctx, newTipSet)
if err != nil {
Expand Down

0 comments on commit 69c9eaf

Please sign in to comment.