Regen cbor marshalers

This commit is contained in:
Łukasz Magiera 2020-02-28 00:34:48 +01:00
parent d8dda1ee66
commit f83bbc2cbe
6 changed files with 460 additions and 49 deletions

View File

@ -432,3 +432,279 @@ func (t *SealedRefs) UnmarshalCBOR(r io.Reader) error {
return nil return nil
} }
func (t *SealTicket) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{162}); err != nil {
return err
}
// t.Value (abi.SealRandomness) (slice)
if len("Value") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"Value\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("Value")))); err != nil {
return err
}
if _, err := w.Write([]byte("Value")); err != nil {
return err
}
if len(t.Value) > cbg.ByteArrayMaxLen {
return xerrors.Errorf("Byte array in field t.Value was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.Value)))); err != nil {
return err
}
if _, err := w.Write(t.Value); err != nil {
return err
}
// t.Epoch (abi.ChainEpoch) (int64)
if len("Epoch") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"Epoch\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("Epoch")))); err != nil {
return err
}
if _, err := w.Write([]byte("Epoch")); err != nil {
return err
}
if t.Epoch >= 0 {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Epoch))); err != nil {
return err
}
} else {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Epoch)-1)); err != nil {
return err
}
}
return nil
}
func (t *SealTicket) UnmarshalCBOR(r io.Reader) error {
br := cbg.GetPeeker(r)
maj, extra, err := cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajMap {
return fmt.Errorf("cbor input should be of type map")
}
if extra > cbg.MaxLength {
return fmt.Errorf("SealTicket: map struct too large (%d)", extra)
}
var name string
n := extra
for i := uint64(0); i < n; i++ {
{
sval, err := cbg.ReadString(br)
if err != nil {
return err
}
name = string(sval)
}
switch name {
// t.Value (abi.SealRandomness) (slice)
case "Value":
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > cbg.ByteArrayMaxLen {
return fmt.Errorf("t.Value: byte array too large (%d)", extra)
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}
t.Value = make([]byte, extra)
if _, err := io.ReadFull(br, t.Value); err != nil {
return err
}
// t.Epoch (abi.ChainEpoch) (int64)
case "Epoch":
{
maj, extra, err := cbg.CborReadHeader(br)
var extraI int64
if err != nil {
return err
}
switch maj {
case cbg.MajUnsignedInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 positive overflow")
}
case cbg.MajNegativeInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 negative oveflow")
}
extraI = -1 - extraI
default:
return fmt.Errorf("wrong type for int64 field: %d", maj)
}
t.Epoch = abi.ChainEpoch(extraI)
}
default:
return fmt.Errorf("unknown struct field %d: '%s'", i, name)
}
}
return nil
}
func (t *SealSeed) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{162}); err != nil {
return err
}
// t.Value (abi.InteractiveSealRandomness) (slice)
if len("Value") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"Value\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("Value")))); err != nil {
return err
}
if _, err := w.Write([]byte("Value")); err != nil {
return err
}
if len(t.Value) > cbg.ByteArrayMaxLen {
return xerrors.Errorf("Byte array in field t.Value was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.Value)))); err != nil {
return err
}
if _, err := w.Write(t.Value); err != nil {
return err
}
// t.Epoch (abi.ChainEpoch) (int64)
if len("Epoch") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"Epoch\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("Epoch")))); err != nil {
return err
}
if _, err := w.Write([]byte("Epoch")); err != nil {
return err
}
if t.Epoch >= 0 {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.Epoch))); err != nil {
return err
}
} else {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.Epoch)-1)); err != nil {
return err
}
}
return nil
}
func (t *SealSeed) UnmarshalCBOR(r io.Reader) error {
br := cbg.GetPeeker(r)
maj, extra, err := cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajMap {
return fmt.Errorf("cbor input should be of type map")
}
if extra > cbg.MaxLength {
return fmt.Errorf("SealSeed: map struct too large (%d)", extra)
}
var name string
n := extra
for i := uint64(0); i < n; i++ {
{
sval, err := cbg.ReadString(br)
if err != nil {
return err
}
name = string(sval)
}
switch name {
// t.Value (abi.InteractiveSealRandomness) (slice)
case "Value":
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > cbg.ByteArrayMaxLen {
return fmt.Errorf("t.Value: byte array too large (%d)", extra)
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}
t.Value = make([]byte, extra)
if _, err := io.ReadFull(br, t.Value); err != nil {
return err
}
// t.Epoch (abi.ChainEpoch) (int64)
case "Epoch":
{
maj, extra, err := cbg.CborReadHeader(br)
var extraI int64
if err != nil {
return err
}
switch maj {
case cbg.MajUnsignedInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 positive overflow")
}
case cbg.MajNegativeInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 negative oveflow")
}
extraI = -1 - extraI
default:
return fmt.Errorf("wrong type for int64 field: %d", maj)
}
t.Epoch = abi.ChainEpoch(extraI)
}
default:
return fmt.Errorf("unknown struct field %d: '%s'", i, name)
}
}
return nil
}

View File

@ -7,7 +7,7 @@ import (
"io" "io"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
cid "github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen" cbg "github.com/whyrusleeping/cbor-gen"
xerrors "golang.org/x/xerrors" xerrors "golang.org/x/xerrors"
) )

View File

@ -9,7 +9,7 @@ import (
"github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/crypto" "github.com/filecoin-project/specs-actors/actors/crypto"
cid "github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen" cbg "github.com/whyrusleeping/cbor-gen"
xerrors "golang.org/x/xerrors" xerrors "golang.org/x/xerrors"
) )
@ -391,7 +391,19 @@ func (t *EPostProof) MarshalCBOR(w io.Writer) error {
return err return err
} }
// t.Proof ([]uint8) (slice) // t.Proofs ([]abi.PoStProof) (slice)
if len(t.Proofs) > cbg.MaxLength {
return xerrors.Errorf("Slice value in field t.Proofs was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.Proofs)))); err != nil {
return err
}
for _, v := range t.Proofs {
if err := v.MarshalCBOR(w); err != nil {
return err
}
}
// t.PostRand ([]uint8) (slice) // t.PostRand ([]uint8) (slice)
if len(t.PostRand) > cbg.ByteArrayMaxLen { if len(t.PostRand) > cbg.ByteArrayMaxLen {
@ -436,19 +448,33 @@ func (t *EPostProof) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("cbor input had wrong number of fields") return fmt.Errorf("cbor input had wrong number of fields")
} }
// t.Proof ([]uint8) (slice) // t.Proofs ([]abi.PoStProof) (slice)
maj, extra, err = cbg.CborReadHeader(br) maj, extra, err = cbg.CborReadHeader(br)
if err != nil { if err != nil {
return err return err
} }
if extra > cbg.ByteArrayMaxLen { if extra > cbg.MaxLength {
return fmt.Errorf("t.Proof: byte array too large (%d)", extra) return fmt.Errorf("t.Proofs: array too large (%d)", extra)
} }
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array") if maj != cbg.MajArray {
return fmt.Errorf("expected cbor array")
} }
if extra > 0 {
t.Proofs = make([]abi.PoStProof, extra)
}
for i := 0; i < int(extra); i++ {
var v abi.PoStProof
if err := v.UnmarshalCBOR(br); err != nil {
return err
}
t.Proofs[i] = v
}
// t.PostRand ([]uint8) (slice) // t.PostRand ([]uint8) (slice)
maj, extra, err = cbg.CborReadHeader(br) maj, extra, err = cbg.CborReadHeader(br)

View File

@ -47,6 +47,8 @@ func main() {
api.PaymentInfo{}, api.PaymentInfo{},
api.SealedRef{}, api.SealedRef{},
api.SealedRefs{}, api.SealedRefs{},
api.SealTicket{},
api.SealSeed{},
) )
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -73,8 +75,6 @@ func main() {
} }
err = gen.WriteMapEncodersToFile("./storage/sealing/cbor_gen.go", "sealing", err = gen.WriteMapEncodersToFile("./storage/sealing/cbor_gen.go", "sealing",
sealing.SealTicket{},
sealing.SealSeed{},
sealing.Piece{}, sealing.Piece{},
sealing.SectorInfo{}, sealing.SectorInfo{},
sealing.Log{}, sealing.Log{},

View File

@ -7,7 +7,7 @@ import (
"io" "io"
"github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/abi"
cid "github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen" cbg "github.com/whyrusleeping/cbor-gen"
xerrors "golang.org/x/xerrors" xerrors "golang.org/x/xerrors"
) )

View File

@ -60,7 +60,7 @@ func (t *Piece) MarshalCBOR(w io.Writer) error {
return err return err
} }
// t.CommP ([]uint8) (slice) // t.CommP (cid.Cid) (struct)
if len("CommP") > cbg.MaxLength { if len("CommP") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"CommP\" was too long") return xerrors.Errorf("Value in field \"CommP\" was too long")
} }
@ -72,6 +72,10 @@ func (t *Piece) MarshalCBOR(w io.Writer) error {
return err return err
} }
if err := cbg.WriteCid(w, t.CommP); err != nil {
return xerrors.Errorf("failed to write cid field t.CommP: %w", err)
}
return nil return nil
} }
@ -147,19 +151,18 @@ func (t *Piece) UnmarshalCBOR(r io.Reader) error {
t.Size = abi.UnpaddedPieceSize(extra) t.Size = abi.UnpaddedPieceSize(extra)
} }
// t.CommP ([]uint8) (slice) // t.CommP (cid.Cid) (struct)
case "CommP": case "CommP":
maj, extra, err = cbg.CborReadHeader(br) {
if err != nil {
return err c, err := cbg.ReadCid(br)
} if err != nil {
return xerrors.Errorf("failed to read cid field t.CommP: %w", err)
}
t.CommP = c
if extra > cbg.ByteArrayMaxLen {
return fmt.Errorf("t.CommP: byte array too large (%d)", extra)
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
} }
default: default:
@ -174,7 +177,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
_, err := w.Write(cbg.CborNull) _, err := w.Write(cbg.CborNull)
return err return err
} }
if _, err := w.Write([]byte{174}); err != nil { if _, err := w.Write([]byte{175}); err != nil {
return err return err
} }
@ -226,6 +229,28 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
return err return err
} }
// t.SectorType (abi.RegisteredProof) (int64)
if len("SectorType") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"SectorType\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("SectorType")))); err != nil {
return err
}
if _, err := w.Write([]byte("SectorType")); err != nil {
return err
}
if t.SectorType >= 0 {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.SectorType))); err != nil {
return err
}
} else {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.SectorType)-1)); err != nil {
return err
}
}
// t.Pieces ([]sealing.Piece) (slice) // t.Pieces ([]sealing.Piece) (slice)
if len("Pieces") > cbg.MaxLength { if len("Pieces") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"Pieces\" was too long") return xerrors.Errorf("Value in field \"Pieces\" was too long")
@ -251,7 +276,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
} }
} }
// t.CommD ([]uint8) (slice) // t.CommD (cid.Cid) (struct)
if len("CommD") > cbg.MaxLength { if len("CommD") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"CommD\" was too long") return xerrors.Errorf("Value in field \"CommD\" was too long")
} }
@ -263,7 +288,17 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
return err return err
} }
// t.CommR ([]uint8) (slice) if t.CommD == nil {
if _, err := w.Write(cbg.CborNull); err != nil {
return err
}
} else {
if err := cbg.WriteCid(w, *t.CommD); err != nil {
return xerrors.Errorf("failed to write cid field t.CommD: %w", err)
}
}
// t.CommR (cid.Cid) (struct)
if len("CommR") > cbg.MaxLength { if len("CommR") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"CommR\" was too long") return xerrors.Errorf("Value in field \"CommR\" was too long")
} }
@ -275,6 +310,16 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
return err return err
} }
if t.CommR == nil {
if _, err := w.Write(cbg.CborNull); err != nil {
return err
}
} else {
if err := cbg.WriteCid(w, *t.CommR); err != nil {
return xerrors.Errorf("failed to write cid field t.CommR: %w", err)
}
}
// t.Proof ([]uint8) (slice) // t.Proof ([]uint8) (slice)
if len("Proof") > cbg.MaxLength { if len("Proof") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"Proof\" was too long") return xerrors.Errorf("Value in field \"Proof\" was too long")
@ -298,7 +343,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
return err return err
} }
// t.Ticket (sealing.SealTicket) (struct) // t.Ticket (api.SealTicket) (struct)
if len("Ticket") > cbg.MaxLength { if len("Ticket") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"Ticket\" was too long") return xerrors.Errorf("Value in field \"Ticket\" was too long")
} }
@ -310,6 +355,10 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
return err return err
} }
if err := t.Ticket.MarshalCBOR(w); err != nil {
return err
}
// t.PreCommitMessage (cid.Cid) (struct) // t.PreCommitMessage (cid.Cid) (struct)
if len("PreCommitMessage") > cbg.MaxLength { if len("PreCommitMessage") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"PreCommitMessage\" was too long") return xerrors.Errorf("Value in field \"PreCommitMessage\" was too long")
@ -332,7 +381,7 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
} }
} }
// t.Seed (sealing.SealSeed) (struct) // t.Seed (api.SealSeed) (struct)
if len("Seed") > cbg.MaxLength { if len("Seed") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"Seed\" was too long") return xerrors.Errorf("Value in field \"Seed\" was too long")
} }
@ -344,6 +393,10 @@ func (t *SectorInfo) MarshalCBOR(w io.Writer) error {
return err return err
} }
if err := t.Seed.MarshalCBOR(w); err != nil {
return err
}
// t.CommitMessage (cid.Cid) (struct) // t.CommitMessage (cid.Cid) (struct)
if len("CommitMessage") > cbg.MaxLength { if len("CommitMessage") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"CommitMessage\" was too long") return xerrors.Errorf("Value in field \"CommitMessage\" was too long")
@ -513,6 +566,32 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error {
t.Nonce = uint64(extra) t.Nonce = uint64(extra)
} }
// t.SectorType (abi.RegisteredProof) (int64)
case "SectorType":
{
maj, extra, err := cbg.CborReadHeader(br)
var extraI int64
if err != nil {
return err
}
switch maj {
case cbg.MajUnsignedInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 positive overflow")
}
case cbg.MajNegativeInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 negative oveflow")
}
extraI = -1 - extraI
default:
return fmt.Errorf("wrong type for int64 field: %d", maj)
}
t.SectorType = abi.RegisteredProof(extraI)
}
// t.Pieces ([]sealing.Piece) (slice) // t.Pieces ([]sealing.Piece) (slice)
case "Pieces": case "Pieces":
@ -541,33 +620,55 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error {
t.Pieces[i] = v t.Pieces[i] = v
} }
// t.CommD ([]uint8) (slice) // t.CommD (cid.Cid) (struct)
case "CommD": case "CommD":
maj, extra, err = cbg.CborReadHeader(br) {
if err != nil {
return err pb, err := br.PeekByte()
} if err != nil {
return err
}
if pb == cbg.CborNull[0] {
var nbuf [1]byte
if _, err := br.Read(nbuf[:]); err != nil {
return err
}
} else {
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.CommD: %w", err)
}
t.CommD = &c
}
if extra > cbg.ByteArrayMaxLen {
return fmt.Errorf("t.CommD: byte array too large (%d)", extra)
} }
if maj != cbg.MajByteString { // t.CommR (cid.Cid) (struct)
return fmt.Errorf("expected byte array")
}
// t.CommR ([]uint8) (slice)
case "CommR": case "CommR":
maj, extra, err = cbg.CborReadHeader(br) {
if err != nil {
return err pb, err := br.PeekByte()
} if err != nil {
return err
}
if pb == cbg.CborNull[0] {
var nbuf [1]byte
if _, err := br.Read(nbuf[:]); err != nil {
return err
}
} else {
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.CommR: %w", err)
}
t.CommR = &c
}
if extra > cbg.ByteArrayMaxLen {
return fmt.Errorf("t.CommR: byte array too large (%d)", extra)
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
} }
// t.Proof ([]uint8) (slice) // t.Proof ([]uint8) (slice)
case "Proof": case "Proof":
@ -587,11 +688,15 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error {
if _, err := io.ReadFull(br, t.Proof); err != nil { if _, err := io.ReadFull(br, t.Proof); err != nil {
return err return err
} }
// t.Ticket (sealing.SealTicket) (struct) // t.Ticket (api.SealTicket) (struct)
case "Ticket": case "Ticket":
{ {
if err := t.Ticket.UnmarshalCBOR(br); err != nil {
return err
}
} }
// t.PreCommitMessage (cid.Cid) (struct) // t.PreCommitMessage (cid.Cid) (struct)
case "PreCommitMessage": case "PreCommitMessage":
@ -618,11 +723,15 @@ func (t *SectorInfo) UnmarshalCBOR(r io.Reader) error {
} }
} }
// t.Seed (sealing.SealSeed) (struct) // t.Seed (api.SealSeed) (struct)
case "Seed": case "Seed":
{ {
if err := t.Seed.UnmarshalCBOR(br); err != nil {
return err
}
} }
// t.CommitMessage (cid.Cid) (struct) // t.CommitMessage (cid.Cid) (struct)
case "CommitMessage": case "CommitMessage":