diff --git a/chain/chain.go b/chain/chain.go index 9c2e5f200..5757219ea 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -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 } diff --git a/miner/miner.go b/miner/miner.go index a69cae230..81aa6e987 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -38,8 +38,9 @@ type api interface { func NewMiner(api api, addr address.Address) *Miner { return &Miner{ - api: api, - Delay: time.Second * 4, + api: api, + address: addr, + Delay: time.Second * 4, } } diff --git a/node/builder.go b/node/builder.go index 0c5326cad..2bef9ab70 100644 --- a/node/builder.go +++ b/node/builder.go @@ -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), diff --git a/node/modules/core.go b/node/modules/core.go index 18ccf453d..388c6df19 100644 --- a/node/modules/core.go +++ b/node/modules/core.go @@ -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 +}