-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ECChain CBOR encoding compatibility tests
- Loading branch information
Showing
5 changed files
with
133 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package gpbft_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/filecoin-project/go-f3/gpbft" | ||
cbg "github.com/whyrusleeping/cbor-gen" | ||
) | ||
|
||
// Most of the file here is manually generated using cbor-gen to test | ||
// compatibility with the old format of ECChain. | ||
type OldECChain []gpbft.TipSet | ||
|
||
func (t *OldECChain) MarshalCBOR(w io.Writer) error { | ||
cw := cbg.NewCborWriter(w) | ||
if len((*t)) > 8192 { | ||
return errors.New("Slice value in field (*t) was too long") | ||
} | ||
|
||
if err := cw.WriteMajorTypeHeader(cbg.MajArray, uint64(len((*t)))); err != nil { | ||
return err | ||
} | ||
for _, v := range *t { | ||
if err := v.MarshalCBOR(cw); err != nil { | ||
return err | ||
} | ||
|
||
} | ||
return nil | ||
} | ||
|
||
func (t *OldECChain) UnmarshalCBOR(r io.Reader) (err error) { | ||
*t = OldECChain{} | ||
cr := cbg.NewCborReader(r) | ||
maj, extra, err := cr.ReadHeader() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if extra > 8192 { | ||
return fmt.Errorf("(*t): array too large (%d)", extra) | ||
} | ||
|
||
if maj != cbg.MajArray { | ||
return fmt.Errorf("expected cbor array") | ||
} | ||
|
||
if extra > 0 { | ||
*t = make([]gpbft.TipSet, extra) | ||
} | ||
|
||
for i := 0; i < int(extra); i++ { | ||
if err := (*t)[i].UnmarshalCBOR(cr); err != nil { | ||
return fmt.Errorf("unmarshaling (*t)[i]: %w", err) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters