diff --git a/chain/address/address.go b/chain/address/address.go index 3c8ee20a5..fd5c28dff 100644 --- a/chain/address/address.go +++ b/chain/address/address.go @@ -337,7 +337,7 @@ func (a Address) MarshalCBOR(w io.Writer) error { return nil } -func (a *Address) UnmarshalCBOR(br cbg.ByteReader) error { +func (a *Address) UnmarshalCBOR(br io.Reader) error { maj, extra, err := cbg.CborReadHeader(br) if err != nil { return err diff --git a/chain/blocksync.go b/chain/blocksync.go index 1f697e2b2..87536b24f 100644 --- a/chain/blocksync.go +++ b/chain/blocksync.go @@ -65,7 +65,7 @@ const ( type BlockSyncResponse struct { Chain []*BSTipSet - Status uint + Status uint64 Message string } @@ -73,10 +73,10 @@ type BSTipSet struct { Blocks []*types.BlockHeader BlsMessages []*types.Message - BlsMsgIncludes [][]int + BlsMsgIncludes [][]uint64 SecpkMessages []*types.SignedMessage - SecpkMsgIncludes [][]int + SecpkMsgIncludes [][]uint64 } func NewBlockSyncService(cs *store.ChainStore) *BlockSyncService { @@ -166,12 +166,12 @@ func (bss *BlockSyncService) collectChainSegment(start []cid.Cid, length uint64, } } -func (bss *BlockSyncService) gatherMessages(ts *types.TipSet) ([]*types.Message, [][]int, []*types.SignedMessage, [][]int, error) { - blsmsgmap := make(map[cid.Cid]int) - secpkmsgmap := make(map[cid.Cid]int) +func (bss *BlockSyncService) gatherMessages(ts *types.TipSet) ([]*types.Message, [][]uint64, []*types.SignedMessage, [][]uint64, error) { + blsmsgmap := make(map[cid.Cid]uint64) + secpkmsgmap := make(map[cid.Cid]uint64) var secpkmsgs []*types.SignedMessage var blsmsgs []*types.Message - var secpkincl, blsincl [][]int + var secpkincl, blsincl [][]uint64 for _, b := range ts.Blocks() { bmsgs, smsgs, err := bss.cs.MessagesForBlock(b) @@ -179,11 +179,11 @@ func (bss *BlockSyncService) gatherMessages(ts *types.TipSet) ([]*types.Message, return nil, nil, nil, nil, err } - bmi := make([]int, 0, len(bmsgs)) + bmi := make([]uint64, 0, len(bmsgs)) for _, m := range bmsgs { i, ok := blsmsgmap[m.Cid()] if !ok { - i = len(blsmsgs) + i = uint64(len(blsmsgs)) blsmsgs = append(blsmsgs, m) blsmsgmap[m.Cid()] = i } @@ -192,11 +192,11 @@ func (bss *BlockSyncService) gatherMessages(ts *types.TipSet) ([]*types.Message, } blsincl = append(blsincl, bmi) - smi := make([]int, 0, len(smsgs)) + smi := make([]uint64, 0, len(smsgs)) for _, m := range smsgs { i, ok := secpkmsgmap[m.Cid()] if !ok { - i = len(secpkmsgs) + i = uint64(len(secpkmsgs)) secpkmsgs = append(secpkmsgs, m) secpkmsgmap[m.Cid()] = i } diff --git a/chain/cbor_gen.go b/chain/cbor_gen.go new file mode 100644 index 000000000..ae9ddc1e0 --- /dev/null +++ b/chain/cbor_gen.go @@ -0,0 +1,579 @@ +package chain + +import ( + "fmt" + "io" + + "github.com/filecoin-project/go-lotus/chain/types" + cid "github.com/ipfs/go-cid" + cbg "github.com/whyrusleeping/cbor-gen" + xerrors "golang.org/x/xerrors" +) + +/* This file was generated by github.com/whyrusleeping/cbor-gen */ + +var _ = xerrors.Errorf + +func (t *BlockSyncRequest) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{131}); err != nil { + return err + } + + // t.t.Start ([]cid.Cid) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.Start)))); err != nil { + return err + } + for _, v := range t.Start { + if err := cbg.WriteCid(w, v); err != nil { + return xerrors.Errorf("failed writing cid field t.Start: %w", err) + } + } + + // t.t.RequestLength (uint64) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.RequestLength)); err != nil { + return err + } + + // t.t.Options (uint64) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.Options)); err != nil { + return err + } + return nil +} + +func (t *BlockSyncRequest) UnmarshalCBOR(br io.Reader) error { + + maj, extra, err := cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajArray { + return fmt.Errorf("cbor input should be of type array") + } + + if extra != 3 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.t.Start ([]cid.Cid) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.Start = make([]cid.Cid, extra) + } + for i := 0; i < int(extra); i++ { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("reading cid field t.Start failed: %w", err) + } + t.Start[i] = c + } + + // t.t.RequestLength (uint64) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajUnsignedInt { + return fmt.Errorf("wrong type for uint64 field") + } + t.RequestLength = extra + // t.t.Options (uint64) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajUnsignedInt { + return fmt.Errorf("wrong type for uint64 field") + } + t.Options = extra + return nil +} + +func (t *BlockSyncResponse) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{131}); err != nil { + return err + } + + // t.t.Chain ([]*chain.BSTipSet) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.Chain)))); err != nil { + return err + } + for _, v := range t.Chain { + if err := v.MarshalCBOR(w); err != nil { + return err + } + } + + // t.t.Status (uint64) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.Status)); err != nil { + return err + } + + // t.t.Message (string) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len(t.Message)))); err != nil { + return err + } + if _, err := w.Write([]byte(t.Message)); err != nil { + return err + } + return nil +} + +func (t *BlockSyncResponse) UnmarshalCBOR(br io.Reader) error { + + maj, extra, err := cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajArray { + return fmt.Errorf("cbor input should be of type array") + } + + if extra != 3 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.t.Chain ([]*chain.BSTipSet) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.Chain = make([]*BSTipSet, extra) + } + for i := 0; i < int(extra); i++ { + var v BSTipSet + if err := v.UnmarshalCBOR(br); err != nil { + return err + } + + t.Chain[i] = &v + } + + // t.t.Status (uint64) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajUnsignedInt { + return fmt.Errorf("wrong type for uint64 field") + } + t.Status = extra + // t.t.Message (string) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + + if maj != cbg.MajTextString { + return fmt.Errorf("expected cbor type 'text string' in input") + } + + if extra > 256*1024 { + return fmt.Errorf("string in cbor input too long") + } + + { + buf := make([]byte, extra) + if _, err := io.ReadFull(br, buf); err != nil { + return err + } + + t.Message = string(buf) + } + return nil +} + +func (t *BSTipSet) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{133}); err != nil { + return err + } + + // t.t.Blocks ([]*types.BlockHeader) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.Blocks)))); err != nil { + return err + } + for _, v := range t.Blocks { + if err := v.MarshalCBOR(w); err != nil { + return err + } + } + + // t.t.BlsMessages ([]*types.Message) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.BlsMessages)))); err != nil { + return err + } + for _, v := range t.BlsMessages { + if err := v.MarshalCBOR(w); err != nil { + return err + } + } + + // t.t.BlsMsgIncludes ([][]uint64) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.BlsMsgIncludes)))); err != nil { + return err + } + for _, v := range t.BlsMsgIncludes { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(v)))); err != nil { + return err + } + for _, v := range v { + if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, v); err != nil { + return err + } + } + } + + // t.t.SecpkMessages ([]*types.SignedMessage) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.SecpkMessages)))); err != nil { + return err + } + for _, v := range t.SecpkMessages { + if err := v.MarshalCBOR(w); err != nil { + return err + } + } + + // t.t.SecpkMsgIncludes ([][]uint64) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.SecpkMsgIncludes)))); err != nil { + return err + } + for _, v := range t.SecpkMsgIncludes { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(v)))); err != nil { + return err + } + for _, v := range v { + if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, v); err != nil { + return err + } + } + } + return nil +} + +func (t *BSTipSet) UnmarshalCBOR(br io.Reader) error { + + maj, extra, err := cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajArray { + return fmt.Errorf("cbor input should be of type array") + } + + if extra != 5 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.t.Blocks ([]*types.BlockHeader) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.Blocks = make([]*types.BlockHeader, extra) + } + for i := 0; i < int(extra); i++ { + var v types.BlockHeader + if err := v.UnmarshalCBOR(br); err != nil { + return err + } + + t.Blocks[i] = &v + } + + // t.t.BlsMessages ([]*types.Message) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.BlsMessages = make([]*types.Message, extra) + } + for i := 0; i < int(extra); i++ { + var v types.Message + if err := v.UnmarshalCBOR(br); err != nil { + return err + } + + t.BlsMessages[i] = &v + } + + // t.t.BlsMsgIncludes ([][]uint64) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.BlsMsgIncludes = make([][]uint64, extra) + } + for i := 0; i < int(extra); i++ { + { + var maj byte + var extra uint64 + var err error + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.BlsMsgIncludes[i] = make([]uint64, extra) + } + for j := 0; j < int(extra); j++ { + + maj, val, err := cbg.CborReadHeader(br) + if err != nil { + return xerrors.Errorf("failed to read uint64 for t.BlsMsgIncludes[i] slice: %w", err) + } + + if maj != cbg.MajUnsignedInt { + return xerrors.Errorf("value read for array t.BlsMsgIncludes[i] was not a uint, instead got %d", maj) + } + + t.BlsMsgIncludes[i][j] = val + } + + } + } + + // t.t.SecpkMessages ([]*types.SignedMessage) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.SecpkMessages = make([]*types.SignedMessage, extra) + } + for i := 0; i < int(extra); i++ { + var v types.SignedMessage + if err := v.UnmarshalCBOR(br); err != nil { + return err + } + + t.SecpkMessages[i] = &v + } + + // t.t.SecpkMsgIncludes ([][]uint64) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.SecpkMsgIncludes = make([][]uint64, extra) + } + for i := 0; i < int(extra); i++ { + { + var maj byte + var extra uint64 + var err error + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.SecpkMsgIncludes[i] = make([]uint64, extra) + } + for j := 0; j < int(extra); j++ { + + maj, val, err := cbg.CborReadHeader(br) + if err != nil { + return xerrors.Errorf("failed to read uint64 for t.SecpkMsgIncludes[i] slice: %w", err) + } + + if maj != cbg.MajUnsignedInt { + return xerrors.Errorf("value read for array t.SecpkMsgIncludes[i] was not a uint, instead got %d", maj) + } + + t.SecpkMsgIncludes[i][j] = val + } + + } + } + + return nil +} + +func (t *BlockMsg) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{131}); err != nil { + return err + } + + // t.t.Header (types.BlockHeader) + if err := t.Header.MarshalCBOR(w); err != nil { + return err + } + + // t.t.BlsMessages ([]cid.Cid) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.BlsMessages)))); err != nil { + return err + } + for _, v := range t.BlsMessages { + if err := cbg.WriteCid(w, v); err != nil { + return xerrors.Errorf("failed writing cid field t.BlsMessages: %w", err) + } + } + + // t.t.SecpkMessages ([]cid.Cid) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.SecpkMessages)))); err != nil { + return err + } + for _, v := range t.SecpkMessages { + if err := cbg.WriteCid(w, v); err != nil { + return xerrors.Errorf("failed writing cid field t.SecpkMessages: %w", err) + } + } + return nil +} + +func (t *BlockMsg) UnmarshalCBOR(br io.Reader) error { + + maj, extra, err := cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajArray { + return fmt.Errorf("cbor input should be of type array") + } + + if extra != 3 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.t.Header (types.BlockHeader) + + t.Header = new(types.BlockHeader) + if err := t.Header.UnmarshalCBOR(br); err != nil { + return err + } + // t.t.BlsMessages ([]cid.Cid) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.BlsMessages = make([]cid.Cid, extra) + } + for i := 0; i < int(extra); i++ { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("reading cid field t.BlsMessages failed: %w", err) + } + t.BlsMessages[i] = c + } + + // t.t.SecpkMessages ([]cid.Cid) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") + } + if extra > 0 { + t.SecpkMessages = make([]cid.Cid, extra) + } + for i := 0; i < int(extra); i++ { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("reading cid field t.SecpkMessages failed: %w", err) + } + t.SecpkMessages[i] = c + } + + return nil +} diff --git a/chain/gen/utils.go b/chain/gen/utils.go index ff580b69b..e8ea48a69 100644 --- a/chain/gen/utils.go +++ b/chain/gen/utils.go @@ -270,14 +270,19 @@ func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.B emptyroot, err := sharray.Build(context.TODO(), 4, []interface{}{}, cst) if err != nil { - return nil, err + return nil, xerrors.Errorf("sharray build failed: %w", err) } - mmcid, err := cst.Put(context.TODO(), &types.MsgMeta{ + + mm := &types.MsgMeta{ BlsMessages: emptyroot, SecpkMessages: emptyroot, - }) + } + mmb, err := mm.ToStorageBlock() if err != nil { - return nil, err + return nil, xerrors.Errorf("serializing msgmeta failed: %w", err) + } + if err := bs.Put(mmb); err != nil { + return nil, xerrors.Errorf("putting msgmeta block to blockstore: %w", err) } fmt.Println("Empty Genesis root: ", emptyroot) @@ -296,18 +301,19 @@ func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.B Height: 0, ParentWeight: types.NewInt(0), StateRoot: stateroot, - Messages: mmcid, + Messages: mmb.Cid(), MessageReceipts: emptyroot, BLSAggregate: types.Signature{Type: types.KTBLS, Data: []byte("signatureeee")}, } + fmt.Println("MESSAGES CID: ", b.Messages) sb, err := b.ToStorageBlock() if err != nil { - return nil, err + return nil, xerrors.Errorf("serializing block header failed: %w", err) } if err := bs.Put(sb); err != nil { - return nil, err + return nil, xerrors.Errorf("putting header to blockstore: %w", err) } return &GenesisBootstrap{ diff --git a/chain/sync.go b/chain/sync.go index f2eeb24e7..f8d929a56 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -148,7 +148,7 @@ func copyBlockstore(from, to bstore.Blockstore) error { // either validate it here, or ensure that its validated elsewhere (maybe make // sure the blocksync code checks it?) // maybe this code should actually live in blocksync?? -func zipTipSetAndMessages(cst *hamt.CborIpldStore, ts *types.TipSet, allbmsgs []*types.Message, allsmsgs []*types.SignedMessage, bmi, smi [][]int) (*store.FullTipSet, error) { +func zipTipSetAndMessages(cst *hamt.CborIpldStore, ts *types.TipSet, allbmsgs []*types.Message, allsmsgs []*types.SignedMessage, bmi, smi [][]uint64) (*store.FullTipSet, error) { if len(ts.Blocks()) != len(smi) || len(ts.Blocks()) != len(bmi) { return nil, fmt.Errorf("msgincl length didnt match tipset size") } diff --git a/chain/sync_test.go b/chain/sync_test.go index 3d03b9f0c..479964db7 100644 --- a/chain/sync_test.go +++ b/chain/sync_test.go @@ -91,6 +91,8 @@ func (tu *syncTestUtil) mineNewBlock(src int) { require.NoError(tu.t, tu.nds[src].MpoolPush(context.TODO(), msg)) } + fmt.Printf("state root: %v\n", fblk.Header.StateRoot) + require.NoError(tu.t, tu.nds[src].ChainSubmitBlock(context.TODO(), fblkToBlkMsg(fblk))) } diff --git a/chain/types.go b/chain/types.go index b3340f0b8..953221aef 100644 --- a/chain/types.go +++ b/chain/types.go @@ -1,6 +1,8 @@ package chain import ( + "bytes" + "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" @@ -136,7 +138,7 @@ type BlockMsg struct { func DecodeBlockMsg(b []byte) (*BlockMsg, error) { var bm BlockMsg - if err := cbor.DecodeInto(b, &bm); err != nil { + if err := bm.UnmarshalCBOR(bytes.NewReader(b)); err != nil { return nil, err } @@ -148,5 +150,9 @@ func (bm *BlockMsg) Cid() cid.Cid { } func (bm *BlockMsg) Serialize() ([]byte, error) { - return cbor.DumpObject(bm) + buf := new(bytes.Buffer) + if err := bm.MarshalCBOR(buf); err != nil { + return nil, err + } + return buf.Bytes(), nil } diff --git a/chain/types/bigint.go b/chain/types/bigint.go index 93dd03914..0e1e18dee 100644 --- a/chain/types/bigint.go +++ b/chain/types/bigint.go @@ -101,6 +101,11 @@ func (bi *BigInt) UnmarshalJSON(b []byte) error { } func (bi *BigInt) MarshalCBOR(w io.Writer) error { + if bi.Int == nil { + zero := NewInt(0) + return zero.MarshalCBOR(w) + } + if bi.Sign() < 0 { // right now we don't support negative integers. // In the spec, everything is listed as a Uint. @@ -126,7 +131,7 @@ func (bi *BigInt) MarshalCBOR(w io.Writer) error { return nil } -func (bi *BigInt) UnmarshalCBOR(br cbg.ByteReader) error { +func (bi *BigInt) UnmarshalCBOR(br io.Reader) error { maj, extra, err := cbg.CborReadHeader(br) if err != nil { return err diff --git a/chain/types/blockheader.go b/chain/types/blockheader.go index bf89a9572..b4d369498 100644 --- a/chain/types/blockheader.go +++ b/chain/types/blockheader.go @@ -6,6 +6,7 @@ import ( block "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" "github.com/multiformats/go-multihash" + xerrors "golang.org/x/xerrors" "github.com/filecoin-project/go-lotus/chain/address" ) @@ -40,11 +41,6 @@ type BlockHeader struct { MessageReceipts cid.Cid } -type MsgMeta struct { - BlsMessages cid.Cid - SecpkMessages cid.Cid -} - func (b *BlockHeader) ToStorageBlock() (block.Block, error) { data, err := b.Serialize() if err != nil { @@ -87,6 +83,34 @@ func (blk *BlockHeader) Serialize() ([]byte, error) { return buf.Bytes(), nil } +type MsgMeta struct { + BlsMessages cid.Cid + SecpkMessages cid.Cid +} + +func (mm *MsgMeta) Cid() cid.Cid { + b, err := mm.ToStorageBlock() + if err != nil { + panic(err) + } + return b.Cid() +} + +func (mm *MsgMeta) ToStorageBlock() (block.Block, error) { + buf := new(bytes.Buffer) + if err := mm.MarshalCBOR(buf); err != nil { + return nil, xerrors.Errorf("failed to marshal MsgMeta: %w", err) + } + + pref := cid.NewPrefixV1(cid.DagCBOR, multihash.BLAKE2B_MIN+31) + c, err := pref.Sum(buf.Bytes()) + if err != nil { + return nil, err + } + + return block.NewBlockWithCid(buf.Bytes(), c) +} + /* func (blk *BlockHeader) MarshalCBOR(w io.Writer) error { panic("no") @@ -111,4 +135,13 @@ func (blk *SignedMessage) MarshalCBOR(w io.Writer) error { func (blk *SignedMessage) UnmarshalCBOR(r io.Reader) error { panic("no") } -*/ + +func (blk *MsgMeta) MarshalCBOR(w io.Writer) error { + panic("no") +} + +func (blk *MsgMeta) UnmarshalCBOR(r io.Reader) error { + panic("no") +} + +//*/ diff --git a/chain/types/blockheader_test.go b/chain/types/blockheader_test.go index 3c4f680ae..e82bd1616 100644 --- a/chain/types/blockheader_test.go +++ b/chain/types/blockheader_test.go @@ -7,7 +7,6 @@ import ( "github.com/filecoin-project/go-lotus/chain/address" cid "github.com/ipfs/go-cid" - cbor "github.com/ipfs/go-ipld-cbor" ) func testBlockHeader(t testing.TB) *BlockHeader { @@ -74,15 +73,3 @@ func BenchmarkBlockHeaderMarshal(b *testing.B) { } } } - -func BenchmarkBlockHeaderMarshalOld(b *testing.B) { - bh := testBlockHeader(b) - - b.ReportAllocs() - - for i := 0; i < b.N; i++ { - if _, err := cbor.DumpObject(bh); err != nil { - b.Fatal(err) - } - } -} diff --git a/chain/types/cbor_gen.go b/chain/types/cbor_gen.go index 55ec1a032..c77ddd923 100644 --- a/chain/types/cbor_gen.go +++ b/chain/types/cbor_gen.go @@ -6,22 +6,24 @@ import ( cid "github.com/ipfs/go-cid" cbg "github.com/whyrusleeping/cbor-gen" + xerrors "golang.org/x/xerrors" ) /* This file was generated by github.com/whyrusleeping/cbor-gen */ +var _ = xerrors.Errorf + func (t *BlockHeader) MarshalCBOR(w io.Writer) error { if _, err := w.Write([]byte{138}); err != nil { return err } - // t.Miner (address.Address) - + // t.t.Miner (address.Address) if err := t.Miner.MarshalCBOR(w); err != nil { return err } - // t.Tickets ([]*types.Ticket) + // t.t.Tickets ([]*types.Ticket) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.Tickets)))); err != nil { return err } @@ -31,7 +33,7 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error { } } - // t.ElectionProof ([]uint8) + // t.t.ElectionProof ([]uint8) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.ElectionProof)))); err != nil { return err } @@ -39,51 +41,49 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error { return err } - // t.Parents ([]cid.Cid) + // t.t.Parents ([]cid.Cid) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.Parents)))); err != nil { return err } for _, v := range t.Parents { if err := cbg.WriteCid(w, v); err != nil { - return err + return xerrors.Errorf("failed writing cid field t.Parents: %w", err) } } - // t.ParentWeight (types.BigInt) - + // t.t.ParentWeight (types.BigInt) if err := t.ParentWeight.MarshalCBOR(w); err != nil { return err } - // t.Height (uint64) + // t.t.Height (uint64) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.Height)); err != nil { return err } - // t.StateRoot (cid.Cid) + // t.t.StateRoot (cid.Cid) if err := cbg.WriteCid(w, t.StateRoot); err != nil { - return err + return xerrors.Errorf("failed to write cid field t.StateRoot: %w", err) } - // t.Messages (cid.Cid) + // t.t.Messages (cid.Cid) if err := cbg.WriteCid(w, t.Messages); err != nil { - return err + return xerrors.Errorf("failed to write cid field t.Messages: %w", err) } - // t.BLSAggregate (types.Signature) - + // t.t.BLSAggregate (types.Signature) if err := t.BLSAggregate.MarshalCBOR(w); err != nil { return err } - // t.MessageReceipts (cid.Cid) + // t.t.MessageReceipts (cid.Cid) if err := cbg.WriteCid(w, t.MessageReceipts); err != nil { - return err + return xerrors.Errorf("failed to write cid field t.MessageReceipts: %w", err) } return nil } -func (t *BlockHeader) UnmarshalCBOR(br cbg.ByteReader) error { +func (t *BlockHeader) UnmarshalCBOR(br io.Reader) error { maj, extra, err := cbg.CborReadHeader(br) if err != nil { @@ -97,12 +97,12 @@ func (t *BlockHeader) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("cbor input had wrong number of fields") } - // t.Miner (address.Address) + // t.t.Miner (address.Address) if err := t.Miner.UnmarshalCBOR(br); err != nil { return err } - // t.Tickets ([]*types.Ticket) + // t.t.Tickets ([]*types.Ticket) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -116,7 +116,7 @@ func (t *BlockHeader) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("expected cbor array") } if extra > 0 { - t.Tickets = make([]*Ticket, 0, extra) + t.Tickets = make([]*Ticket, extra) } for i := 0; i < int(extra); i++ { var v Ticket @@ -124,10 +124,10 @@ func (t *BlockHeader) UnmarshalCBOR(br cbg.ByteReader) error { return err } - t.Tickets = append(t.Tickets, &v) + t.Tickets[i] = &v } - // t.ElectionProof ([]uint8) + // t.t.ElectionProof ([]uint8) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -144,7 +144,7 @@ func (t *BlockHeader) UnmarshalCBOR(br cbg.ByteReader) error { if _, err := io.ReadFull(br, t.ElectionProof); err != nil { return err } - // t.Parents ([]cid.Cid) + // t.t.Parents ([]cid.Cid) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -158,23 +158,23 @@ func (t *BlockHeader) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("expected cbor array") } if extra > 0 { - t.Parents = make([]cid.Cid, 0, extra) + t.Parents = make([]cid.Cid, extra) } for i := 0; i < int(extra); i++ { c, err := cbg.ReadCid(br) if err != nil { - return err + return xerrors.Errorf("reading cid field t.Parents failed: %w", err) } - t.Parents = append(t.Parents, c) + t.Parents[i] = c } - // t.ParentWeight (types.BigInt) + // t.t.ParentWeight (types.BigInt) if err := t.ParentWeight.UnmarshalCBOR(br); err != nil { return err } - // t.Height (uint64) + // t.t.Height (uint64) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -184,35 +184,35 @@ func (t *BlockHeader) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("wrong type for uint64 field") } t.Height = extra - // t.StateRoot (cid.Cid) + // t.t.StateRoot (cid.Cid) { c, err := cbg.ReadCid(br) if err != nil { - return err + return xerrors.Errorf("failed to read cid field t.StateRoot: %w", err) } t.StateRoot = c } - // t.Messages (cid.Cid) + // t.t.Messages (cid.Cid) { c, err := cbg.ReadCid(br) if err != nil { - return err + return xerrors.Errorf("failed to read cid field t.Messages: %w", err) } t.Messages = c } - // t.BLSAggregate (types.Signature) + // t.t.BLSAggregate (types.Signature) if err := t.BLSAggregate.UnmarshalCBOR(br); err != nil { return err } - // t.MessageReceipts (cid.Cid) + // t.t.MessageReceipts (cid.Cid) { c, err := cbg.ReadCid(br) if err != nil { - return err + return xerrors.Errorf("failed to read cid field t.MessageReceipts: %w", err) } t.MessageReceipts = c } @@ -224,7 +224,7 @@ func (t *Ticket) MarshalCBOR(w io.Writer) error { return err } - // t.VRFProof ([]uint8) + // t.t.VRFProof ([]uint8) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.VRFProof)))); err != nil { return err } @@ -232,7 +232,7 @@ func (t *Ticket) MarshalCBOR(w io.Writer) error { return err } - // t.VDFResult ([]uint8) + // t.t.VDFResult ([]uint8) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.VDFResult)))); err != nil { return err } @@ -240,7 +240,7 @@ func (t *Ticket) MarshalCBOR(w io.Writer) error { return err } - // t.VDFProof ([]uint8) + // t.t.VDFProof ([]uint8) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.VDFProof)))); err != nil { return err } @@ -250,7 +250,7 @@ func (t *Ticket) MarshalCBOR(w io.Writer) error { return nil } -func (t *Ticket) UnmarshalCBOR(br cbg.ByteReader) error { +func (t *Ticket) UnmarshalCBOR(br io.Reader) error { maj, extra, err := cbg.CborReadHeader(br) if err != nil { @@ -264,7 +264,7 @@ func (t *Ticket) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("cbor input had wrong number of fields") } - // t.VRFProof ([]uint8) + // t.t.VRFProof ([]uint8) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -281,7 +281,7 @@ func (t *Ticket) UnmarshalCBOR(br cbg.ByteReader) error { if _, err := io.ReadFull(br, t.VRFProof); err != nil { return err } - // t.VDFResult ([]uint8) + // t.t.VDFResult ([]uint8) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -298,7 +298,7 @@ func (t *Ticket) UnmarshalCBOR(br cbg.ByteReader) error { if _, err := io.ReadFull(br, t.VDFResult); err != nil { return err } - // t.VDFProof ([]uint8) + // t.t.VDFProof ([]uint8) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -323,47 +323,42 @@ func (t *Message) MarshalCBOR(w io.Writer) error { return err } - // t.To (address.Address) - + // t.t.To (address.Address) if err := t.To.MarshalCBOR(w); err != nil { return err } - // t.From (address.Address) - + // t.t.From (address.Address) if err := t.From.MarshalCBOR(w); err != nil { return err } - // t.Nonce (uint64) + // t.t.Nonce (uint64) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.Nonce)); err != nil { return err } - // t.Value (types.BigInt) - + // t.t.Value (types.BigInt) if err := t.Value.MarshalCBOR(w); err != nil { return err } - // t.GasPrice (types.BigInt) - + // t.t.GasPrice (types.BigInt) if err := t.GasPrice.MarshalCBOR(w); err != nil { return err } - // t.GasLimit (types.BigInt) - + // t.t.GasLimit (types.BigInt) if err := t.GasLimit.MarshalCBOR(w); err != nil { return err } - // t.Method (uint64) + // t.t.Method (uint64) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.Method)); err != nil { return err } - // t.Params ([]uint8) + // t.t.Params ([]uint8) if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.Params)))); err != nil { return err } @@ -373,7 +368,7 @@ func (t *Message) MarshalCBOR(w io.Writer) error { return nil } -func (t *Message) UnmarshalCBOR(br cbg.ByteReader) error { +func (t *Message) UnmarshalCBOR(br io.Reader) error { maj, extra, err := cbg.CborReadHeader(br) if err != nil { @@ -387,17 +382,17 @@ func (t *Message) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("cbor input had wrong number of fields") } - // t.To (address.Address) + // t.t.To (address.Address) if err := t.To.UnmarshalCBOR(br); err != nil { return err } - // t.From (address.Address) + // t.t.From (address.Address) if err := t.From.UnmarshalCBOR(br); err != nil { return err } - // t.Nonce (uint64) + // t.t.Nonce (uint64) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -407,22 +402,22 @@ func (t *Message) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("wrong type for uint64 field") } t.Nonce = extra - // t.Value (types.BigInt) + // t.t.Value (types.BigInt) if err := t.Value.UnmarshalCBOR(br); err != nil { return err } - // t.GasPrice (types.BigInt) + // t.t.GasPrice (types.BigInt) if err := t.GasPrice.UnmarshalCBOR(br); err != nil { return err } - // t.GasLimit (types.BigInt) + // t.t.GasLimit (types.BigInt) if err := t.GasLimit.UnmarshalCBOR(br); err != nil { return err } - // t.Method (uint64) + // t.t.Method (uint64) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -432,7 +427,7 @@ func (t *Message) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("wrong type for uint64 field") } t.Method = extra - // t.Params ([]uint8) + // t.t.Params ([]uint8) maj, extra, err = cbg.CborReadHeader(br) if err != nil { @@ -457,21 +452,19 @@ func (t *SignedMessage) MarshalCBOR(w io.Writer) error { return err } - // t.Message (types.Message) - + // t.t.Message (types.Message) if err := t.Message.MarshalCBOR(w); err != nil { return err } - // t.Signature (types.Signature) - + // t.t.Signature (types.Signature) if err := t.Signature.MarshalCBOR(w); err != nil { return err } return nil } -func (t *SignedMessage) UnmarshalCBOR(br cbg.ByteReader) error { +func (t *SignedMessage) UnmarshalCBOR(br io.Reader) error { maj, extra, err := cbg.CborReadHeader(br) if err != nil { @@ -485,12 +478,12 @@ func (t *SignedMessage) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("cbor input had wrong number of fields") } - // t.Message (types.Message) + // t.t.Message (types.Message) if err := t.Message.UnmarshalCBOR(br); err != nil { return err } - // t.Signature (types.Signature) + // t.t.Signature (types.Signature) if err := t.Signature.UnmarshalCBOR(br); err != nil { return err @@ -503,19 +496,19 @@ func (t *MsgMeta) MarshalCBOR(w io.Writer) error { return err } - // t.BlsMessages (cid.Cid) + // t.t.BlsMessages (cid.Cid) if err := cbg.WriteCid(w, t.BlsMessages); err != nil { - return err + return xerrors.Errorf("failed to write cid field t.BlsMessages: %w", err) } - // t.SecpkMessages (cid.Cid) + // t.t.SecpkMessages (cid.Cid) if err := cbg.WriteCid(w, t.SecpkMessages); err != nil { - return err + return xerrors.Errorf("failed to write cid field t.SecpkMessages: %w", err) } return nil } -func (t *MsgMeta) UnmarshalCBOR(br cbg.ByteReader) error { +func (t *MsgMeta) UnmarshalCBOR(br io.Reader) error { maj, extra, err := cbg.CborReadHeader(br) if err != nil { @@ -529,21 +522,21 @@ func (t *MsgMeta) UnmarshalCBOR(br cbg.ByteReader) error { return fmt.Errorf("cbor input had wrong number of fields") } - // t.BlsMessages (cid.Cid) + // t.t.BlsMessages (cid.Cid) { c, err := cbg.ReadCid(br) if err != nil { - return err + return xerrors.Errorf("failed to read cid field t.BlsMessages: %w", err) } t.BlsMessages = c } - // t.SecpkMessages (cid.Cid) + // t.t.SecpkMessages (cid.Cid) { c, err := cbg.ReadCid(br) if err != nil { - return err + return xerrors.Errorf("failed to read cid field t.SecpkMessages: %w", err) } t.SecpkMessages = c } diff --git a/chain/types/signature.go b/chain/types/signature.go index 306e6475a..70154e51d 100644 --- a/chain/types/signature.go +++ b/chain/types/signature.go @@ -135,7 +135,7 @@ func (s *Signature) MarshalCBOR(w io.Writer) error { return nil } -func (s *Signature) UnmarshalCBOR(br cbg.ByteReader) error { +func (s *Signature) UnmarshalCBOR(br io.Reader) error { maj, l, err := cbg.CborReadHeader(br) if err != nil { return err diff --git a/gen/main.go b/gen/main.go index bb225f4d8..4eb8d9d3f 100644 --- a/gen/main.go +++ b/gen/main.go @@ -4,35 +4,66 @@ import ( "fmt" "os" + "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/types" gen "github.com/whyrusleeping/cbor-gen" ) func main() { - fi, err := os.Create("./chain/types/cbor_gen.go") - if err != nil { - fmt.Println("failed to open file: ", err) - os.Exit(1) - } - defer fi.Close() - - if err := gen.PrintHeaderAndUtilityMethods(fi, "types"); err != nil { - fmt.Println("failed to write header: ", err) - os.Exit(1) - } - - types := []interface{}{ - types.BlockHeader{}, - types.Ticket{}, - types.Message{}, - types.SignedMessage{}, - types.MsgMeta{}, - } - - for _, t := range types { - if err := gen.GenTupleEncodersForType(t, fi); err != nil { - fmt.Println("failed to generate encoders: ", err) + { + fi, err := os.Create("./chain/types/cbor_gen.go") + if err != nil { + fmt.Println("failed to open file: ", err) os.Exit(1) } + defer fi.Close() + + if err := gen.PrintHeaderAndUtilityMethods(fi, "types"); err != nil { + fmt.Println("failed to write header: ", err) + os.Exit(1) + } + + types := []interface{}{ + types.BlockHeader{}, + types.Ticket{}, + types.Message{}, + types.SignedMessage{}, + types.MsgMeta{}, + } + + for _, t := range types { + if err := gen.GenTupleEncodersForType(t, fi); err != nil { + fmt.Println("failed to generate encoders: ", err) + os.Exit(1) + } + } + } + + { + fi, err := os.Create("./chain/cbor_gen.go") + if err != nil { + fmt.Println("failed to open file: ", err) + os.Exit(1) + } + defer fi.Close() + + if err := gen.PrintHeaderAndUtilityMethods(fi, "chain"); err != nil { + fmt.Println("failed to write header: ", err) + os.Exit(1) + } + + types := []interface{}{ + chain.BlockSyncRequest{}, + chain.BlockSyncResponse{}, + chain.BSTipSet{}, + chain.BlockMsg{}, + } + + for _, t := range types { + if err := gen.GenTupleEncodersForType(t, fi); err != nil { + fmt.Println("failed to generate encoders: ", err) + os.Exit(1) + } + } } } diff --git a/go.mod b/go.mod index 4505cfe92..0ef4bd42c 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/ipfs/go-ds-badger v0.0.5 github.com/ipfs/go-filestore v0.0.2 github.com/ipfs/go-fs-lock v0.0.1 - github.com/ipfs/go-hamt-ipld v0.0.11 + github.com/ipfs/go-hamt-ipld v0.0.12-0.20190822003241-7ff276389cbf github.com/ipfs/go-ipfs-blockstore v0.0.1 github.com/ipfs/go-ipfs-chunker v0.0.1 github.com/ipfs/go-ipfs-exchange-interface v0.0.1 @@ -66,7 +66,7 @@ require ( github.com/smartystreets/assertions v1.0.1 // indirect github.com/smartystreets/goconvey v0.0.0-20190710185942-9d28bd7c0945 // indirect github.com/stretchr/testify v1.3.0 - github.com/whyrusleeping/cbor-gen v0.0.0-20190821171244-dffbd9058edd + github.com/whyrusleeping/cbor-gen v0.0.0-20190822012446-bb2210dd2804 github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 github.com/whyrusleeping/pubsub v0.0.0-20131020042734-02de8aa2db3d github.com/whyrusleeping/sharray v0.0.0-20190718051354-e41931821e33 diff --git a/go.sum b/go.sum index f1db857ff..48e5c0003 100644 --- a/go.sum +++ b/go.sum @@ -209,6 +209,10 @@ github.com/ipfs/go-fs-lock v0.0.1/go.mod h1:DNBekbboPKcxs1aukPSaOtFA3QfSdi5C855v github.com/ipfs/go-hamt-ipld v0.0.1/go.mod h1:WrX60HHX2SeMb602Z1s9Ztnf/4fzNHzwH9gxNTVpEmk= github.com/ipfs/go-hamt-ipld v0.0.11 h1:iUHlbycdlheWf7QLU3FjHonK2lEnd+/85SeM5gvcUZE= github.com/ipfs/go-hamt-ipld v0.0.11/go.mod h1:WrX60HHX2SeMb602Z1s9Ztnf/4fzNHzwH9gxNTVpEmk= +github.com/ipfs/go-hamt-ipld v0.0.12-0.20190821195640-795e4447673a h1:yK+o0FBJa29uMNHl5PWyChB6JTNmIJNg42kF1+FeyJ8= +github.com/ipfs/go-hamt-ipld v0.0.12-0.20190821195640-795e4447673a/go.mod h1:zP3UV7pioj8zOYREVPDSVYvhGQjPm0ybQZzoh5BVwME= +github.com/ipfs/go-hamt-ipld v0.0.12-0.20190822003241-7ff276389cbf h1:P9Kkd8YCG4gCvfi8O839HHK2c+p5sdtyXMHcc1rjp2M= +github.com/ipfs/go-hamt-ipld v0.0.12-0.20190822003241-7ff276389cbf/go.mod h1:gaK14QN1GOlYGgq+o+t5+WTExZZogkMt0k0IIBNjXsM= github.com/ipfs/go-ipfs-blockstore v0.0.1 h1:O9n3PbmTYZoNhkgkEyrXTznbmktIXif62xLX+8dPHzc= github.com/ipfs/go-ipfs-blockstore v0.0.1/go.mod h1:d3WClOmRQKFnJ0Jz/jj/zmksX0ma1gROTlovZKBmN08= github.com/ipfs/go-ipfs-blocksutil v0.0.1 h1:Eh/H4pc1hsvhzsQoMEP3Bke/aW5P5rVM1IWFJMcGIPQ= @@ -634,6 +638,20 @@ github.com/whyrusleeping/cbor-gen v0.0.0-20190821011005-d6302bef16d9 h1:4X7YSN2+ github.com/whyrusleeping/cbor-gen v0.0.0-20190821011005-d6302bef16d9/go.mod h1:38LwjsrZy8ga8AbyOueCsLUAtdc1BDj/I929R+LnJfI= github.com/whyrusleeping/cbor-gen v0.0.0-20190821171244-dffbd9058edd h1:/+iPDLukqBcSkxCg5gANy2sv0weWQmLAsdeV669XttU= github.com/whyrusleeping/cbor-gen v0.0.0-20190821171244-dffbd9058edd/go.mod h1:38LwjsrZy8ga8AbyOueCsLUAtdc1BDj/I929R+LnJfI= +github.com/whyrusleeping/cbor-gen v0.0.0-20190821210025-1debc4e58b33 h1:nZ8z0NgNK8jTvIJ/yfZeKO9SpqHkWeZ6pO4yzx0WNVE= +github.com/whyrusleeping/cbor-gen v0.0.0-20190821210025-1debc4e58b33/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY= +github.com/whyrusleeping/cbor-gen v0.0.0-20190821212642-ed163f2ae2ce h1:dQYEkliyMDTlAD8rdoZug9iax/C9P+6CIssimrx/Mao= +github.com/whyrusleeping/cbor-gen v0.0.0-20190821212642-ed163f2ae2ce/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY= +github.com/whyrusleeping/cbor-gen v0.0.0-20190821221733-3af823a2ae84 h1:/RNpME6ZnMG+CTdT6oysD4ktyLZgADWGSIfU7wp8IWg= +github.com/whyrusleeping/cbor-gen v0.0.0-20190821221733-3af823a2ae84/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY= +github.com/whyrusleeping/cbor-gen v0.0.0-20190822001309-1bb175e007ba h1:xSh2X08mYhzcr8RSM6IjTad3++3pT9/3VRPBlEGybXg= +github.com/whyrusleeping/cbor-gen v0.0.0-20190822001309-1bb175e007ba/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY= +github.com/whyrusleeping/cbor-gen v0.0.0-20190822002002-a1b8389c520b h1:vxOSthYp6bL/XtGVhJvDEkZN204Q0er8+aeTzOM64gQ= +github.com/whyrusleeping/cbor-gen v0.0.0-20190822002002-a1b8389c520b/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY= +github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1 h1:wDIXmhgPVpV1gWj68IIj1zQNyp7QzBvr5d5UOvMqRNw= +github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY= +github.com/whyrusleeping/cbor-gen v0.0.0-20190822012446-bb2210dd2804 h1:wK83hcnZgKf1AyH8804pc4zVZOB2ND+d2cagsuhQmZc= +github.com/whyrusleeping/cbor-gen v0.0.0-20190822012446-bb2210dd2804/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY= github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f h1:jQa4QT2UP9WYv2nzyawpKMOCl+Z/jW7djv2/J50lj9E= github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f/go.mod h1:p9UJB6dDgdPgMJZs7UjUOdulKyRr9fqkS+6JKAInPy8= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= diff --git a/lib/cborrpc/rpc.go b/lib/cborrpc/rpc.go index f55776a1f..db3553212 100644 --- a/lib/cborrpc/rpc.go +++ b/lib/cborrpc/rpc.go @@ -4,11 +4,15 @@ import ( "io" cbor "github.com/ipfs/go-ipld-cbor" + cbg "github.com/whyrusleeping/cbor-gen" ) const MessageSizeLimit = 1 << 20 func WriteCborRPC(w io.Writer, obj interface{}) error { + if m, ok := obj.(cbg.CBORMarshaler); ok { + return m.MarshalCBOR(w) + } data, err := cbor.DumpObject(obj) if err != nil { return err @@ -19,5 +23,8 @@ func WriteCborRPC(w io.Writer, obj interface{}) error { } func ReadCborRPC(r io.Reader, out interface{}) error { + if um, ok := out.(cbg.CBORUnmarshaler); ok { + return um.UnmarshalCBOR(r) + } return cbor.DecodeReader(r, out) } diff --git a/node/impl/full/chain.go b/node/impl/full/chain.go index 28fb01b62..bb96c2da1 100644 --- a/node/impl/full/chain.go +++ b/node/impl/full/chain.go @@ -12,6 +12,7 @@ import ( "github.com/filecoin-project/go-lotus/chain/types" "github.com/filecoin-project/go-lotus/chain/vm" "github.com/filecoin-project/go-lotus/lib/bufbstore" + "golang.org/x/xerrors" "github.com/ipfs/go-cid" "github.com/ipfs/go-hamt-ipld" @@ -32,12 +33,12 @@ func (a *ChainAPI) ChainNotify(ctx context.Context) (<-chan *store.HeadChange, e func (a *ChainAPI) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error { if err := a.Chain.AddBlock(blk.Header); err != nil { - return err + return xerrors.Errorf("AddBlock failed: %w", err) } b, err := blk.Serialize() if err != nil { - return err + return xerrors.Errorf("serializing block for pubsub publishing failed: %w", err) } // TODO: anything else to do here?