From f83bbc2cbe87b7602a5573c237515f976ecf1bb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 28 Feb 2020 00:34:48 +0100 Subject: [PATCH] Regen cbor marshalers --- api/cbor_gen.go | 276 ++++++++++++++++++++++++++++++++++++ chain/blocksync/cbor_gen.go | 2 +- chain/types/cbor_gen.go | 40 +++++- gen/main.go | 4 +- node/hello/cbor_gen.go | 2 +- storage/sealing/cbor_gen.go | 185 +++++++++++++++++++----- 6 files changed, 460 insertions(+), 49 deletions(-) diff --git a/api/cbor_gen.go b/api/cbor_gen.go index 71c551ebe..2a72de578 100644 --- a/api/cbor_gen.go +++ b/api/cbor_gen.go @@ -432,3 +432,279 @@ func (t *SealedRefs) UnmarshalCBOR(r io.Reader) error { return nil } +func (t *SealTicket) MarshalCBOR(w io.Writer) error { + if t == nil { + _, err := w.Write(cbg.CborNull) + return err + } + if _, err := w.Write([]byte{162}); err != nil { + return err + } + + // t.Value (abi.SealRandomness) (slice) + if len("Value") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Value\" was too long") + } + + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("Value")))); err != nil { + return err + } + if _, err := w.Write([]byte("Value")); err != nil { + return err + } + + if len(t.Value) > cbg.ByteArrayMaxLen { + return xerrors.Errorf("Byte array in field t.Value was too long") + } + + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.Value)))); err != nil { + return err + } + if _, err := w.Write(t.Value); err != nil { + return err + } + + // t.Epoch (abi.ChainEpoch) (int64) + if len("Epoch") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Epoch\" was too long") + } + + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("Epoch")))); err != nil { + return err + } + if _, err := w.Write([]byte("Epoch")); err != nil { + return err + } + + if t.Epoch >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Epoch))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Epoch)-1)); err != nil { + return err + } + } + return nil +} + +func (t *SealTicket) UnmarshalCBOR(r io.Reader) error { + br := cbg.GetPeeker(r) + + maj, extra, err := cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajMap { + return fmt.Errorf("cbor input should be of type map") + } + + if extra > cbg.MaxLength { + return fmt.Errorf("SealTicket: map struct too large (%d)", extra) + } + + var name string + n := extra + + for i := uint64(0); i < n; i++ { + + { + sval, err := cbg.ReadString(br) + if err != nil { + return err + } + + name = string(sval) + } + + switch name { + // t.Value (abi.SealRandomness) (slice) + case "Value": + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + + if extra > cbg.ByteArrayMaxLen { + return fmt.Errorf("t.Value: byte array too large (%d)", extra) + } + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + t.Value = make([]byte, extra) + if _, err := io.ReadFull(br, t.Value); err != nil { + return err + } + // t.Epoch (abi.ChainEpoch) (int64) + case "Epoch": + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } + + t.Epoch = abi.ChainEpoch(extraI) + } + + default: + return fmt.Errorf("unknown struct field %d: '%s'", i, name) + } + } + + return nil +} +func (t *SealSeed) MarshalCBOR(w io.Writer) error { + if t == nil { + _, err := w.Write(cbg.CborNull) + return err + } + if _, err := w.Write([]byte{162}); err != nil { + return err + } + + // t.Value (abi.InteractiveSealRandomness) (slice) + if len("Value") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Value\" was too long") + } + + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("Value")))); err != nil { + return err + } + if _, err := w.Write([]byte("Value")); err != nil { + return err + } + + if len(t.Value) > cbg.ByteArrayMaxLen { + return xerrors.Errorf("Byte array in field t.Value was too long") + } + + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.Value)))); err != nil { + return err + } + if _, err := w.Write(t.Value); err != nil { + return err + } + + // t.Epoch (abi.ChainEpoch) (int64) + if len("Epoch") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"Epoch\" was too long") + } + + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("Epoch")))); err != nil { + return err + } + if _, err := w.Write([]byte("Epoch")); err != nil { + return err + } + + if t.Epoch >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Epoch))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Epoch)-1)); err != nil { + return err + } + } + return nil +} + +func (t *SealSeed) UnmarshalCBOR(r io.Reader) error { + br := cbg.GetPeeker(r) + + maj, extra, err := cbg.CborReadHeader(br) + if err != nil { + return err + } + if maj != cbg.MajMap { + return fmt.Errorf("cbor input should be of type map") + } + + if extra > cbg.MaxLength { + return fmt.Errorf("SealSeed: map struct too large (%d)", extra) + } + + var name string + n := extra + + for i := uint64(0); i < n; i++ { + + { + sval, err := cbg.ReadString(br) + if err != nil { + return err + } + + name = string(sval) + } + + switch name { + // t.Value (abi.InteractiveSealRandomness) (slice) + case "Value": + + maj, extra, err = cbg.CborReadHeader(br) + if err != nil { + return err + } + + if extra > cbg.ByteArrayMaxLen { + return fmt.Errorf("t.Value: byte array too large (%d)", extra) + } + if maj != cbg.MajByteString { + return fmt.Errorf("expected byte array") + } + t.Value = make([]byte, extra) + if _, err := io.ReadFull(br, t.Value); err != nil { + return err + } + // t.Epoch (abi.ChainEpoch) (int64) + case "Epoch": + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } + + t.Epoch = abi.ChainEpoch(extraI) + } + + default: + return fmt.Errorf("unknown struct field %d: '%s'", i, name) + } + } + + return nil +} diff --git a/chain/blocksync/cbor_gen.go b/chain/blocksync/cbor_gen.go index 6668eb3a6..60cb1c0b3 100644 --- a/chain/blocksync/cbor_gen.go +++ b/chain/blocksync/cbor_gen.go @@ -7,7 +7,7 @@ import ( "io" "github.com/filecoin-project/lotus/chain/types" - cid "github.com/ipfs/go-cid" + "github.com/ipfs/go-cid" cbg "github.com/whyrusleeping/cbor-gen" xerrors "golang.org/x/xerrors" ) diff --git a/chain/types/cbor_gen.go b/chain/types/cbor_gen.go index 6e0e49f8c..801a3645d 100644 --- a/chain/types/cbor_gen.go +++ b/chain/types/cbor_gen.go @@ -9,7 +9,7 @@ import ( "github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/crypto" - cid "github.com/ipfs/go-cid" + "github.com/ipfs/go-cid" cbg "github.com/whyrusleeping/cbor-gen" xerrors "golang.org/x/xerrors" ) @@ -391,7 +391,19 @@ func (t *EPostProof) MarshalCBOR(w io.Writer) error { return err } - // t.Proof ([]uint8) (slice) + // t.Proofs ([]abi.PoStProof) (slice) + if len(t.Proofs) > cbg.MaxLength { + return xerrors.Errorf("Slice value in field t.Proofs was too long") + } + + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.Proofs)))); err != nil { + return err + } + for _, v := range t.Proofs { + if err := v.MarshalCBOR(w); err != nil { + return err + } + } // t.PostRand ([]uint8) (slice) if len(t.PostRand) > cbg.ByteArrayMaxLen { @@ -436,19 +448,33 @@ func (t *EPostProof) UnmarshalCBOR(r io.Reader) error { return fmt.Errorf("cbor input had wrong number of fields") } - // t.Proof ([]uint8) (slice) + // t.Proofs ([]abi.PoStProof) (slice) maj, extra, err = cbg.CborReadHeader(br) if err != nil { return err } - if extra > cbg.ByteArrayMaxLen { - return fmt.Errorf("t.Proof: byte array too large (%d)", extra) + if extra > cbg.MaxLength { + return fmt.Errorf("t.Proofs: array too large (%d)", extra) } - if maj != cbg.MajByteString { - return fmt.Errorf("expected byte array") + + if maj != cbg.MajArray { + return fmt.Errorf("expected cbor array") } + if extra > 0 { + t.Proofs = make([]abi.PoStProof, extra) + } + for i := 0; i < int(extra); i++ { + + var v abi.PoStProof + if err := v.UnmarshalCBOR(br); err != nil { + return err + } + + t.Proofs[i] = v + } + // t.PostRand ([]uint8) (slice) maj, extra, err = cbg.CborReadHeader(br) diff --git a/gen/main.go b/gen/main.go index f8e830852..03b82ad5b 100644 --- a/gen/main.go +++ b/gen/main.go @@ -47,6 +47,8 @@ func main() { api.PaymentInfo{}, api.SealedRef{}, api.SealedRefs{}, + api.SealTicket{}, + api.SealSeed{}, ) if err != nil { fmt.Println(err) @@ -73,8 +75,6 @@ func main() { } err = gen.WriteMapEncodersToFile("./storage/sealing/cbor_gen.go", "sealing", - sealing.SealTicket{}, - sealing.SealSeed{}, sealing.Piece{}, sealing.SectorInfo{}, sealing.Log{}, diff --git a/node/hello/cbor_gen.go b/node/hello/cbor_gen.go index da4bf0c00..3572e1594 100644 --- a/node/hello/cbor_gen.go +++ b/node/hello/cbor_gen.go @@ -7,7 +7,7 @@ import ( "io" "github.com/filecoin-project/specs-actors/actors/abi" - cid "github.com/ipfs/go-cid" + "github.com/ipfs/go-cid" cbg "github.com/whyrusleeping/cbor-gen" xerrors "golang.org/x/xerrors" ) diff --git a/storage/sealing/cbor_gen.go b/storage/sealing/cbor_gen.go index e8cf29ec2..6fe46d985 100644 --- a/storage/sealing/cbor_gen.go +++ b/storage/sealing/cbor_gen.go @@ -60,7 +60,7 @@ func (t *Piece) MarshalCBOR(w io.Writer) error { return err } - // t.CommP ([]uint8) (slice) + // t.CommP (cid.Cid) (struct) if len("CommP") > cbg.MaxLength { return xerrors.Errorf("Value in field \"CommP\" was too long") } @@ -72,6 +72,10 @@ func (t *Piece) MarshalCBOR(w io.Writer) error { return err } + if err := cbg.WriteCid(w, t.CommP); err != nil { + return xerrors.Errorf("failed to write cid field t.CommP: %w", err) + } + return nil } @@ -147,19 +151,18 @@ func (t *Piece) UnmarshalCBOR(r io.Reader) error { t.Size = abi.UnpaddedPieceSize(extra) } - // t.CommP ([]uint8) (slice) + // t.CommP (cid.Cid) (struct) case "CommP": - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err - } + { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("failed to read cid field t.CommP: %w", err) + } + + t.CommP = c - if extra > cbg.ByteArrayMaxLen { - return fmt.Errorf("t.CommP: byte array too large (%d)", extra) - } - if maj != cbg.MajByteString { - return fmt.Errorf("expected byte array") } default: @@ -174,7 +177,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{174}); err != nil { + if _, err := w.Write([]byte{175}); err != nil { return err } @@ -226,6 +229,28 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return err } + // t.SectorType (abi.RegisteredProof) (int64) + if len("SectorType") > cbg.MaxLength { + return xerrors.Errorf("Value in field \"SectorType\" was too long") + } + + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("SectorType")))); err != nil { + return err + } + if _, err := w.Write([]byte("SectorType")); err != nil { + return err + } + + if t.SectorType >= 0 { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.SectorType))); err != nil { + return err + } + } else { + if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.SectorType)-1)); err != nil { + return err + } + } + // t.Pieces ([]sealing.Piece) (slice) if len("Pieces") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Pieces\" was too long") @@ -251,7 +276,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } } - // t.CommD ([]uint8) (slice) + // t.CommD (cid.Cid) (struct) if len("CommD") > cbg.MaxLength { return xerrors.Errorf("Value in field \"CommD\" was too long") } @@ -263,7 +288,17 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return err } - // t.CommR ([]uint8) (slice) + if t.CommD == nil { + if _, err := w.Write(cbg.CborNull); err != nil { + return err + } + } else { + if err := cbg.WriteCid(w, *t.CommD); err != nil { + return xerrors.Errorf("failed to write cid field t.CommD: %w", err) + } + } + + // t.CommR (cid.Cid) (struct) if len("CommR") > cbg.MaxLength { return xerrors.Errorf("Value in field \"CommR\" was too long") } @@ -275,6 +310,16 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return err } + if t.CommR == nil { + if _, err := w.Write(cbg.CborNull); err != nil { + return err + } + } else { + if err := cbg.WriteCid(w, *t.CommR); err != nil { + return xerrors.Errorf("failed to write cid field t.CommR: %w", err) + } + } + // t.Proof ([]uint8) (slice) if len("Proof") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Proof\" was too long") @@ -298,7 +343,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return err } - // t.Ticket (sealing.SealTicket) (struct) + // t.Ticket (api.SealTicket) (struct) if len("Ticket") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Ticket\" was too long") } @@ -310,6 +355,10 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return err } + if err := t.Ticket.MarshalCBOR(w); err != nil { + return err + } + // t.PreCommitMessage (cid.Cid) (struct) if len("PreCommitMessage") > cbg.MaxLength { return xerrors.Errorf("Value in field \"PreCommitMessage\" was too long") @@ -332,7 +381,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } } - // t.Seed (sealing.SealSeed) (struct) + // t.Seed (api.SealSeed) (struct) if len("Seed") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Seed\" was too long") } @@ -344,6 +393,10 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return err } + if err := t.Seed.MarshalCBOR(w); err != nil { + return err + } + // t.CommitMessage (cid.Cid) (struct) if len("CommitMessage") > cbg.MaxLength { return xerrors.Errorf("Value in field \"CommitMessage\" was too long") @@ -513,6 +566,32 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { t.Nonce = uint64(extra) } + // t.SectorType (abi.RegisteredProof) (int64) + case "SectorType": + { + maj, extra, err := cbg.CborReadHeader(br) + var extraI int64 + if err != nil { + return err + } + switch maj { + case cbg.MajUnsignedInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 positive overflow") + } + case cbg.MajNegativeInt: + extraI = int64(extra) + if extraI < 0 { + return fmt.Errorf("int64 negative oveflow") + } + extraI = -1 - extraI + default: + return fmt.Errorf("wrong type for int64 field: %d", maj) + } + + t.SectorType = abi.RegisteredProof(extraI) + } // t.Pieces ([]sealing.Piece) (slice) case "Pieces": @@ -541,33 +620,55 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { t.Pieces[i] = v } - // t.CommD ([]uint8) (slice) + // t.CommD (cid.Cid) (struct) case "CommD": - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err - } + { + + pb, err := br.PeekByte() + if err != nil { + return err + } + if pb == cbg.CborNull[0] { + var nbuf [1]byte + if _, err := br.Read(nbuf[:]); err != nil { + return err + } + } else { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("failed to read cid field t.CommD: %w", err) + } + + t.CommD = &c + } - if extra > cbg.ByteArrayMaxLen { - return fmt.Errorf("t.CommD: byte array too large (%d)", extra) } - if maj != cbg.MajByteString { - return fmt.Errorf("expected byte array") - } - // t.CommR ([]uint8) (slice) + // t.CommR (cid.Cid) (struct) case "CommR": - maj, extra, err = cbg.CborReadHeader(br) - if err != nil { - return err - } + { + + pb, err := br.PeekByte() + if err != nil { + return err + } + if pb == cbg.CborNull[0] { + var nbuf [1]byte + if _, err := br.Read(nbuf[:]); err != nil { + return err + } + } else { + + c, err := cbg.ReadCid(br) + if err != nil { + return xerrors.Errorf("failed to read cid field t.CommR: %w", err) + } + + t.CommR = &c + } - if extra > cbg.ByteArrayMaxLen { - return fmt.Errorf("t.CommR: byte array too large (%d)", extra) - } - if maj != cbg.MajByteString { - return fmt.Errorf("expected byte array") } // t.Proof ([]uint8) (slice) case "Proof": @@ -587,11 +688,15 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { if _, err := io.ReadFull(br, t.Proof); err != nil { return err } - // t.Ticket (sealing.SealTicket) (struct) + // t.Ticket (api.SealTicket) (struct) case "Ticket": { + if err := t.Ticket.UnmarshalCBOR(br); err != nil { + return err + } + } // t.PreCommitMessage (cid.Cid) (struct) case "PreCommitMessage": @@ -618,11 +723,15 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { } } - // t.Seed (sealing.SealSeed) (struct) + // t.Seed (api.SealSeed) (struct) case "Seed": { + if err := t.Seed.UnmarshalCBOR(br); err != nil { + return err + } + } // t.CommitMessage (cid.Cid) (struct) case "CommitMessage":