diff --git a/chain/store/index.go b/chain/store/index.go index 7d642eb10..15d5d7025 100644 --- a/chain/store/index.go +++ b/chain/store/index.go @@ -30,6 +30,7 @@ func NewChainIndex(lts loadTipSetFunc) *ChainIndex { type lbEntry struct { ts *types.TipSet parentHeight abi.ChainEpoch + targetHeight abi.ChainEpoch target types.TipSetKey } @@ -57,7 +58,7 @@ func (ci *ChainIndex) GetTipsetByHeight(ctx context.Context, from *types.TipSet, lbe := cval.(*lbEntry) if lbe.ts.Height() == to || lbe.parentHeight < to { return lbe.ts, nil - } else if to > lbe.ts.Height()-ci.skipLength { + } else if to > lbe.targetHeight { return ci.walkBack(lbe.ts, to) } @@ -65,6 +66,10 @@ func (ci *ChainIndex) GetTipsetByHeight(ctx context.Context, from *types.TipSet, } } +func (ci *ChainIndex) GetTipsetByHeightWithoutCache(from *types.TipSet, to abi.ChainEpoch) (*types.TipSet, error) { + return ci.walkBack(from, to) +} + func (ci *ChainIndex) fillCache(tsk types.TipSetKey) (*lbEntry, error) { ts, err := ci.loadTipSet(tsk) if err != nil { @@ -100,6 +105,7 @@ func (ci *ChainIndex) fillCache(tsk types.TipSetKey) (*lbEntry, error) { lbe := &lbEntry{ ts: ts, parentHeight: parent.Height(), + targetHeight: skipTarget.Height(), target: skipTarget.Key(), } ci.skipCache.Add(tsk, lbe) diff --git a/chain/store/store.go b/chain/store/store.go index dcd1a5ead..94d94da79 100644 --- a/chain/store/store.go +++ b/chain/store/store.go @@ -962,6 +962,14 @@ func (cs *ChainStore) GetTipsetByHeight(ctx context.Context, h abi.ChainEpoch, t return nil, err } + if lbts.Height() < h { + log.Warnf("chain index returned the wrong tipset at height %d, using slow retrieval", h) + lbts, err = cs.cindex.GetTipsetByHeightWithoutCache(ts, h) + if err != nil { + return nil, err + } + } + if lbts.Height() == h || !prev { return lbts, nil }