2019-09-05 07:40:50 +00:00
|
|
|
package events
|
2019-09-03 17:45:55 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-18 13:32:21 +00:00
|
|
|
"context"
|
2019-10-08 05:51:34 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2019-09-03 17:45:55 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-09-03 17:45:55 +00:00
|
|
|
)
|
|
|
|
|
2020-09-07 12:43:06 +00:00
|
|
|
type tsCacheAPI interface {
|
|
|
|
ChainGetTipSetByHeight(context.Context, abi.ChainEpoch, types.TipSetKey) (*types.TipSet, error)
|
|
|
|
ChainHead(context.Context) (*types.TipSet, error)
|
|
|
|
}
|
2019-09-18 13:32:21 +00:00
|
|
|
|
2019-09-03 17:45:55 +00:00
|
|
|
// tipSetCache implements a simple ring-buffer cache to keep track of recent
|
|
|
|
// tipsets
|
|
|
|
type tipSetCache struct {
|
|
|
|
cache []*types.TipSet
|
|
|
|
start int
|
|
|
|
len int
|
2019-09-18 13:32:21 +00:00
|
|
|
|
2020-09-07 12:43:06 +00:00
|
|
|
storage tsCacheAPI
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 12:43:06 +00:00
|
|
|
func newTSCache(cap abi.ChainEpoch, storage tsCacheAPI) *tipSetCache {
|
2019-09-03 17:45:55 +00:00
|
|
|
return &tipSetCache{
|
|
|
|
cache: make([]*types.TipSet, cap),
|
|
|
|
start: 0,
|
|
|
|
len: 0,
|
2019-09-18 13:32:21 +00:00
|
|
|
|
|
|
|
storage: storage,
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsc *tipSetCache) add(ts *types.TipSet) error {
|
|
|
|
if tsc.len > 0 {
|
2019-10-04 22:43:04 +00:00
|
|
|
if tsc.cache[tsc.start].Height() >= ts.Height() {
|
|
|
|
return xerrors.Errorf("tipSetCache.add: expected new tipset height to be at least %d, was %d", tsc.cache[tsc.start].Height()+1, ts.Height())
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:43:04 +00:00
|
|
|
nextH := ts.Height()
|
|
|
|
if tsc.len > 0 {
|
|
|
|
nextH = tsc.cache[tsc.start].Height() + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill null blocks
|
|
|
|
for nextH != ts.Height() {
|
|
|
|
tsc.start = normalModulo(tsc.start+1, len(tsc.cache))
|
|
|
|
tsc.cache[tsc.start] = nil
|
|
|
|
if tsc.len < len(tsc.cache) {
|
|
|
|
tsc.len++
|
|
|
|
}
|
|
|
|
nextH++
|
|
|
|
}
|
|
|
|
|
2019-09-27 12:35:09 +00:00
|
|
|
tsc.start = normalModulo(tsc.start+1, len(tsc.cache))
|
2019-09-03 17:45:55 +00:00
|
|
|
tsc.cache[tsc.start] = ts
|
|
|
|
if tsc.len < len(tsc.cache) {
|
|
|
|
tsc.len++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsc *tipSetCache) revert(ts *types.TipSet) error {
|
|
|
|
if tsc.len == 0 {
|
2019-12-03 17:38:37 +00:00
|
|
|
return nil // this can happen, and it's fine
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !tsc.cache[tsc.start].Equals(ts) {
|
|
|
|
return xerrors.New("tipSetCache.revert: revert tipset didn't match cache head")
|
|
|
|
}
|
|
|
|
|
|
|
|
tsc.cache[tsc.start] = nil
|
2019-09-27 12:35:09 +00:00
|
|
|
tsc.start = normalModulo(tsc.start-1, len(tsc.cache))
|
2019-09-03 17:45:55 +00:00
|
|
|
tsc.len--
|
2019-10-04 22:43:04 +00:00
|
|
|
|
|
|
|
_ = tsc.revert(nil) // revert null block gap
|
2019-09-03 17:45:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func (tsc *tipSetCache) getNonNull(height abi.ChainEpoch) (*types.TipSet, error) {
|
2019-10-05 23:43:10 +00:00
|
|
|
for {
|
|
|
|
ts, err := tsc.get(height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ts != nil {
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
height++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func (tsc *tipSetCache) get(height abi.ChainEpoch) (*types.TipSet, error) {
|
2019-09-03 17:45:55 +00:00
|
|
|
if tsc.len == 0 {
|
2019-12-03 17:41:31 +00:00
|
|
|
log.Warnf("tipSetCache.get: cache is empty, requesting from storage (h=%d)", height)
|
2020-09-07 12:43:06 +00:00
|
|
|
return tsc.storage.ChainGetTipSetByHeight(context.TODO(), height, types.EmptyTSK)
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
headH := tsc.cache[tsc.start].Height()
|
|
|
|
|
|
|
|
if height > headH {
|
|
|
|
return nil, xerrors.Errorf("tipSetCache.get: requested tipset not in cache (req: %d, cache head: %d)", height, headH)
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:17:26 +00:00
|
|
|
clen := len(tsc.cache)
|
2019-10-04 23:26:36 +00:00
|
|
|
var tail *types.TipSet
|
|
|
|
for i := 1; i <= tsc.len; i++ {
|
|
|
|
tail = tsc.cache[normalModulo(tsc.start-tsc.len+i, clen)]
|
|
|
|
if tail != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-09-03 17:45:55 +00:00
|
|
|
|
2019-09-30 22:55:59 +00:00
|
|
|
if height < tail.Height() {
|
|
|
|
log.Warnf("tipSetCache.get: requested tipset not in cache, requesting from storage (h=%d; tail=%d)", height, tail.Height())
|
2020-09-07 12:43:06 +00:00
|
|
|
return tsc.storage.ChainGetTipSetByHeight(context.TODO(), height, tail.Key())
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 12:35:09 +00:00
|
|
|
return tsc.cache[normalModulo(tsc.start-int(headH-height), clen)], nil
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 12:43:06 +00:00
|
|
|
func (tsc *tipSetCache) best() (*types.TipSet, error) {
|
|
|
|
best := tsc.cache[tsc.start]
|
|
|
|
if best == nil {
|
|
|
|
return tsc.storage.ChainHead(context.TODO())
|
|
|
|
}
|
|
|
|
return best, nil
|
2019-09-03 17:45:55 +00:00
|
|
|
}
|
2019-09-27 12:35:09 +00:00
|
|
|
|
|
|
|
func normalModulo(n, m int) int {
|
2019-09-30 22:37:01 +00:00
|
|
|
return ((n % m) + m) % m
|
2019-09-27 12:35:09 +00:00
|
|
|
}
|