2019-08-20 16:48:33 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-19 15:50:18 +00:00
|
|
|
"fmt"
|
2020-01-20 23:51:02 +00:00
|
|
|
"io"
|
2019-12-19 15:50:18 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-amt-ipld"
|
|
|
|
"github.com/ipfs/go-blockservice"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-hamt-ipld"
|
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
offline "github.com/ipfs/go-ipfs-exchange-offline"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
|
|
|
"github.com/ipfs/go-merkledag"
|
|
|
|
"github.com/ipfs/go-path"
|
|
|
|
"github.com/ipfs/go-path/resolver"
|
|
|
|
mh "github.com/multiformats/go-multihash"
|
2020-01-20 23:51:02 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2019-12-19 15:50:18 +00:00
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
2019-08-20 16:48:33 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-12-18 15:37:47 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-08-20 16:48:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ChainAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
2019-08-27 00:46:39 +00:00
|
|
|
WalletAPI
|
|
|
|
|
2019-10-14 14:21:37 +00:00
|
|
|
Chain *store.ChainStore
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 11:01:52 +00:00
|
|
|
func (a *ChainAPI) ChainNotify(ctx context.Context) (<-chan []*store.HeadChange, error) {
|
2019-08-20 16:48:33 +00:00
|
|
|
return a.Chain.SubHeadChanges(ctx), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ChainAPI) ChainHead(context.Context) (*types.TipSet, error) {
|
|
|
|
return a.Chain.GetHeaviestTipSet(), nil
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:53:00 +00:00
|
|
|
func (a *ChainAPI) ChainGetRandomness(ctx context.Context, pts types.TipSetKey, round int64) ([]byte, error) {
|
|
|
|
return a.Chain.GetRandomness(ctx, pts.Cids(), round)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ChainAPI) ChainGetBlock(ctx context.Context, msg cid.Cid) (*types.BlockHeader, error) {
|
|
|
|
return a.Chain.GetBlock(msg)
|
|
|
|
}
|
|
|
|
|
2019-11-08 05:36:50 +00:00
|
|
|
func (a *ChainAPI) ChainGetTipSet(ctx context.Context, key types.TipSetKey) (*types.TipSet, error) {
|
2019-12-16 19:22:56 +00:00
|
|
|
return a.Chain.LoadTipSet(key)
|
2019-10-11 06:25:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 16:48:33 +00:00
|
|
|
func (a *ChainAPI) ChainGetBlockMessages(ctx context.Context, msg cid.Cid) (*api.BlockMessages, error) {
|
|
|
|
b, err := a.Chain.GetBlock(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bmsgs, smsgs, err := a.Chain.MessagesForBlock(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-23 11:15:16 +00:00
|
|
|
cids := make([]cid.Cid, len(bmsgs)+len(smsgs))
|
|
|
|
|
|
|
|
for i, m := range bmsgs {
|
|
|
|
cids[i] = m.Cid()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, m := range smsgs {
|
|
|
|
cids[i+len(bmsgs)] = m.Cid()
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:48:33 +00:00
|
|
|
return &api.BlockMessages{
|
|
|
|
BlsMessages: bmsgs,
|
|
|
|
SecpkMessages: smsgs,
|
2019-09-23 11:15:16 +00:00
|
|
|
Cids: cids,
|
2019-08-20 16:48:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:24:08 +00:00
|
|
|
func (a *ChainAPI) ChainGetPath(ctx context.Context, from types.TipSetKey, to types.TipSetKey) ([]*store.HeadChange, error) {
|
|
|
|
return a.Chain.GetPath(ctx, from, to)
|
|
|
|
}
|
|
|
|
|
2019-10-06 00:37:28 +00:00
|
|
|
func (a *ChainAPI) ChainGetParentMessages(ctx context.Context, bcid cid.Cid) ([]api.Message, error) {
|
2019-08-20 16:48:33 +00:00
|
|
|
b, err := a.Chain.GetBlock(bcid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-05 16:04:58 +00:00
|
|
|
// genesis block has no parent messages...
|
|
|
|
if b.Height == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:48:33 +00:00
|
|
|
// TODO: need to get the number of messages better than this
|
2019-12-16 19:22:56 +00:00
|
|
|
pts, err := a.Chain.LoadTipSet(types.NewTipSetKey(b.Parents...))
|
2019-10-03 20:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cm, err := a.Chain.MessagesForTipset(pts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-06 00:37:28 +00:00
|
|
|
var out []api.Message
|
2019-10-03 20:22:21 +00:00
|
|
|
for _, m := range cm {
|
2019-10-06 00:37:28 +00:00
|
|
|
out = append(out, api.Message{
|
|
|
|
Cid: m.Cid(),
|
|
|
|
Message: m.VMMessage(),
|
|
|
|
})
|
2019-10-03 20:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ChainAPI) ChainGetParentReceipts(ctx context.Context, bcid cid.Cid) ([]*types.MessageReceipt, error) {
|
|
|
|
b, err := a.Chain.GetBlock(bcid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-05 16:04:58 +00:00
|
|
|
if b.Height == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 20:22:21 +00:00
|
|
|
// TODO: need to get the number of messages better than this
|
2019-12-16 19:22:56 +00:00
|
|
|
pts, err := a.Chain.LoadTipSet(types.NewTipSetKey(b.Parents...))
|
2019-10-03 20:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cm, err := a.Chain.MessagesForTipset(pts)
|
2019-08-20 16:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []*types.MessageReceipt
|
2019-10-03 20:22:21 +00:00
|
|
|
for i := 0; i < len(cm); i++ {
|
|
|
|
r, err := a.Chain.GetParentReceipt(b, i)
|
2019-08-20 16:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
2019-09-18 00:08:49 +00:00
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *ChainAPI) ChainGetTipSetByHeight(ctx context.Context, h uint64, tsk types.TipSetKey) (*types.TipSet, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-09-18 13:32:21 +00:00
|
|
|
return a.Chain.GetTipsetByHeight(ctx, h, ts)
|
2019-09-18 00:08:49 +00:00
|
|
|
}
|
2019-10-01 16:28:07 +00:00
|
|
|
|
|
|
|
func (a *ChainAPI) ChainReadObj(ctx context.Context, obj cid.Cid) ([]byte, error) {
|
|
|
|
blk, err := a.Chain.Blockstore().Get(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("blockstore get: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blk.RawData(), nil
|
|
|
|
}
|
2019-10-10 03:50:50 +00:00
|
|
|
|
2020-02-04 02:45:20 +00:00
|
|
|
func (a *ChainAPI) ChainHasObj(ctx context.Context, obj cid.Cid) (bool, error) {
|
|
|
|
return a.Chain.Blockstore().Has(obj)
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *ChainAPI) ChainSetHead(ctx context.Context, tsk types.TipSetKey) error {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-10-10 03:50:50 +00:00
|
|
|
return a.Chain.SetHead(ts)
|
|
|
|
}
|
2019-10-11 02:14:22 +00:00
|
|
|
|
|
|
|
func (a *ChainAPI) ChainGetGenesis(ctx context.Context) (*types.TipSet, error) {
|
|
|
|
genb, err := a.Chain.GetGenesis()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.NewTipSet([]*types.BlockHeader{genb})
|
|
|
|
}
|
2019-10-15 05:00:30 +00:00
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *ChainAPI) ChainTipSetWeight(ctx context.Context, tsk types.TipSetKey) (types.BigInt, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyInt, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2019-10-15 05:00:30 +00:00
|
|
|
return a.Chain.Weight(ctx, ts)
|
|
|
|
}
|
2019-12-19 15:50:18 +00:00
|
|
|
|
|
|
|
func resolveOnce(bs blockstore.Blockstore) func(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, names []string) (*ipld.Link, []string, error) {
|
|
|
|
return func(ctx context.Context, ds ipld.NodeGetter, nd ipld.Node, names []string) (*ipld.Link, []string, error) {
|
|
|
|
if strings.HasPrefix(names[0], "@Ha:") {
|
|
|
|
addr, err := address.NewFromString(names[0][4:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("parsing addr: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
names[0] = "@H:" + string(addr.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(names[0], "@H:") {
|
|
|
|
cst := hamt.CSTFromBstore(bs)
|
|
|
|
|
|
|
|
h, err := hamt.LoadNode(ctx, cst, nd.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("resolving hamt link: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var m interface{}
|
|
|
|
if err := h.Find(ctx, names[0][3:], &m); err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("resolve hamt: %w", err)
|
|
|
|
}
|
|
|
|
if c, ok := m.(cid.Cid); ok {
|
|
|
|
return &ipld.Link{
|
|
|
|
Name: names[0][3:],
|
|
|
|
Cid: c,
|
|
|
|
}, names[1:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := cbor.WrapObject(m, mh.SHA2_256, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bs.Put(n); err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("put hamt val: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(names) == 1 {
|
|
|
|
return &ipld.Link{
|
|
|
|
Name: names[0][3:],
|
|
|
|
Cid: n.Cid(),
|
|
|
|
}, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolveOnce(bs)(ctx, ds, n, names[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(names[0], "@A:") {
|
|
|
|
a, err := amt.LoadAMT(amt.WrapBlockstore(bs), nd.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("load amt: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, err := strconv.ParseUint(names[0][3:], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("parsing amt index: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var m interface{}
|
|
|
|
if err := a.Get(idx, &m); err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("amt get: %w", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("AG %T %v\n", m, m)
|
|
|
|
if c, ok := m.(cid.Cid); ok {
|
|
|
|
return &ipld.Link{
|
|
|
|
Name: names[0][3:],
|
|
|
|
Cid: c,
|
|
|
|
}, names[1:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := cbor.WrapObject(m, mh.SHA2_256, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bs.Put(n); err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("put amt val: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(names) == 1 {
|
|
|
|
return &ipld.Link{
|
|
|
|
Name: names[0][3:],
|
|
|
|
Size: 0,
|
|
|
|
Cid: n.Cid(),
|
|
|
|
}, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolveOnce(bs)(ctx, ds, n, names[1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
return nd.ResolveLink(names)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ChainAPI) ChainGetNode(ctx context.Context, p string) (interface{}, error) {
|
|
|
|
ip, err := path.ParsePath(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("parsing path: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := a.Chain.Blockstore()
|
|
|
|
bsvc := blockservice.New(bs, offline.Exchange(bs))
|
|
|
|
|
|
|
|
dag := merkledag.NewDAGService(bsvc)
|
|
|
|
|
|
|
|
r := &resolver.Resolver{
|
|
|
|
DAG: dag,
|
|
|
|
ResolveOnce: resolveOnce(bs),
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := r.ResolvePath(ctx, ip)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
2020-01-07 19:03:11 +00:00
|
|
|
|
|
|
|
func (a *ChainAPI) ChainGetMessage(ctx context.Context, mc cid.Cid) (*types.Message, error) {
|
|
|
|
cm, err := a.Chain.GetCMessage(mc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cm.VMMessage(), nil
|
|
|
|
}
|
2020-01-20 23:51:02 +00:00
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
func (a *ChainAPI) ChainExport(ctx context.Context, tsk types.TipSetKey) (<-chan []byte, error) {
|
|
|
|
ts, err := a.Chain.GetTipSetFromKey(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading tipset %s: %w", tsk, err)
|
|
|
|
}
|
2020-01-20 23:51:02 +00:00
|
|
|
r, w := io.Pipe()
|
|
|
|
out := make(chan []byte)
|
|
|
|
go func() {
|
2020-01-21 01:53:55 +00:00
|
|
|
defer w.Close()
|
2020-01-20 23:51:02 +00:00
|
|
|
if err := a.Chain.Export(ctx, ts, w); err != nil {
|
|
|
|
log.Errorf("chain export call failed: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2020-01-21 01:53:55 +00:00
|
|
|
defer close(out)
|
2020-01-20 23:51:02 +00:00
|
|
|
for {
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
n, err := r.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("chain export pipe read failed: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case out <- buf[:n]:
|
|
|
|
case <-ctx.Done():
|
2020-01-21 01:53:55 +00:00
|
|
|
log.Warnf("export writer failed: %s", ctx.Err())
|
2020-01-20 23:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|