From 5cfedacf9fbe0de4585d3730f6577feaad124086 Mon Sep 17 00:00:00 2001 From: Jennifer Wang Date: Mon, 23 May 2022 15:11:46 -0400 Subject: [PATCH] make jen --- api/cbor_gen.go | 294 ++++++----- chain/exchange/cbor_gen.go | 168 +++--- chain/market/cbor_gen.go | 38 +- chain/types/cbor_gen.go | 541 +++++++++++--------- chain/vm/cbor_gen.go | 48 +- cmd/lotus-shed/shedgen/cbor_gen.go | 32 +- conformance/chaos/cbor_gen.go | 312 ++++++----- extern/sector-storage/cbor_gen.go | 144 +++--- extern/sector-storage/storiface/cbor_gen.go | 38 +- extern/storage-sealing/cbor_gen.go | 400 ++++++++------- node/hello/cbor_gen.go | 74 +-- paychmgr/cbor_gen.go | 214 ++++---- 12 files changed, 1298 insertions(+), 1005 deletions(-) diff --git a/api/cbor_gen.go b/api/cbor_gen.go index eb4c34f8a46..62e523df12d 100644 --- a/api/cbor_gen.go +++ b/api/cbor_gen.go @@ -26,25 +26,26 @@ func (t *PaymentInfo) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{163}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{163}); err != nil { return err } - scratch := make([]byte, 9) - // t.Channel (address.Address) (struct) if len("Channel") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Channel\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Channel"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Channel"))); err != nil { return err } if _, err := io.WriteString(w, string("Channel")); err != nil { return err } - if err := t.Channel.MarshalCBOR(w); err != nil { + if err := t.Channel.MarshalCBOR(cw); err != nil { return err } @@ -53,14 +54,14 @@ func (t *PaymentInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"WaitSentinel\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("WaitSentinel"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("WaitSentinel"))); err != nil { return err } if _, err := io.WriteString(w, string("WaitSentinel")); err != nil { return err } - if err := cbg.WriteCidBuf(scratch, w, t.WaitSentinel); err != nil { + if err := cbg.WriteCid(cw, t.WaitSentinel); err != nil { return xerrors.Errorf("failed to write cid field t.WaitSentinel: %w", err) } @@ -69,7 +70,7 @@ func (t *PaymentInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Vouchers\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Vouchers"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Vouchers"))); err != nil { return err } if _, err := io.WriteString(w, string("Vouchers")); err != nil { @@ -80,27 +81,32 @@ func (t *PaymentInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Vouchers was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Vouchers))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Vouchers))); err != nil { return err } for _, v := range t.Vouchers { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } return nil } -func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error { +func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) (err error) { *t = PaymentInfo{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -115,7 +121,7 @@ func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -129,7 +135,7 @@ func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error { { - if err := t.Channel.UnmarshalCBOR(br); err != nil { + if err := t.Channel.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Channel: %w", err) } @@ -139,7 +145,7 @@ func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.WaitSentinel: %w", err) } @@ -150,7 +156,7 @@ func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error { // t.Vouchers ([]*paych.SignedVoucher) (slice) case "Vouchers": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -170,7 +176,7 @@ func (t *PaymentInfo) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v paych.SignedVoucher - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -190,25 +196,26 @@ func (t *SealedRef) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{163}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{163}); err != nil { return err } - scratch := make([]byte, 9) - // t.SectorID (abi.SectorNumber) (uint64) if len("SectorID") > cbg.MaxLength { return xerrors.Errorf("Value in field \"SectorID\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("SectorID"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("SectorID"))); err != nil { return err } if _, err := io.WriteString(w, string("SectorID")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.SectorID)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.SectorID)); err != nil { return err } @@ -217,14 +224,14 @@ func (t *SealedRef) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Offset\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Offset"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Offset"))); err != nil { return err } if _, err := io.WriteString(w, string("Offset")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Offset)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Offset)); err != nil { return err } @@ -233,30 +240,35 @@ func (t *SealedRef) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Size\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Size"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Size"))); err != nil { return err } if _, err := io.WriteString(w, string("Size")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Size)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Size)); err != nil { return err } return nil } -func (t *SealedRef) UnmarshalCBOR(r io.Reader) error { +func (t *SealedRef) UnmarshalCBOR(r io.Reader) (err error) { *t = SealedRef{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -271,7 +283,7 @@ func (t *SealedRef) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -285,7 +297,7 @@ func (t *SealedRef) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -300,7 +312,7 @@ func (t *SealedRef) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -315,7 +327,7 @@ func (t *SealedRef) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -339,18 +351,19 @@ func (t *SealedRefs) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{161}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{161}); err != nil { return err } - scratch := make([]byte, 9) - // t.Refs ([]api.SealedRef) (slice) if len("Refs") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Refs\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Refs"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Refs"))); err != nil { return err } if _, err := io.WriteString(w, string("Refs")); err != nil { @@ -361,27 +374,32 @@ func (t *SealedRefs) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Refs was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Refs))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Refs))); err != nil { return err } for _, v := range t.Refs { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } return nil } -func (t *SealedRefs) UnmarshalCBOR(r io.Reader) error { +func (t *SealedRefs) UnmarshalCBOR(r io.Reader) (err error) { *t = SealedRefs{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -396,7 +414,7 @@ func (t *SealedRefs) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -408,7 +426,7 @@ func (t *SealedRefs) UnmarshalCBOR(r io.Reader) error { // t.Refs ([]api.SealedRef) (slice) case "Refs": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -428,7 +446,7 @@ func (t *SealedRefs) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v SealedRef - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -448,18 +466,19 @@ func (t *SealTicket) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{162}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{162}); err != nil { return err } - scratch := make([]byte, 9) - // t.Value (abi.SealRandomness) (slice) if len("Value") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Value\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Value"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Value"))); err != nil { return err } if _, err := io.WriteString(w, string("Value")); err != nil { @@ -470,11 +489,11 @@ func (t *SealTicket) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Value was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Value))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Value))); err != nil { return err } - if _, err := w.Write(t.Value[:]); err != nil { + if _, err := cw.Write(t.Value[:]); err != nil { return err } @@ -483,7 +502,7 @@ func (t *SealTicket) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Epoch\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Epoch"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Epoch"))); err != nil { return err } if _, err := io.WriteString(w, string("Epoch")); err != nil { @@ -491,27 +510,32 @@ func (t *SealTicket) MarshalCBOR(w io.Writer) error { } if t.Epoch >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Epoch)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Epoch)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Epoch-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Epoch-1)); err != nil { return err } } return nil } -func (t *SealTicket) UnmarshalCBOR(r io.Reader) error { +func (t *SealTicket) UnmarshalCBOR(r io.Reader) (err error) { *t = SealTicket{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -526,7 +550,7 @@ func (t *SealTicket) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -538,7 +562,7 @@ func (t *SealTicket) UnmarshalCBOR(r io.Reader) error { // t.Value (abi.SealRandomness) (slice) case "Value": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -554,13 +578,13 @@ func (t *SealTicket) UnmarshalCBOR(r io.Reader) error { t.Value = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Value[:]); err != nil { + if _, err := io.ReadFull(cr, t.Value[:]); err != nil { return err } // t.Epoch (abi.ChainEpoch) (int64) case "Epoch": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -597,18 +621,19 @@ func (t *SealSeed) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{162}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{162}); err != nil { return err } - scratch := make([]byte, 9) - // t.Value (abi.InteractiveSealRandomness) (slice) if len("Value") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Value\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Value"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Value"))); err != nil { return err } if _, err := io.WriteString(w, string("Value")); err != nil { @@ -619,11 +644,11 @@ func (t *SealSeed) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Value was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Value))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Value))); err != nil { return err } - if _, err := w.Write(t.Value[:]); err != nil { + if _, err := cw.Write(t.Value[:]); err != nil { return err } @@ -632,7 +657,7 @@ func (t *SealSeed) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Epoch\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Epoch"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Epoch"))); err != nil { return err } if _, err := io.WriteString(w, string("Epoch")); err != nil { @@ -640,27 +665,32 @@ func (t *SealSeed) MarshalCBOR(w io.Writer) error { } if t.Epoch >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Epoch)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Epoch)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Epoch-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Epoch-1)); err != nil { return err } } return nil } -func (t *SealSeed) UnmarshalCBOR(r io.Reader) error { +func (t *SealSeed) UnmarshalCBOR(r io.Reader) (err error) { *t = SealSeed{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -675,7 +705,7 @@ func (t *SealSeed) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -687,7 +717,7 @@ func (t *SealSeed) UnmarshalCBOR(r io.Reader) error { // t.Value (abi.InteractiveSealRandomness) (slice) case "Value": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -703,13 +733,13 @@ func (t *SealSeed) UnmarshalCBOR(r io.Reader) error { t.Value = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Value[:]); err != nil { + if _, err := io.ReadFull(cr, t.Value[:]); err != nil { return err } // t.Epoch (abi.ChainEpoch) (int64) case "Epoch": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -746,18 +776,19 @@ func (t *PieceDealInfo) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{165}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{165}); err != nil { return err } - scratch := make([]byte, 9) - // t.PublishCid (cid.Cid) (struct) if len("PublishCid") > cbg.MaxLength { return xerrors.Errorf("Value in field \"PublishCid\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("PublishCid"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("PublishCid"))); err != nil { return err } if _, err := io.WriteString(w, string("PublishCid")); err != nil { @@ -765,11 +796,11 @@ func (t *PieceDealInfo) MarshalCBOR(w io.Writer) error { } if t.PublishCid == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.PublishCid); err != nil { + if err := cbg.WriteCid(cw, *t.PublishCid); err != nil { return xerrors.Errorf("failed to write cid field t.PublishCid: %w", err) } } @@ -779,14 +810,14 @@ func (t *PieceDealInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"DealID\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("DealID"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("DealID"))); err != nil { return err } if _, err := io.WriteString(w, string("DealID")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.DealID)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.DealID)); err != nil { return err } @@ -795,14 +826,14 @@ func (t *PieceDealInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"DealProposal\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("DealProposal"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("DealProposal"))); err != nil { return err } if _, err := io.WriteString(w, string("DealProposal")); err != nil { return err } - if err := t.DealProposal.MarshalCBOR(w); err != nil { + if err := t.DealProposal.MarshalCBOR(cw); err != nil { return err } @@ -811,14 +842,14 @@ func (t *PieceDealInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"DealSchedule\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("DealSchedule"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("DealSchedule"))); err != nil { return err } if _, err := io.WriteString(w, string("DealSchedule")); err != nil { return err } - if err := t.DealSchedule.MarshalCBOR(w); err != nil { + if err := t.DealSchedule.MarshalCBOR(cw); err != nil { return err } @@ -827,7 +858,7 @@ func (t *PieceDealInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"KeepUnsealed\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("KeepUnsealed"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("KeepUnsealed"))); err != nil { return err } if _, err := io.WriteString(w, string("KeepUnsealed")); err != nil { @@ -840,16 +871,21 @@ func (t *PieceDealInfo) MarshalCBOR(w io.Writer) error { return nil } -func (t *PieceDealInfo) UnmarshalCBOR(r io.Reader) error { +func (t *PieceDealInfo) UnmarshalCBOR(r io.Reader) (err error) { *t = PieceDealInfo{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -864,7 +900,7 @@ func (t *PieceDealInfo) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -878,16 +914,16 @@ func (t *PieceDealInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.PublishCid: %w", err) } @@ -901,7 +937,7 @@ func (t *PieceDealInfo) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -916,16 +952,16 @@ func (t *PieceDealInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.DealProposal = new(market.DealProposal) - if err := t.DealProposal.UnmarshalCBOR(br); err != nil { + if err := t.DealProposal.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.DealProposal pointer: %w", err) } } @@ -936,7 +972,7 @@ func (t *PieceDealInfo) UnmarshalCBOR(r io.Reader) error { { - if err := t.DealSchedule.UnmarshalCBOR(br); err != nil { + if err := t.DealSchedule.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.DealSchedule: %w", err) } @@ -944,7 +980,7 @@ func (t *PieceDealInfo) UnmarshalCBOR(r io.Reader) error { // t.KeepUnsealed (bool) (bool) case "KeepUnsealed": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -973,18 +1009,19 @@ func (t *DealSchedule) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{162}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{162}); err != nil { return err } - scratch := make([]byte, 9) - // t.StartEpoch (abi.ChainEpoch) (int64) if len("StartEpoch") > cbg.MaxLength { return xerrors.Errorf("Value in field \"StartEpoch\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("StartEpoch"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("StartEpoch"))); err != nil { return err } if _, err := io.WriteString(w, string("StartEpoch")); err != nil { @@ -992,11 +1029,11 @@ func (t *DealSchedule) MarshalCBOR(w io.Writer) error { } if t.StartEpoch >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.StartEpoch)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.StartEpoch)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.StartEpoch-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.StartEpoch-1)); err != nil { return err } } @@ -1006,7 +1043,7 @@ func (t *DealSchedule) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"EndEpoch\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("EndEpoch"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("EndEpoch"))); err != nil { return err } if _, err := io.WriteString(w, string("EndEpoch")); err != nil { @@ -1014,27 +1051,32 @@ func (t *DealSchedule) MarshalCBOR(w io.Writer) error { } if t.EndEpoch >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.EndEpoch)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.EndEpoch)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.EndEpoch-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.EndEpoch-1)); err != nil { return err } } return nil } -func (t *DealSchedule) UnmarshalCBOR(r io.Reader) error { +func (t *DealSchedule) UnmarshalCBOR(r io.Reader) (err error) { *t = DealSchedule{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -1049,7 +1091,7 @@ func (t *DealSchedule) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1061,7 +1103,7 @@ func (t *DealSchedule) UnmarshalCBOR(r io.Reader) error { // t.StartEpoch (abi.ChainEpoch) (int64) case "StartEpoch": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1087,7 +1129,7 @@ func (t *DealSchedule) UnmarshalCBOR(r io.Reader) error { // t.EndEpoch (abi.ChainEpoch) (int64) case "EndEpoch": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err diff --git a/chain/exchange/cbor_gen.go b/chain/exchange/cbor_gen.go index 7a8597fd01f..9c7f68ab8e1 100644 --- a/chain/exchange/cbor_gen.go +++ b/chain/exchange/cbor_gen.go @@ -26,51 +26,57 @@ func (t *Request) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufRequest); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufRequest); err != nil { return err } - scratch := make([]byte, 9) - // t.Head ([]cid.Cid) (slice) if len(t.Head) > cbg.MaxLength { return xerrors.Errorf("Slice value in field t.Head was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Head))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Head))); err != nil { return err } for _, v := range t.Head { - if err := cbg.WriteCidBuf(scratch, w, v); err != nil { + if err := cbg.WriteCid(w, v); err != nil { return xerrors.Errorf("failed writing cid field t.Head: %w", err) } } // t.Length (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Length)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Length)); err != nil { return err } // t.Options (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Options)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Options)); err != nil { return err } return nil } -func (t *Request) UnmarshalCBOR(r io.Reader) error { +func (t *Request) UnmarshalCBOR(r io.Reader) (err error) { *t = Request{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -81,7 +87,7 @@ func (t *Request) UnmarshalCBOR(r io.Reader) error { // t.Head ([]cid.Cid) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -100,7 +106,7 @@ func (t *Request) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("reading cid field t.Head failed: %w", err) } @@ -111,7 +117,7 @@ func (t *Request) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -125,7 +131,7 @@ func (t *Request) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -145,15 +151,16 @@ func (t *Response) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufResponse); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufResponse); err != nil { return err } - scratch := make([]byte, 9) - // t.Status (exchange.status) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Status)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Status)); err != nil { return err } @@ -162,7 +169,7 @@ func (t *Response) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.ErrorMessage was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.ErrorMessage))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.ErrorMessage))); err != nil { return err } if _, err := io.WriteString(w, string(t.ErrorMessage)); err != nil { @@ -174,27 +181,32 @@ func (t *Response) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Chain was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Chain))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Chain))); err != nil { return err } for _, v := range t.Chain { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } return nil } -func (t *Response) UnmarshalCBOR(r io.Reader) error { +func (t *Response) UnmarshalCBOR(r io.Reader) (err error) { *t = Response{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -207,7 +219,7 @@ func (t *Response) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -220,7 +232,7 @@ func (t *Response) UnmarshalCBOR(r io.Reader) error { // t.ErrorMessage (string) (string) { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -229,7 +241,7 @@ func (t *Response) UnmarshalCBOR(r io.Reader) error { } // t.Chain ([]*exchange.BSTipSet) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -249,7 +261,7 @@ func (t *Response) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v BSTipSet - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -266,22 +278,23 @@ func (t *CompactedMessages) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufCompactedMessages); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufCompactedMessages); err != nil { return err } - scratch := make([]byte, 9) - // t.Bls ([]*types.Message) (slice) if len(t.Bls) > cbg.MaxLength { return xerrors.Errorf("Slice value in field t.Bls was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Bls))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Bls))); err != nil { return err } for _, v := range t.Bls { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -291,7 +304,7 @@ func (t *CompactedMessages) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.BlsIncludes was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.BlsIncludes))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.BlsIncludes))); err != nil { return err } for _, v := range t.BlsIncludes { @@ -299,11 +312,11 @@ func (t *CompactedMessages) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field v was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(v))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(v))); err != nil { return err } for _, v := range v { - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } @@ -314,11 +327,11 @@ func (t *CompactedMessages) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Secpk was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Secpk))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Secpk))); err != nil { return err } for _, v := range t.Secpk { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -328,7 +341,7 @@ func (t *CompactedMessages) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.SecpkIncludes was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.SecpkIncludes))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.SecpkIncludes))); err != nil { return err } for _, v := range t.SecpkIncludes { @@ -336,11 +349,11 @@ func (t *CompactedMessages) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field v was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(v))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(v))); err != nil { return err } for _, v := range v { - if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, uint64(v)); err != nil { + if err := cw.CborWriteHeader(cbg.MajUnsignedInt, uint64(v)); err != nil { return err } } @@ -348,16 +361,21 @@ func (t *CompactedMessages) MarshalCBOR(w io.Writer) error { return nil } -func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { +func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) (err error) { *t = CompactedMessages{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -368,7 +386,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { // t.Bls ([]*types.Message) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -388,7 +406,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v types.Message - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -397,7 +415,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { // t.BlsIncludes ([][]uint64) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -420,7 +438,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { var extra uint64 var err error - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -439,7 +457,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { for j := 0; j < int(extra); j++ { - maj, val, err := cbg.CborReadHeaderBuf(br, scratch) + maj, val, err := cr.ReadHeader() if err != nil { return xerrors.Errorf("failed to read uint64 for t.BlsIncludes[i] slice: %w", err) } @@ -456,7 +474,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { // t.Secpk ([]*types.SignedMessage) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -476,7 +494,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v types.SignedMessage - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -485,7 +503,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { // t.SecpkIncludes ([][]uint64) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -508,7 +526,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { var extra uint64 var err error - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -527,7 +545,7 @@ func (t *CompactedMessages) UnmarshalCBOR(r io.Reader) error { for j := 0; j < int(extra); j++ { - maj, val, err := cbg.CborReadHeaderBuf(br, scratch) + maj, val, err := cr.ReadHeader() if err != nil { return xerrors.Errorf("failed to read uint64 for t.SecpkIncludes[i] slice: %w", err) } @@ -552,43 +570,49 @@ func (t *BSTipSet) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufBSTipSet); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufBSTipSet); err != nil { return err } - scratch := make([]byte, 9) - // t.Blocks ([]*types.BlockHeader) (slice) if len(t.Blocks) > cbg.MaxLength { return xerrors.Errorf("Slice value in field t.Blocks was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Blocks))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Blocks))); err != nil { return err } for _, v := range t.Blocks { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } // t.Messages (exchange.CompactedMessages) (struct) - if err := t.Messages.MarshalCBOR(w); err != nil { + if err := t.Messages.MarshalCBOR(cw); err != nil { return err } return nil } -func (t *BSTipSet) UnmarshalCBOR(r io.Reader) error { +func (t *BSTipSet) UnmarshalCBOR(r io.Reader) (err error) { *t = BSTipSet{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -599,7 +623,7 @@ func (t *BSTipSet) UnmarshalCBOR(r io.Reader) error { // t.Blocks ([]*types.BlockHeader) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -619,7 +643,7 @@ func (t *BSTipSet) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v types.BlockHeader - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -630,16 +654,16 @@ func (t *BSTipSet) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Messages = new(CompactedMessages) - if err := t.Messages.UnmarshalCBOR(br); err != nil { + if err := t.Messages.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Messages pointer: %w", err) } } diff --git a/chain/market/cbor_gen.go b/chain/market/cbor_gen.go index 1c13e9ddc28..9c9ef1a94c6 100644 --- a/chain/market/cbor_gen.go +++ b/chain/market/cbor_gen.go @@ -25,30 +25,31 @@ func (t *FundedAddressState) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufFundedAddressState); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufFundedAddressState); err != nil { return err } - scratch := make([]byte, 9) - // t.Addr (address.Address) (struct) - if err := t.Addr.MarshalCBOR(w); err != nil { + if err := t.Addr.MarshalCBOR(cw); err != nil { return err } // t.AmtReserved (big.Int) (struct) - if err := t.AmtReserved.MarshalCBOR(w); err != nil { + if err := t.AmtReserved.MarshalCBOR(cw); err != nil { return err } // t.MsgCid (cid.Cid) (struct) if t.MsgCid == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.MsgCid); err != nil { + if err := cbg.WriteCid(cw, *t.MsgCid); err != nil { return xerrors.Errorf("failed to write cid field t.MsgCid: %w", err) } } @@ -56,16 +57,21 @@ func (t *FundedAddressState) MarshalCBOR(w io.Writer) error { return nil } -func (t *FundedAddressState) UnmarshalCBOR(r io.Reader) error { +func (t *FundedAddressState) UnmarshalCBOR(r io.Reader) (err error) { *t = FundedAddressState{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -78,7 +84,7 @@ func (t *FundedAddressState) UnmarshalCBOR(r io.Reader) error { { - if err := t.Addr.UnmarshalCBOR(br); err != nil { + if err := t.Addr.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Addr: %w", err) } @@ -87,7 +93,7 @@ func (t *FundedAddressState) UnmarshalCBOR(r io.Reader) error { { - if err := t.AmtReserved.UnmarshalCBOR(br); err != nil { + if err := t.AmtReserved.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.AmtReserved: %w", err) } @@ -96,16 +102,16 @@ func (t *FundedAddressState) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.MsgCid: %w", err) } diff --git a/chain/types/cbor_gen.go b/chain/types/cbor_gen.go index b54c18c0795..544728a3c62 100644 --- a/chain/types/cbor_gen.go +++ b/chain/types/cbor_gen.go @@ -29,24 +29,25 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufBlockHeader); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufBlockHeader); err != nil { return err } - scratch := make([]byte, 9) - // t.Miner (address.Address) (struct) - if err := t.Miner.MarshalCBOR(w); err != nil { + if err := t.Miner.MarshalCBOR(cw); err != nil { return err } // t.Ticket (types.Ticket) (struct) - if err := t.Ticket.MarshalCBOR(w); err != nil { + if err := t.Ticket.MarshalCBOR(cw); err != nil { return err } // t.ElectionProof (types.ElectionProof) (struct) - if err := t.ElectionProof.MarshalCBOR(w); err != nil { + if err := t.ElectionProof.MarshalCBOR(cw); err != nil { return err } @@ -55,11 +56,11 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.BeaconEntries was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.BeaconEntries))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.BeaconEntries))); err != nil { return err } for _, v := range t.BeaconEntries { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -69,11 +70,11 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.WinPoStProof was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.WinPoStProof))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.WinPoStProof))); err != nil { return err } for _, v := range t.WinPoStProof { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -83,88 +84,93 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Parents was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Parents))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Parents))); err != nil { return err } for _, v := range t.Parents { - if err := cbg.WriteCidBuf(scratch, w, v); err != nil { + if err := cbg.WriteCid(w, v); err != nil { return xerrors.Errorf("failed writing cid field t.Parents: %w", err) } } // t.ParentWeight (big.Int) (struct) - if err := t.ParentWeight.MarshalCBOR(w); err != nil { + if err := t.ParentWeight.MarshalCBOR(cw); err != nil { return err } // t.Height (abi.ChainEpoch) (int64) if t.Height >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Height)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Height)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Height-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Height-1)); err != nil { return err } } // t.ParentStateRoot (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.ParentStateRoot); err != nil { + if err := cbg.WriteCid(cw, t.ParentStateRoot); err != nil { return xerrors.Errorf("failed to write cid field t.ParentStateRoot: %w", err) } // t.ParentMessageReceipts (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.ParentMessageReceipts); err != nil { + if err := cbg.WriteCid(cw, t.ParentMessageReceipts); err != nil { return xerrors.Errorf("failed to write cid field t.ParentMessageReceipts: %w", err) } // t.Messages (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.Messages); err != nil { + if err := cbg.WriteCid(cw, t.Messages); err != nil { return xerrors.Errorf("failed to write cid field t.Messages: %w", err) } // t.BLSAggregate (crypto.Signature) (struct) - if err := t.BLSAggregate.MarshalCBOR(w); err != nil { + if err := t.BLSAggregate.MarshalCBOR(cw); err != nil { return err } // t.Timestamp (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Timestamp)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Timestamp)); err != nil { return err } // t.BlockSig (crypto.Signature) (struct) - if err := t.BlockSig.MarshalCBOR(w); err != nil { + if err := t.BlockSig.MarshalCBOR(cw); err != nil { return err } // t.ForkSignaling (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.ForkSignaling)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.ForkSignaling)); err != nil { return err } // t.ParentBaseFee (big.Int) (struct) - if err := t.ParentBaseFee.MarshalCBOR(w); err != nil { + if err := t.ParentBaseFee.MarshalCBOR(cw); err != nil { return err } return nil } -func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { +func (t *BlockHeader) UnmarshalCBOR(r io.Reader) (err error) { *t = BlockHeader{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -177,7 +183,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - if err := t.Miner.UnmarshalCBOR(br); err != nil { + if err := t.Miner.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Miner: %w", err) } @@ -186,16 +192,16 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Ticket = new(Ticket) - if err := t.Ticket.UnmarshalCBOR(br); err != nil { + if err := t.Ticket.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Ticket pointer: %w", err) } } @@ -205,16 +211,16 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.ElectionProof = new(ElectionProof) - if err := t.ElectionProof.UnmarshalCBOR(br); err != nil { + if err := t.ElectionProof.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.ElectionProof pointer: %w", err) } } @@ -222,7 +228,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { } // t.BeaconEntries ([]types.BeaconEntry) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -242,7 +248,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v BeaconEntry - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -251,7 +257,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { // t.WinPoStProof ([]proof.PoStProof) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -271,7 +277,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v proof.PoStProof - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -280,7 +286,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { // t.Parents ([]cid.Cid) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -299,7 +305,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("reading cid field t.Parents failed: %w", err) } @@ -310,14 +316,14 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - if err := t.ParentWeight.UnmarshalCBOR(br); err != nil { + if err := t.ParentWeight.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.ParentWeight: %w", err) } } // t.Height (abi.ChainEpoch) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -344,7 +350,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.ParentStateRoot: %w", err) } @@ -356,7 +362,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.ParentMessageReceipts: %w", err) } @@ -368,7 +374,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.Messages: %w", err) } @@ -380,16 +386,16 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.BLSAggregate = new(crypto.Signature) - if err := t.BLSAggregate.UnmarshalCBOR(br); err != nil { + if err := t.BLSAggregate.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.BLSAggregate pointer: %w", err) } } @@ -399,7 +405,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -413,16 +419,16 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.BlockSig = new(crypto.Signature) - if err := t.BlockSig.UnmarshalCBOR(br); err != nil { + if err := t.BlockSig.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.BlockSig pointer: %w", err) } } @@ -432,7 +438,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -446,7 +452,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error { { - if err := t.ParentBaseFee.UnmarshalCBOR(br); err != nil { + if err := t.ParentBaseFee.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.ParentBaseFee: %w", err) } @@ -461,37 +467,43 @@ func (t *Ticket) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufTicket); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufTicket); err != nil { return err } - scratch := make([]byte, 9) - // t.VRFProof ([]uint8) (slice) if len(t.VRFProof) > cbg.ByteArrayMaxLen { return xerrors.Errorf("Byte array in field t.VRFProof was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.VRFProof))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.VRFProof))); err != nil { return err } - if _, err := w.Write(t.VRFProof[:]); err != nil { + if _, err := cw.Write(t.VRFProof[:]); err != nil { return err } return nil } -func (t *Ticket) UnmarshalCBOR(r io.Reader) error { +func (t *Ticket) UnmarshalCBOR(r io.Reader) (err error) { *t = Ticket{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -502,7 +514,7 @@ func (t *Ticket) UnmarshalCBOR(r io.Reader) error { // t.VRFProof ([]uint8) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -518,7 +530,7 @@ func (t *Ticket) UnmarshalCBOR(r io.Reader) error { t.VRFProof = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.VRFProof[:]); err != nil { + if _, err := io.ReadFull(cr, t.VRFProof[:]); err != nil { return err } return nil @@ -531,19 +543,20 @@ func (t *ElectionProof) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufElectionProof); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufElectionProof); err != nil { return err } - scratch := make([]byte, 9) - // t.WinCount (int64) (int64) if t.WinCount >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.WinCount)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.WinCount)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.WinCount-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.WinCount-1)); err != nil { return err } } @@ -553,26 +566,31 @@ func (t *ElectionProof) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.VRFProof was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.VRFProof))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.VRFProof))); err != nil { return err } - if _, err := w.Write(t.VRFProof[:]); err != nil { + if _, err := cw.Write(t.VRFProof[:]); err != nil { return err } return nil } -func (t *ElectionProof) UnmarshalCBOR(r io.Reader) error { +func (t *ElectionProof) UnmarshalCBOR(r io.Reader) (err error) { *t = ElectionProof{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -583,7 +601,7 @@ func (t *ElectionProof) UnmarshalCBOR(r io.Reader) error { // t.WinCount (int64) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -608,7 +626,7 @@ func (t *ElectionProof) UnmarshalCBOR(r io.Reader) error { } // t.VRFProof ([]uint8) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -624,7 +642,7 @@ func (t *ElectionProof) UnmarshalCBOR(r io.Reader) error { t.VRFProof = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.VRFProof[:]); err != nil { + if _, err := io.ReadFull(cr, t.VRFProof[:]); err != nil { return err } return nil @@ -637,63 +655,64 @@ func (t *Message) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufMessage); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufMessage); err != nil { return err } - scratch := make([]byte, 9) - // t.Version (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Version)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Version)); err != nil { return err } // t.To (address.Address) (struct) - if err := t.To.MarshalCBOR(w); err != nil { + if err := t.To.MarshalCBOR(cw); err != nil { return err } // t.From (address.Address) (struct) - if err := t.From.MarshalCBOR(w); err != nil { + if err := t.From.MarshalCBOR(cw); err != nil { return err } // t.Nonce (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Nonce)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Nonce)); err != nil { return err } // t.Value (big.Int) (struct) - if err := t.Value.MarshalCBOR(w); err != nil { + if err := t.Value.MarshalCBOR(cw); err != nil { return err } // t.GasLimit (int64) (int64) if t.GasLimit >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.GasLimit)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.GasLimit)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.GasLimit-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.GasLimit-1)); err != nil { return err } } // t.GasFeeCap (big.Int) (struct) - if err := t.GasFeeCap.MarshalCBOR(w); err != nil { + if err := t.GasFeeCap.MarshalCBOR(cw); err != nil { return err } // t.GasPremium (big.Int) (struct) - if err := t.GasPremium.MarshalCBOR(w); err != nil { + if err := t.GasPremium.MarshalCBOR(cw); err != nil { return err } // t.Method (abi.MethodNum) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Method)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Method)); err != nil { return err } @@ -702,26 +721,31 @@ func (t *Message) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Params was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Params))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Params))); err != nil { return err } - if _, err := w.Write(t.Params[:]); err != nil { + if _, err := cw.Write(t.Params[:]); err != nil { return err } return nil } -func (t *Message) UnmarshalCBOR(r io.Reader) error { +func (t *Message) UnmarshalCBOR(r io.Reader) (err error) { *t = Message{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -734,7 +758,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -748,7 +772,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { { - if err := t.To.UnmarshalCBOR(br); err != nil { + if err := t.To.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.To: %w", err) } @@ -757,7 +781,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { { - if err := t.From.UnmarshalCBOR(br); err != nil { + if err := t.From.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.From: %w", err) } @@ -766,7 +790,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -780,14 +804,14 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { { - if err := t.Value.UnmarshalCBOR(br); err != nil { + if err := t.Value.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Value: %w", err) } } // t.GasLimit (int64) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -814,7 +838,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { { - if err := t.GasFeeCap.UnmarshalCBOR(br); err != nil { + if err := t.GasFeeCap.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.GasFeeCap: %w", err) } @@ -823,7 +847,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { { - if err := t.GasPremium.UnmarshalCBOR(br); err != nil { + if err := t.GasPremium.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.GasPremium: %w", err) } @@ -832,7 +856,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -844,7 +868,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { } // t.Params ([]uint8) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -860,7 +884,7 @@ func (t *Message) UnmarshalCBOR(r io.Reader) error { t.Params = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Params[:]); err != nil { + if _, err := io.ReadFull(cr, t.Params[:]); err != nil { return err } return nil @@ -873,32 +897,40 @@ func (t *SignedMessage) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufSignedMessage); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufSignedMessage); err != nil { return err } // t.Message (types.Message) (struct) - if err := t.Message.MarshalCBOR(w); err != nil { + if err := t.Message.MarshalCBOR(cw); err != nil { return err } // t.Signature (crypto.Signature) (struct) - if err := t.Signature.MarshalCBOR(w); err != nil { + if err := t.Signature.MarshalCBOR(cw); err != nil { return err } return nil } -func (t *SignedMessage) UnmarshalCBOR(r io.Reader) error { +func (t *SignedMessage) UnmarshalCBOR(r io.Reader) (err error) { *t = SignedMessage{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -911,7 +943,7 @@ func (t *SignedMessage) UnmarshalCBOR(r io.Reader) error { { - if err := t.Message.UnmarshalCBOR(br); err != nil { + if err := t.Message.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Message: %w", err) } @@ -920,7 +952,7 @@ func (t *SignedMessage) UnmarshalCBOR(r io.Reader) error { { - if err := t.Signature.UnmarshalCBOR(br); err != nil { + if err := t.Signature.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Signature: %w", err) } @@ -935,37 +967,43 @@ func (t *MsgMeta) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufMsgMeta); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufMsgMeta); err != nil { return err } - scratch := make([]byte, 9) - // t.BlsMessages (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.BlsMessages); err != nil { + if err := cbg.WriteCid(cw, t.BlsMessages); err != nil { return xerrors.Errorf("failed to write cid field t.BlsMessages: %w", err) } // t.SecpkMessages (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.SecpkMessages); err != nil { + if err := cbg.WriteCid(cw, t.SecpkMessages); err != nil { return xerrors.Errorf("failed to write cid field t.SecpkMessages: %w", err) } return nil } -func (t *MsgMeta) UnmarshalCBOR(r io.Reader) error { +func (t *MsgMeta) UnmarshalCBOR(r io.Reader) (err error) { *t = MsgMeta{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -978,7 +1016,7 @@ func (t *MsgMeta) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.BlsMessages: %w", err) } @@ -990,7 +1028,7 @@ func (t *MsgMeta) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.SecpkMessages: %w", err) } @@ -1008,47 +1046,53 @@ func (t *Actor) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufActor); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufActor); err != nil { return err } - scratch := make([]byte, 9) - // t.Code (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.Code); err != nil { + if err := cbg.WriteCid(cw, t.Code); err != nil { return xerrors.Errorf("failed to write cid field t.Code: %w", err) } // t.Head (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.Head); err != nil { + if err := cbg.WriteCid(cw, t.Head); err != nil { return xerrors.Errorf("failed to write cid field t.Head: %w", err) } // t.Nonce (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Nonce)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Nonce)); err != nil { return err } // t.Balance (big.Int) (struct) - if err := t.Balance.MarshalCBOR(w); err != nil { + if err := t.Balance.MarshalCBOR(cw); err != nil { return err } return nil } -func (t *Actor) UnmarshalCBOR(r io.Reader) error { +func (t *Actor) UnmarshalCBOR(r io.Reader) (err error) { *t = Actor{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -1061,7 +1105,7 @@ func (t *Actor) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.Code: %w", err) } @@ -1073,7 +1117,7 @@ func (t *Actor) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.Head: %w", err) } @@ -1085,7 +1129,7 @@ func (t *Actor) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1099,7 +1143,7 @@ func (t *Actor) UnmarshalCBOR(r io.Reader) error { { - if err := t.Balance.UnmarshalCBOR(br); err != nil { + if err := t.Balance.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Balance: %w", err) } @@ -1114,19 +1158,20 @@ func (t *MessageReceipt) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufMessageReceipt); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufMessageReceipt); err != nil { return err } - scratch := make([]byte, 9) - // t.ExitCode (exitcode.ExitCode) (int64) if t.ExitCode >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.ExitCode)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.ExitCode)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.ExitCode-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.ExitCode-1)); err != nil { return err } } @@ -1136,37 +1181,42 @@ func (t *MessageReceipt) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Return was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Return))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Return))); err != nil { return err } - if _, err := w.Write(t.Return[:]); err != nil { + if _, err := cw.Write(t.Return[:]); err != nil { return err } // t.GasUsed (int64) (int64) if t.GasUsed >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.GasUsed)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.GasUsed)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.GasUsed-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.GasUsed-1)); err != nil { return err } } return nil } -func (t *MessageReceipt) UnmarshalCBOR(r io.Reader) error { +func (t *MessageReceipt) UnmarshalCBOR(r io.Reader) (err error) { *t = MessageReceipt{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -1177,7 +1227,7 @@ func (t *MessageReceipt) UnmarshalCBOR(r io.Reader) error { // t.ExitCode (exitcode.ExitCode) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1202,7 +1252,7 @@ func (t *MessageReceipt) UnmarshalCBOR(r io.Reader) error { } // t.Return ([]uint8) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1218,12 +1268,12 @@ func (t *MessageReceipt) UnmarshalCBOR(r io.Reader) error { t.Return = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Return[:]); err != nil { + if _, err := io.ReadFull(cr, t.Return[:]); err != nil { return err } // t.GasUsed (int64) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1256,14 +1306,15 @@ func (t *BlockMsg) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufBlockMsg); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufBlockMsg); err != nil { return err } - scratch := make([]byte, 9) - // t.Header (types.BlockHeader) (struct) - if err := t.Header.MarshalCBOR(w); err != nil { + if err := t.Header.MarshalCBOR(cw); err != nil { return err } @@ -1272,11 +1323,11 @@ func (t *BlockMsg) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.BlsMessages was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.BlsMessages))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.BlsMessages))); err != nil { return err } for _, v := range t.BlsMessages { - if err := cbg.WriteCidBuf(scratch, w, v); err != nil { + if err := cbg.WriteCid(w, v); err != nil { return xerrors.Errorf("failed writing cid field t.BlsMessages: %w", err) } } @@ -1286,27 +1337,32 @@ func (t *BlockMsg) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.SecpkMessages was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.SecpkMessages))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.SecpkMessages))); err != nil { return err } for _, v := range t.SecpkMessages { - if err := cbg.WriteCidBuf(scratch, w, v); err != nil { + 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(r io.Reader) error { +func (t *BlockMsg) UnmarshalCBOR(r io.Reader) (err error) { *t = BlockMsg{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -1319,16 +1375,16 @@ func (t *BlockMsg) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Header = new(BlockHeader) - if err := t.Header.UnmarshalCBOR(br); err != nil { + if err := t.Header.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Header pointer: %w", err) } } @@ -1336,7 +1392,7 @@ func (t *BlockMsg) UnmarshalCBOR(r io.Reader) error { } // t.BlsMessages ([]cid.Cid) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1355,7 +1411,7 @@ func (t *BlockMsg) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("reading cid field t.BlsMessages failed: %w", err) } @@ -1364,7 +1420,7 @@ func (t *BlockMsg) UnmarshalCBOR(r io.Reader) error { // t.SecpkMessages ([]cid.Cid) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1383,7 +1439,7 @@ func (t *BlockMsg) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("reading cid field t.SecpkMessages failed: %w", err) } @@ -1400,22 +1456,23 @@ func (t *ExpTipSet) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufExpTipSet); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufExpTipSet); err != nil { return err } - scratch := make([]byte, 9) - // t.Cids ([]cid.Cid) (slice) if len(t.Cids) > cbg.MaxLength { return xerrors.Errorf("Slice value in field t.Cids was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Cids))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Cids))); err != nil { return err } for _, v := range t.Cids { - if err := cbg.WriteCidBuf(scratch, w, v); err != nil { + if err := cbg.WriteCid(w, v); err != nil { return xerrors.Errorf("failed writing cid field t.Cids: %w", err) } } @@ -1425,38 +1482,43 @@ func (t *ExpTipSet) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Blocks was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Blocks))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Blocks))); err != nil { return err } for _, v := range t.Blocks { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } // t.Height (abi.ChainEpoch) (int64) if t.Height >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Height)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Height)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Height-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Height-1)); err != nil { return err } } return nil } -func (t *ExpTipSet) UnmarshalCBOR(r io.Reader) error { +func (t *ExpTipSet) UnmarshalCBOR(r io.Reader) (err error) { *t = ExpTipSet{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -1467,7 +1529,7 @@ func (t *ExpTipSet) UnmarshalCBOR(r io.Reader) error { // t.Cids ([]cid.Cid) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1486,7 +1548,7 @@ func (t *ExpTipSet) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("reading cid field t.Cids failed: %w", err) } @@ -1495,7 +1557,7 @@ func (t *ExpTipSet) UnmarshalCBOR(r io.Reader) error { // t.Blocks ([]*types.BlockHeader) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1515,7 +1577,7 @@ func (t *ExpTipSet) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v BlockHeader - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -1524,7 +1586,7 @@ func (t *ExpTipSet) UnmarshalCBOR(r io.Reader) error { // t.Height (abi.ChainEpoch) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1557,15 +1619,16 @@ func (t *BeaconEntry) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufBeaconEntry); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufBeaconEntry); err != nil { return err } - scratch := make([]byte, 9) - // t.Round (uint64) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Round)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Round)); err != nil { return err } @@ -1574,26 +1637,31 @@ func (t *BeaconEntry) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Data was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Data))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Data))); err != nil { return err } - if _, err := w.Write(t.Data[:]); err != nil { + if _, err := cw.Write(t.Data[:]); err != nil { return err } return nil } -func (t *BeaconEntry) UnmarshalCBOR(r io.Reader) error { +func (t *BeaconEntry) UnmarshalCBOR(r io.Reader) (err error) { *t = BeaconEntry{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -1606,7 +1674,7 @@ func (t *BeaconEntry) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1618,7 +1686,7 @@ func (t *BeaconEntry) UnmarshalCBOR(r io.Reader) error { } // t.Data ([]uint8) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1634,7 +1702,7 @@ func (t *BeaconEntry) UnmarshalCBOR(r io.Reader) error { t.Data = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Data[:]); err != nil { + if _, err := io.ReadFull(cr, t.Data[:]); err != nil { return err } return nil @@ -1647,43 +1715,49 @@ func (t *StateRoot) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufStateRoot); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufStateRoot); err != nil { return err } - scratch := make([]byte, 9) - // t.Version (types.StateTreeVersion) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Version)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Version)); err != nil { return err } // t.Actors (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.Actors); err != nil { + if err := cbg.WriteCid(cw, t.Actors); err != nil { return xerrors.Errorf("failed to write cid field t.Actors: %w", err) } // t.Info (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.Info); err != nil { + if err := cbg.WriteCid(cw, t.Info); err != nil { return xerrors.Errorf("failed to write cid field t.Info: %w", err) } return nil } -func (t *StateRoot) UnmarshalCBOR(r io.Reader) error { +func (t *StateRoot) UnmarshalCBOR(r io.Reader) (err error) { *t = StateRoot{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -1696,7 +1770,7 @@ func (t *StateRoot) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1710,7 +1784,7 @@ func (t *StateRoot) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.Actors: %w", err) } @@ -1722,7 +1796,7 @@ func (t *StateRoot) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.Info: %w", err) } @@ -1740,23 +1814,30 @@ func (t *StateInfo0) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufStateInfo0); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufStateInfo0); err != nil { return err } - return nil } -func (t *StateInfo0) UnmarshalCBOR(r io.Reader) error { +func (t *StateInfo0) UnmarshalCBOR(r io.Reader) (err error) { *t = StateInfo0{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } diff --git a/chain/vm/cbor_gen.go b/chain/vm/cbor_gen.go index 76d8e9d48d5..0e1677c95e2 100644 --- a/chain/vm/cbor_gen.go +++ b/chain/vm/cbor_gen.go @@ -26,19 +26,20 @@ func (t *FvmExecutionTrace) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufFvmExecutionTrace); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufFvmExecutionTrace); err != nil { return err } - scratch := make([]byte, 9) - // t.Msg (types.Message) (struct) - if err := t.Msg.MarshalCBOR(w); err != nil { + if err := t.Msg.MarshalCBOR(cw); err != nil { return err } // t.MsgRct (types.MessageReceipt) (struct) - if err := t.MsgRct.MarshalCBOR(w); err != nil { + if err := t.MsgRct.MarshalCBOR(cw); err != nil { return err } @@ -47,7 +48,7 @@ func (t *FvmExecutionTrace) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Error was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Error))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Error))); err != nil { return err } if _, err := io.WriteString(w, string(t.Error)); err != nil { @@ -59,27 +60,32 @@ func (t *FvmExecutionTrace) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Subcalls was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Subcalls))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Subcalls))); err != nil { return err } for _, v := range t.Subcalls { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } return nil } -func (t *FvmExecutionTrace) UnmarshalCBOR(r io.Reader) error { +func (t *FvmExecutionTrace) UnmarshalCBOR(r io.Reader) (err error) { *t = FvmExecutionTrace{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -92,16 +98,16 @@ func (t *FvmExecutionTrace) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Msg = new(types.Message) - if err := t.Msg.UnmarshalCBOR(br); err != nil { + if err := t.Msg.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Msg pointer: %w", err) } } @@ -111,16 +117,16 @@ func (t *FvmExecutionTrace) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.MsgRct = new(types.MessageReceipt) - if err := t.MsgRct.UnmarshalCBOR(br); err != nil { + if err := t.MsgRct.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.MsgRct pointer: %w", err) } } @@ -129,7 +135,7 @@ func (t *FvmExecutionTrace) UnmarshalCBOR(r io.Reader) error { // t.Error (string) (string) { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -138,7 +144,7 @@ func (t *FvmExecutionTrace) UnmarshalCBOR(r io.Reader) error { } // t.Subcalls ([]vm.FvmExecutionTrace) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -158,7 +164,7 @@ func (t *FvmExecutionTrace) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v FvmExecutionTrace - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } diff --git a/cmd/lotus-shed/shedgen/cbor_gen.go b/cmd/lotus-shed/shedgen/cbor_gen.go index 37ed9553997..a04d52c8e14 100644 --- a/cmd/lotus-shed/shedgen/cbor_gen.go +++ b/cmd/lotus-shed/shedgen/cbor_gen.go @@ -23,18 +23,19 @@ func (t *CarbNode) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{161}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{161}); err != nil { return err } - scratch := make([]byte, 9) - // t.Sub ([]cid.Cid) (slice) if len("Sub") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Sub\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Sub"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Sub"))); err != nil { return err } if _, err := io.WriteString(w, string("Sub")); err != nil { @@ -45,27 +46,32 @@ func (t *CarbNode) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Sub was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Sub))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Sub))); err != nil { return err } for _, v := range t.Sub { - if err := cbg.WriteCidBuf(scratch, w, v); err != nil { + if err := cbg.WriteCid(w, v); err != nil { return xerrors.Errorf("failed writing cid field t.Sub: %w", err) } } return nil } -func (t *CarbNode) UnmarshalCBOR(r io.Reader) error { +func (t *CarbNode) UnmarshalCBOR(r io.Reader) (err error) { *t = CarbNode{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -80,7 +86,7 @@ func (t *CarbNode) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -92,7 +98,7 @@ func (t *CarbNode) UnmarshalCBOR(r io.Reader) error { // t.Sub ([]cid.Cid) (slice) case "Sub": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -111,7 +117,7 @@ func (t *CarbNode) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("reading cid field t.Sub failed: %w", err) } diff --git a/conformance/chaos/cbor_gen.go b/conformance/chaos/cbor_gen.go index d0d1ad62723..436849cdc85 100644 --- a/conformance/chaos/cbor_gen.go +++ b/conformance/chaos/cbor_gen.go @@ -28,18 +28,19 @@ func (t *State) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufState); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufState); err != nil { return err } - scratch := make([]byte, 9) - // t.Value (string) (string) if len(t.Value) > cbg.MaxLength { return xerrors.Errorf("Value in field t.Value was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Value))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Value))); err != nil { return err } if _, err := io.WriteString(w, string(t.Value)); err != nil { @@ -51,27 +52,32 @@ func (t *State) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Unmarshallable was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Unmarshallable))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Unmarshallable))); err != nil { return err } for _, v := range t.Unmarshallable { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } return nil } -func (t *State) UnmarshalCBOR(r io.Reader) error { +func (t *State) UnmarshalCBOR(r io.Reader) (err error) { *t = State{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -83,7 +89,7 @@ func (t *State) UnmarshalCBOR(r io.Reader) error { // t.Value (string) (string) { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -92,7 +98,7 @@ func (t *State) UnmarshalCBOR(r io.Reader) error { } // t.Unmarshallable ([]*chaos.UnmarshallableCBOR) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -112,7 +118,7 @@ func (t *State) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v UnmarshallableCBOR - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -129,19 +135,20 @@ func (t *CallerValidationArgs) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufCallerValidationArgs); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufCallerValidationArgs); err != nil { return err } - scratch := make([]byte, 9) - // t.Branch (chaos.CallerValidationBranch) (int64) if t.Branch >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Branch)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Branch)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Branch-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Branch-1)); err != nil { return err } } @@ -151,11 +158,11 @@ func (t *CallerValidationArgs) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Addrs was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Addrs))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Addrs))); err != nil { return err } for _, v := range t.Addrs { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -165,27 +172,32 @@ func (t *CallerValidationArgs) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Types was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Types))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Types))); err != nil { return err } for _, v := range t.Types { - if err := cbg.WriteCidBuf(scratch, w, v); err != nil { + if err := cbg.WriteCid(w, v); err != nil { return xerrors.Errorf("failed writing cid field t.Types: %w", err) } } return nil } -func (t *CallerValidationArgs) UnmarshalCBOR(r io.Reader) error { +func (t *CallerValidationArgs) UnmarshalCBOR(r io.Reader) (err error) { *t = CallerValidationArgs{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -196,7 +208,7 @@ func (t *CallerValidationArgs) UnmarshalCBOR(r io.Reader) error { // t.Branch (chaos.CallerValidationBranch) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -221,7 +233,7 @@ func (t *CallerValidationArgs) UnmarshalCBOR(r io.Reader) error { } // t.Addrs ([]address.Address) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -241,7 +253,7 @@ func (t *CallerValidationArgs) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v address.Address - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -250,7 +262,7 @@ func (t *CallerValidationArgs) UnmarshalCBOR(r io.Reader) error { // t.Types ([]cid.Cid) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -269,7 +281,7 @@ func (t *CallerValidationArgs) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("reading cid field t.Types failed: %w", err) } @@ -286,12 +298,13 @@ func (t *CreateActorArgs) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufCreateActorArgs); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufCreateActorArgs); err != nil { return err } - scratch := make([]byte, 9) - // t.UndefActorCID (bool) (bool) if err := cbg.WriteBool(w, t.UndefActorCID); err != nil { return err @@ -299,7 +312,7 @@ func (t *CreateActorArgs) MarshalCBOR(w io.Writer) error { // t.ActorCID (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.ActorCID); err != nil { + if err := cbg.WriteCid(cw, t.ActorCID); err != nil { return xerrors.Errorf("failed to write cid field t.ActorCID: %w", err) } @@ -309,22 +322,27 @@ func (t *CreateActorArgs) MarshalCBOR(w io.Writer) error { } // t.Address (address.Address) (struct) - if err := t.Address.MarshalCBOR(w); err != nil { + if err := t.Address.MarshalCBOR(cw); err != nil { return err } return nil } -func (t *CreateActorArgs) UnmarshalCBOR(r io.Reader) error { +func (t *CreateActorArgs) UnmarshalCBOR(r io.Reader) (err error) { *t = CreateActorArgs{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -335,7 +353,7 @@ func (t *CreateActorArgs) UnmarshalCBOR(r io.Reader) error { // t.UndefActorCID (bool) (bool) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -354,7 +372,7 @@ func (t *CreateActorArgs) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.ActorCID: %w", err) } @@ -364,7 +382,7 @@ func (t *CreateActorArgs) UnmarshalCBOR(r io.Reader) error { } // t.UndefAddress (bool) (bool) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -383,7 +401,7 @@ func (t *CreateActorArgs) UnmarshalCBOR(r io.Reader) error { { - if err := t.Address.UnmarshalCBOR(br); err != nil { + if err := t.Address.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Address: %w", err) } @@ -398,12 +416,15 @@ func (t *ResolveAddressResponse) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufResolveAddressResponse); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufResolveAddressResponse); err != nil { return err } // t.Address (address.Address) (struct) - if err := t.Address.MarshalCBOR(w); err != nil { + if err := t.Address.MarshalCBOR(cw); err != nil { return err } @@ -414,16 +435,21 @@ func (t *ResolveAddressResponse) MarshalCBOR(w io.Writer) error { return nil } -func (t *ResolveAddressResponse) UnmarshalCBOR(r io.Reader) error { +func (t *ResolveAddressResponse) UnmarshalCBOR(r io.Reader) (err error) { *t = ResolveAddressResponse{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -436,14 +462,14 @@ func (t *ResolveAddressResponse) UnmarshalCBOR(r io.Reader) error { { - if err := t.Address.UnmarshalCBOR(br); err != nil { + if err := t.Address.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Address: %w", err) } } // t.Success (bool) (bool) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -468,25 +494,26 @@ func (t *SendArgs) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufSendArgs); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufSendArgs); err != nil { return err } - scratch := make([]byte, 9) - // t.To (address.Address) (struct) - if err := t.To.MarshalCBOR(w); err != nil { + if err := t.To.MarshalCBOR(cw); err != nil { return err } // t.Value (big.Int) (struct) - if err := t.Value.MarshalCBOR(w); err != nil { + if err := t.Value.MarshalCBOR(cw); err != nil { return err } // t.Method (abi.MethodNum) (uint64) - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Method)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Method)); err != nil { return err } @@ -495,26 +522,31 @@ func (t *SendArgs) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Params was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Params))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Params))); err != nil { return err } - if _, err := w.Write(t.Params[:]); err != nil { + if _, err := cw.Write(t.Params[:]); err != nil { return err } return nil } -func (t *SendArgs) UnmarshalCBOR(r io.Reader) error { +func (t *SendArgs) UnmarshalCBOR(r io.Reader) (err error) { *t = SendArgs{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -527,7 +559,7 @@ func (t *SendArgs) UnmarshalCBOR(r io.Reader) error { { - if err := t.To.UnmarshalCBOR(br); err != nil { + if err := t.To.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.To: %w", err) } @@ -536,7 +568,7 @@ func (t *SendArgs) UnmarshalCBOR(r io.Reader) error { { - if err := t.Value.UnmarshalCBOR(br); err != nil { + if err := t.Value.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Value: %w", err) } @@ -545,7 +577,7 @@ func (t *SendArgs) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -557,7 +589,7 @@ func (t *SendArgs) UnmarshalCBOR(r io.Reader) error { } // t.Params ([]uint8) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -573,7 +605,7 @@ func (t *SendArgs) UnmarshalCBOR(r io.Reader) error { t.Params = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Params[:]); err != nil { + if _, err := io.ReadFull(cr, t.Params[:]); err != nil { return err } return nil @@ -586,48 +618,54 @@ func (t *SendReturn) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufSendReturn); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufSendReturn); err != nil { return err } - scratch := make([]byte, 9) - // t.Return (builtin.CBORBytes) (slice) if len(t.Return) > cbg.ByteArrayMaxLen { return xerrors.Errorf("Byte array in field t.Return was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Return))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Return))); err != nil { return err } - if _, err := w.Write(t.Return[:]); err != nil { + if _, err := cw.Write(t.Return[:]); err != nil { return err } // t.Code (exitcode.ExitCode) (int64) if t.Code >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Code)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Code)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Code-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Code-1)); err != nil { return err } } return nil } -func (t *SendReturn) UnmarshalCBOR(r io.Reader) error { +func (t *SendReturn) UnmarshalCBOR(r io.Reader) (err error) { *t = SendReturn{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -638,7 +676,7 @@ func (t *SendReturn) UnmarshalCBOR(r io.Reader) error { // t.Return (builtin.CBORBytes) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -654,12 +692,12 @@ func (t *SendReturn) UnmarshalCBOR(r io.Reader) error { t.Return = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Return[:]); err != nil { + if _, err := io.ReadFull(cr, t.Return[:]); err != nil { return err } // t.Code (exitcode.ExitCode) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -692,18 +730,19 @@ func (t *MutateStateArgs) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufMutateStateArgs); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufMutateStateArgs); err != nil { return err } - scratch := make([]byte, 9) - // t.Value (string) (string) if len(t.Value) > cbg.MaxLength { return xerrors.Errorf("Value in field t.Value was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Value))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Value))); err != nil { return err } if _, err := io.WriteString(w, string(t.Value)); err != nil { @@ -712,27 +751,32 @@ func (t *MutateStateArgs) MarshalCBOR(w io.Writer) error { // t.Branch (chaos.MutateStateBranch) (int64) if t.Branch >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Branch)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Branch)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Branch-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Branch-1)); err != nil { return err } } return nil } -func (t *MutateStateArgs) UnmarshalCBOR(r io.Reader) error { +func (t *MutateStateArgs) UnmarshalCBOR(r io.Reader) (err error) { *t = MutateStateArgs{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -744,7 +788,7 @@ func (t *MutateStateArgs) UnmarshalCBOR(r io.Reader) error { // t.Value (string) (string) { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -753,7 +797,7 @@ func (t *MutateStateArgs) UnmarshalCBOR(r io.Reader) error { } // t.Branch (chaos.MutateStateBranch) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -786,19 +830,20 @@ func (t *AbortWithArgs) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufAbortWithArgs); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufAbortWithArgs); err != nil { return err } - scratch := make([]byte, 9) - // t.Code (exitcode.ExitCode) (int64) if t.Code >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Code)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Code)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.Code-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.Code-1)); err != nil { return err } } @@ -808,7 +853,7 @@ func (t *AbortWithArgs) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Message was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Message))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Message))); err != nil { return err } if _, err := io.WriteString(w, string(t.Message)); err != nil { @@ -822,16 +867,21 @@ func (t *AbortWithArgs) MarshalCBOR(w io.Writer) error { return nil } -func (t *AbortWithArgs) UnmarshalCBOR(r io.Reader) error { +func (t *AbortWithArgs) UnmarshalCBOR(r io.Reader) (err error) { *t = AbortWithArgs{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -842,7 +892,7 @@ func (t *AbortWithArgs) UnmarshalCBOR(r io.Reader) error { // t.Code (exitcode.ExitCode) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -868,7 +918,7 @@ func (t *AbortWithArgs) UnmarshalCBOR(r io.Reader) error { // t.Message (string) (string) { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -877,7 +927,7 @@ func (t *AbortWithArgs) UnmarshalCBOR(r io.Reader) error { } // t.Uncontrolled (bool) (bool) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -902,60 +952,66 @@ func (t *InspectRuntimeReturn) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufInspectRuntimeReturn); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufInspectRuntimeReturn); err != nil { return err } - scratch := make([]byte, 9) - // t.Caller (address.Address) (struct) - if err := t.Caller.MarshalCBOR(w); err != nil { + if err := t.Caller.MarshalCBOR(cw); err != nil { return err } // t.Receiver (address.Address) (struct) - if err := t.Receiver.MarshalCBOR(w); err != nil { + if err := t.Receiver.MarshalCBOR(cw); err != nil { return err } // t.ValueReceived (big.Int) (struct) - if err := t.ValueReceived.MarshalCBOR(w); err != nil { + if err := t.ValueReceived.MarshalCBOR(cw); err != nil { return err } // t.CurrEpoch (abi.ChainEpoch) (int64) if t.CurrEpoch >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.CurrEpoch)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.CurrEpoch)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.CurrEpoch-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.CurrEpoch-1)); err != nil { return err } } // t.CurrentBalance (big.Int) (struct) - if err := t.CurrentBalance.MarshalCBOR(w); err != nil { + if err := t.CurrentBalance.MarshalCBOR(cw); err != nil { return err } // t.State (chaos.State) (struct) - if err := t.State.MarshalCBOR(w); err != nil { + if err := t.State.MarshalCBOR(cw); err != nil { return err } return nil } -func (t *InspectRuntimeReturn) UnmarshalCBOR(r io.Reader) error { +func (t *InspectRuntimeReturn) UnmarshalCBOR(r io.Reader) (err error) { *t = InspectRuntimeReturn{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -968,7 +1024,7 @@ func (t *InspectRuntimeReturn) UnmarshalCBOR(r io.Reader) error { { - if err := t.Caller.UnmarshalCBOR(br); err != nil { + if err := t.Caller.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Caller: %w", err) } @@ -977,7 +1033,7 @@ func (t *InspectRuntimeReturn) UnmarshalCBOR(r io.Reader) error { { - if err := t.Receiver.UnmarshalCBOR(br); err != nil { + if err := t.Receiver.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Receiver: %w", err) } @@ -986,14 +1042,14 @@ func (t *InspectRuntimeReturn) UnmarshalCBOR(r io.Reader) error { { - if err := t.ValueReceived.UnmarshalCBOR(br); err != nil { + if err := t.ValueReceived.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.ValueReceived: %w", err) } } // t.CurrEpoch (abi.ChainEpoch) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1020,7 +1076,7 @@ func (t *InspectRuntimeReturn) UnmarshalCBOR(r io.Reader) error { { - if err := t.CurrentBalance.UnmarshalCBOR(br); err != nil { + if err := t.CurrentBalance.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.CurrentBalance: %w", err) } @@ -1029,7 +1085,7 @@ func (t *InspectRuntimeReturn) UnmarshalCBOR(r io.Reader) error { { - if err := t.State.UnmarshalCBOR(br); err != nil { + if err := t.State.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.State: %w", err) } diff --git a/extern/sector-storage/cbor_gen.go b/extern/sector-storage/cbor_gen.go index 623d75b33d8..fd99576ccf4 100644 --- a/extern/sector-storage/cbor_gen.go +++ b/extern/sector-storage/cbor_gen.go @@ -24,25 +24,26 @@ func (t *Call) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{164}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{164}); err != nil { return err } - scratch := make([]byte, 9) - // t.ID (storiface.CallID) (struct) if len("ID") > cbg.MaxLength { return xerrors.Errorf("Value in field \"ID\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("ID"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("ID"))); err != nil { return err } if _, err := io.WriteString(w, string("ID")); err != nil { return err } - if err := t.ID.MarshalCBOR(w); err != nil { + if err := t.ID.MarshalCBOR(cw); err != nil { return err } @@ -51,7 +52,7 @@ func (t *Call) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"RetType\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("RetType"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("RetType"))); err != nil { return err } if _, err := io.WriteString(w, string("RetType")); err != nil { @@ -62,7 +63,7 @@ func (t *Call) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.RetType was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.RetType))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.RetType))); err != nil { return err } if _, err := io.WriteString(w, string(t.RetType)); err != nil { @@ -74,14 +75,14 @@ func (t *Call) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"State\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("State"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("State"))); err != nil { return err } if _, err := io.WriteString(w, string("State")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.State)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.State)); err != nil { return err } @@ -90,29 +91,34 @@ func (t *Call) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Result\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Result"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Result"))); err != nil { return err } if _, err := io.WriteString(w, string("Result")); err != nil { return err } - if err := t.Result.MarshalCBOR(w); err != nil { + if err := t.Result.MarshalCBOR(cw); err != nil { return err } return nil } -func (t *Call) UnmarshalCBOR(r io.Reader) error { +func (t *Call) UnmarshalCBOR(r io.Reader) (err error) { *t = Call{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -127,7 +133,7 @@ func (t *Call) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -141,7 +147,7 @@ func (t *Call) UnmarshalCBOR(r io.Reader) error { { - if err := t.ID.UnmarshalCBOR(br); err != nil { + if err := t.ID.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.ID: %w", err) } @@ -150,7 +156,7 @@ func (t *Call) UnmarshalCBOR(r io.Reader) error { case "RetType": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -162,7 +168,7 @@ func (t *Call) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -177,16 +183,16 @@ func (t *Call) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Result = new(ManyBytes) - if err := t.Result.UnmarshalCBOR(br); err != nil { + if err := t.Result.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Result pointer: %w", err) } } @@ -206,25 +212,26 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{166}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{166}); err != nil { return err } - scratch := make([]byte, 9) - // t.ID (sectorstorage.WorkID) (struct) if len("ID") > cbg.MaxLength { return xerrors.Errorf("Value in field \"ID\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("ID"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("ID"))); err != nil { return err } if _, err := io.WriteString(w, string("ID")); err != nil { return err } - if err := t.ID.MarshalCBOR(w); err != nil { + if err := t.ID.MarshalCBOR(cw); err != nil { return err } @@ -233,7 +240,7 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Status\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Status"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Status"))); err != nil { return err } if _, err := io.WriteString(w, string("Status")); err != nil { @@ -244,7 +251,7 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Status was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Status))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Status))); err != nil { return err } if _, err := io.WriteString(w, string(t.Status)); err != nil { @@ -256,14 +263,14 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"WorkerCall\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("WorkerCall"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("WorkerCall"))); err != nil { return err } if _, err := io.WriteString(w, string("WorkerCall")); err != nil { return err } - if err := t.WorkerCall.MarshalCBOR(w); err != nil { + if err := t.WorkerCall.MarshalCBOR(cw); err != nil { return err } @@ -272,7 +279,7 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"WorkError\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("WorkError"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("WorkError"))); err != nil { return err } if _, err := io.WriteString(w, string("WorkError")); err != nil { @@ -283,7 +290,7 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.WorkError was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.WorkError))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.WorkError))); err != nil { return err } if _, err := io.WriteString(w, string(t.WorkError)); err != nil { @@ -295,7 +302,7 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"WorkerHostname\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("WorkerHostname"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("WorkerHostname"))); err != nil { return err } if _, err := io.WriteString(w, string("WorkerHostname")); err != nil { @@ -306,7 +313,7 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.WorkerHostname was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.WorkerHostname))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.WorkerHostname))); err != nil { return err } if _, err := io.WriteString(w, string(t.WorkerHostname)); err != nil { @@ -318,7 +325,7 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"StartTime\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("StartTime"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("StartTime"))); err != nil { return err } if _, err := io.WriteString(w, string("StartTime")); err != nil { @@ -326,27 +333,32 @@ func (t *WorkState) MarshalCBOR(w io.Writer) error { } if t.StartTime >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.StartTime)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.StartTime)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.StartTime-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.StartTime-1)); err != nil { return err } } return nil } -func (t *WorkState) UnmarshalCBOR(r io.Reader) error { +func (t *WorkState) UnmarshalCBOR(r io.Reader) (err error) { *t = WorkState{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -361,7 +373,7 @@ func (t *WorkState) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -375,7 +387,7 @@ func (t *WorkState) UnmarshalCBOR(r io.Reader) error { { - if err := t.ID.UnmarshalCBOR(br); err != nil { + if err := t.ID.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.ID: %w", err) } @@ -384,7 +396,7 @@ func (t *WorkState) UnmarshalCBOR(r io.Reader) error { case "Status": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -396,7 +408,7 @@ func (t *WorkState) UnmarshalCBOR(r io.Reader) error { { - if err := t.WorkerCall.UnmarshalCBOR(br); err != nil { + if err := t.WorkerCall.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.WorkerCall: %w", err) } @@ -405,7 +417,7 @@ func (t *WorkState) UnmarshalCBOR(r io.Reader) error { case "WorkError": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -416,7 +428,7 @@ func (t *WorkState) UnmarshalCBOR(r io.Reader) error { case "WorkerHostname": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -426,7 +438,7 @@ func (t *WorkState) UnmarshalCBOR(r io.Reader) error { // t.StartTime (int64) (int64) case "StartTime": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -463,18 +475,19 @@ func (t *WorkID) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{162}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{162}); err != nil { return err } - scratch := make([]byte, 9) - // t.Method (sealtasks.TaskType) (string) if len("Method") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Method\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Method"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Method"))); err != nil { return err } if _, err := io.WriteString(w, string("Method")); err != nil { @@ -485,7 +498,7 @@ func (t *WorkID) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Method was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Method))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Method))); err != nil { return err } if _, err := io.WriteString(w, string(t.Method)); err != nil { @@ -497,7 +510,7 @@ func (t *WorkID) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Params\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Params"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Params"))); err != nil { return err } if _, err := io.WriteString(w, string("Params")); err != nil { @@ -508,7 +521,7 @@ func (t *WorkID) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Params was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Params))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Params))); err != nil { return err } if _, err := io.WriteString(w, string(t.Params)); err != nil { @@ -517,16 +530,21 @@ func (t *WorkID) MarshalCBOR(w io.Writer) error { return nil } -func (t *WorkID) UnmarshalCBOR(r io.Reader) error { +func (t *WorkID) UnmarshalCBOR(r io.Reader) (err error) { *t = WorkID{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -541,7 +559,7 @@ func (t *WorkID) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -554,7 +572,7 @@ func (t *WorkID) UnmarshalCBOR(r io.Reader) error { case "Method": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -565,7 +583,7 @@ func (t *WorkID) UnmarshalCBOR(r io.Reader) error { case "Params": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } diff --git a/extern/sector-storage/storiface/cbor_gen.go b/extern/sector-storage/storiface/cbor_gen.go index 44b5ae9ee54..5b46231750f 100644 --- a/extern/sector-storage/storiface/cbor_gen.go +++ b/extern/sector-storage/storiface/cbor_gen.go @@ -23,25 +23,26 @@ func (t *CallID) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{162}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{162}); err != nil { return err } - scratch := make([]byte, 9) - // t.Sector (abi.SectorID) (struct) if len("Sector") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Sector\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Sector"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Sector"))); err != nil { return err } if _, err := io.WriteString(w, string("Sector")); err != nil { return err } - if err := t.Sector.MarshalCBOR(w); err != nil { + if err := t.Sector.MarshalCBOR(cw); err != nil { return err } @@ -50,7 +51,7 @@ func (t *CallID) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"ID\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("ID"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("ID"))); err != nil { return err } if _, err := io.WriteString(w, string("ID")); err != nil { @@ -61,26 +62,31 @@ func (t *CallID) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.ID was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.ID))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.ID))); err != nil { return err } - if _, err := w.Write(t.ID[:]); err != nil { + if _, err := cw.Write(t.ID[:]); err != nil { return err } return nil } -func (t *CallID) UnmarshalCBOR(r io.Reader) error { +func (t *CallID) UnmarshalCBOR(r io.Reader) (err error) { *t = CallID{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -95,7 +101,7 @@ func (t *CallID) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -109,7 +115,7 @@ func (t *CallID) UnmarshalCBOR(r io.Reader) error { { - if err := t.Sector.UnmarshalCBOR(br); err != nil { + if err := t.Sector.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Sector: %w", err) } @@ -117,7 +123,7 @@ func (t *CallID) UnmarshalCBOR(r io.Reader) error { // t.ID (uuid.UUID) (array) case "ID": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -135,7 +141,7 @@ func (t *CallID) UnmarshalCBOR(r io.Reader) error { t.ID = [16]uint8{} - if _, err := io.ReadFull(br, t.ID[:]); err != nil { + if _, err := io.ReadFull(cr, t.ID[:]); err != nil { return err } diff --git a/extern/storage-sealing/cbor_gen.go b/extern/storage-sealing/cbor_gen.go index c1e2b08fa4c..8794fdf4d0e 100644 --- a/extern/storage-sealing/cbor_gen.go +++ b/extern/storage-sealing/cbor_gen.go @@ -26,25 +26,26 @@ func (t *Piece) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{162}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{162}); err != nil { return err } - scratch := make([]byte, 9) - // t.Piece (abi.PieceInfo) (struct) if len("Piece") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Piece\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Piece"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Piece"))); err != nil { return err } if _, err := io.WriteString(w, string("Piece")); err != nil { return err } - if err := t.Piece.MarshalCBOR(w); err != nil { + if err := t.Piece.MarshalCBOR(cw); err != nil { return err } @@ -53,29 +54,34 @@ func (t *Piece) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"DealInfo\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("DealInfo"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("DealInfo"))); err != nil { return err } if _, err := io.WriteString(w, string("DealInfo")); err != nil { return err } - if err := t.DealInfo.MarshalCBOR(w); err != nil { + if err := t.DealInfo.MarshalCBOR(cw); err != nil { return err } return nil } -func (t *Piece) UnmarshalCBOR(r io.Reader) error { +func (t *Piece) UnmarshalCBOR(r io.Reader) (err error) { *t = Piece{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -90,7 +96,7 @@ func (t *Piece) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -104,7 +110,7 @@ func (t *Piece) UnmarshalCBOR(r io.Reader) error { { - if err := t.Piece.UnmarshalCBOR(br); err != nil { + if err := t.Piece.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Piece: %w", err) } @@ -114,16 +120,16 @@ func (t *Piece) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.DealInfo = new(api.PieceDealInfo) - if err := t.DealInfo.UnmarshalCBOR(br); err != nil { + if err := t.DealInfo.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.DealInfo pointer: %w", err) } } @@ -143,18 +149,19 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{184, 32}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{184, 32}); err != nil { return err } - scratch := make([]byte, 9) - // t.State (sealing.SectorState) (string) if len("State") > cbg.MaxLength { return xerrors.Errorf("Value in field \"State\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("State"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("State"))); err != nil { return err } if _, err := io.WriteString(w, string("State")); err != nil { @@ -165,7 +172,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.State was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.State))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.State))); err != nil { return err } if _, err := io.WriteString(w, string(t.State)); err != nil { @@ -177,14 +184,14 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"SectorNumber\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("SectorNumber"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("SectorNumber"))); err != nil { return err } if _, err := io.WriteString(w, string("SectorNumber")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.SectorNumber)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.SectorNumber)); err != nil { return err } @@ -193,7 +200,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"SectorType\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("SectorType"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("SectorType"))); err != nil { return err } if _, err := io.WriteString(w, string("SectorType")); err != nil { @@ -201,11 +208,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.SectorType >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.SectorType)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.SectorType)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.SectorType-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.SectorType-1)); err != nil { return err } } @@ -215,7 +222,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"CreationTime\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("CreationTime"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("CreationTime"))); err != nil { return err } if _, err := io.WriteString(w, string("CreationTime")); err != nil { @@ -223,11 +230,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.CreationTime >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.CreationTime)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.CreationTime)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.CreationTime-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.CreationTime-1)); err != nil { return err } } @@ -237,7 +244,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Pieces\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Pieces"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Pieces"))); err != nil { return err } if _, err := io.WriteString(w, string("Pieces")); err != nil { @@ -248,11 +255,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Pieces was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Pieces))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Pieces))); err != nil { return err } for _, v := range t.Pieces { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -262,7 +269,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"TicketValue\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("TicketValue"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("TicketValue"))); err != nil { return err } if _, err := io.WriteString(w, string("TicketValue")); err != nil { @@ -273,11 +280,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.TicketValue was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.TicketValue))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.TicketValue))); err != nil { return err } - if _, err := w.Write(t.TicketValue[:]); err != nil { + if _, err := cw.Write(t.TicketValue[:]); err != nil { return err } @@ -286,7 +293,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"TicketEpoch\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("TicketEpoch"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("TicketEpoch"))); err != nil { return err } if _, err := io.WriteString(w, string("TicketEpoch")); err != nil { @@ -294,11 +301,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.TicketEpoch >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.TicketEpoch)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.TicketEpoch)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.TicketEpoch-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.TicketEpoch-1)); err != nil { return err } } @@ -308,7 +315,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"PreCommit1Out\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("PreCommit1Out"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("PreCommit1Out"))); err != nil { return err } if _, err := io.WriteString(w, string("PreCommit1Out")); err != nil { @@ -319,11 +326,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.PreCommit1Out was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.PreCommit1Out))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.PreCommit1Out))); err != nil { return err } - if _, err := w.Write(t.PreCommit1Out[:]); err != nil { + if _, err := cw.Write(t.PreCommit1Out[:]); err != nil { return err } @@ -332,7 +339,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"CommD\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("CommD"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("CommD"))); err != nil { return err } if _, err := io.WriteString(w, string("CommD")); err != nil { @@ -340,11 +347,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.CommD == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.CommD); err != nil { + if err := cbg.WriteCid(cw, *t.CommD); err != nil { return xerrors.Errorf("failed to write cid field t.CommD: %w", err) } } @@ -354,7 +361,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"CommR\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("CommR"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("CommR"))); err != nil { return err } if _, err := io.WriteString(w, string("CommR")); err != nil { @@ -362,11 +369,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.CommR == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.CommR); err != nil { + if err := cbg.WriteCid(cw, *t.CommR); err != nil { return xerrors.Errorf("failed to write cid field t.CommR: %w", err) } } @@ -376,7 +383,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Proof\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Proof"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Proof"))); err != nil { return err } if _, err := io.WriteString(w, string("Proof")); err != nil { @@ -387,11 +394,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Proof was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Proof))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Proof))); err != nil { return err } - if _, err := w.Write(t.Proof[:]); err != nil { + if _, err := cw.Write(t.Proof[:]); err != nil { return err } @@ -400,14 +407,14 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"PreCommitInfo\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("PreCommitInfo"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("PreCommitInfo"))); err != nil { return err } if _, err := io.WriteString(w, string("PreCommitInfo")); err != nil { return err } - if err := t.PreCommitInfo.MarshalCBOR(w); err != nil { + if err := t.PreCommitInfo.MarshalCBOR(cw); err != nil { return err } @@ -416,14 +423,14 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"PreCommitDeposit\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("PreCommitDeposit"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("PreCommitDeposit"))); err != nil { return err } if _, err := io.WriteString(w, string("PreCommitDeposit")); err != nil { return err } - if err := t.PreCommitDeposit.MarshalCBOR(w); err != nil { + if err := t.PreCommitDeposit.MarshalCBOR(cw); err != nil { return err } @@ -432,7 +439,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"PreCommitMessage\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("PreCommitMessage"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("PreCommitMessage"))); err != nil { return err } if _, err := io.WriteString(w, string("PreCommitMessage")); err != nil { @@ -440,11 +447,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.PreCommitMessage == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.PreCommitMessage); err != nil { + if err := cbg.WriteCid(cw, *t.PreCommitMessage); err != nil { return xerrors.Errorf("failed to write cid field t.PreCommitMessage: %w", err) } } @@ -454,7 +461,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"PreCommitTipSet\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("PreCommitTipSet"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("PreCommitTipSet"))); err != nil { return err } if _, err := io.WriteString(w, string("PreCommitTipSet")); err != nil { @@ -465,11 +472,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.PreCommitTipSet was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.PreCommitTipSet))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.PreCommitTipSet))); err != nil { return err } - if _, err := w.Write(t.PreCommitTipSet[:]); err != nil { + if _, err := cw.Write(t.PreCommitTipSet[:]); err != nil { return err } @@ -478,14 +485,14 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"PreCommit2Fails\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("PreCommit2Fails"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("PreCommit2Fails"))); err != nil { return err } if _, err := io.WriteString(w, string("PreCommit2Fails")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.PreCommit2Fails)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.PreCommit2Fails)); err != nil { return err } @@ -494,7 +501,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"SeedValue\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("SeedValue"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("SeedValue"))); err != nil { return err } if _, err := io.WriteString(w, string("SeedValue")); err != nil { @@ -505,11 +512,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.SeedValue was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.SeedValue))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.SeedValue))); err != nil { return err } - if _, err := w.Write(t.SeedValue[:]); err != nil { + if _, err := cw.Write(t.SeedValue[:]); err != nil { return err } @@ -518,7 +525,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"SeedEpoch\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("SeedEpoch"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("SeedEpoch"))); err != nil { return err } if _, err := io.WriteString(w, string("SeedEpoch")); err != nil { @@ -526,11 +533,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.SeedEpoch >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.SeedEpoch)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.SeedEpoch)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.SeedEpoch-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.SeedEpoch-1)); err != nil { return err } } @@ -540,7 +547,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"CommitMessage\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("CommitMessage"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("CommitMessage"))); err != nil { return err } if _, err := io.WriteString(w, string("CommitMessage")); err != nil { @@ -548,11 +555,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.CommitMessage == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.CommitMessage); err != nil { + if err := cbg.WriteCid(cw, *t.CommitMessage); err != nil { return xerrors.Errorf("failed to write cid field t.CommitMessage: %w", err) } } @@ -562,14 +569,14 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"InvalidProofs\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("InvalidProofs"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("InvalidProofs"))); err != nil { return err } if _, err := io.WriteString(w, string("InvalidProofs")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.InvalidProofs)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.InvalidProofs)); err != nil { return err } @@ -578,7 +585,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"CCUpdate\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("CCUpdate"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("CCUpdate"))); err != nil { return err } if _, err := io.WriteString(w, string("CCUpdate")); err != nil { @@ -594,7 +601,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"CCPieces\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("CCPieces"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("CCPieces"))); err != nil { return err } if _, err := io.WriteString(w, string("CCPieces")); err != nil { @@ -605,11 +612,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.CCPieces was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.CCPieces))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.CCPieces))); err != nil { return err } for _, v := range t.CCPieces { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -619,7 +626,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"UpdateSealed\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("UpdateSealed"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("UpdateSealed"))); err != nil { return err } if _, err := io.WriteString(w, string("UpdateSealed")); err != nil { @@ -627,11 +634,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.UpdateSealed == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.UpdateSealed); err != nil { + if err := cbg.WriteCid(cw, *t.UpdateSealed); err != nil { return xerrors.Errorf("failed to write cid field t.UpdateSealed: %w", err) } } @@ -641,7 +648,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"UpdateUnsealed\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("UpdateUnsealed"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("UpdateUnsealed"))); err != nil { return err } if _, err := io.WriteString(w, string("UpdateUnsealed")); err != nil { @@ -649,11 +656,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.UpdateUnsealed == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.UpdateUnsealed); err != nil { + if err := cbg.WriteCid(cw, *t.UpdateUnsealed); err != nil { return xerrors.Errorf("failed to write cid field t.UpdateUnsealed: %w", err) } } @@ -663,7 +670,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"ReplicaUpdateProof\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("ReplicaUpdateProof"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("ReplicaUpdateProof"))); err != nil { return err } if _, err := io.WriteString(w, string("ReplicaUpdateProof")); err != nil { @@ -674,11 +681,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.ReplicaUpdateProof was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.ReplicaUpdateProof))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.ReplicaUpdateProof))); err != nil { return err } - if _, err := w.Write(t.ReplicaUpdateProof[:]); err != nil { + if _, err := cw.Write(t.ReplicaUpdateProof[:]); err != nil { return err } @@ -687,7 +694,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"ReplicaUpdateMessage\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("ReplicaUpdateMessage"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("ReplicaUpdateMessage"))); err != nil { return err } if _, err := io.WriteString(w, string("ReplicaUpdateMessage")); err != nil { @@ -695,11 +702,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.ReplicaUpdateMessage == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.ReplicaUpdateMessage); err != nil { + if err := cbg.WriteCid(cw, *t.ReplicaUpdateMessage); err != nil { return xerrors.Errorf("failed to write cid field t.ReplicaUpdateMessage: %w", err) } } @@ -709,7 +716,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"FaultReportMsg\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("FaultReportMsg"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("FaultReportMsg"))); err != nil { return err } if _, err := io.WriteString(w, string("FaultReportMsg")); err != nil { @@ -717,11 +724,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.FaultReportMsg == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.FaultReportMsg); err != nil { + if err := cbg.WriteCid(cw, *t.FaultReportMsg); err != nil { return xerrors.Errorf("failed to write cid field t.FaultReportMsg: %w", err) } } @@ -731,7 +738,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Return\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Return"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Return"))); err != nil { return err } if _, err := io.WriteString(w, string("Return")); err != nil { @@ -742,7 +749,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Return was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Return))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Return))); err != nil { return err } if _, err := io.WriteString(w, string(t.Return)); err != nil { @@ -754,7 +761,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"TerminateMessage\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("TerminateMessage"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("TerminateMessage"))); err != nil { return err } if _, err := io.WriteString(w, string("TerminateMessage")); err != nil { @@ -762,11 +769,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.TerminateMessage == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.TerminateMessage); err != nil { + if err := cbg.WriteCid(cw, *t.TerminateMessage); err != nil { return xerrors.Errorf("failed to write cid field t.TerminateMessage: %w", err) } } @@ -776,7 +783,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"TerminatedAt\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("TerminatedAt"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("TerminatedAt"))); err != nil { return err } if _, err := io.WriteString(w, string("TerminatedAt")); err != nil { @@ -784,11 +791,11 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { } if t.TerminatedAt >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.TerminatedAt)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.TerminatedAt)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.TerminatedAt-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.TerminatedAt-1)); err != nil { return err } } @@ -798,7 +805,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"LastErr\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("LastErr"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("LastErr"))); err != nil { return err } if _, err := io.WriteString(w, string("LastErr")); err != nil { @@ -809,7 +816,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.LastErr was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.LastErr))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.LastErr))); err != nil { return err } if _, err := io.WriteString(w, string(t.LastErr)); err != nil { @@ -821,7 +828,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Log\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Log"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Log"))); err != nil { return err } if _, err := io.WriteString(w, string("Log")); err != nil { @@ -832,27 +839,32 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Log was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Log))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Log))); err != nil { return err } for _, v := range t.Log { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } return nil } -func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { +func (t *SectorInfo) UnmarshalCBOR(r io.Reader) (err error) { *t = SectorInfo{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -867,7 +879,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -880,7 +892,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { case "State": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -892,7 +904,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -905,7 +917,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.SectorType (abi.RegisteredSealProof) (int64) case "SectorType": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -931,7 +943,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.CreationTime (int64) (int64) case "CreationTime": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -957,7 +969,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.Pieces ([]sealing.Piece) (slice) case "Pieces": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -977,7 +989,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v Piece - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -987,7 +999,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.TicketValue (abi.SealRandomness) (slice) case "TicketValue": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1003,13 +1015,13 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { t.TicketValue = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.TicketValue[:]); err != nil { + if _, err := io.ReadFull(cr, t.TicketValue[:]); err != nil { return err } // t.TicketEpoch (abi.ChainEpoch) (int64) case "TicketEpoch": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1035,7 +1047,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.PreCommit1Out (storage.PreCommit1Out) (slice) case "PreCommit1Out": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1051,7 +1063,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { t.PreCommit1Out = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.PreCommit1Out[:]); err != nil { + if _, err := io.ReadFull(cr, t.PreCommit1Out[:]); err != nil { return err } // t.CommD (cid.Cid) (struct) @@ -1059,16 +1071,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.CommD: %w", err) } @@ -1082,16 +1094,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.CommR: %w", err) } @@ -1103,7 +1115,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.Proof ([]uint8) (slice) case "Proof": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1119,7 +1131,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { t.Proof = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Proof[:]); err != nil { + if _, err := io.ReadFull(cr, t.Proof[:]); err != nil { return err } // t.PreCommitInfo (miner.SectorPreCommitInfo) (struct) @@ -1127,16 +1139,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.PreCommitInfo = new(miner.SectorPreCommitInfo) - if err := t.PreCommitInfo.UnmarshalCBOR(br); err != nil { + if err := t.PreCommitInfo.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.PreCommitInfo pointer: %w", err) } } @@ -1147,7 +1159,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - if err := t.PreCommitDeposit.UnmarshalCBOR(br); err != nil { + if err := t.PreCommitDeposit.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.PreCommitDeposit: %w", err) } @@ -1157,16 +1169,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.PreCommitMessage: %w", err) } @@ -1178,7 +1190,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.PreCommitTipSet (sealing.TipSetToken) (slice) case "PreCommitTipSet": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1194,7 +1206,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { t.PreCommitTipSet = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.PreCommitTipSet[:]); err != nil { + if _, err := io.ReadFull(cr, t.PreCommitTipSet[:]); err != nil { return err } // t.PreCommit2Fails (uint64) (uint64) @@ -1202,7 +1214,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1215,7 +1227,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.SeedValue (abi.InteractiveSealRandomness) (slice) case "SeedValue": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1231,13 +1243,13 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { t.SeedValue = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.SeedValue[:]); err != nil { + if _, err := io.ReadFull(cr, t.SeedValue[:]); err != nil { return err } // t.SeedEpoch (abi.ChainEpoch) (int64) case "SeedEpoch": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1265,16 +1277,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.CommitMessage: %w", err) } @@ -1288,7 +1300,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1301,7 +1313,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.CCUpdate (bool) (bool) case "CCUpdate": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1319,7 +1331,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.CCPieces ([]sealing.Piece) (slice) case "CCPieces": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1339,7 +1351,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v Piece - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -1351,16 +1363,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.UpdateSealed: %w", err) } @@ -1374,16 +1386,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.UpdateUnsealed: %w", err) } @@ -1395,7 +1407,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.ReplicaUpdateProof (storage.ReplicaUpdateProof) (slice) case "ReplicaUpdateProof": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1411,7 +1423,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { t.ReplicaUpdateProof = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.ReplicaUpdateProof[:]); err != nil { + if _, err := io.ReadFull(cr, t.ReplicaUpdateProof[:]); err != nil { return err } // t.ReplicaUpdateMessage (cid.Cid) (struct) @@ -1419,16 +1431,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.ReplicaUpdateMessage: %w", err) } @@ -1442,16 +1454,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.FaultReportMsg: %w", err) } @@ -1464,7 +1476,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { case "Return": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1476,16 +1488,16 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.TerminateMessage: %w", err) } @@ -1497,7 +1509,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.TerminatedAt (abi.ChainEpoch) (int64) case "TerminatedAt": { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -1524,7 +1536,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { case "LastErr": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1534,7 +1546,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { // t.Log ([]sealing.Log) (slice) case "Log": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1554,7 +1566,7 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v Log - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -1574,25 +1586,26 @@ func (t *Log) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{164}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{164}); err != nil { return err } - scratch := make([]byte, 9) - // t.Timestamp (uint64) (uint64) if len("Timestamp") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Timestamp\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Timestamp"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Timestamp"))); err != nil { return err } if _, err := io.WriteString(w, string("Timestamp")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Timestamp)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Timestamp)); err != nil { return err } @@ -1601,7 +1614,7 @@ func (t *Log) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Trace\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Trace"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Trace"))); err != nil { return err } if _, err := io.WriteString(w, string("Trace")); err != nil { @@ -1612,7 +1625,7 @@ func (t *Log) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Trace was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Trace))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Trace))); err != nil { return err } if _, err := io.WriteString(w, string(t.Trace)); err != nil { @@ -1624,7 +1637,7 @@ func (t *Log) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Message\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Message"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Message"))); err != nil { return err } if _, err := io.WriteString(w, string("Message")); err != nil { @@ -1635,7 +1648,7 @@ func (t *Log) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Message was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Message))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Message))); err != nil { return err } if _, err := io.WriteString(w, string(t.Message)); err != nil { @@ -1647,7 +1660,7 @@ func (t *Log) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Kind\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Kind"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Kind"))); err != nil { return err } if _, err := io.WriteString(w, string("Kind")); err != nil { @@ -1658,7 +1671,7 @@ func (t *Log) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Kind was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Kind))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Kind))); err != nil { return err } if _, err := io.WriteString(w, string(t.Kind)); err != nil { @@ -1667,16 +1680,21 @@ func (t *Log) MarshalCBOR(w io.Writer) error { return nil } -func (t *Log) UnmarshalCBOR(r io.Reader) error { +func (t *Log) UnmarshalCBOR(r io.Reader) (err error) { *t = Log{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -1691,7 +1709,7 @@ func (t *Log) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1705,7 +1723,7 @@ func (t *Log) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -1719,7 +1737,7 @@ func (t *Log) UnmarshalCBOR(r io.Reader) error { case "Trace": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1730,7 +1748,7 @@ func (t *Log) UnmarshalCBOR(r io.Reader) error { case "Message": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -1741,7 +1759,7 @@ func (t *Log) UnmarshalCBOR(r io.Reader) error { case "Kind": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } diff --git a/node/hello/cbor_gen.go b/node/hello/cbor_gen.go index 145a19728af..68c82e2bbee 100644 --- a/node/hello/cbor_gen.go +++ b/node/hello/cbor_gen.go @@ -26,61 +26,67 @@ func (t *HelloMessage) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufHelloMessage); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufHelloMessage); err != nil { return err } - scratch := make([]byte, 9) - // t.HeaviestTipSet ([]cid.Cid) (slice) if len(t.HeaviestTipSet) > cbg.MaxLength { return xerrors.Errorf("Slice value in field t.HeaviestTipSet was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.HeaviestTipSet))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.HeaviestTipSet))); err != nil { return err } for _, v := range t.HeaviestTipSet { - if err := cbg.WriteCidBuf(scratch, w, v); err != nil { + if err := cbg.WriteCid(w, v); err != nil { return xerrors.Errorf("failed writing cid field t.HeaviestTipSet: %w", err) } } // t.HeaviestTipSetHeight (abi.ChainEpoch) (int64) if t.HeaviestTipSetHeight >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.HeaviestTipSetHeight)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.HeaviestTipSetHeight)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.HeaviestTipSetHeight-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.HeaviestTipSetHeight-1)); err != nil { return err } } // t.HeaviestTipSetWeight (big.Int) (struct) - if err := t.HeaviestTipSetWeight.MarshalCBOR(w); err != nil { + if err := t.HeaviestTipSetWeight.MarshalCBOR(cw); err != nil { return err } // t.GenesisHash (cid.Cid) (struct) - if err := cbg.WriteCidBuf(scratch, w, t.GenesisHash); err != nil { + if err := cbg.WriteCid(cw, t.GenesisHash); err != nil { return xerrors.Errorf("failed to write cid field t.GenesisHash: %w", err) } return nil } -func (t *HelloMessage) UnmarshalCBOR(r io.Reader) error { +func (t *HelloMessage) UnmarshalCBOR(r io.Reader) (err error) { *t = HelloMessage{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -91,7 +97,7 @@ func (t *HelloMessage) UnmarshalCBOR(r io.Reader) error { // t.HeaviestTipSet ([]cid.Cid) (slice) - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -110,7 +116,7 @@ func (t *HelloMessage) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("reading cid field t.HeaviestTipSet failed: %w", err) } @@ -119,7 +125,7 @@ func (t *HelloMessage) UnmarshalCBOR(r io.Reader) error { // t.HeaviestTipSetHeight (abi.ChainEpoch) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -146,7 +152,7 @@ func (t *HelloMessage) UnmarshalCBOR(r io.Reader) error { { - if err := t.HeaviestTipSetWeight.UnmarshalCBOR(br); err != nil { + if err := t.HeaviestTipSetWeight.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.HeaviestTipSetWeight: %w", err) } @@ -155,7 +161,7 @@ func (t *HelloMessage) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.GenesisHash: %w", err) } @@ -173,46 +179,52 @@ func (t *LatencyMessage) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write(lengthBufLatencyMessage); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write(lengthBufLatencyMessage); err != nil { return err } - scratch := make([]byte, 9) - // t.TArrival (int64) (int64) if t.TArrival >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.TArrival)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.TArrival)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.TArrival-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.TArrival-1)); err != nil { return err } } // t.TSent (int64) (int64) if t.TSent >= 0 { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.TSent)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.TSent)); err != nil { return err } } else { - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajNegativeInt, uint64(-t.TSent-1)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajNegativeInt, uint64(-t.TSent-1)); err != nil { return err } } return nil } -func (t *LatencyMessage) UnmarshalCBOR(r io.Reader) error { +func (t *LatencyMessage) UnmarshalCBOR(r io.Reader) (err error) { *t = LatencyMessage{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajArray { return fmt.Errorf("cbor input should be of type array") } @@ -223,7 +235,7 @@ func (t *LatencyMessage) UnmarshalCBOR(r io.Reader) error { // t.TArrival (int64) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err @@ -248,7 +260,7 @@ func (t *LatencyMessage) UnmarshalCBOR(r io.Reader) error { } // t.TSent (int64) (int64) { - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() var extraI int64 if err != nil { return err diff --git a/paychmgr/cbor_gen.go b/paychmgr/cbor_gen.go index 428c09a9e05..9f12b60a276 100644 --- a/paychmgr/cbor_gen.go +++ b/paychmgr/cbor_gen.go @@ -25,25 +25,26 @@ func (t *VoucherInfo) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{163}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{163}); err != nil { return err } - scratch := make([]byte, 9) - // t.Voucher (paych.SignedVoucher) (struct) if len("Voucher") > cbg.MaxLength { return xerrors.Errorf("Value in field \"Voucher\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Voucher"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Voucher"))); err != nil { return err } if _, err := io.WriteString(w, string("Voucher")); err != nil { return err } - if err := t.Voucher.MarshalCBOR(w); err != nil { + if err := t.Voucher.MarshalCBOR(cw); err != nil { return err } @@ -52,7 +53,7 @@ func (t *VoucherInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Proof\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Proof"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Proof"))); err != nil { return err } if _, err := io.WriteString(w, string("Proof")); err != nil { @@ -63,11 +64,11 @@ func (t *VoucherInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Byte array in field t.Proof was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.Proof))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajByteString, uint64(len(t.Proof))); err != nil { return err } - if _, err := w.Write(t.Proof[:]); err != nil { + if _, err := cw.Write(t.Proof[:]); err != nil { return err } @@ -76,7 +77,7 @@ func (t *VoucherInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Submitted\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Submitted"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Submitted"))); err != nil { return err } if _, err := io.WriteString(w, string("Submitted")); err != nil { @@ -89,16 +90,21 @@ func (t *VoucherInfo) MarshalCBOR(w io.Writer) error { return nil } -func (t *VoucherInfo) UnmarshalCBOR(r io.Reader) error { +func (t *VoucherInfo) UnmarshalCBOR(r io.Reader) (err error) { *t = VoucherInfo{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -113,7 +119,7 @@ func (t *VoucherInfo) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -127,16 +133,16 @@ func (t *VoucherInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Voucher = new(paych.SignedVoucher) - if err := t.Voucher.UnmarshalCBOR(br); err != nil { + if err := t.Voucher.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Voucher pointer: %w", err) } } @@ -145,7 +151,7 @@ func (t *VoucherInfo) UnmarshalCBOR(r io.Reader) error { // t.Proof ([]uint8) (slice) case "Proof": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -161,13 +167,13 @@ func (t *VoucherInfo) UnmarshalCBOR(r io.Reader) error { t.Proof = make([]uint8, extra) } - if _, err := io.ReadFull(br, t.Proof[:]); err != nil { + if _, err := io.ReadFull(cr, t.Proof[:]); err != nil { return err } // t.Submitted (bool) (bool) case "Submitted": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -196,18 +202,19 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{174}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{174}); err != nil { return err } - scratch := make([]byte, 9) - // t.ChannelID (string) (string) if len("ChannelID") > cbg.MaxLength { return xerrors.Errorf("Value in field \"ChannelID\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("ChannelID"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("ChannelID"))); err != nil { return err } if _, err := io.WriteString(w, string("ChannelID")); err != nil { @@ -218,7 +225,7 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.ChannelID was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.ChannelID))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.ChannelID))); err != nil { return err } if _, err := io.WriteString(w, string(t.ChannelID)); err != nil { @@ -230,14 +237,14 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Channel\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Channel"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Channel"))); err != nil { return err } if _, err := io.WriteString(w, string("Channel")); err != nil { return err } - if err := t.Channel.MarshalCBOR(w); err != nil { + if err := t.Channel.MarshalCBOR(cw); err != nil { return err } @@ -246,14 +253,14 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Control\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Control"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Control"))); err != nil { return err } if _, err := io.WriteString(w, string("Control")); err != nil { return err } - if err := t.Control.MarshalCBOR(w); err != nil { + if err := t.Control.MarshalCBOR(cw); err != nil { return err } @@ -262,14 +269,14 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Target\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Target"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Target"))); err != nil { return err } if _, err := io.WriteString(w, string("Target")); err != nil { return err } - if err := t.Target.MarshalCBOR(w); err != nil { + if err := t.Target.MarshalCBOR(cw); err != nil { return err } @@ -278,14 +285,14 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Direction\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Direction"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Direction"))); err != nil { return err } if _, err := io.WriteString(w, string("Direction")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.Direction)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.Direction)); err != nil { return err } @@ -294,7 +301,7 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Vouchers\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Vouchers"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Vouchers"))); err != nil { return err } if _, err := io.WriteString(w, string("Vouchers")); err != nil { @@ -305,11 +312,11 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Slice value in field t.Vouchers was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(t.Vouchers))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len(t.Vouchers))); err != nil { return err } for _, v := range t.Vouchers { - if err := v.MarshalCBOR(w); err != nil { + if err := v.MarshalCBOR(cw); err != nil { return err } } @@ -319,14 +326,14 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"NextLane\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("NextLane"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("NextLane"))); err != nil { return err } if _, err := io.WriteString(w, string("NextLane")); err != nil { return err } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajUnsignedInt, uint64(t.NextLane)); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(t.NextLane)); err != nil { return err } @@ -335,14 +342,14 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Amount\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Amount"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Amount"))); err != nil { return err } if _, err := io.WriteString(w, string("Amount")); err != nil { return err } - if err := t.Amount.MarshalCBOR(w); err != nil { + if err := t.Amount.MarshalCBOR(cw); err != nil { return err } @@ -351,14 +358,14 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"AvailableAmount\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("AvailableAmount"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("AvailableAmount"))); err != nil { return err } if _, err := io.WriteString(w, string("AvailableAmount")); err != nil { return err } - if err := t.AvailableAmount.MarshalCBOR(w); err != nil { + if err := t.AvailableAmount.MarshalCBOR(cw); err != nil { return err } @@ -367,14 +374,14 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"PendingAvailableAmount\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("PendingAvailableAmount"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("PendingAvailableAmount"))); err != nil { return err } if _, err := io.WriteString(w, string("PendingAvailableAmount")); err != nil { return err } - if err := t.PendingAvailableAmount.MarshalCBOR(w); err != nil { + if err := t.PendingAvailableAmount.MarshalCBOR(cw); err != nil { return err } @@ -383,14 +390,14 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"PendingAmount\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("PendingAmount"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("PendingAmount"))); err != nil { return err } if _, err := io.WriteString(w, string("PendingAmount")); err != nil { return err } - if err := t.PendingAmount.MarshalCBOR(w); err != nil { + if err := t.PendingAmount.MarshalCBOR(cw); err != nil { return err } @@ -399,7 +406,7 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"CreateMsg\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("CreateMsg"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("CreateMsg"))); err != nil { return err } if _, err := io.WriteString(w, string("CreateMsg")); err != nil { @@ -407,11 +414,11 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { } if t.CreateMsg == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.CreateMsg); err != nil { + if err := cbg.WriteCid(cw, *t.CreateMsg); err != nil { return xerrors.Errorf("failed to write cid field t.CreateMsg: %w", err) } } @@ -421,7 +428,7 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"AddFundsMsg\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("AddFundsMsg"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("AddFundsMsg"))); err != nil { return err } if _, err := io.WriteString(w, string("AddFundsMsg")); err != nil { @@ -429,11 +436,11 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { } if t.AddFundsMsg == nil { - if _, err := w.Write(cbg.CborNull); err != nil { + if _, err := cw.Write(cbg.CborNull); err != nil { return err } } else { - if err := cbg.WriteCidBuf(scratch, w, *t.AddFundsMsg); err != nil { + if err := cbg.WriteCid(cw, *t.AddFundsMsg); err != nil { return xerrors.Errorf("failed to write cid field t.AddFundsMsg: %w", err) } } @@ -443,7 +450,7 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Settling\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Settling"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Settling"))); err != nil { return err } if _, err := io.WriteString(w, string("Settling")); err != nil { @@ -456,16 +463,21 @@ func (t *ChannelInfo) MarshalCBOR(w io.Writer) error { return nil } -func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { +func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) (err error) { *t = ChannelInfo{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -480,7 +492,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -493,7 +505,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { case "ChannelID": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -505,16 +517,16 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } t.Channel = new(address.Address) - if err := t.Channel.UnmarshalCBOR(br); err != nil { + if err := t.Channel.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Channel pointer: %w", err) } } @@ -525,7 +537,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - if err := t.Control.UnmarshalCBOR(br); err != nil { + if err := t.Control.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Control: %w", err) } @@ -535,7 +547,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - if err := t.Target.UnmarshalCBOR(br); err != nil { + if err := t.Target.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Target: %w", err) } @@ -545,7 +557,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -558,7 +570,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { // t.Vouchers ([]*paychmgr.VoucherInfo) (slice) case "Vouchers": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -578,7 +590,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { for i := 0; i < int(extra); i++ { var v VoucherInfo - if err := v.UnmarshalCBOR(br); err != nil { + if err := v.UnmarshalCBOR(cr); err != nil { return err } @@ -590,7 +602,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -605,7 +617,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - if err := t.Amount.UnmarshalCBOR(br); err != nil { + if err := t.Amount.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.Amount: %w", err) } @@ -615,7 +627,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - if err := t.AvailableAmount.UnmarshalCBOR(br); err != nil { + if err := t.AvailableAmount.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.AvailableAmount: %w", err) } @@ -625,7 +637,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - if err := t.PendingAvailableAmount.UnmarshalCBOR(br); err != nil { + if err := t.PendingAvailableAmount.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.PendingAvailableAmount: %w", err) } @@ -635,7 +647,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - if err := t.PendingAmount.UnmarshalCBOR(br); err != nil { + if err := t.PendingAmount.UnmarshalCBOR(cr); err != nil { return xerrors.Errorf("unmarshaling t.PendingAmount: %w", err) } @@ -645,16 +657,16 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.CreateMsg: %w", err) } @@ -668,16 +680,16 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { { - b, err := br.ReadByte() + b, err := cr.ReadByte() if err != nil { return err } if b != cbg.CborNull[0] { - if err := br.UnreadByte(); err != nil { + if err := cr.UnreadByte(); err != nil { return err } - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.AddFundsMsg: %w", err) } @@ -689,7 +701,7 @@ func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error { // t.Settling (bool) (bool) case "Settling": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -718,18 +730,19 @@ func (t *MsgInfo) MarshalCBOR(w io.Writer) error { _, err := w.Write(cbg.CborNull) return err } - if _, err := w.Write([]byte{164}); err != nil { + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{164}); err != nil { return err } - scratch := make([]byte, 9) - // t.ChannelID (string) (string) if len("ChannelID") > cbg.MaxLength { return xerrors.Errorf("Value in field \"ChannelID\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("ChannelID"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("ChannelID"))); err != nil { return err } if _, err := io.WriteString(w, string("ChannelID")); err != nil { @@ -740,7 +753,7 @@ func (t *MsgInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.ChannelID was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.ChannelID))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.ChannelID))); err != nil { return err } if _, err := io.WriteString(w, string(t.ChannelID)); err != nil { @@ -752,14 +765,14 @@ func (t *MsgInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"MsgCid\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("MsgCid"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("MsgCid"))); err != nil { return err } if _, err := io.WriteString(w, string("MsgCid")); err != nil { return err } - if err := cbg.WriteCidBuf(scratch, w, t.MsgCid); err != nil { + if err := cbg.WriteCid(cw, t.MsgCid); err != nil { return xerrors.Errorf("failed to write cid field t.MsgCid: %w", err) } @@ -768,7 +781,7 @@ func (t *MsgInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Received\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Received"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Received"))); err != nil { return err } if _, err := io.WriteString(w, string("Received")); err != nil { @@ -784,7 +797,7 @@ func (t *MsgInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field \"Err\" was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len("Err"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("Err"))); err != nil { return err } if _, err := io.WriteString(w, string("Err")); err != nil { @@ -795,7 +808,7 @@ func (t *MsgInfo) MarshalCBOR(w io.Writer) error { return xerrors.Errorf("Value in field t.Err was too long") } - if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajTextString, uint64(len(t.Err))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Err))); err != nil { return err } if _, err := io.WriteString(w, string(t.Err)); err != nil { @@ -804,16 +817,21 @@ func (t *MsgInfo) MarshalCBOR(w io.Writer) error { return nil } -func (t *MsgInfo) UnmarshalCBOR(r io.Reader) error { +func (t *MsgInfo) UnmarshalCBOR(r io.Reader) (err error) { *t = MsgInfo{} - br := cbg.GetPeeker(r) - scratch := make([]byte, 8) + cr := cbg.NewCborReader(r) - maj, extra, err := cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err := cr.ReadHeader() if err != nil { return err } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + if maj != cbg.MajMap { return fmt.Errorf("cbor input should be of type map") } @@ -828,7 +846,7 @@ func (t *MsgInfo) UnmarshalCBOR(r io.Reader) error { for i := uint64(0); i < n; i++ { { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -841,7 +859,7 @@ func (t *MsgInfo) UnmarshalCBOR(r io.Reader) error { case "ChannelID": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err } @@ -853,7 +871,7 @@ func (t *MsgInfo) UnmarshalCBOR(r io.Reader) error { { - c, err := cbg.ReadCid(br) + c, err := cbg.ReadCid(cr) if err != nil { return xerrors.Errorf("failed to read cid field t.MsgCid: %w", err) } @@ -864,7 +882,7 @@ func (t *MsgInfo) UnmarshalCBOR(r io.Reader) error { // t.Received (bool) (bool) case "Received": - maj, extra, err = cbg.CborReadHeaderBuf(br, scratch) + maj, extra, err = cr.ReadHeader() if err != nil { return err } @@ -883,7 +901,7 @@ func (t *MsgInfo) UnmarshalCBOR(r io.Reader) error { case "Err": { - sval, err := cbg.ReadStringBuf(br, scratch) + sval, err := cbg.ReadString(cr) if err != nil { return err }