2020-11-26 14:51:16 +00:00
|
|
|
package splitstore
|
2020-11-24 14:51:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-24 17:41:07 +00:00
|
|
|
"encoding/binary"
|
2020-11-24 14:51:00 +00:00
|
|
|
"errors"
|
2020-12-01 15:17:34 +00:00
|
|
|
"path/filepath"
|
2020-11-25 07:10:58 +00:00
|
|
|
"sync"
|
2020-11-29 13:10:30 +00:00
|
|
|
"sync/atomic"
|
2020-11-25 08:11:42 +00:00
|
|
|
"time"
|
2020-11-24 14:51:00 +00:00
|
|
|
|
2020-11-26 15:49:47 +00:00
|
|
|
"github.com/bmatsuo/lmdb-go/lmdb"
|
|
|
|
|
2020-11-24 14:51:00 +00:00
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
cid "github.com/ipfs/go-cid"
|
2020-11-24 17:41:07 +00:00
|
|
|
dstore "github.com/ipfs/go-datastore"
|
2020-11-26 14:51:16 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-11-24 14:51:00 +00:00
|
|
|
|
2020-12-01 16:14:34 +00:00
|
|
|
lmdbbs "github.com/filecoin-project/go-bs-lmdb"
|
2020-11-24 14:51:00 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-11-24 17:15:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-11-26 14:51:16 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
2020-11-24 14:51:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-11-25 07:07:48 +00:00
|
|
|
bstore "github.com/filecoin-project/lotus/lib/blockstore"
|
2020-11-24 14:51:00 +00:00
|
|
|
)
|
|
|
|
|
2020-11-24 17:26:28 +00:00
|
|
|
const CompactionThreshold = 5 * build.Finality
|
|
|
|
|
2020-11-24 17:41:07 +00:00
|
|
|
var baseEpochKey = dstore.NewKey("baseEpoch")
|
|
|
|
|
2020-11-26 14:51:16 +00:00
|
|
|
var log = logging.Logger("splitstore")
|
|
|
|
|
2020-11-24 14:51:00 +00:00
|
|
|
type SplitStore struct {
|
2020-11-29 13:10:30 +00:00
|
|
|
compacting int32
|
|
|
|
|
|
|
|
baseEpoch abi.ChainEpoch
|
|
|
|
|
|
|
|
mx sync.Mutex
|
|
|
|
curTs *types.TipSet
|
2020-11-24 14:51:00 +00:00
|
|
|
|
2020-12-01 15:17:34 +00:00
|
|
|
cs *store.ChainStore
|
|
|
|
ds dstore.Datastore
|
|
|
|
hot bstore.Blockstore
|
|
|
|
cold bstore.Blockstore
|
2020-11-24 14:51:00 +00:00
|
|
|
snoop TrackingStore
|
2020-11-24 17:26:28 +00:00
|
|
|
|
2020-11-26 15:49:47 +00:00
|
|
|
env *lmdb.Env
|
2020-11-24 14:51:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 07:07:48 +00:00
|
|
|
var _ bstore.Blockstore = (*SplitStore)(nil)
|
2020-11-24 14:51:00 +00:00
|
|
|
|
2020-12-01 15:17:34 +00:00
|
|
|
// NewSplitStore creates a new SplitStore instance, given a path for the hotstore dbs and a cold
|
|
|
|
// blockstore. The SplitStore must be attached to the ChainStore with Start in order to trigger
|
|
|
|
// compaction.
|
|
|
|
func NewSplitStore(path string, ds dstore.Datastore, cold bstore.Blockstore) (*SplitStore, error) {
|
|
|
|
// the hot store
|
|
|
|
hot, err := lmdbbs.Open(filepath.Join(path, "hot.db"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the tracking store
|
|
|
|
snoop, err := NewTrackingStore(filepath.Join(path, "snoop.db"))
|
|
|
|
if err != nil {
|
|
|
|
hot.Close() //nolint:errcheck
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// the liveset env
|
|
|
|
env, err := NewLiveSetEnv(filepath.Join(path, "sweep.db"))
|
|
|
|
if err != nil {
|
|
|
|
hot.Close() //nolint:errcheck
|
|
|
|
snoop.Close() //nolint:errcheck
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// and now we can make a SplitStore
|
|
|
|
ss := &SplitStore{
|
|
|
|
ds: ds,
|
|
|
|
hot: hot,
|
|
|
|
cold: cold,
|
|
|
|
snoop: snoop,
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 14:51:00 +00:00
|
|
|
// Blockstore interface
|
|
|
|
func (s *SplitStore) DeleteBlock(cid cid.Cid) error {
|
|
|
|
// afaict we don't seem to be using this method, so it's not implemented
|
2020-11-24 22:01:10 +00:00
|
|
|
return errors.New("DeleteBlock not implemented on SplitStore; don't do this Luke!") //nolint
|
2020-11-24 14:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitStore) Has(cid cid.Cid) (bool, error) {
|
|
|
|
has, err := s.hot.Has(cid)
|
|
|
|
|
2020-11-29 11:00:36 +00:00
|
|
|
if err != nil || has {
|
|
|
|
return has, err
|
2020-11-24 14:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.cold.Has(cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitStore) Get(cid cid.Cid) (blocks.Block, error) {
|
|
|
|
blk, err := s.hot.Get(cid)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return blk, nil
|
|
|
|
|
|
|
|
case bstore.ErrNotFound:
|
|
|
|
return s.cold.Get(cid)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitStore) GetSize(cid cid.Cid) (int, error) {
|
|
|
|
size, err := s.hot.GetSize(cid)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return size, nil
|
|
|
|
|
|
|
|
case bstore.ErrNotFound:
|
|
|
|
return s.cold.GetSize(cid)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitStore) Put(blk blocks.Block) error {
|
2020-11-29 10:48:52 +00:00
|
|
|
s.mx.Lock()
|
2020-12-01 15:56:22 +00:00
|
|
|
if s.curTs == nil {
|
|
|
|
s.mx.Unlock()
|
|
|
|
return s.cold.Put(blk)
|
|
|
|
}
|
|
|
|
|
2020-11-24 17:15:38 +00:00
|
|
|
epoch := s.curTs.Height()
|
2020-11-29 10:48:52 +00:00
|
|
|
s.mx.Unlock()
|
|
|
|
|
2020-11-24 17:15:38 +00:00
|
|
|
err := s.snoop.Put(blk.Cid(), epoch)
|
2020-11-24 14:51:00 +00:00
|
|
|
if err != nil {
|
2020-11-29 10:50:59 +00:00
|
|
|
log.Errorf("error tracking CID in hotstore: %s; falling back to coldstore", err)
|
|
|
|
return s.cold.Put(blk)
|
2020-11-24 14:51:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 17:15:38 +00:00
|
|
|
return s.hot.Put(blk)
|
2020-11-24 14:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitStore) PutMany(blks []blocks.Block) error {
|
2020-12-01 15:56:22 +00:00
|
|
|
s.mx.Lock()
|
|
|
|
if s.curTs == nil {
|
|
|
|
s.mx.Unlock()
|
2020-11-29 10:50:59 +00:00
|
|
|
return s.cold.PutMany(blks)
|
2020-11-24 14:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
epoch := s.curTs.Height()
|
2020-11-29 10:48:52 +00:00
|
|
|
s.mx.Unlock()
|
2020-11-24 14:51:00 +00:00
|
|
|
|
2020-12-01 15:56:22 +00:00
|
|
|
err := s.hot.PutMany(blks)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error tracking CIDs in hotstore: %s; falling back to coldstore", err)
|
|
|
|
return s.cold.PutMany(blks)
|
|
|
|
}
|
|
|
|
|
2020-11-24 14:51:00 +00:00
|
|
|
batch := make([]cid.Cid, 0, len(blks))
|
|
|
|
for _, blk := range blks {
|
|
|
|
batch = append(batch, blk.Cid())
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.snoop.PutBatch(batch, epoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitStore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
chHot, err := s.hot.AllKeysChan(ctx)
|
|
|
|
if err != nil {
|
2020-11-24 22:01:10 +00:00
|
|
|
cancel()
|
2020-11-24 14:51:00 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
chCold, err := s.cold.AllKeysChan(ctx)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan cid.Cid)
|
|
|
|
go func() {
|
|
|
|
defer cancel()
|
2020-11-25 07:07:06 +00:00
|
|
|
defer close(ch)
|
2020-11-24 14:51:00 +00:00
|
|
|
|
|
|
|
for _, in := range []<-chan cid.Cid{chHot, chCold} {
|
|
|
|
for cid := range in {
|
|
|
|
select {
|
|
|
|
case ch <- cid:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitStore) HashOnRead(enabled bool) {
|
|
|
|
s.hot.HashOnRead(enabled)
|
|
|
|
s.cold.HashOnRead(enabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitStore) View(cid cid.Cid, cb func([]byte) error) error {
|
|
|
|
err := s.hot.View(cid, cb)
|
|
|
|
switch err {
|
|
|
|
case bstore.ErrNotFound:
|
|
|
|
return s.cold.View(cid, cb)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 17:15:38 +00:00
|
|
|
|
2020-11-24 17:26:28 +00:00
|
|
|
// State tracking
|
2020-11-26 14:51:16 +00:00
|
|
|
func (s *SplitStore) Start(cs *store.ChainStore) error {
|
2020-11-24 17:26:28 +00:00
|
|
|
s.cs = cs
|
|
|
|
s.curTs = cs.GetHeaviestTipSet()
|
2020-11-24 17:41:07 +00:00
|
|
|
|
|
|
|
// load base epoch from metadata ds
|
|
|
|
// if none, then use current epoch because it's a fresh start
|
|
|
|
bs, err := s.ds.Get(baseEpochKey)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2020-11-26 18:37:02 +00:00
|
|
|
s.baseEpoch = bytesToEpoch(bs)
|
2020-11-24 17:41:07 +00:00
|
|
|
|
|
|
|
case dstore.ErrNotFound:
|
2020-12-01 15:56:22 +00:00
|
|
|
if s.curTs == nil {
|
|
|
|
// this can happen in some tests
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-11-24 17:41:07 +00:00
|
|
|
err = s.setBaseEpoch(s.curTs.Height())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch the chain
|
2020-11-24 17:26:28 +00:00
|
|
|
cs.SubscribeHeadChanges(s.HeadChange)
|
2020-11-24 17:41:07 +00:00
|
|
|
|
|
|
|
return nil
|
2020-11-24 17:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 15:49:47 +00:00
|
|
|
func (s *SplitStore) Close() error {
|
2020-11-29 13:10:30 +00:00
|
|
|
if atomic.LoadInt32(&s.compacting) == 1 {
|
2020-11-26 15:49:47 +00:00
|
|
|
log.Warn("ongoing compaction; waiting for it to finish...")
|
2020-11-29 13:10:30 +00:00
|
|
|
for atomic.LoadInt32(&s.compacting) == 1 {
|
2020-11-26 15:49:47 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.env.Close()
|
|
|
|
}
|
|
|
|
|
2020-11-24 17:26:28 +00:00
|
|
|
func (s *SplitStore) HeadChange(revert, apply []*types.TipSet) error {
|
2020-11-29 10:48:52 +00:00
|
|
|
s.mx.Lock()
|
2020-11-24 17:26:28 +00:00
|
|
|
s.curTs = apply[len(apply)-1]
|
|
|
|
epoch := s.curTs.Height()
|
2020-11-29 10:48:52 +00:00
|
|
|
s.mx.Unlock()
|
|
|
|
|
2020-11-29 13:10:30 +00:00
|
|
|
if !atomic.CompareAndSwapInt32(&s.compacting, 0, 1) {
|
|
|
|
// we are currently compacting, do nothing and wait for the next head change
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if epoch-s.baseEpoch > CompactionThreshold {
|
2020-11-24 17:26:28 +00:00
|
|
|
go func() {
|
2020-11-29 13:10:30 +00:00
|
|
|
defer atomic.StoreInt32(&s.compacting, 0)
|
2020-11-25 08:11:42 +00:00
|
|
|
|
|
|
|
log.Info("compacting splitstore")
|
|
|
|
start := time.Now()
|
|
|
|
|
2020-11-24 17:26:28 +00:00
|
|
|
s.compact()
|
2020-11-25 08:11:42 +00:00
|
|
|
|
|
|
|
log.Infow("compaction done", "took", time.Since(start))
|
2020-11-24 17:26:28 +00:00
|
|
|
}()
|
2020-11-29 13:10:30 +00:00
|
|
|
} else {
|
|
|
|
// no compaction necessary
|
|
|
|
atomic.StoreInt32(&s.compacting, 0)
|
2020-11-24 17:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 17:15:38 +00:00
|
|
|
// Compaction/GC Algorithm
|
|
|
|
func (s *SplitStore) compact() {
|
2020-11-24 21:55:57 +00:00
|
|
|
// create two on disk live sets, one for marking the cold finality region
|
|
|
|
// and one for marking the hot region
|
2020-11-26 15:49:47 +00:00
|
|
|
hotSet, err := NewLiveSet(s.env, "hot")
|
2020-11-24 21:55:57 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer hotSet.Close() //nolint:errcheck
|
|
|
|
|
2020-11-26 15:49:47 +00:00
|
|
|
coldSet, err := NewLiveSet(s.env, "cold")
|
2020-11-24 21:55:57 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer coldSet.Close() //nolint:errcheck
|
|
|
|
|
2020-11-25 08:11:42 +00:00
|
|
|
// Phase 1: marking
|
|
|
|
log.Info("marking live objects")
|
|
|
|
startMark := time.Now()
|
|
|
|
|
2020-11-24 21:55:57 +00:00
|
|
|
// Phase 1a: mark all reachable CIDs in the hot range
|
2020-11-29 10:48:52 +00:00
|
|
|
s.mx.Lock()
|
2020-11-24 17:15:38 +00:00
|
|
|
curTs := s.curTs
|
2020-11-29 10:48:52 +00:00
|
|
|
s.mx.Unlock()
|
|
|
|
|
2020-11-24 17:15:38 +00:00
|
|
|
epoch := curTs.Height()
|
2020-11-24 21:55:57 +00:00
|
|
|
coldEpoch := s.baseEpoch + build.Finality
|
|
|
|
err = s.cs.WalkSnapshot(context.Background(), curTs, epoch-coldEpoch+1, false, false,
|
2020-11-24 17:15:38 +00:00
|
|
|
func(cid cid.Cid) error {
|
2020-11-24 21:55:57 +00:00
|
|
|
return hotSet.Mark(cid)
|
2020-11-24 17:15:38 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-11-24 21:55:57 +00:00
|
|
|
// Phase 1b: mark all reachable CIDs in the cold range
|
|
|
|
coldTs, err := s.cs.GetTipsetByHeight(context.Background(), coldEpoch-1, curTs, true)
|
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.cs.WalkSnapshot(context.Background(), coldTs, build.Finality, false, false,
|
|
|
|
func(cid cid.Cid) error {
|
|
|
|
return coldSet.Mark(cid)
|
|
|
|
})
|
2020-11-24 17:15:38 +00:00
|
|
|
|
2020-11-24 21:55:57 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-11-25 08:11:42 +00:00
|
|
|
log.Infow("marking done", "took", time.Since(startMark))
|
|
|
|
|
2020-11-24 21:55:57 +00:00
|
|
|
// Phase 2: sweep cold objects:
|
|
|
|
// - If a cold object is reachable in the hot range, it stays in the hotstore.
|
|
|
|
// - If a cold object is reachable in the cold range, it is moved to the coldstore.
|
|
|
|
// - If a cold object is unreachable, it is deleted.
|
2020-11-26 18:37:02 +00:00
|
|
|
ch, err := s.snoop.Keys(context.Background())
|
2020-11-24 17:15:38 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-11-25 08:11:42 +00:00
|
|
|
startSweep := time.Now()
|
|
|
|
log.Info("sweeping cold objects")
|
|
|
|
|
|
|
|
// some stats for logging
|
|
|
|
var stHot, stCold, stDead int
|
|
|
|
|
2020-11-24 17:15:38 +00:00
|
|
|
for cid := range ch {
|
|
|
|
wrEpoch, err := s.snoop.Get(cid)
|
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the object stil hot?
|
|
|
|
if wrEpoch >= coldEpoch {
|
2020-11-24 21:55:57 +00:00
|
|
|
// yes, stay in the hotstore
|
2020-11-25 08:11:42 +00:00
|
|
|
stHot++
|
2020-11-24 17:15:38 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-24 21:55:57 +00:00
|
|
|
// the object is cold -- check whether it is reachable in the hot range
|
|
|
|
mark, err := hotSet.Has(cid)
|
2020-11-24 17:15:38 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mark {
|
2020-11-24 21:55:57 +00:00
|
|
|
// the object is reachable in the hot range, stay in the hotstore
|
2020-11-25 08:11:42 +00:00
|
|
|
stHot++
|
2020-11-24 21:55:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check whether it is reachable in the cold range
|
|
|
|
mark, err = coldSet.Has(cid)
|
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mark {
|
|
|
|
// the object is reachable in the cold range, move it to the cold store
|
2020-11-24 17:15:38 +00:00
|
|
|
blk, err := s.hot.Get(cid)
|
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.cold.Put(blk)
|
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-11-25 08:11:42 +00:00
|
|
|
|
|
|
|
stCold++
|
|
|
|
} else {
|
|
|
|
stDead++
|
2020-11-24 17:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete the object from the hotstore
|
|
|
|
err = s.hot.DeleteBlock(cid)
|
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the snoop tracking
|
|
|
|
err = s.snoop.Delete(cid)
|
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 08:11:42 +00:00
|
|
|
log.Infow("sweeping done", "took", time.Since(startSweep))
|
|
|
|
log.Infow("compaction stats", "hot", stHot, "cold", stCold, "dead", stDead)
|
|
|
|
|
2020-11-24 17:41:07 +00:00
|
|
|
err = s.setBaseEpoch(coldEpoch)
|
|
|
|
if err != nil {
|
|
|
|
// TODO do something better here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SplitStore) setBaseEpoch(epoch abi.ChainEpoch) error {
|
|
|
|
s.baseEpoch = epoch
|
|
|
|
// write to datastore
|
2020-11-26 18:37:02 +00:00
|
|
|
return s.ds.Put(baseEpochKey, epochToBytes(epoch))
|
|
|
|
}
|
|
|
|
|
|
|
|
func epochToBytes(epoch abi.ChainEpoch) []byte {
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
n := binary.PutUvarint(buf, uint64(epoch))
|
|
|
|
return buf[:n]
|
|
|
|
}
|
|
|
|
|
|
|
|
func bytesToEpoch(buf []byte) abi.ChainEpoch {
|
|
|
|
epoch, n := binary.Uvarint(buf)
|
|
|
|
if n < 0 {
|
|
|
|
panic("bogus base epoch bytes")
|
|
|
|
}
|
|
|
|
return abi.ChainEpoch(epoch)
|
2020-11-24 17:15:38 +00:00
|
|
|
}
|