Parametrise whether sync validators should use cache

This commit is contained in:
Aayush Rajasekaran 2020-09-30 01:39:06 -04:00
parent 73d193bd9c
commit c45c8f34a1
3 changed files with 19 additions and 22 deletions

View File

@ -597,7 +597,7 @@ func isPermanent(err error) bool {
return !errors.Is(err, ErrTemporal) return !errors.Is(err, ErrTemporal)
} }
func (syncer *Syncer) ValidateTipSet(ctx context.Context, fts *store.FullTipSet) error { func (syncer *Syncer) ValidateTipSet(ctx context.Context, fts *store.FullTipSet, useCache bool) error {
ctx, span := trace.StartSpan(ctx, "validateTipSet") ctx, span := trace.StartSpan(ctx, "validateTipSet")
defer span.End() defer span.End()
@ -613,7 +613,7 @@ func (syncer *Syncer) ValidateTipSet(ctx context.Context, fts *store.FullTipSet)
b := b // rebind to a scoped variable b := b // rebind to a scoped variable
futures = append(futures, async.Err(func() error { futures = append(futures, async.Err(func() error {
if err := syncer.ValidateBlock(ctx, b); err != nil { if err := syncer.ValidateBlock(ctx, b, useCache); err != nil {
if isPermanent(err) { if isPermanent(err) {
syncer.bad.Add(b.Cid(), NewBadBlockReason([]cid.Cid{b.Cid()}, err.Error())) syncer.bad.Add(b.Cid(), NewBadBlockReason([]cid.Cid{b.Cid()}, err.Error()))
} }
@ -680,7 +680,7 @@ func blockSanityChecks(h *types.BlockHeader) error {
} }
// ValidateBlock should match up with 'Semantical Validation' in validation.md in the spec // ValidateBlock should match up with 'Semantical Validation' in validation.md in the spec
func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) (err error) { func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock, useCache bool) (err error) {
defer func() { defer func() {
// b.Cid() could panic for empty blocks that are used in tests. // b.Cid() could panic for empty blocks that are used in tests.
if rerr := recover(); rerr != nil { if rerr := recover(); rerr != nil {
@ -689,13 +689,15 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) (er
} }
}() }()
isValidated, err := syncer.store.IsBlockValidated(ctx, b.Cid()) if useCache {
if err != nil { isValidated, err := syncer.store.IsBlockValidated(ctx, b.Cid())
return xerrors.Errorf("check block validation cache %s: %w", b.Cid(), err) if err != nil {
} return xerrors.Errorf("check block validation cache %s: %w", b.Cid(), err)
}
if isValidated { if isValidated {
return nil return nil
}
} }
validationStart := build.Clock.Now() validationStart := build.Clock.Now()
@ -959,8 +961,10 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) (er
return mulErr return mulErr
} }
if err := syncer.store.MarkBlockAsValidated(ctx, b.Cid()); err != nil { if useCache {
return xerrors.Errorf("caching block validation %s: %w", b.Cid(), err) if err := syncer.store.MarkBlockAsValidated(ctx, b.Cid()); err != nil {
return xerrors.Errorf("caching block validation %s: %w", b.Cid(), err)
}
} }
return nil return nil
@ -1462,7 +1466,7 @@ func (syncer *Syncer) syncMessagesAndCheckState(ctx context.Context, headers []*
return syncer.iterFullTipsets(ctx, headers, func(ctx context.Context, fts *store.FullTipSet) error { return syncer.iterFullTipsets(ctx, headers, func(ctx context.Context, fts *store.FullTipSet) error {
log.Debugw("validating tipset", "height", fts.TipSet().Height(), "size", len(fts.TipSet().Cids())) log.Debugw("validating tipset", "height", fts.TipSet().Height(), "size", len(fts.TipSet().Cids()))
if err := syncer.ValidateTipSet(ctx, fts); err != nil { if err := syncer.ValidateTipSet(ctx, fts, true); err != nil {
log.Errorf("failed to validate tipset: %+v", err) log.Errorf("failed to validate tipset: %+v", err)
return xerrors.Errorf("message processing failed: %w", err) return xerrors.Errorf("message processing failed: %w", err)
} }

View File

@ -732,7 +732,7 @@ func TestSyncInputs(t *testing.T) {
err := s.ValidateBlock(context.TODO(), &types.FullBlock{ err := s.ValidateBlock(context.TODO(), &types.FullBlock{
Header: &types.BlockHeader{}, Header: &types.BlockHeader{},
}) }, false)
if err == nil { if err == nil {
t.Fatal("should error on empty block") t.Fatal("should error on empty block")
} }
@ -741,7 +741,7 @@ func TestSyncInputs(t *testing.T) {
h.ElectionProof = nil h.ElectionProof = nil
err = s.ValidateBlock(context.TODO(), &types.FullBlock{Header: h}) err = s.ValidateBlock(context.TODO(), &types.FullBlock{Header: h}, false)
if err == nil { if err == nil {
t.Fatal("should error on block with nil election proof") t.Fatal("should error on block with nil election proof")
} }

View File

@ -138,14 +138,7 @@ func (a *SyncAPI) SyncValidateTipset(ctx context.Context, tsk types.TipSetKey) (
return false, err return false, err
} }
for _, blk := range tsk.Cids() { err = a.Syncer.ValidateTipSet(ctx, fts, false)
err = a.Syncer.ChainStore().UnmarkBlockAsValidated(ctx, blk)
if err != nil {
return false, err
}
}
err = a.Syncer.ValidateTipSet(ctx, fts)
if err != nil { if err != nil {
return false, err return false, err
} }