Merge pull request #276 from filecoin-project/fix/block-printing

improve accuracy of block printing with getblock
This commit is contained in:
Whyrusleeping 2019-10-04 09:39:47 -06:00 committed by GitHub
commit b6d50c07c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 50 deletions

View File

@ -54,7 +54,8 @@ type FullNode interface {
ChainWaitMsg(context.Context, cid.Cid) (*MsgWait, error)
ChainGetBlock(context.Context, cid.Cid) (*types.BlockHeader, error)
ChainGetBlockMessages(context.Context, cid.Cid) (*BlockMessages, error)
ChainGetBlockReceipts(context.Context, cid.Cid) ([]*types.MessageReceipt, error)
ChainGetParentReceipts(context.Context, cid.Cid) ([]*types.MessageReceipt, error)
ChainGetParentMessages(context.Context, cid.Cid) ([]cid.Cid, error)
ChainGetTipSetByHeight(context.Context, uint64, *types.TipSet) (*types.TipSet, error)
ChainReadObj(context.Context, cid.Cid) ([]byte, error)

View File

@ -44,7 +44,8 @@ type FullNodeStruct struct {
ChainWaitMsg func(context.Context, cid.Cid) (*MsgWait, error) `perm:"read"`
ChainGetBlock func(context.Context, cid.Cid) (*types.BlockHeader, error) `perm:"read"`
ChainGetBlockMessages func(context.Context, cid.Cid) (*BlockMessages, error) `perm:"read"`
ChainGetBlockReceipts func(context.Context, cid.Cid) ([]*types.MessageReceipt, error) `perm:"read"`
ChainGetParentReceipts func(context.Context, cid.Cid) ([]*types.MessageReceipt, error) `perm:"read"`
ChainGetParentMessages func(context.Context, cid.Cid) ([]cid.Cid, error) `perm:"read"`
ChainGetTipSetByHeight func(context.Context, uint64, *types.TipSet) (*types.TipSet, error) `perm:"read"`
ChainReadObj func(context.Context, cid.Cid) ([]byte, error) `perm:"read"`
@ -279,8 +280,12 @@ func (c *FullNodeStruct) ChainGetBlockMessages(ctx context.Context, b cid.Cid) (
return c.Internal.ChainGetBlockMessages(ctx, b)
}
func (c *FullNodeStruct) ChainGetBlockReceipts(ctx context.Context, b cid.Cid) ([]*types.MessageReceipt, error) {
return c.Internal.ChainGetBlockReceipts(ctx, b)
func (c *FullNodeStruct) ChainGetParentReceipts(ctx context.Context, b cid.Cid) ([]*types.MessageReceipt, error) {
return c.Internal.ChainGetParentReceipts(ctx, b)
}
func (c *FullNodeStruct) ChainGetParentMessages(ctx context.Context, b cid.Cid) ([]cid.Cid, error) {
return c.Internal.ChainGetParentMessages(ctx, b)
}
func (c *FullNodeStruct) ChainNotify(ctx context.Context) (<-chan []*store.HeadChange, error) {

View File

@ -617,7 +617,7 @@ func (cs *ChainStore) MessagesForBlock(b *types.BlockHeader) ([]*types.Message,
return blsmsgs, secpkmsgs, nil
}
func (cs *ChainStore) GetReceipt(b *types.BlockHeader, i int) (*types.MessageReceipt, error) {
func (cs *ChainStore) GetParentReceipt(b *types.BlockHeader, i int) (*types.MessageReceipt, error) {
bs := amt.WrapBlockstore(cs.bs)
a, err := amt.LoadAMT(bs, b.ParentMessageReceipts)
if err != nil {
@ -718,39 +718,7 @@ func (cs *ChainStore) tipsetExecutedMessage(ts *types.TipSet, msg cid.Cid) (*typ
for i, m := range cm {
if m.Cid() == msg {
return cs.GetReceipt(ts.Blocks()[0], i)
}
}
return nil, nil
}
func (cs *ChainStore) blockContainsMsg(blk *types.BlockHeader, msg cid.Cid) (*types.MessageReceipt, error) {
cst := hamt.CSTFromBstore(cs.bs)
var msgmeta types.MsgMeta
if err := cst.Get(context.TODO(), blk.Messages, &msgmeta); err != nil {
return nil, err
}
blscids, err := cs.readAMTCids(msgmeta.BlsMessages)
if err != nil {
return nil, errors.Wrap(err, "loading bls message cids for block")
}
for i, c := range blscids {
if c == msg {
return cs.GetReceipt(blk, i)
}
}
secpkcids, err := cs.readAMTCids(msgmeta.SecpkMessages)
if err != nil {
return nil, errors.Wrap(err, "loading secpk message cids for block")
}
for i, c := range secpkcids {
if c == msg {
return cs.GetReceipt(blk, i+len(blscids))
return cs.GetParentReceipt(ts.Blocks()[0], i)
}
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"golang.org/x/xerrors"
"gopkg.in/urfave/cli.v2"
types "github.com/filecoin-project/go-lotus/chain/types"
@ -69,7 +70,7 @@ var chainGetBlock = &cli.Command{
blk, err := api.ChainGetBlock(ctx, bcid)
if err != nil {
return err
return xerrors.Errorf("get block failed: %w", err)
}
if cctx.Bool("raw") {
@ -84,25 +85,33 @@ var chainGetBlock = &cli.Command{
msgs, err := api.ChainGetBlockMessages(ctx, bcid)
if err != nil {
return err
return xerrors.Errorf("failed to get messages: %w", err)
}
recpts, err := api.ChainGetBlockReceipts(ctx, bcid)
pmsgs, err := api.ChainGetParentMessages(ctx, bcid)
if err != nil {
return err
return xerrors.Errorf("failed to get parent messages: %w", err)
}
recpts, err := api.ChainGetParentReceipts(ctx, bcid)
if err != nil {
log.Warn(err)
//return xerrors.Errorf("failed to get receipts: %w", err)
}
cblock := struct {
types.BlockHeader
BlsMessages []*types.Message
SecpkMessages []*types.SignedMessage
MessageReceipts []*types.MessageReceipt
BlsMessages []*types.Message
SecpkMessages []*types.SignedMessage
ParentReceipts []*types.MessageReceipt
ParentMessages []cid.Cid
}{}
cblock.BlockHeader = *blk
cblock.BlsMessages = msgs.BlsMessages
cblock.SecpkMessages = msgs.SecpkMessages
cblock.MessageReceipts = recpts
cblock.ParentReceipts = recpts
cblock.ParentMessages = pmsgs
out, err := json.MarshalIndent(cblock, "", " ")
if err != nil {

View File

@ -93,21 +93,51 @@ func (a *ChainAPI) ChainGetBlockMessages(ctx context.Context, msg cid.Cid) (*api
}, nil
}
func (a *ChainAPI) ChainGetBlockReceipts(ctx context.Context, bcid cid.Cid) ([]*types.MessageReceipt, error) {
func (a *ChainAPI) ChainGetParentMessages(ctx context.Context, bcid cid.Cid) ([]cid.Cid, error) {
b, err := a.Chain.GetBlock(bcid)
if err != nil {
return nil, err
}
// TODO: need to get the number of messages better than this
bm, sm, err := a.Chain.MessagesForBlock(b)
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
}
var out []cid.Cid
for _, m := range cm {
out = append(out, m.Cid())
}
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
}
// 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)
if err != nil {
return nil, err
}
var out []*types.MessageReceipt
for i := 0; i < len(bm)+len(sm); i++ {
r, err := a.Chain.GetReceipt(b, i)
for i := 0; i < len(cm); i++ {
r, err := a.Chain.GetParentReceipt(b, i)
if err != nil {
return nil, err
}