2019-07-26 04:54:22 +00:00
|
|
|
package store
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-17 03:05:55 +00:00
|
|
|
"encoding/json"
|
2019-07-05 14:29:17 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2019-07-08 12:51:45 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
2019-07-25 22:15:03 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/state"
|
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"
|
|
|
|
pubsub "github.com/whyrusleeping/pubsub"
|
2019-07-05 14:36:08 +00:00
|
|
|
sharray "github.com/whyrusleeping/sharray"
|
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-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-05 14:29:17 +00:00
|
|
|
return &ChainStore{
|
|
|
|
bs: bs,
|
|
|
|
ds: ds,
|
|
|
|
bestTips: pubsub.New(64),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) SubNewTips() chan *types.TipSet {
|
2019-07-17 03:05:55 +00:00
|
|
|
subch := cs.bestTips.Sub("best")
|
2019-07-26 04:54:22 +00:00
|
|
|
out := make(chan *types.TipSet)
|
2019-07-17 03:05:55 +00:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
for val := range subch {
|
2019-07-26 04:54:22 +00:00
|
|
|
out <- val.(*types.TipSet)
|
2019-07-17 03:05:55 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
HCRevert = "revert"
|
|
|
|
HCApply = "apply"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HeadChange struct {
|
|
|
|
Type string
|
2019-07-26 04:54:22 +00:00
|
|
|
Val *types.TipSet
|
2019-07-17 03:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ChainStore) SubHeadChanges() chan *HeadChange {
|
|
|
|
subch := cs.bestTips.Sub("headchange")
|
|
|
|
out := make(chan *HeadChange, 16)
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
for val := range subch {
|
|
|
|
out <- val.(*HeadChange)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
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-07-26 04:54:22 +00:00
|
|
|
gents, err := types.NewTipSet([]*types.BlockHeader{b})
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fts := &FullTipSet{
|
2019-07-25 22:15:03 +00:00
|
|
|
Blocks: []*types.FullBlock{
|
2019-07-05 14:29:17 +00:00
|
|
|
{Header: b},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cs.heaviest = gents
|
|
|
|
|
|
|
|
if err := cs.PutTipSet(fts); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ChainStore) PutTipSet(ts *FullTipSet) error {
|
|
|
|
for _, b := range ts.Blocks {
|
|
|
|
if err := cs.persistBlock(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
cs.MaybeTakeHeavierTipSet(ts.TipSet())
|
2019-07-05 14:29:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) MaybeTakeHeavierTipSet(ts *types.TipSet) error {
|
2019-07-05 14:29:17 +00:00
|
|
|
cs.heaviestLk.Lock()
|
|
|
|
defer cs.heaviestLk.Unlock()
|
|
|
|
if cs.heaviest == nil || cs.Weight(ts) > cs.Weight(cs.heaviest) {
|
|
|
|
revert, apply, err := cs.ReorgOps(cs.heaviest, ts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-17 03:05:55 +00:00
|
|
|
for _, hcf := range cs.headChangeNotifs {
|
|
|
|
hcf(revert, apply)
|
|
|
|
}
|
2019-07-11 05:17:49 +00:00
|
|
|
log.Infof("New heaviest tipset! %s", ts.Cids())
|
2019-07-05 14:29:17 +00:00
|
|
|
cs.heaviest = ts
|
2019-07-24 02:45:00 +00:00
|
|
|
|
|
|
|
if err := cs.writeHead(ts); err != nil {
|
2019-07-24 21:10:27 +00:00
|
|
|
log.Errorf("failed to write chain head: %s", err)
|
|
|
|
return nil
|
2019-07-24 02:45:00 +00:00
|
|
|
}
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
right = par
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return leftChain, rightChain, nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) Weight(ts *types.TipSet) uint64 {
|
2019-07-05 14:29:17 +00:00
|
|
|
return ts.Blocks()[0].ParentWeight.Uint64() + uint64(len(ts.Cids()))
|
|
|
|
}
|
|
|
|
|
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-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-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) persistBlock(b *types.FullBlock) error {
|
2019-07-26 04:54:22 +00:00
|
|
|
if err := cs.PersistBlockHeader(b.Header); err != nil {
|
2019-07-05 14:29:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range b.Messages {
|
2019-07-26 04:54:22 +00:00
|
|
|
if _, err := cs.PutMessage(m); err != nil {
|
2019-07-05 14:29:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
type storable interface {
|
|
|
|
ToStorageBlock() (block.Block, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ChainStore) PutMessage(m storable) (cid.Cid, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
sb, err := m.ToStorageBlock()
|
|
|
|
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-26 04:54:22 +00:00
|
|
|
return sb.Cid(), cs.bs.Put(sb)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) AddBlock(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-07-26 04:54:22 +00:00
|
|
|
ts, _ := types.NewTipSet([]*types.BlockHeader{b})
|
|
|
|
cs.MaybeTakeHeavierTipSet(ts)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ChainStore) TipSetState(cids []cid.Cid) (cid.Cid, error) {
|
|
|
|
ts, err := cs.LoadTipSet(cids)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed loading tipset: ", cids)
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ts.Blocks()) == 1 {
|
|
|
|
return ts.Blocks()[0].StateRoot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("cant handle multiblock tipsets yet")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) GetMessage(c cid.Cid) (*types.SignedMessage, 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.DecodeSignedMessage(sb.RawData())
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) MessageCidsForBlock(b *types.BlockHeader) ([]cid.Cid, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
cst := hamt.CSTFromBstore(cs.bs)
|
|
|
|
shar, err := sharray.Load(context.TODO(), b.Messages, 4, cst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "sharray load")
|
|
|
|
}
|
|
|
|
|
|
|
|
var cids []cid.Cid
|
|
|
|
err = shar.ForEach(context.TODO(), func(i interface{}) error {
|
|
|
|
c, ok := i.(cid.Cid)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("value in message sharray was not a cid")
|
|
|
|
}
|
|
|
|
|
|
|
|
cids = append(cids, c)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-17 03:05:55 +00:00
|
|
|
return cids, nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) MessagesForBlock(b *types.BlockHeader) ([]*types.SignedMessage, error) {
|
2019-07-17 03:05:55 +00:00
|
|
|
cids, err := cs.MessageCidsForBlock(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
return cs.LoadMessagesFromCids(cids)
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) GetReceipt(b *types.BlockHeader, i int) (*types.MessageReceipt, error) {
|
2019-07-17 03:05:55 +00:00
|
|
|
cst := hamt.CSTFromBstore(cs.bs)
|
|
|
|
shar, err := sharray.Load(context.TODO(), b.MessageReceipts, 4, cst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "sharray load")
|
|
|
|
}
|
|
|
|
|
|
|
|
ival, err := shar.Get(context.TODO(), i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// @warpfork, @EricMyhre help me. save me.
|
|
|
|
out, err := json.Marshal(ival)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.MessageReceipt
|
|
|
|
if err := json.Unmarshal(out, &r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &r, nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) LoadMessagesFromCids(cids []cid.Cid) ([]*types.SignedMessage, error) {
|
|
|
|
msgs := make([]*types.SignedMessage, 0, len(cids))
|
2019-07-05 14:29:17 +00:00
|
|
|
for _, c := range cids {
|
|
|
|
m, err := cs.GetMessage(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs = append(msgs, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgs, nil
|
|
|
|
}
|
2019-07-18 20:26:04 +00:00
|
|
|
|
|
|
|
func (cs *ChainStore) GetBalance(addr address.Address) (types.BigInt, error) {
|
2019-07-26 04:54:22 +00:00
|
|
|
act, err := cs.GetActor(addr)
|
2019-07-18 20:26:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.BigInt{}, err
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
return act.Balance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ChainStore) GetActor(addr address.Address) (*types.Actor, error) {
|
|
|
|
ts := cs.GetHeaviestTipSet()
|
|
|
|
stcid, err := cs.TipSetState(ts.Cids())
|
2019-07-18 20:26:04 +00:00
|
|
|
if err != nil {
|
2019-07-26 04:54:22 +00:00
|
|
|
return nil, err
|
2019-07-18 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
cst := hamt.CSTFromBstore(cs.bs)
|
|
|
|
state, err := state.LoadStateTree(cst, stcid)
|
2019-07-18 20:26:04 +00:00
|
|
|
if err != nil {
|
2019-07-26 04:54:22 +00:00
|
|
|
return nil, err
|
2019-07-18 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
return state.GetActor(addr)
|
2019-07-18 20:26:04 +00:00
|
|
|
}
|
2019-07-17 03:05:55 +00:00
|
|
|
|
|
|
|
func (cs *ChainStore) WaitForMessage(ctx context.Context, mcid cid.Cid) (cid.Cid, *types.MessageReceipt, error) {
|
|
|
|
tsub := cs.SubHeadChanges()
|
|
|
|
|
|
|
|
head := cs.GetHeaviestTipSet()
|
|
|
|
|
|
|
|
bc, r, err := cs.tipsetContainsMsg(head, mcid)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
return bc, r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case val := <-tsub:
|
|
|
|
switch val.Type {
|
|
|
|
case HCRevert:
|
|
|
|
continue
|
|
|
|
case HCApply:
|
|
|
|
bc, r, err := cs.tipsetContainsMsg(val.Val, mcid)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, nil, err
|
|
|
|
}
|
|
|
|
if r != nil {
|
|
|
|
return bc, r, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return cid.Undef, nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
func (cs *ChainStore) tipsetContainsMsg(ts *types.TipSet, msg cid.Cid) (cid.Cid, *types.MessageReceipt, error) {
|
2019-07-17 03:05:55 +00:00
|
|
|
for _, b := range ts.Blocks() {
|
|
|
|
r, err := cs.blockContainsMsg(b, msg)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, nil, err
|
|
|
|
}
|
|
|
|
if r != nil {
|
|
|
|
return b.Cid(), r, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cid.Undef, nil, nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
func (cs *ChainStore) blockContainsMsg(blk *types.BlockHeader, msg cid.Cid) (*types.MessageReceipt, error) {
|
2019-07-17 03:05:55 +00:00
|
|
|
msgs, err := cs.MessageCidsForBlock(blk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, mc := range msgs {
|
|
|
|
if mc == msg {
|
|
|
|
return cs.GetReceipt(blk, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-07-26 04:54:22 +00:00
|
|
|
|
|
|
|
func (cs *ChainStore) Blockstore() blockstore.Blockstore {
|
|
|
|
return cs.bs
|
|
|
|
}
|