sync: batching PersistHeaders
This commit is contained in:
parent
92b9f67327
commit
2d8ad4a1de
@ -269,7 +269,7 @@ func (cg *ChainGen) NextTipSetFromMiners(base *types.TipSet, miners []address.Ad
|
||||
return nil, xerrors.Errorf("making a block for next tipset failed: %w", err)
|
||||
}
|
||||
|
||||
if err := cg.cs.PersistBlockHeader(fblk.Header); err != nil {
|
||||
if err := cg.cs.PersistBlockHeaders(fblk.Header); err != nil {
|
||||
return nil, xerrors.Errorf("chainstore AddBlock: %w", err)
|
||||
}
|
||||
|
||||
|
@ -198,7 +198,7 @@ func (cs *ChainStore) SetGenesis(b *types.BlockHeader) error {
|
||||
|
||||
func (cs *ChainStore) PutTipSet(ctx context.Context, ts *types.TipSet) error {
|
||||
for _, b := range ts.Blocks() {
|
||||
if err := cs.PersistBlockHeader(b); err != nil {
|
||||
if err := cs.PersistBlockHeaders(b); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -423,13 +423,17 @@ func (cs *ChainStore) AddToTipSetTracker(b *types.BlockHeader) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *ChainStore) PersistBlockHeader(b *types.BlockHeader) error {
|
||||
sb, err := b.ToStorageBlock()
|
||||
if err != nil {
|
||||
return err
|
||||
func (cs *ChainStore) PersistBlockHeaders(b ...*types.BlockHeader) (err error) {
|
||||
sbs := make([]block.Block, len(b))
|
||||
|
||||
for i, header := range b {
|
||||
sbs[i], err = header.ToStorageBlock()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return cs.bs.Put(sb)
|
||||
return cs.bs.PutMany(sbs)
|
||||
}
|
||||
|
||||
type storable interface {
|
||||
@ -487,7 +491,7 @@ func (cs *ChainStore) expandTipset(b *types.BlockHeader) (*types.TipSet, error)
|
||||
}
|
||||
|
||||
func (cs *ChainStore) AddBlock(ctx context.Context, b *types.BlockHeader) error {
|
||||
if err := cs.PersistBlockHeader(b); err != nil {
|
||||
if err := cs.PersistBlockHeaders(b); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -1004,13 +1004,14 @@ func (syncer *Syncer) collectChain(ctx context.Context, ts *types.TipSet) error
|
||||
|
||||
syncer.syncState.SetStage(api.StagePersistHeaders)
|
||||
|
||||
toPersist := make([]*types.BlockHeader, 0, len(headers)*build.BlocksPerEpoch)
|
||||
for _, ts := range headers {
|
||||
for _, b := range ts.Blocks() {
|
||||
if err := syncer.store.PersistBlockHeader(b); err != nil {
|
||||
return xerrors.Errorf("failed to persist synced blocks to the chainstore: %w", err)
|
||||
}
|
||||
}
|
||||
toPersist = append(toPersist, ts.Blocks()...)
|
||||
}
|
||||
if err := syncer.store.PersistBlockHeaders(toPersist...); err != nil {
|
||||
return xerrors.Errorf("failed to persist synced blocks to the chainstore: %w", err)
|
||||
}
|
||||
toPersist = nil
|
||||
|
||||
syncer.syncState.SetStage(api.StageMessages)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user