From 53be71fb73a1233d68b186617f3c8338d662aac6 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Wed, 21 Aug 2019 10:15:28 -0700 Subject: [PATCH] Start using codegen --- Makefile | 3 + chain/gen/utils.go | 1 + chain/types.go | 29 +- chain/types/blockheader.go | 127 ++------ chain/types/blockheader_test.go | 88 +++++ chain/types/cbor_gen.go | 551 ++++++++++++++++++++++++++++++++ chain/types/message.go | 73 +---- chain/types/signedmessage.go | 43 +-- gen/main.go | 38 +++ go.mod | 4 +- go.sum | 12 + 11 files changed, 758 insertions(+), 211 deletions(-) create mode 100644 chain/types/blockheader_test.go create mode 100644 chain/types/cbor_gen.go create mode 100644 gen/main.go diff --git a/Makefile b/Makefile index 62f27ac94..f26794f9b 100644 --- a/Makefile +++ b/Makefile @@ -68,6 +68,9 @@ dist-clean: git submodule deinit --all -f .PHONY: dist-clean +type-gen: + rm -f ./chain/types/cbor_gen.go + go run ./gen/main.go print-%: @echo $*=$($*) diff --git a/chain/gen/utils.go b/chain/gen/utils.go index f3a7b4565..ff580b69b 100644 --- a/chain/gen/utils.go +++ b/chain/gen/utils.go @@ -298,6 +298,7 @@ func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.B StateRoot: stateroot, Messages: mmcid, MessageReceipts: emptyroot, + BLSAggregate: types.Signature{Type: types.KTBLS, Data: []byte("signatureeee")}, } sb, err := b.ToStorageBlock() diff --git a/chain/types.go b/chain/types.go index e9c1da3d7..b3340f0b8 100644 --- a/chain/types.go +++ b/chain/types.go @@ -1,22 +1,19 @@ package chain import ( - "fmt" - - block "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" - ipld "github.com/ipfs/go-ipld-format" "github.com/filecoin-project/go-lotus/chain/types" ) func init() { - ipld.Register(0x1f, IpldDecode) + //ipld.Register(0x1f, IpldDecode) cbor.RegisterCborType(BlockMsg{}) } +/* func IpldDecode(block block.Block) (ipld.Node, error) { var i interface{} if err := cbor.DecodeInto(block.RawData(), &i); err != nil { @@ -38,6 +35,8 @@ func (f *filecoinIpldNode) Cid() cid.Cid { case types.SignedMessage: return t.Cid() default: + fmt.Printf("bad type: %T\n", f.val) + fmt.Printf("what even is this: %#v\n", f.val) panic("whats going on") } } @@ -70,16 +69,15 @@ func (f *filecoinIpldNode) Links() []*ipld.Link { } func (f *filecoinIpldNode) Resolve(path []string) (interface{}, []string, error) { - /* - switch t := f.val.(type) { - case Block: - switch path[0] { - } - case Message: - default: - panic("whats going on") - } - */ + // + //switch t := f.val.(type) { + //case Block: + //switch path[0] { + //} + //case Message: + //default: + //panic("whats going on") + //} panic("please dont call this") } @@ -128,6 +126,7 @@ func (f *filecoinIpldNode) RawData() []byte { func (f *filecoinIpldNode) String() string { return "cats" } +*/ type BlockMsg struct { Header *types.BlockHeader diff --git a/chain/types/blockheader.go b/chain/types/blockheader.go index c491d3c0c..bf89a9572 100644 --- a/chain/types/blockheader.go +++ b/chain/types/blockheader.go @@ -1,103 +1,15 @@ package types import ( - "fmt" + "bytes" block "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" - cbor "github.com/ipfs/go-ipld-cbor" "github.com/multiformats/go-multihash" - "github.com/polydawn/refmt/obj/atlas" "github.com/filecoin-project/go-lotus/chain/address" ) -func init() { - cbor.RegisterCborType(atlas.BuildEntry(BlockHeader{}).UseTag(43).Transform(). - TransformMarshal(atlas.MakeMarshalTransformFunc( - func(blk BlockHeader) ([]interface{}, error) { - if blk.Tickets == nil { - blk.Tickets = []*Ticket{} - } - if blk.Parents == nil { - blk.Parents = []cid.Cid{} - } - - var tickarrs [][][]byte // oh boy - for _, t := range blk.Tickets { - tickarrs = append(tickarrs, [][]byte{ - t.VRFProof, t.VDFResult, t.VDFProof, - }) - } - - return []interface{}{ - blk.Miner.Bytes(), - tickarrs, - blk.ElectionProof, - blk.Parents, - blk.ParentWeight, - blk.Height, - blk.StateRoot, - blk.Messages, - blk.MessageReceipts, - }, nil - })). - TransformUnmarshal(atlas.MakeUnmarshalTransformFunc( - func(arr []interface{}) (BlockHeader, error) { - miner, err := address.NewFromBytes(arr[0].([]byte)) - if err != nil { - return BlockHeader{}, err - } - - tickets := []*Ticket{} - ticketarr, _ := arr[1].([]interface{}) - for _, t := range ticketarr { - ticklist, ok := t.([]interface{}) - if !ok { - return BlockHeader{}, fmt.Errorf("tickets were incorrectly formatted (type = %T)", t) - } - - if len(ticklist) != 3 { - return BlockHeader{}, fmt.Errorf("ticket should be a three item array of Byte arrays (got len = %d)", len(ticklist)) - } - - tickets = append(tickets, &Ticket{ - VRFProof: ticklist[0].([]byte), - VDFResult: ticklist[1].([]byte), - VDFProof: ticklist[2].([]byte), - }) - } - electionProof, _ := arr[2].([]byte) - - parents := []cid.Cid{} - parentsArr, _ := arr[3].([]interface{}) - for _, p := range parentsArr { - parents = append(parents, p.(cid.Cid)) - } - parentWeight := arr[4].(BigInt) - height := arr[5].(uint64) - stateRoot := arr[6].(cid.Cid) - - msgscid := arr[7].(cid.Cid) - recscid := arr[8].(cid.Cid) - - return BlockHeader{ - Miner: miner, - Tickets: tickets, - ElectionProof: electionProof, - Parents: parents, - ParentWeight: parentWeight, - Height: height, - StateRoot: stateRoot, - Messages: msgscid, - MessageReceipts: recscid, - }, nil - })). - Complete()) - cbor.RegisterCborType(MsgMeta{}) - cbor.RegisterCborType(Ticket{}) -} - type Ticket struct { VRFProof []byte VDFResult []byte @@ -139,7 +51,7 @@ func (b *BlockHeader) ToStorageBlock() (block.Block, error) { return nil, err } - pref := cid.NewPrefixV1(0x1f, multihash.BLAKE2B_MIN+31) + pref := cid.NewPrefixV1(cid.DagCBOR, multihash.BLAKE2B_MIN+31) c, err := pref.Sum(data) if err != nil { return nil, err @@ -159,7 +71,7 @@ func (b *BlockHeader) Cid() cid.Cid { func DecodeBlock(b []byte) (*BlockHeader, error) { var blk BlockHeader - if err := cbor.DecodeInto(b, &blk); err != nil { + if err := blk.UnmarshalCBOR(bytes.NewReader(b)); err != nil { return nil, err } @@ -167,5 +79,36 @@ func DecodeBlock(b []byte) (*BlockHeader, error) { } func (blk *BlockHeader) Serialize() ([]byte, error) { - return cbor.DumpObject(blk) + buf := new(bytes.Buffer) + if err := blk.MarshalCBOR(buf); err != nil { + return nil, err + } + + return buf.Bytes(), nil } + +/* +func (blk *BlockHeader) MarshalCBOR(w io.Writer) error { + panic("no") +} + +func (blk *BlockHeader) UnmarshalCBOR(r io.Reader) error { + panic("no") +} + +func (blk *Message) MarshalCBOR(w io.Writer) error { + panic("no") +} + +func (blk *Message) UnmarshalCBOR(r io.Reader) error { + panic("no") +} + +func (blk *SignedMessage) MarshalCBOR(w io.Writer) error { + panic("no") +} + +func (blk *SignedMessage) UnmarshalCBOR(r io.Reader) error { + panic("no") +} +*/ diff --git a/chain/types/blockheader_test.go b/chain/types/blockheader_test.go new file mode 100644 index 000000000..3c4f680ae --- /dev/null +++ b/chain/types/blockheader_test.go @@ -0,0 +1,88 @@ +package types + +import ( + "bytes" + "reflect" + "testing" + + "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 { + t.Helper() + + addr, err := address.NewIDAddress(12512063) + if err != nil { + t.Fatal(err) + } + + c, err := cid.Decode("bafyreicmaj5hhoy5mgqvamfhgexxyergw7hdeshizghodwkjg6qmpoco7i") + if err != nil { + t.Fatal(err) + } + + return &BlockHeader{ + Miner: addr, + ElectionProof: []byte("cats won the election"), + Tickets: []*Ticket{ + &Ticket{ + VRFProof: []byte("vrf proof"), + VDFResult: []byte("vdf result"), + VDFProof: []byte("vrf proof"), + }, + }, + Parents: []cid.Cid{c, c}, + MessageReceipts: c, + BLSAggregate: Signature{Type: KTBLS, Data: []byte("boo! im a signature")}, + ParentWeight: NewInt(123125126212), + Messages: c, + Height: 85919298723, + StateRoot: c, + } +} + +func TestBlockHeaderSerialization(t *testing.T) { + bh := testBlockHeader(t) + + buf := new(bytes.Buffer) + if err := bh.MarshalCBOR(buf); err != nil { + t.Fatal(err) + } + + var out BlockHeader + if err := out.UnmarshalCBOR(buf); err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(&out, bh) { + t.Fatal("not equal") + } +} + +func BenchmarkBlockHeaderMarshal(b *testing.B) { + bh := testBlockHeader(b) + + b.ReportAllocs() + + buf := new(bytes.Buffer) + for i := 0; i < b.N; i++ { + buf.Reset() + if err := bh.MarshalCBOR(buf); err != nil { + b.Fatal(err) + } + } +} + +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 new file mode 100644 index 000000000..55ec1a032 --- /dev/null +++ b/chain/types/cbor_gen.go @@ -0,0 +1,551 @@ +package types + +import ( + "fmt" + "io" + + cid "github.com/ipfs/go-cid" + cbg "github.com/whyrusleeping/cbor-gen" +) + +/* This file was generated by github.com/whyrusleeping/cbor-gen */ + +func (t *BlockHeader) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{138}); err != nil { + return err + } + + // t.Miner (address.Address) + + if err := t.Miner.MarshalCBOR(w); err != nil { + return err + } + + // t.Tickets ([]*types.Ticket) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.Tickets)))); err != nil { + return err + } + for _, v := range t.Tickets { + if err := v.MarshalCBOR(w); err != nil { + return err + } + } + + // t.ElectionProof ([]uint8) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.ElectionProof)))); err != nil { + return err + } + if _, err := w.Write(t.ElectionProof); err != nil { + return err + } + + // 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 + } + } + + // t.ParentWeight (types.BigInt) + + if err := t.ParentWeight.MarshalCBOR(w); err != nil { + return err + } + + // t.Height (uint64) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.Height)); err != nil { + return err + } + + // t.StateRoot (cid.Cid) + if err := cbg.WriteCid(w, t.StateRoot); err != nil { + return err + } + + // t.Messages (cid.Cid) + if err := cbg.WriteCid(w, t.Messages); err != nil { + return err + } + + // t.BLSAggregate (types.Signature) + + if err := t.BLSAggregate.MarshalCBOR(w); err != nil { + return err + } + + // t.MessageReceipts (cid.Cid) + if err := cbg.WriteCid(w, t.MessageReceipts); err != nil { + return err + } + return nil +} + +func (t *BlockHeader) UnmarshalCBOR(br cbg.ByteReader) 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 != 10 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.Miner (address.Address) + + if err := t.Miner.UnmarshalCBOR(br); err != nil { + return err + } + // t.Tickets ([]*types.Ticket) + + 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.Tickets = make([]*Ticket, 0, extra) + } + for i := 0; i < int(extra); i++ { + var v Ticket + if err := v.UnmarshalCBOR(br); err != nil { + return err + } + + t.Tickets = append(t.Tickets, &v) + } + + // t.ElectionProof ([]uint8) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + t.ElectionProof = make([]byte, extra) + if _, err := io.ReadFull(br, t.ElectionProof); err != nil { + return err + } + // t.Parents ([]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.Parents = make([]cid.Cid, 0, extra) + } + for i := 0; i < int(extra); i++ { + + c, err := cbg.ReadCid(br) + if err != nil { + return err + } + t.Parents = append(t.Parents, c) + } + + // t.ParentWeight (types.BigInt) + + if err := t.ParentWeight.UnmarshalCBOR(br); err != nil { + return err + } + // t.Height (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.Height = extra + // t.StateRoot (cid.Cid) + + { + c, err := cbg.ReadCid(br) + if err != nil { + return err + } + t.StateRoot = c + } + // t.Messages (cid.Cid) + + { + c, err := cbg.ReadCid(br) + if err != nil { + return err + } + t.Messages = c + } + // t.BLSAggregate (types.Signature) + + if err := t.BLSAggregate.UnmarshalCBOR(br); err != nil { + return err + } + // t.MessageReceipts (cid.Cid) + + { + c, err := cbg.ReadCid(br) + if err != nil { + return err + } + t.MessageReceipts = c + } + return nil +} + +func (t *Ticket) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{131}); err != nil { + return err + } + + // t.VRFProof ([]uint8) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.VRFProof)))); err != nil { + return err + } + if _, err := w.Write(t.VRFProof); err != nil { + return err + } + + // t.VDFResult ([]uint8) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.VDFResult)))); err != nil { + return err + } + if _, err := w.Write(t.VDFResult); err != nil { + return err + } + + // t.VDFProof ([]uint8) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.VDFProof)))); err != nil { + return err + } + if _, err := w.Write(t.VDFProof); err != nil { + return err + } + return nil +} + +func (t *Ticket) UnmarshalCBOR(br cbg.ByteReader) 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.VRFProof ([]uint8) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + t.VRFProof = make([]byte, extra) + if _, err := io.ReadFull(br, t.VRFProof); err != nil { + return err + } + // t.VDFResult ([]uint8) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + t.VDFResult = make([]byte, extra) + if _, err := io.ReadFull(br, t.VDFResult); err != nil { + return err + } + // t.VDFProof ([]uint8) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + t.VDFProof = make([]byte, extra) + if _, err := io.ReadFull(br, t.VDFProof); err != nil { + return err + } + return nil +} + +func (t *Message) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{136}); err != nil { + return err + } + + // t.To (address.Address) + + if err := t.To.MarshalCBOR(w); err != nil { + return err + } + + // t.From (address.Address) + + if err := t.From.MarshalCBOR(w); err != nil { + return err + } + + // t.Nonce (uint64) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.Nonce)); err != nil { + return err + } + + // t.Value (types.BigInt) + + if err := t.Value.MarshalCBOR(w); err != nil { + return err + } + + // t.GasPrice (types.BigInt) + + if err := t.GasPrice.MarshalCBOR(w); err != nil { + return err + } + + // t.GasLimit (types.BigInt) + + if err := t.GasLimit.MarshalCBOR(w); err != nil { + return err + } + + // t.Method (uint64) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.Method)); err != nil { + return err + } + + // t.Params ([]uint8) + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.Params)))); err != nil { + return err + } + if _, err := w.Write(t.Params); err != nil { + return err + } + return nil +} + +func (t *Message) UnmarshalCBOR(br cbg.ByteReader) 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 != 8 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.To (address.Address) + + if err := t.To.UnmarshalCBOR(br); err != nil { + return err + } + // t.From (address.Address) + + if err := t.From.UnmarshalCBOR(br); err != nil { + return err + } + // t.Nonce (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.Nonce = extra + // t.Value (types.BigInt) + + if err := t.Value.UnmarshalCBOR(br); err != nil { + return err + } + // t.GasPrice (types.BigInt) + + if err := t.GasPrice.UnmarshalCBOR(br); err != nil { + return err + } + // t.GasLimit (types.BigInt) + + if err := t.GasLimit.UnmarshalCBOR(br); err != nil { + return err + } + // t.Method (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.Method = extra + // t.Params ([]uint8) + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + if extra > 8192 { + return fmt.Errorf("array too large") + } + + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + t.Params = make([]byte, extra) + if _, err := io.ReadFull(br, t.Params); err != nil { + return err + } + return nil +} + +func (t *SignedMessage) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{130}); err != nil { + return err + } + + // t.Message (types.Message) + + if err := t.Message.MarshalCBOR(w); err != nil { + return err + } + + // t.Signature (types.Signature) + + if err := t.Signature.MarshalCBOR(w); err != nil { + return err + } + return nil +} + +func (t *SignedMessage) UnmarshalCBOR(br cbg.ByteReader) 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 != 2 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.Message (types.Message) + + if err := t.Message.UnmarshalCBOR(br); err != nil { + return err + } + // t.Signature (types.Signature) + + if err := t.Signature.UnmarshalCBOR(br); err != nil { + return err + } + return nil +} + +func (t *MsgMeta) MarshalCBOR(w io.Writer) error { + if _, err := w.Write([]byte{130}); err != nil { + return err + } + + // t.BlsMessages (cid.Cid) + if err := cbg.WriteCid(w, t.BlsMessages); err != nil { + return err + } + + // t.SecpkMessages (cid.Cid) + if err := cbg.WriteCid(w, t.SecpkMessages); err != nil { + return err + } + return nil +} + +func (t *MsgMeta) UnmarshalCBOR(br cbg.ByteReader) 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 != 2 { + return fmt.Errorf("cbor input had wrong number of fields") + } + + // t.BlsMessages (cid.Cid) + + { + c, err := cbg.ReadCid(br) + if err != nil { + return err + } + t.BlsMessages = c + } + // t.SecpkMessages (cid.Cid) + + { + c, err := cbg.ReadCid(br) + if err != nil { + return err + } + t.SecpkMessages = c + } + return nil +} diff --git a/chain/types/message.go b/chain/types/message.go index b6d9008f5..579329eca 100644 --- a/chain/types/message.go +++ b/chain/types/message.go @@ -1,77 +1,16 @@ package types import ( + "bytes" "fmt" block "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" - cbor "github.com/ipfs/go-ipld-cbor" "github.com/multiformats/go-multihash" - "github.com/polydawn/refmt/obj/atlas" "github.com/filecoin-project/go-lotus/chain/address" ) -func init() { - cbor.RegisterCborType(atlas.BuildEntry(Message{}).UseTag(44).Transform(). - TransformMarshal(atlas.MakeMarshalTransformFunc( - func(m Message) ([]interface{}, error) { - return []interface{}{ - m.To.Bytes(), - m.From.Bytes(), - m.Nonce, - m.Value, - m.GasPrice, - m.GasLimit, - m.Method, - m.Params, - }, nil - })). - TransformUnmarshal(atlas.MakeUnmarshalTransformFunc( - func(arr []interface{}) (Message, error) { - to, err := address.NewFromBytes(arr[0].([]byte)) - if err != nil { - return Message{}, err - } - - from, err := address.NewFromBytes(arr[1].([]byte)) - if err != nil { - return Message{}, err - } - - nonce, ok := arr[2].(uint64) - if !ok { - return Message{}, fmt.Errorf("expected uint64 nonce at index 2") - } - - value := arr[3].(BigInt) - gasPrice := arr[4].(BigInt) - gasLimit := arr[5].(BigInt) - method, _ := arr[6].(uint64) - params, _ := arr[7].([]byte) - - if gasPrice.Nil() { - gasPrice = NewInt(0) - } - - if gasLimit.Nil() { - gasLimit = NewInt(0) - } - - return Message{ - To: to, - From: from, - Nonce: nonce, - Value: value, - GasPrice: gasPrice, - GasLimit: gasLimit, - Method: method, - Params: params, - }, nil - })). - Complete()) -} - type Message struct { To address.Address From address.Address @@ -89,7 +28,7 @@ type Message struct { func DecodeMessage(b []byte) (*Message, error) { var msg Message - if err := cbor.DecodeInto(b, &msg); err != nil { + if err := msg.UnmarshalCBOR(bytes.NewReader(b)); err != nil { return nil, err } @@ -97,7 +36,11 @@ func DecodeMessage(b []byte) (*Message, error) { } func (m *Message) Serialize() ([]byte, error) { - return cbor.DumpObject(m) + buf := new(bytes.Buffer) + if err := m.MarshalCBOR(buf); err != nil { + return nil, err + } + return buf.Bytes(), nil } func (m *Message) ToStorageBlock() (block.Block, error) { @@ -106,7 +49,7 @@ func (m *Message) ToStorageBlock() (block.Block, error) { return nil, err } - pref := cid.NewPrefixV1(0x1f, multihash.BLAKE2B_MIN+31) + pref := cid.NewPrefixV1(cid.DagCBOR, multihash.BLAKE2B_MIN+31) c, err := pref.Sum(data) if err != nil { return nil, err diff --git a/chain/types/signedmessage.go b/chain/types/signedmessage.go index d2d0d6fcf..946e31a6d 100644 --- a/chain/types/signedmessage.go +++ b/chain/types/signedmessage.go @@ -1,51 +1,20 @@ package types import ( - "fmt" + "bytes" block "github.com/ipfs/go-block-format" "github.com/ipfs/go-cid" - cbor "github.com/ipfs/go-ipld-cbor" "github.com/multiformats/go-multihash" - "github.com/polydawn/refmt/obj/atlas" ) -func init() { - cbor.RegisterCborType(atlas.BuildEntry(SignedMessage{}).UseTag(45).Transform(). - TransformMarshal(atlas.MakeMarshalTransformFunc( - func(sm SignedMessage) ([]interface{}, error) { - return []interface{}{ - sm.Message, - sm.Signature, - }, nil - })). - TransformUnmarshal(atlas.MakeUnmarshalTransformFunc( - func(x []interface{}) (SignedMessage, error) { - sigb, ok := x[1].([]byte) - if !ok { - return SignedMessage{}, fmt.Errorf("signature in signed message was not bytes") - } - - sig, err := SignatureFromBytes(sigb) - if err != nil { - return SignedMessage{}, err - } - - return SignedMessage{ - Message: x[0].(Message), - Signature: sig, - }, nil - })). - Complete()) -} - func (m *SignedMessage) ToStorageBlock() (block.Block, error) { data, err := m.Serialize() if err != nil { return nil, err } - pref := cid.NewPrefixV1(0x1f, multihash.BLAKE2B_MIN+31) + pref := cid.NewPrefixV1(cid.DagCBOR, multihash.BLAKE2B_MIN+31) c, err := pref.Sum(data) if err != nil { return nil, err @@ -74,7 +43,7 @@ type SignedMessage struct { func DecodeSignedMessage(data []byte) (*SignedMessage, error) { var msg SignedMessage - if err := cbor.DecodeInto(data, &msg); err != nil { + if err := msg.UnmarshalCBOR(bytes.NewReader(data)); err != nil { return nil, err } @@ -82,9 +51,9 @@ func DecodeSignedMessage(data []byte) (*SignedMessage, error) { } func (sm *SignedMessage) Serialize() ([]byte, error) { - data, err := cbor.DumpObject(sm) - if err != nil { + buf := new(bytes.Buffer) + if err := sm.MarshalCBOR(buf); err != nil { return nil, err } - return data, nil + return buf.Bytes(), nil } diff --git a/gen/main.go b/gen/main.go new file mode 100644 index 000000000..bb225f4d8 --- /dev/null +++ b/gen/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "os" + + "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) + os.Exit(1) + } + } +} diff --git a/go.mod b/go.mod index 4b9150a64..4505cfe92 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/ipfs/go-block-format v0.0.2 github.com/ipfs/go-blockservice v0.1.2 github.com/ipfs/go-car v0.0.1 - github.com/ipfs/go-cid v0.0.4-0.20190820001312-aa319eb8b553 + github.com/ipfs/go-cid v0.0.3 github.com/ipfs/go-datastore v0.0.5 github.com/ipfs/go-ds-badger v0.0.5 github.com/ipfs/go-filestore v0.0.2 @@ -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-20190820042349-0aae2ad6ba87 + github.com/whyrusleeping/cbor-gen v0.0.0-20190821171244-dffbd9058edd 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 67985d3c2..f1db857ff 100644 --- a/go.sum +++ b/go.sum @@ -622,6 +622,18 @@ github.com/whyrusleeping/cbor-gen v0.0.0-20190820011425-d5123893e85a h1:MYglUpcz github.com/whyrusleeping/cbor-gen v0.0.0-20190820011425-d5123893e85a/go.mod h1:38LwjsrZy8ga8AbyOueCsLUAtdc1BDj/I929R+LnJfI= github.com/whyrusleeping/cbor-gen v0.0.0-20190820042349-0aae2ad6ba87 h1:8LlRl/4dvh1pXP7cHo5xOekrix/V6wotqgFq7I8W2ng= github.com/whyrusleeping/cbor-gen v0.0.0-20190820042349-0aae2ad6ba87/go.mod h1:38LwjsrZy8ga8AbyOueCsLUAtdc1BDj/I929R+LnJfI= +github.com/whyrusleeping/cbor-gen v0.0.0-20190820044539-77a6481b8f41 h1:MjywORkHGWYI06NKxqXxT0kiAHvfdiBIGe+LpynWk/M= +github.com/whyrusleeping/cbor-gen v0.0.0-20190820044539-77a6481b8f41/go.mod h1:38LwjsrZy8ga8AbyOueCsLUAtdc1BDj/I929R+LnJfI= +github.com/whyrusleeping/cbor-gen v0.0.0-20190820051759-9d467b76c1eb h1:FgJPcjNwWpg/pl8HSjGuy8ZaJc8ROd91vXWpTOQq9vw= +github.com/whyrusleeping/cbor-gen v0.0.0-20190820051759-9d467b76c1eb/go.mod h1:38LwjsrZy8ga8AbyOueCsLUAtdc1BDj/I929R+LnJfI= +github.com/whyrusleeping/cbor-gen v0.0.0-20190820051858-16f189bf46d3 h1:gNOpmUR1HlfijJYQKCF0NHKGkstLJhQ97pza3YXi3Cw= +github.com/whyrusleeping/cbor-gen v0.0.0-20190820051858-16f189bf46d3/go.mod h1:38LwjsrZy8ga8AbyOueCsLUAtdc1BDj/I929R+LnJfI= +github.com/whyrusleeping/cbor-gen v0.0.0-20190820052000-229250b95787 h1:prDQ+H+BkcdxU4OqagFmC4EGKKyeJDkm3/eD0pvf8SI= +github.com/whyrusleeping/cbor-gen v0.0.0-20190820052000-229250b95787/go.mod h1:38LwjsrZy8ga8AbyOueCsLUAtdc1BDj/I929R+LnJfI= +github.com/whyrusleeping/cbor-gen v0.0.0-20190821011005-d6302bef16d9 h1:4X7YSN2+18aCdJ7de0Mv9Ttzo+vwl1fpcWGS9yZDLKA= +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/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=