2020-06-04 00:14:36 +00:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-11 22:07:17 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2023-03-08 16:36:17 +00:00
|
|
|
"sync"
|
2020-06-04 00:14:36 +00:00
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-06-04 00:14:36 +00:00
|
|
|
)
|
|
|
|
|
2023-03-08 16:36:17 +00:00
|
|
|
var DefaultChainIndexCacheSize = 32 << 15
|
2020-08-11 22:07:17 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
if s := os.Getenv("LOTUS_CHAIN_INDEX_CACHE"); s != "" {
|
|
|
|
lcic, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to parse 'LOTUS_CHAIN_INDEX_CACHE' env var: %s", err)
|
|
|
|
}
|
|
|
|
DefaultChainIndexCacheSize = lcic
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-04 00:14:36 +00:00
|
|
|
type ChainIndex struct {
|
2023-03-08 16:36:17 +00:00
|
|
|
indexCacheLk sync.Mutex
|
|
|
|
indexCache map[types.TipSetKey]*lbEntry
|
2020-06-04 00:14:36 +00:00
|
|
|
|
|
|
|
loadTipSet loadTipSetFunc
|
|
|
|
|
|
|
|
skipLength abi.ChainEpoch
|
|
|
|
}
|
2021-12-11 21:03:00 +00:00
|
|
|
type loadTipSetFunc func(context.Context, types.TipSetKey) (*types.TipSet, error)
|
2020-06-04 00:14:36 +00:00
|
|
|
|
|
|
|
func NewChainIndex(lts loadTipSetFunc) *ChainIndex {
|
|
|
|
return &ChainIndex{
|
2023-03-08 16:36:17 +00:00
|
|
|
indexCache: make(map[types.TipSetKey]*lbEntry, DefaultChainIndexCacheSize),
|
2020-06-04 00:14:36 +00:00
|
|
|
loadTipSet: lts,
|
|
|
|
skipLength: 20,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lbEntry struct {
|
2020-06-04 21:56:57 +00:00
|
|
|
targetHeight abi.ChainEpoch
|
2020-06-04 00:14:36 +00:00
|
|
|
target types.TipSetKey
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (ci *ChainIndex) GetTipsetByHeight(ctx context.Context, from *types.TipSet, to abi.ChainEpoch) (*types.TipSet, error) {
|
2020-06-04 00:14:36 +00:00
|
|
|
if from.Height()-to <= ci.skipLength {
|
2021-12-11 21:03:00 +00:00
|
|
|
return ci.walkBack(ctx, from, to)
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
rounded, err := ci.roundDown(ctx, from)
|
2020-06-04 00:14:36 +00:00
|
|
|
if err != nil {
|
2023-03-08 16:36:17 +00:00
|
|
|
return nil, xerrors.Errorf("failed to round down: %w", err)
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:36:17 +00:00
|
|
|
ci.indexCacheLk.Lock()
|
|
|
|
defer ci.indexCacheLk.Unlock()
|
2020-06-04 00:14:36 +00:00
|
|
|
cur := rounded.Key()
|
|
|
|
for {
|
2023-03-08 16:36:17 +00:00
|
|
|
lbe, ok := ci.indexCache[cur]
|
2020-06-04 00:14:36 +00:00
|
|
|
if !ok {
|
2021-12-11 21:03:00 +00:00
|
|
|
fc, err := ci.fillCache(ctx, cur)
|
2020-06-04 00:14:36 +00:00
|
|
|
if err != nil {
|
2023-03-08 16:36:17 +00:00
|
|
|
return nil, xerrors.Errorf("failed to fill cache: %w", err)
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
2023-03-08 16:36:17 +00:00
|
|
|
lbe = fc
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:36:17 +00:00
|
|
|
if to == lbe.targetHeight {
|
|
|
|
ts, err := ci.loadTipSet(ctx, lbe.target)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load tipset: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
if to > lbe.targetHeight {
|
|
|
|
ts, err := ci.loadTipSet(ctx, cur)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load tipset: %w", err)
|
|
|
|
}
|
|
|
|
return ci.walkBack(ctx, ts, to)
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cur = lbe.target
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (ci *ChainIndex) GetTipsetByHeightWithoutCache(ctx context.Context, from *types.TipSet, to abi.ChainEpoch) (*types.TipSet, error) {
|
|
|
|
return ci.walkBack(ctx, from, to)
|
2020-06-04 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:36:17 +00:00
|
|
|
// Caller must hold indexCacheLk
|
2021-12-11 21:03:00 +00:00
|
|
|
func (ci *ChainIndex) fillCache(ctx context.Context, tsk types.TipSetKey) (*lbEntry, error) {
|
|
|
|
ts, err := ci.loadTipSet(ctx, tsk)
|
2020-06-04 00:14:36 +00:00
|
|
|
if err != nil {
|
2023-03-08 16:36:17 +00:00
|
|
|
return nil, xerrors.Errorf("failed to load tipset: %w", err)
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 01:25:41 +00:00
|
|
|
if ts.Height() == 0 {
|
|
|
|
return &lbEntry{
|
2023-03-08 16:36:17 +00:00
|
|
|
targetHeight: 0,
|
|
|
|
target: tsk,
|
2020-06-04 01:25:41 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 00:14:36 +00:00
|
|
|
// will either be equal to ts.Height, or at least > ts.Parent.Height()
|
|
|
|
rheight := ci.roundHeight(ts.Height())
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
parent, err := ci.loadTipSet(ctx, ts.Parents())
|
2020-06-04 00:14:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rheight -= ci.skipLength
|
2021-05-27 17:42:42 +00:00
|
|
|
if rheight < 0 {
|
|
|
|
rheight = 0
|
|
|
|
}
|
2020-06-04 00:14:36 +00:00
|
|
|
|
2020-06-11 05:15:46 +00:00
|
|
|
var skipTarget *types.TipSet
|
|
|
|
if parent.Height() < rheight {
|
|
|
|
skipTarget = parent
|
|
|
|
} else {
|
2021-12-11 21:03:00 +00:00
|
|
|
skipTarget, err = ci.walkBack(ctx, parent, rheight)
|
2020-06-11 05:15:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("fillCache walkback: %w", err)
|
|
|
|
}
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lbe := &lbEntry{
|
2020-06-04 21:56:57 +00:00
|
|
|
targetHeight: skipTarget.Height(),
|
2020-06-04 00:14:36 +00:00
|
|
|
target: skipTarget.Key(),
|
|
|
|
}
|
2023-03-08 16:36:17 +00:00
|
|
|
ci.indexCache[tsk] = lbe
|
2020-06-04 00:14:36 +00:00
|
|
|
|
|
|
|
return lbe, nil
|
|
|
|
}
|
|
|
|
|
2020-06-11 12:49:48 +00:00
|
|
|
// floors to nearest skipLength multiple
|
2020-06-04 00:14:36 +00:00
|
|
|
func (ci *ChainIndex) roundHeight(h abi.ChainEpoch) abi.ChainEpoch {
|
2020-06-11 12:49:48 +00:00
|
|
|
return (h / ci.skipLength) * ci.skipLength
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (ci *ChainIndex) roundDown(ctx context.Context, ts *types.TipSet) (*types.TipSet, error) {
|
2020-06-04 00:14:36 +00:00
|
|
|
target := ci.roundHeight(ts.Height())
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
rounded, err := ci.walkBack(ctx, ts, target)
|
2020-06-04 00:14:36 +00:00
|
|
|
if err != nil {
|
2023-03-08 16:36:17 +00:00
|
|
|
return nil, xerrors.Errorf("failed to walk back: %w", err)
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rounded, nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (ci *ChainIndex) walkBack(ctx context.Context, from *types.TipSet, to abi.ChainEpoch) (*types.TipSet, error) {
|
2020-06-04 00:14:36 +00:00
|
|
|
if to > from.Height() {
|
|
|
|
return nil, xerrors.Errorf("looking for tipset with height greater than start point")
|
|
|
|
}
|
|
|
|
|
|
|
|
if to == from.Height() {
|
|
|
|
return from, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := from
|
|
|
|
|
|
|
|
for {
|
2021-12-11 21:03:00 +00:00
|
|
|
pts, err := ci.loadTipSet(ctx, ts.Parents())
|
2020-06-04 00:14:36 +00:00
|
|
|
if err != nil {
|
2023-03-08 16:36:17 +00:00
|
|
|
return nil, xerrors.Errorf("failed to load tipset: %w", err)
|
2020-06-04 00:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if to > pts.Height() {
|
2020-06-11 12:49:48 +00:00
|
|
|
// in case pts is lower than the epoch we're looking for (null blocks)
|
|
|
|
// return a tipset above that height
|
2020-06-04 00:14:36 +00:00
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
if to == pts.Height() {
|
|
|
|
return pts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ts = pts
|
|
|
|
}
|
|
|
|
}
|