Fix bigint serialization

This commit is contained in:
Łukasz Magiera 2019-09-19 20:15:14 +02:00
parent 839597012d
commit 18b72b1584
4 changed files with 79 additions and 14 deletions

View File

@ -294,7 +294,13 @@ type SubmitPoStParams struct {
// TODO: this is a dummy method that allows us to plumb in other parts of the
// system for now.
func (sma StorageMinerActor) SubmitPoSt(act *types.Actor, vmctx types.VMContext, params *SubmitPoStParams) ([]byte, ActorError) {
func (sma StorageMinerActor) SubmitPoSt(act *types.Actor, vmctx types.VMContext, params *SubmitPoStParams) (_ []byte, paerr ActorError) {
defer func() {
if paerr != nil {
log.Error(paerr)
}
}()
oldstate, self, err := loadState(vmctx)
if err != nil {
return nil, err

View File

@ -759,7 +759,20 @@ func (t *SubmitPoStParams) MarshalCBOR(w io.Writer) error {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{128}); err != nil {
if _, err := w.Write([]byte{130}); err != nil {
return err
}
// t.t.Proof ([]uint8)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.Proof)))); err != nil {
return err
}
if _, err := w.Write(t.Proof); err != nil {
return err
}
// t.t.DoneSet (types.BitField)
if err := t.DoneSet.MarshalCBOR(w); err != nil {
return err
}
return nil
@ -776,10 +789,36 @@ func (t *SubmitPoStParams) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("cbor input should be of type array")
}
if extra != 0 {
if extra != 2 {
return fmt.Errorf("cbor input had wrong number of fields")
}
// t.t.Proof ([]uint8)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > 8192 {
return fmt.Errorf("array too large")
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}
t.Proof = make([]byte, extra)
if _, err := io.ReadFull(br, t.Proof); err != nil {
return err
}
// t.t.DoneSet (types.BitField)
{
if err := t.DoneSet.UnmarshalCBOR(br); err != nil {
return err
}
}
return nil
}

View File

@ -107,13 +107,9 @@ func (bi *BigInt) MarshalCBOR(w io.Writer) error {
return zero.MarshalCBOR(w)
}
if bi.Sign() < 0 {
// right now we don't support negative integers.
// In the spec, everything is listed as a Uint.
return fmt.Errorf("BigInt does not support negative integers")
}
tag := uint64(^(bi.Sign()|1)+6) >> 1
header := cbg.CborEncodeMajorType(cbg.MajTag, 2)
header := cbg.CborEncodeMajorType(cbg.MajTag, tag)
if _, err := w.Write(header); err != nil {
return err
}
@ -138,10 +134,12 @@ func (bi *BigInt) UnmarshalCBOR(br io.Reader) error {
return err
}
if maj != cbg.MajTag && extra != 2 {
if maj != cbg.MajTag && extra != 2 && extra != 3 {
return fmt.Errorf("cbor input for big int was not a tagged big int")
}
minus := extra & 1
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
@ -161,6 +159,9 @@ func (bi *BigInt) UnmarshalCBOR(br io.Reader) error {
}
bi.Int = big.NewInt(0).SetBytes(buf)
if minus > 0 {
bi.Int.Neg(bi.Int)
}
return nil
}

View File

@ -1199,6 +1199,10 @@ func (t *MessageReceipt) UnmarshalCBOR(r io.Reader) error {
}
func (t *BlockMsg) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{131}); err != nil {
return err
}
@ -1230,7 +1234,8 @@ func (t *BlockMsg) MarshalCBOR(w io.Writer) error {
return nil
}
func (t *BlockMsg) UnmarshalCBOR(br io.Reader) error {
func (t *BlockMsg) UnmarshalCBOR(r io.Reader) error {
br := cbg.GetPeeker(r)
maj, extra, err := cbg.CborReadHeader(br)
if err != nil {
@ -1246,10 +1251,24 @@ func (t *BlockMsg) UnmarshalCBOR(br io.Reader) error {
// t.t.Header (types.BlockHeader)
t.Header = new(BlockHeader)
{
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 {
t.Header = new(BlockHeader)
if err := t.Header.UnmarshalCBOR(br); err != nil {
return err
}
}
if err := t.Header.UnmarshalCBOR(br); err != nil {
return err
}
// t.t.BlsMessages ([]cid.Cid)