Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: chainstore: store tipsetkeys in the blockstore #9904

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 20 additions & 34 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 @@ -650,18 +648,6 @@ func (cs *ChainStore) takeHeaviestTipSet(ctx context.Context, ts *types.TipSet)
return err
}

tskBlk, err := ts.Key().ToStorageBlock()
if err != nil {
log.Errorf("failed to create a block from tsk: %s", ts.Key())
return err
}

err = cs.chainLocalBlockstore.Put(ctx, tskBlk)
if err != nil {
log.Errorf("failed to put block for tsk: %s", ts.Key())
return err
}

return nil
}

Expand Down Expand Up @@ -971,7 +957,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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not something we want to refactor now, but PutTipSet and PersistTipSet are confusing.

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 +1042,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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may have performance implications (although it may also have been a premature optimization?).

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