2019-08-20 16:48:33 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-08-22 01:29:19 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-08-20 16:48:33 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ChainAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
2019-08-27 00:46:39 +00:00
|
|
|
WalletAPI
|
|
|
|
|
2019-08-20 16:48:33 +00:00
|
|
|
Chain *store.ChainStore
|
|
|
|
PubSub *pubsub.PubSub
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-18 02:50:03 +00:00
|
|
|
func (a *ChainAPI) ChainSubmitBlock(ctx context.Context, blk *types.BlockMsg) error {
|
2019-10-05 23:18:39 +00:00
|
|
|
// TODO: should we have some sort of fast path to adding a local block?
|
2019-08-20 16:48:33 +00:00
|
|
|
|
|
|
|
b, err := blk.Serialize()
|
|
|
|
if err != nil {
|
2019-08-22 01:29:19 +00:00
|
|
|
return xerrors.Errorf("serializing block for pubsub publishing failed: %w", err)
|
2019-08-20 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: anything else to do here?
|
|
|
|
return a.PubSub.Publish("/fil/blocks", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ChainAPI) ChainHead(context.Context) (*types.TipSet, error) {
|
|
|
|
return a.Chain.GetHeaviestTipSet(), nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
func (a *ChainAPI) ChainGetRandomness(ctx context.Context, pts *types.TipSet, tickets []*types.Ticket, lb int) ([]byte, error) {
|
2019-09-20 12:22:46 +00:00
|
|
|
return a.Chain.GetRandomness(ctx, pts.Cids(), tickets, int64(lb))
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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-10-03 20:22:21 +00:00
|
|
|
pts, err := a.Chain.LoadTipSet(b.Parents)
|
|
|
|
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
|
|
|
|
pts, err := a.Chain.LoadTipSet(b.Parents)
|
|
|
|
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
|
|
|
|
|
|
|
func (a *ChainAPI) ChainGetTipSetByHeight(ctx context.Context, h uint64, ts *types.TipSet) (*types.TipSet, error) {
|
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
|
|
|
|
|
|
|
func (a *ChainAPI) ChainSetHead(ctx context.Context, ts *types.TipSet) error {
|
|
|
|
return a.Chain.SetHead(ts)
|
|
|
|
}
|