Skip to content

Commit

Permalink
sectorstorage: calltracker: work around cbor-gen bytearray len limit
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Sep 22, 2020
1 parent 04ad179 commit 4115dca
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 29 deletions.
44 changes: 17 additions & 27 deletions extern/sector-storage/cbor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 60 additions & 2 deletions extern/sector-storage/worker_calltracker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package sectorstorage

import (
"fmt"
"io"

"github.com/filecoin-project/go-statestore"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"

"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
)
Expand All @@ -24,7 +29,7 @@ type Call struct {

State CallState

Result []byte // json bytes
Result *ManyBytes // json bytes
}

func (wt *workerCallTracker) onStart(ci storiface.CallID, rt ReturnType) error {
Expand All @@ -39,7 +44,7 @@ func (wt *workerCallTracker) onDone(ci storiface.CallID, ret []byte) error {
st := wt.st.Get(ci)
return st.Mutate(func(cs *Call) error {
cs.State = CallDone
cs.Result = ret
cs.Result = &ManyBytes{ret}
return nil
})
}
Expand All @@ -53,3 +58,56 @@ func (wt *workerCallTracker) unfinished() ([]Call, error) {
var out []Call
return out, wt.st.List(&out)
}

// Ideally this would be a tag on the struct field telling cbor-gen to enforce higher max-len
type ManyBytes struct {
b []byte
}

const many = 100 << 20

func (t *ManyBytes) MarshalCBOR(w io.Writer) error {
if len(t.b) > many {
return xerrors.Errorf("byte array in field t.Result was too long")
}

scratch := make([]byte, 9)

if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajByteString, uint64(len(t.b))); err != nil {
return err
}

if _, err := w.Write(t.b[:]); err != nil {
return err
}
return nil
}

func (t *ManyBytes) UnmarshalCBOR(r io.Reader) error {
*t = ManyBytes{}

br := cbg.GetPeeker(r)
scratch := make([]byte, 9)

maj, extra, err := cbg.CborReadHeaderBuf(br, scratch)
if err != nil {
return err
}

if extra > many {
return fmt.Errorf("byte array too large (%d)", extra)
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}

if extra > 0 {
t.b = make([]uint8, extra)
}

if _, err := io.ReadFull(br, t.b[:]); err != nil {
return err
}

return nil
}

0 comments on commit 4115dca

Please sign in to comment.