track base epoch in metadata ds
This commit is contained in:
parent
b192adfd2e
commit
fd08786048
@ -2,10 +2,12 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
blocks "github.com/ipfs/go-block-format"
|
blocks "github.com/ipfs/go-block-format"
|
||||||
cid "github.com/ipfs/go-cid"
|
cid "github.com/ipfs/go-cid"
|
||||||
|
dstore "github.com/ipfs/go-datastore"
|
||||||
bstore "github.com/ipfs/go-ipfs-blockstore"
|
bstore "github.com/ipfs/go-ipfs-blockstore"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-state-types/abi"
|
"github.com/filecoin-project/go-state-types/abi"
|
||||||
@ -16,11 +18,14 @@ import (
|
|||||||
|
|
||||||
const CompactionThreshold = 5 * build.Finality
|
const CompactionThreshold = 5 * build.Finality
|
||||||
|
|
||||||
|
var baseEpochKey = dstore.NewKey("baseEpoch")
|
||||||
|
|
||||||
type SplitStore struct {
|
type SplitStore struct {
|
||||||
baseEpoch abi.ChainEpoch
|
baseEpoch abi.ChainEpoch
|
||||||
curTs *types.TipSet
|
curTs *types.TipSet
|
||||||
|
|
||||||
cs *ChainStore
|
cs *ChainStore
|
||||||
|
ds dstore.Datastore
|
||||||
|
|
||||||
hot bstore2.Blockstore
|
hot bstore2.Blockstore
|
||||||
cold bstore2.Blockstore
|
cold bstore2.Blockstore
|
||||||
@ -167,11 +172,35 @@ func (s *SplitStore) View(cid cid.Cid, cb func([]byte) error) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// State tracking
|
// State tracking
|
||||||
func (s *SplitStore) Start(cs *ChainStore) {
|
func (s *SplitStore) Start(cs *ChainStore) error {
|
||||||
// TODO load base epoch from metadata ds -- if none, then use current epoch
|
|
||||||
s.cs = cs
|
s.cs = cs
|
||||||
s.curTs = cs.GetHeaviestTipSet()
|
s.curTs = cs.GetHeaviestTipSet()
|
||||||
|
|
||||||
|
// 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:
|
||||||
|
epoch, n := binary.Uvarint(bs)
|
||||||
|
if n < 0 {
|
||||||
|
panic("bogus base epoch")
|
||||||
|
}
|
||||||
|
s.baseEpoch = abi.ChainEpoch(epoch)
|
||||||
|
|
||||||
|
case dstore.ErrNotFound:
|
||||||
|
err = s.setBaseEpoch(s.curTs.Height())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// watch the chain
|
||||||
cs.SubscribeHeadChanges(s.HeadChange)
|
cs.SubscribeHeadChanges(s.HeadChange)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SplitStore) HeadChange(revert, apply []*types.TipSet) error {
|
func (s *SplitStore) HeadChange(revert, apply []*types.TipSet) error {
|
||||||
@ -288,6 +317,18 @@ func (s *SplitStore) compact() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO persist base epoch to metadata ds
|
err = s.setBaseEpoch(coldEpoch)
|
||||||
s.baseEpoch = 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
|
||||||
|
bs := make([]byte, 16)
|
||||||
|
n := binary.PutUvarint(bs, uint64(epoch))
|
||||||
|
bs = bs[:n]
|
||||||
|
return s.ds.Put(baseEpochKey, bs)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user