Merge pull request #73 from filecoin-project/feat/persist-chain-head

store and load latest chain head in the datastore
This commit is contained in:
Whyrusleeping 2019-07-24 15:27:13 -07:00 committed by GitHub
commit 0569075442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 3 deletions

View File

@ -24,6 +24,8 @@ const ForkLengthThreshold = 20
var log = logging.Logger("f2")
var chainHeadKey = dstore.NewKey("head")
type GenesisBootstrap struct {
Genesis *BlockHeader
MinerKey address.Address
@ -232,6 +234,44 @@ func NewChainStore(bs bstore.Blockstore, ds dstore.Batching) *ChainStore {
}
}
func (cs *ChainStore) Load() error {
head, err := cs.ds.Get(chainHeadKey)
if err == dstore.ErrNotFound {
log.Warn("no previous chain state found")
return nil
}
if err != nil {
return errors.Wrap(err, "failed to load chain state from datastore")
}
var tscids []cid.Cid
if err := json.Unmarshal(head, &tscids); err != nil {
return errors.Wrap(err, "failed to unmarshal stored chain head")
}
ts, err := cs.LoadTipSet(tscids)
if err != nil {
return err
}
cs.heaviest = ts
return nil
}
func (cs *ChainStore) writeHead(ts *TipSet) error {
data, err := json.Marshal(ts.Cids())
if err != nil {
return errors.Wrap(err, "failed to marshal tipset")
}
if err := cs.ds.Put(chainHeadKey, data); err != nil {
return errors.Wrap(err, "failed to write chain head to datastore")
}
return nil
}
func (cs *ChainStore) SubNewTips() chan *TipSet {
subch := cs.bestTips.Sub("best")
out := make(chan *TipSet)
@ -315,6 +355,11 @@ func (cs *ChainStore) maybeTakeHeavierTipSet(ts *TipSet) error {
}
log.Infof("New heaviest tipset! %s", ts.Cids())
cs.heaviest = ts
if err := cs.writeHead(ts); err != nil {
log.Errorf("failed to write chain head: %s", err)
return nil
}
}
return nil
}

View File

@ -39,6 +39,7 @@ type api interface {
func NewMiner(api api, addr address.Address) *Miner {
return &Miner{
api: api,
address: addr,
Delay: time.Second * 4,
}
}

View File

@ -128,6 +128,9 @@ func defaults() []Option {
return []Option{
Override(new(helpers.MetricsCtx), context.Background),
Override(new(record.Validator), modules.RecordValidator),
// Filecoin modules
}
}
@ -180,7 +183,7 @@ func Online() Option {
Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages),
Override(new(*chain.ChainStore), chain.NewChainStore),
Override(new(*chain.ChainStore), modules.ChainStore),
Override(new(blockstore.GCLocker), blockstore.NewGCLocker),
Override(new(blockstore.GCBlockstore), blockstore.NewGCBlockstore),

View File

@ -8,6 +8,7 @@ import (
"path/filepath"
"github.com/gbrlsnchs/jwt/v3"
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice"
@ -163,3 +164,15 @@ func ClientDAG(lc fx.Lifecycle, fstore *filestore.Filestore) ipld.DAGService {
return dag
}
func ChainStore(lc fx.Lifecycle, bs blockstore.Blockstore, ds datastore.Batching) *chain.ChainStore {
chain := chain.NewChainStore(bs, ds)
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return chain.Load()
},
})
return chain
}