2019-07-26 04:54:22 +00:00
|
|
|
package store
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-06 06:26:02 +00:00
|
|
|
"crypto/sha256"
|
2019-07-17 03:05:55 +00:00
|
|
|
"encoding/json"
|
2019-07-05 14:29:17 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2019-10-16 08:01:41 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/vm"
|
|
|
|
|
2019-09-30 23:55:35 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/build"
|
2019-09-29 06:29:03 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/state"
|
2019-10-13 09:08:34 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-10-05 15:59:35 +00:00
|
|
|
"go.uber.org/zap"
|
2019-09-30 23:55:35 +00:00
|
|
|
|
2019-09-17 01:56:37 +00:00
|
|
|
amt "github.com/filecoin-project/go-amt-ipld"
|
2019-07-12 10:23:05 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-08 12:51:45 +00:00
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
block "github.com/ipfs/go-block-format"
|
2019-07-05 14:29:17 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
dstore "github.com/ipfs/go-datastore"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
2019-07-26 04:54:22 +00:00
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
2019-07-05 14:29:17 +00:00
|
|
|
bstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
"github.com/pkg/errors"
|
2019-09-17 01:56:37 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-07-05 14:29:17 +00:00
|
|
|
pubsub "github.com/whyrusleeping/pubsub"
|
2019-09-17 01:56:37 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
var log = logging.Logger("chainstore")
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2019-07-24 02:45:00 +00:00
|
|
|
var chainHeadKey = dstore.NewKey("head")
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
type ChainStore struct {
|
|
|
|
bs bstore.Blockstore
|
2019-07-12 03:59:55 +00:00
|
|
|
ds dstore.Datastore
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
heaviestLk sync.Mutex
|
2019-07-26 04:54:22 +00:00
|
|
|
heaviest *types.TipSet
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
bestTips *pubsub.PubSub
|
2019-09-17 22:43:47 +00:00
|
|
|
pubLk sync.Mutex
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2019-09-03 04:36:07 +00:00
|
|
|
tstLk sync.Mutex
|
|
|
|
tipsets map[uint64][]cid.Cid
|
|
|
|
|
2019-10-10 23:50:49 +00:00
|
|
|
reorgCh chan<- reorg
|
2019-07-26 04:54:22 +00:00
|
|
|
headChangeNotifs []func(rev, app []*types.TipSet) error
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 03:59:55 +00:00
|
|
|
func NewChainStore(bs bstore.Blockstore, ds dstore.Batching) *ChainStore {
|
2019-07-28 05:35:32 +00:00
|
|
|
cs := &ChainStore{
|
2019-07-05 14:29:17 +00:00
|
|
|
bs: bs,
|
|
|
|
ds: ds,
|
|
|
|
bestTips: pubsub.New(64),
|
2019-09-03 04:36:07 +00:00
|
|
|
tipsets: make(map[uint64][]cid.Cid),
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-07-28 05:35:32 +00:00
|
|
|
|
2019-10-10 23:50:49 +00:00
|
|
|
cs.reorgCh = cs.reorgWorker(context.TODO())
|
|
|
|
|
2019-07-28 05:35:32 +00:00
|
|
|
hcnf := func(rev, app []*types.TipSet) error {
|
2019-09-17 22:43:47 +00:00
|
|
|
cs.pubLk.Lock()
|
|
|
|
defer cs.pubLk.Unlock()
|
2019-09-18 11:01:52 +00:00
|
|
|
|
|
|
|
notif := make([]*HeadChange, len(rev)+len(app))
|
|
|
|
|
|
|
|
for i, r := range rev {
|
|
|
|
notif[i] = &HeadChange{
|
2019-07-28 05:35:32 +00:00
|
|
|
Type: HCRevert,
|
|
|
|
Val: r,
|
2019-09-18 11:01:52 +00:00
|
|
|
}
|
2019-07-28 05:35:32 +00:00
|
|
|
}
|
2019-09-18 11:01:52 +00:00
|
|
|
for i, r := range app {
|
|
|
|
notif[i+len(rev)] = &HeadChange{
|
2019-07-28 05:35:32 +00:00
|
|
|
Type: HCApply,
|
|
|
|
Val: r,
|
2019-09-18 11:01:52 +00:00
|
|
|
}
|
2019-07-28 05:35:32 +00:00
|
|
|
}
|
2019-09-18 11:01:52 +00:00
|
|
|
|
|
|
|
cs.bestTips.Pub(notif, "headchange")
|
2019-07-28 05:35:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cs.headChangeNotifs = append(cs.headChangeNotifs, hcnf)
|
|
|
|
|
|
|
|
return cs
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 02:45:00 +00:00
|
|
|
func (cs *ChainStore) Load() error {
|
|
|
|
head, err := cs.ds.Get(chainHeadKey)
|
2019-07-24 21:10:27 +00:00
|
|
|
if err == dstore.ErrNotFound {
|
|
|
|
log.Warn("no previous chain state found")
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-24 02:45:00 +00:00
|
|
|
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 {
|
2019-09-30 23:55:35 +00:00
|
|
|
return xerrors.Errorf("loading tipset: %w", err)
|
2019-07-24 02:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cs.heaviest = ts
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) writeHead(ts *types.TipSet) error {
|
2019-07-24 02:45:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-17 03:05:55 +00:00
|
|
|
const (
|
2019-09-17 22:43:47 +00:00
|
|
|
HCRevert = "revert"
|
|
|
|
HCApply = "apply"
|
|
|
|
HCCurrent = "current"
|
2019-07-17 03:05:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HeadChange struct {
|
|
|
|
Type string
|
2019-07-26 04:54:22 +00:00
|
|
|
Val *types.TipSet
|
2019-07-17 03:05:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 11:01:52 +00:00
|
|
|
func (cs *ChainStore) SubHeadChanges(ctx context.Context) chan []*HeadChange {
|
2019-09-17 22:43:47 +00:00
|
|
|
cs.pubLk.Lock()
|
2019-07-17 03:05:55 +00:00
|
|
|
subch := cs.bestTips.Sub("headchange")
|
2019-09-17 22:43:47 +00:00
|
|
|
head := cs.GetHeaviestTipSet()
|
|
|
|
cs.pubLk.Unlock()
|
|
|
|
|
2019-09-18 11:01:52 +00:00
|
|
|
out := make(chan []*HeadChange, 16)
|
|
|
|
out <- []*HeadChange{{
|
2019-09-17 22:43:47 +00:00
|
|
|
Type: HCCurrent,
|
|
|
|
Val: head,
|
2019-09-18 11:01:52 +00:00
|
|
|
}}
|
2019-09-17 22:43:47 +00:00
|
|
|
|
2019-07-17 03:05:55 +00:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2019-07-28 19:19:33 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case val, ok := <-subch:
|
|
|
|
if !ok {
|
2019-08-31 01:03:10 +00:00
|
|
|
log.Warn("chain head sub exit loop")
|
2019-07-28 19:19:33 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-18 11:01:52 +00:00
|
|
|
if len(out) > 0 {
|
|
|
|
log.Warnf("head change sub is slow, has %d buffered entries", len(out))
|
|
|
|
}
|
2019-08-31 01:03:10 +00:00
|
|
|
select {
|
2019-09-18 11:01:52 +00:00
|
|
|
case out <- val.([]*HeadChange):
|
2019-08-31 01:03:10 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2019-07-28 19:19:33 +00:00
|
|
|
case <-ctx.Done():
|
2019-08-31 01:03:10 +00:00
|
|
|
go cs.bestTips.Unsub(subch)
|
2019-07-28 19:19:33 +00:00
|
|
|
}
|
2019-07-17 03:05:55 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) SubscribeHeadChanges(f func(rev, app []*types.TipSet) error) {
|
2019-07-17 03:05:55 +00:00
|
|
|
cs.headChangeNotifs = append(cs.headChangeNotifs, f)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) SetGenesis(b *types.BlockHeader) error {
|
2019-10-06 03:32:56 +00:00
|
|
|
ts, err := types.NewTipSet([]*types.BlockHeader{b})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 04:33:29 +00:00
|
|
|
if err := cs.PutTipSet(context.TODO(), ts); err != nil {
|
2019-07-05 14:29:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:59:55 +00:00
|
|
|
return cs.ds.Put(dstore.NewKey("0"), b.Cid().Bytes())
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 04:33:29 +00:00
|
|
|
func (cs *ChainStore) PutTipSet(ctx context.Context, ts *types.TipSet) error {
|
2019-10-06 03:32:56 +00:00
|
|
|
for _, b := range ts.Blocks() {
|
|
|
|
if err := cs.PersistBlockHeader(b); err != nil {
|
2019-07-05 14:29:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-06 03:32:56 +00:00
|
|
|
expanded, err := cs.expandTipset(ts.Blocks()[0])
|
2019-09-08 20:14:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("errored while expanding tipset: %w", err)
|
|
|
|
}
|
2019-10-06 03:32:56 +00:00
|
|
|
log.Debugf("expanded %s into %s\n", ts.Cids(), expanded.Cids())
|
2019-09-08 20:14:01 +00:00
|
|
|
|
2019-10-15 04:33:29 +00:00
|
|
|
if err := cs.MaybeTakeHeavierTipSet(ctx, expanded); err != nil {
|
2019-07-28 05:35:32 +00:00
|
|
|
return errors.Wrap(err, "MaybeTakeHeavierTipSet failed in PutTipSet")
|
|
|
|
}
|
2019-07-05 14:29:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-15 04:33:29 +00:00
|
|
|
func (cs *ChainStore) MaybeTakeHeavierTipSet(ctx context.Context, ts *types.TipSet) error {
|
2019-07-05 14:29:17 +00:00
|
|
|
cs.heaviestLk.Lock()
|
|
|
|
defer cs.heaviestLk.Unlock()
|
2019-10-15 04:33:29 +00:00
|
|
|
w, err := cs.Weight(ctx, ts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
heaviestW, err := cs.Weight(ctx, cs.heaviest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.GreaterThan(heaviestW) {
|
2019-07-31 07:13:49 +00:00
|
|
|
// TODO: don't do this for initial sync. Now that we don't have a
|
|
|
|
// difference between 'bootstrap sync' and 'caught up' sync, we need
|
|
|
|
// some other heuristic.
|
2019-10-10 03:50:50 +00:00
|
|
|
return cs.takeHeaviestTipSet(ts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:50:49 +00:00
|
|
|
type reorg struct {
|
|
|
|
old *types.TipSet
|
|
|
|
new *types.TipSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ChainStore) reorgWorker(ctx context.Context) chan<- reorg {
|
|
|
|
out := make(chan reorg, 32)
|
|
|
|
go func() {
|
|
|
|
defer log.Warn("reorgWorker quit")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r := <-out:
|
|
|
|
revert, apply, err := cs.ReorgOps(r.old, r.new)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("computing reorg ops failed: ", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, hcf := range cs.headChangeNotifs {
|
|
|
|
if err := hcf(revert, apply); err != nil {
|
|
|
|
log.Error("head change func errored (BAD): ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-10-10 03:50:50 +00:00
|
|
|
func (cs *ChainStore) takeHeaviestTipSet(ts *types.TipSet) error {
|
2019-10-10 23:50:49 +00:00
|
|
|
if cs.heaviest != nil { // buf
|
|
|
|
if len(cs.reorgCh) > 0 {
|
|
|
|
log.Warnf("Reorg channel running behind, %d reorgs buffered", len(cs.reorgCh))
|
2019-10-10 03:50:50 +00:00
|
|
|
}
|
2019-10-10 23:50:49 +00:00
|
|
|
cs.reorgCh <- reorg{
|
|
|
|
old: cs.heaviest,
|
|
|
|
new: ts,
|
2019-07-17 03:05:55 +00:00
|
|
|
}
|
2019-10-10 03:50:50 +00:00
|
|
|
} else {
|
2019-10-16 08:01:41 +00:00
|
|
|
log.Warnf("no heaviest tipset found, using %s", ts.Cids())
|
2019-10-10 03:50:50 +00:00
|
|
|
}
|
2019-08-14 03:31:58 +00:00
|
|
|
|
2019-10-10 03:50:50 +00:00
|
|
|
log.Debugf("New heaviest tipset! %s", ts.Cids())
|
|
|
|
cs.heaviest = ts
|
2019-07-24 02:45:00 +00:00
|
|
|
|
2019-10-10 03:50:50 +00:00
|
|
|
if err := cs.writeHead(ts); err != nil {
|
|
|
|
log.Errorf("failed to write chain head: %s", err)
|
|
|
|
return nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-10-10 03:50:50 +00:00
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-10 03:50:50 +00:00
|
|
|
// SetHead sets the chainstores current 'best' head node.
|
|
|
|
// This should only be called if something is broken and needs fixing
|
|
|
|
func (cs *ChainStore) SetHead(ts *types.TipSet) error {
|
|
|
|
cs.heaviestLk.Lock()
|
|
|
|
defer cs.heaviestLk.Unlock()
|
|
|
|
return cs.takeHeaviestTipSet(ts)
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) Contains(ts *types.TipSet) (bool, error) {
|
|
|
|
for _, c := range ts.Cids() {
|
2019-07-05 14:29:17 +00:00
|
|
|
has, err := cs.bs.Has(c)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) GetBlock(c cid.Cid) (*types.BlockHeader, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
sb, err := cs.bs.Get(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
return types.DecodeBlock(sb.RawData())
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) LoadTipSet(cids []cid.Cid) (*types.TipSet, error) {
|
2019-07-25 22:15:03 +00:00
|
|
|
var blks []*types.BlockHeader
|
2019-07-05 14:29:17 +00:00
|
|
|
for _, c := range cids {
|
|
|
|
b, err := cs.GetBlock(c)
|
|
|
|
if err != nil {
|
2019-09-30 23:55:35 +00:00
|
|
|
return nil, xerrors.Errorf("get block %s: %w", c, err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blks = append(blks, b)
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
return types.NewTipSet(blks)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if 'a' is an ancestor of 'b'
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) IsAncestorOf(a, b *types.TipSet) (bool, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
if b.Height() <= a.Height() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cur := b
|
|
|
|
for !a.Equals(cur) && cur.Height() > a.Height() {
|
|
|
|
next, err := cs.LoadTipSet(b.Parents())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = next
|
|
|
|
}
|
|
|
|
|
|
|
|
return cur.Equals(a), nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) NearestCommonAncestor(a, b *types.TipSet) (*types.TipSet, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
l, _, err := cs.ReorgOps(a, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cs.LoadTipSet(l[len(l)-1].Parents())
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) ReorgOps(a, b *types.TipSet) ([]*types.TipSet, []*types.TipSet, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
left := a
|
|
|
|
right := b
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
var leftChain, rightChain []*types.TipSet
|
2019-07-05 14:29:17 +00:00
|
|
|
for !left.Equals(right) {
|
|
|
|
if left.Height() > right.Height() {
|
|
|
|
leftChain = append(leftChain, left)
|
|
|
|
par, err := cs.LoadTipSet(left.Parents())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
left = par
|
|
|
|
} else {
|
|
|
|
rightChain = append(rightChain, right)
|
|
|
|
par, err := cs.LoadTipSet(right.Parents())
|
|
|
|
if err != nil {
|
2019-07-31 07:13:49 +00:00
|
|
|
log.Infof("failed to fetch right.Parents: %s", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
right = par
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return leftChain, rightChain, nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) GetHeaviestTipSet() *types.TipSet {
|
2019-07-05 14:29:17 +00:00
|
|
|
cs.heaviestLk.Lock()
|
|
|
|
defer cs.heaviestLk.Unlock()
|
|
|
|
return cs.heaviest
|
|
|
|
}
|
|
|
|
|
2019-10-10 03:04:10 +00:00
|
|
|
func (cs *ChainStore) AddToTipSetTracker(b *types.BlockHeader) error {
|
2019-09-03 04:36:07 +00:00
|
|
|
cs.tstLk.Lock()
|
|
|
|
defer cs.tstLk.Unlock()
|
|
|
|
|
|
|
|
tss := cs.tipsets[b.Height]
|
|
|
|
for _, oc := range tss {
|
|
|
|
if oc == b.Cid() {
|
2019-09-25 13:38:59 +00:00
|
|
|
log.Debug("tried to add block to tipset tracker that was already there")
|
2019-09-03 04:36:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cs.tipsets[b.Height] = append(tss, b.Cid())
|
|
|
|
|
|
|
|
// TODO: do we want to look for slashable submissions here? might as well...
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) PersistBlockHeader(b *types.BlockHeader) error {
|
2019-07-05 14:29:17 +00:00
|
|
|
sb, err := b.ToStorageBlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cs.bs.Put(sb)
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
type storable interface {
|
|
|
|
ToStorageBlock() (block.Block, error)
|
|
|
|
}
|
|
|
|
|
2019-07-31 07:13:49 +00:00
|
|
|
func PutMessage(bs blockstore.Blockstore, m storable) (cid.Cid, error) {
|
|
|
|
b, err := m.ToStorageBlock()
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
2019-07-26 04:54:22 +00:00
|
|
|
return cid.Undef, err
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 07:13:49 +00:00
|
|
|
if err := bs.Put(b); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Cid(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ChainStore) PutMessage(m storable) (cid.Cid, error) {
|
|
|
|
return PutMessage(cs.bs, m)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 04:36:07 +00:00
|
|
|
func (cs *ChainStore) expandTipset(b *types.BlockHeader) (*types.TipSet, error) {
|
|
|
|
// Hold lock for the whole function for now, if it becomes a problem we can
|
|
|
|
// fix pretty easily
|
|
|
|
cs.tstLk.Lock()
|
|
|
|
defer cs.tstLk.Unlock()
|
|
|
|
|
|
|
|
all := []*types.BlockHeader{b}
|
|
|
|
|
|
|
|
tsets, ok := cs.tipsets[b.Height]
|
|
|
|
if !ok {
|
|
|
|
return types.NewTipSet(all)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, bhc := range tsets {
|
|
|
|
if bhc == b.Cid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
h, err := cs.GetBlock(bhc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load block (%s) for tipset expansion: %w", bhc, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if types.CidArrsEqual(h.Parents, b.Parents) {
|
|
|
|
all = append(all, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: other validation...?
|
|
|
|
|
|
|
|
return types.NewTipSet(all)
|
|
|
|
}
|
|
|
|
|
2019-10-15 04:33:29 +00:00
|
|
|
func (cs *ChainStore) AddBlock(ctx context.Context, b *types.BlockHeader) error {
|
2019-07-26 04:54:22 +00:00
|
|
|
if err := cs.PersistBlockHeader(b); err != nil {
|
2019-07-05 14:29:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-03 04:36:07 +00:00
|
|
|
ts, err := cs.expandTipset(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-15 04:33:29 +00:00
|
|
|
if err := cs.MaybeTakeHeavierTipSet(ctx, ts); err != nil {
|
2019-07-28 05:35:32 +00:00
|
|
|
return errors.Wrap(err, "MaybeTakeHeavierTipSet failed")
|
|
|
|
}
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) GetGenesis() (*types.BlockHeader, error) {
|
2019-07-12 03:59:55 +00:00
|
|
|
data, err := cs.ds.Get(dstore.NewKey("0"))
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := cid.Cast(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
genb, err := cs.bs.Get(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
return types.DecodeBlock(genb.RawData())
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:51:34 +00:00
|
|
|
func (cs *ChainStore) GetCMessage(c cid.Cid) (ChainMsg, error) {
|
|
|
|
m, err := cs.GetMessage(c)
|
|
|
|
if err == nil {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return cs.GetSignedMessage(c)
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:40:47 +00:00
|
|
|
func (cs *ChainStore) GetMessage(c cid.Cid) (*types.Message, error) {
|
|
|
|
sb, err := cs.bs.Get(c)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("get message get failed: %s: %s", c, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.DecodeMessage(sb.RawData())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ChainStore) GetSignedMessage(c cid.Cid) (*types.SignedMessage, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
sb, err := cs.bs.Get(c)
|
|
|
|
if err != nil {
|
2019-07-31 07:13:49 +00:00
|
|
|
log.Errorf("get message get failed: %s: %s", c, err)
|
2019-07-05 14:29:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
return types.DecodeSignedMessage(sb.RawData())
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 01:56:37 +00:00
|
|
|
func (cs *ChainStore) readAMTCids(root cid.Cid) ([]cid.Cid, error) {
|
|
|
|
bs := amt.WrapBlockstore(cs.bs)
|
|
|
|
a, err := amt.LoadAMT(bs, root)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
2019-09-17 01:56:37 +00:00
|
|
|
return nil, xerrors.Errorf("amt load: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cids []cid.Cid
|
2019-09-17 01:56:37 +00:00
|
|
|
for i := uint64(0); i < a.Count; i++ {
|
|
|
|
var c cbg.CborCid
|
|
|
|
if err := a.Get(i, &c); err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load cid from amt: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 01:56:37 +00:00
|
|
|
cids = append(cids, cid.Cid(c))
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 03:05:55 +00:00
|
|
|
return cids, nil
|
|
|
|
}
|
|
|
|
|
2019-09-29 06:29:03 +00:00
|
|
|
type ChainMsg interface {
|
|
|
|
Cid() cid.Cid
|
|
|
|
VMMessage() *types.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ChainStore) MessagesForTipset(ts *types.TipSet) ([]ChainMsg, error) {
|
|
|
|
applied := make(map[address.Address]uint64)
|
|
|
|
balances := make(map[address.Address]types.BigInt)
|
|
|
|
|
|
|
|
cst := hamt.CSTFromBstore(cs.bs)
|
|
|
|
st, err := state.LoadStateTree(cst, ts.Blocks()[0].ParentStateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load state tree")
|
|
|
|
}
|
|
|
|
|
|
|
|
preloadAddr := func(a address.Address) error {
|
|
|
|
if _, ok := applied[a]; !ok {
|
|
|
|
act, err := st.GetActor(a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
applied[a] = act.Nonce
|
|
|
|
balances[a] = act.Balance
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []ChainMsg
|
|
|
|
for _, b := range ts.Blocks() {
|
|
|
|
bms, sms, err := cs.MessagesForBlock(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get messages for block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmsgs := make([]ChainMsg, 0, len(bms)+len(sms))
|
|
|
|
for _, m := range bms {
|
|
|
|
cmsgs = append(cmsgs, m)
|
|
|
|
}
|
|
|
|
for _, sm := range sms {
|
|
|
|
cmsgs = append(cmsgs, sm)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cm := range cmsgs {
|
|
|
|
m := cm.VMMessage()
|
|
|
|
if err := preloadAddr(m.From); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if applied[m.From] != m.Nonce {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
applied[m.From]++
|
|
|
|
|
|
|
|
if balances[m.From].LessThan(m.RequiredFunds()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
balances[m.From] = types.BigSub(balances[m.From], m.RequiredFunds())
|
|
|
|
|
|
|
|
out = append(out, cm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:40:47 +00:00
|
|
|
func (cs *ChainStore) MessagesForBlock(b *types.BlockHeader) ([]*types.Message, []*types.SignedMessage, error) {
|
|
|
|
cst := hamt.CSTFromBstore(cs.bs)
|
|
|
|
var msgmeta types.MsgMeta
|
|
|
|
if err := cst.Get(context.TODO(), b.Messages, &msgmeta); err != nil {
|
2019-09-06 20:03:28 +00:00
|
|
|
return nil, nil, xerrors.Errorf("failed to load msgmeta: %w", err)
|
2019-08-01 20:40:47 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 01:56:37 +00:00
|
|
|
blscids, err := cs.readAMTCids(msgmeta.BlsMessages)
|
2019-08-01 20:40:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "loading bls message cids for block")
|
|
|
|
}
|
|
|
|
|
2019-09-17 01:56:37 +00:00
|
|
|
secpkcids, err := cs.readAMTCids(msgmeta.SecpkMessages)
|
2019-08-01 20:40:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "loading secpk message cids for block")
|
|
|
|
}
|
|
|
|
|
|
|
|
blsmsgs, err := cs.LoadMessagesFromCids(blscids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "loading bls messages for block")
|
|
|
|
}
|
|
|
|
|
|
|
|
secpkmsgs, err := cs.LoadSignedMessagesFromCids(secpkcids)
|
2019-07-17 03:05:55 +00:00
|
|
|
if err != nil {
|
2019-08-01 20:40:47 +00:00
|
|
|
return nil, nil, errors.Wrap(err, "loading secpk messages for block")
|
2019-07-17 03:05:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 20:40:47 +00:00
|
|
|
return blsmsgs, secpkmsgs, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 20:22:21 +00:00
|
|
|
func (cs *ChainStore) GetParentReceipt(b *types.BlockHeader, i int) (*types.MessageReceipt, error) {
|
2019-09-17 01:56:37 +00:00
|
|
|
bs := amt.WrapBlockstore(cs.bs)
|
2019-09-27 23:55:15 +00:00
|
|
|
a, err := amt.LoadAMT(bs, b.ParentMessageReceipts)
|
2019-07-17 03:05:55 +00:00
|
|
|
if err != nil {
|
2019-09-17 01:56:37 +00:00
|
|
|
return nil, errors.Wrap(err, "amt load")
|
2019-07-17 03:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var r types.MessageReceipt
|
2019-09-17 01:56:37 +00:00
|
|
|
if err := a.Get(uint64(i), &r); err != nil {
|
2019-07-17 03:05:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:40:47 +00:00
|
|
|
func (cs *ChainStore) LoadMessagesFromCids(cids []cid.Cid) ([]*types.Message, error) {
|
|
|
|
msgs := make([]*types.Message, 0, len(cids))
|
2019-07-31 07:13:49 +00:00
|
|
|
for i, c := range cids {
|
2019-07-05 14:29:17 +00:00
|
|
|
m, err := cs.GetMessage(c)
|
|
|
|
if err != nil {
|
2019-07-31 07:13:49 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to get message: (%s):%d", c, i)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msgs = append(msgs, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgs, nil
|
|
|
|
}
|
2019-07-18 20:26:04 +00:00
|
|
|
|
2019-08-01 20:40:47 +00:00
|
|
|
func (cs *ChainStore) LoadSignedMessagesFromCids(cids []cid.Cid) ([]*types.SignedMessage, error) {
|
|
|
|
msgs := make([]*types.SignedMessage, 0, len(cids))
|
|
|
|
for i, c := range cids {
|
|
|
|
m, err := cs.GetSignedMessage(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to get message: (%s):%d", c, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs = append(msgs, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgs, nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) Blockstore() blockstore.Blockstore {
|
|
|
|
return cs.bs
|
|
|
|
}
|
2019-08-02 00:13:57 +00:00
|
|
|
|
|
|
|
func (cs *ChainStore) TryFillTipSet(ts *types.TipSet) (*FullTipSet, error) {
|
|
|
|
var out []*types.FullBlock
|
|
|
|
|
|
|
|
for _, b := range ts.Blocks() {
|
2019-08-01 20:40:47 +00:00
|
|
|
bmsgs, smsgs, err := cs.MessagesForBlock(b)
|
2019-08-02 00:13:57 +00:00
|
|
|
if err != nil {
|
2019-08-02 00:57:29 +00:00
|
|
|
// TODO: check for 'not found' errors, and only return nil if this
|
|
|
|
// is actually a 'not found' error
|
2019-08-02 00:13:57 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fb := &types.FullBlock{
|
2019-08-01 20:40:47 +00:00
|
|
|
Header: b,
|
|
|
|
BlsMessages: bmsgs,
|
|
|
|
SecpkMessages: smsgs,
|
2019-08-02 00:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, fb)
|
|
|
|
}
|
|
|
|
return NewFullTipSet(out), nil
|
|
|
|
}
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2019-09-20 12:22:46 +00:00
|
|
|
func (cs *ChainStore) GetRandomness(ctx context.Context, blks []cid.Cid, tickets []*types.Ticket, lb int64) ([]byte, error) {
|
2019-10-13 09:08:34 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "store.GetRandomness")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-09-20 02:54:52 +00:00
|
|
|
if lb < 0 {
|
|
|
|
return nil, fmt.Errorf("negative lookback parameters are not valid (got %d)", lb)
|
|
|
|
}
|
|
|
|
lt := int64(len(tickets))
|
|
|
|
if lb < lt {
|
2019-10-11 00:02:54 +00:00
|
|
|
log.Desugar().Warn("self sampling randomness. this should be extremely rare, if you see this often it may be a bug", zap.Stack("stacktrace"))
|
2019-09-09 20:03:10 +00:00
|
|
|
|
2019-09-20 02:54:52 +00:00
|
|
|
t := tickets[lt-(1+lb)]
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2019-10-09 04:38:59 +00:00
|
|
|
return t.VRFProof, nil
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 02:54:52 +00:00
|
|
|
nv := lb - lt
|
2019-09-06 06:26:02 +00:00
|
|
|
|
|
|
|
for {
|
2019-09-20 12:22:46 +00:00
|
|
|
nts, err := cs.LoadTipSet(blks)
|
2019-09-06 06:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mtb := nts.MinTicketBlock()
|
2019-09-20 02:54:52 +00:00
|
|
|
lt := int64(len(mtb.Tickets))
|
|
|
|
if nv < lt {
|
|
|
|
t := mtb.Tickets[lt-(1+nv)]
|
2019-10-09 04:38:59 +00:00
|
|
|
return t.VRFProof, nil
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 02:54:52 +00:00
|
|
|
nv -= lt
|
2019-09-06 06:26:02 +00:00
|
|
|
|
|
|
|
// special case for lookback behind genesis block
|
|
|
|
// TODO(spec): this is not in the spec, need to sync that
|
|
|
|
if mtb.Height == 0 {
|
|
|
|
|
|
|
|
t := mtb.Tickets[0]
|
|
|
|
|
2019-10-09 04:38:59 +00:00
|
|
|
rval := t.VRFProof
|
2019-09-20 02:54:52 +00:00
|
|
|
for i := int64(0); i < nv; i++ {
|
2019-09-06 06:26:02 +00:00
|
|
|
h := sha256.Sum256(rval)
|
|
|
|
rval = h[:]
|
|
|
|
}
|
|
|
|
return rval, nil
|
|
|
|
}
|
2019-09-06 20:03:28 +00:00
|
|
|
|
2019-09-20 12:22:46 +00:00
|
|
|
blks = mtb.Parents
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-18 03:25:12 +00:00
|
|
|
|
|
|
|
func (cs *ChainStore) GetTipsetByHeight(ctx context.Context, h uint64, ts *types.TipSet) (*types.TipSet, error) {
|
|
|
|
if ts == nil {
|
|
|
|
ts = cs.GetHeaviestTipSet()
|
|
|
|
}
|
|
|
|
|
|
|
|
if h > ts.Height() {
|
|
|
|
return nil, xerrors.Errorf("looking for tipset with height less than start point")
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:34:18 +00:00
|
|
|
if ts.Height()-h > build.ForkLengthThreshold {
|
|
|
|
log.Warnf("expensive call to GetTipsetByHeight, seeking %d levels", ts.Height()-h)
|
|
|
|
}
|
|
|
|
|
2019-09-18 03:25:12 +00:00
|
|
|
for {
|
|
|
|
mtb := ts.MinTicketBlock()
|
|
|
|
if h >= ts.Height()-uint64(len(mtb.Tickets)) {
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pts, err := cs.LoadTipSet(ts.Parents())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ts = pts
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 04:33:29 +00:00
|
|
|
|
|
|
|
type chainRand struct {
|
|
|
|
cs *ChainStore
|
|
|
|
blks []cid.Cid
|
|
|
|
bh uint64
|
|
|
|
tickets []*types.Ticket
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChainRand(cs *ChainStore, blks []cid.Cid, bheight uint64, tickets []*types.Ticket) vm.Rand {
|
|
|
|
return &chainRand{
|
|
|
|
cs: cs,
|
|
|
|
blks: blks,
|
|
|
|
bh: bheight,
|
|
|
|
tickets: tickets,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *chainRand) GetRandomness(ctx context.Context, h int64) ([]byte, error) {
|
|
|
|
lb := (int64(cr.bh) + int64(len(cr.tickets))) - h
|
|
|
|
return cr.cs.GetRandomness(ctx, cr.blks, cr.tickets, lb)
|
|
|
|
}
|