Use tuple encoding

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-02-12 19:23:15 +01:00
parent 50702f309f
commit 8610bcbec3
3 changed files with 132 additions and 202 deletions

View File

@ -59,7 +59,7 @@ func main() {
os.Exit(1)
}
err = gen.WriteMapEncodersToFile("./node/hello/cbor_gen.go", "hello",
err = gen.WriteTupleEncodersToFile("./node/hello/cbor_gen.go", "hello",
hello.HelloMessage{},
hello.LatencyMessage{},
)

View File

@ -18,22 +18,11 @@ func (t *HelloMessage) MarshalCBOR(w io.Writer) error {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{164}); err != nil {
if _, err := w.Write([]byte{132}); err != nil {
return err
}
// t.HeaviestTipSet ([]cid.Cid) (slice)
if len("HeaviestTipSet") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"HeaviestTipSet\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("HeaviestTipSet")))); err != nil {
return err
}
if _, err := w.Write([]byte("HeaviestTipSet")); err != nil {
return err
}
if len(t.HeaviestTipSet) > cbg.MaxLength {
return xerrors.Errorf("Slice value in field t.HeaviestTipSet was too long")
}
@ -48,48 +37,16 @@ func (t *HelloMessage) MarshalCBOR(w io.Writer) error {
}
// t.HeaviestTipSetHeight (uint64) (uint64)
if len("HeaviestTipSetHeight") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"HeaviestTipSetHeight\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("HeaviestTipSetHeight")))); err != nil {
return err
}
if _, err := w.Write([]byte("HeaviestTipSetHeight")); err != nil {
return err
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.HeaviestTipSetHeight))); err != nil {
return err
}
// t.HeaviestTipSetWeight (types.BigInt) (struct)
if len("HeaviestTipSetWeight") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"HeaviestTipSetWeight\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("HeaviestTipSetWeight")))); err != nil {
return err
}
if _, err := w.Write([]byte("HeaviestTipSetWeight")); err != nil {
return err
}
if err := t.HeaviestTipSetWeight.MarshalCBOR(w); err != nil {
return err
}
// t.GenesisHash (cid.Cid) (struct)
if len("GenesisHash") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"GenesisHash\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("GenesisHash")))); err != nil {
return err
}
if _, err := w.Write([]byte("GenesisHash")); err != nil {
return err
}
if err := cbg.WriteCid(w, t.GenesisHash); err != nil {
return xerrors.Errorf("failed to write cid field t.GenesisHash: %w", err)
@ -105,137 +62,103 @@ func (t *HelloMessage) UnmarshalCBOR(r io.Reader) error {
if err != nil {
return err
}
if maj != cbg.MajMap {
return fmt.Errorf("cbor input should be of type map")
if maj != cbg.MajArray {
return fmt.Errorf("cbor input should be of type array")
}
if extra != 4 {
return fmt.Errorf("cbor input had wrong number of fields")
}
// t.HeaviestTipSet ([]cid.Cid) (slice)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > cbg.MaxLength {
return fmt.Errorf("HelloMessage: map struct too large (%d)", extra)
return fmt.Errorf("t.HeaviestTipSet: array too large (%d)", extra)
}
var name string
n := extra
if maj != cbg.MajArray {
return fmt.Errorf("expected cbor array")
}
if extra > 0 {
t.HeaviestTipSet = make([]cid.Cid, extra)
}
for i := 0; i < int(extra); i++ {
for i := uint64(0); i < n; i++ {
{
sval, err := cbg.ReadString(br)
if err != nil {
return err
}
name = string(sval)
}
switch name {
// t.HeaviestTipSet ([]cid.Cid) (slice)
case "HeaviestTipSet":
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > cbg.MaxLength {
return fmt.Errorf("t.HeaviestTipSet: array too large (%d)", extra)
}
if maj != cbg.MajArray {
return fmt.Errorf("expected cbor array")
}
if extra > 0 {
t.HeaviestTipSet = make([]cid.Cid, extra)
}
for i := 0; i < int(extra); i++ {
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("reading cid field t.HeaviestTipSet failed: %w", err)
}
t.HeaviestTipSet[i] = c
}
// t.HeaviestTipSetHeight (uint64) (uint64)
case "HeaviestTipSetHeight":
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajUnsignedInt {
return fmt.Errorf("wrong type for uint64 field")
}
t.HeaviestTipSetHeight = uint64(extra)
// t.HeaviestTipSetWeight (types.BigInt) (struct)
case "HeaviestTipSetWeight":
{
if err := t.HeaviestTipSetWeight.UnmarshalCBOR(br); err != nil {
return err
}
}
// t.GenesisHash (cid.Cid) (struct)
case "GenesisHash":
{
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.GenesisHash: %w", err)
}
t.GenesisHash = c
}
default:
return fmt.Errorf("unknown struct field %d: '%s'", i, name)
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("reading cid field t.HeaviestTipSet failed: %w", err)
}
t.HeaviestTipSet[i] = c
}
// t.HeaviestTipSetHeight (uint64) (uint64)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajUnsignedInt {
return fmt.Errorf("wrong type for uint64 field")
}
t.HeaviestTipSetHeight = uint64(extra)
// t.HeaviestTipSetWeight (types.BigInt) (struct)
{
if err := t.HeaviestTipSetWeight.UnmarshalCBOR(br); err != nil {
return err
}
}
// t.GenesisHash (cid.Cid) (struct)
{
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.GenesisHash: %w", err)
}
t.GenesisHash = c
}
return nil
}
func (t *LatencyMessage) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{162}); err != nil {
if _, err := w.Write([]byte{130}); err != nil {
return err
}
// t.TArrial (uint64) (uint64)
if len("TArrial") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"TArrial\" was too long")
// t.TArrial (int64) (int64)
if t.TArrial >= 0 {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.TArrial))); err != nil {
return err
}
} else {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.TArrial)-1)); err != nil {
return err
}
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("TArrial")))); err != nil {
return err
}
if _, err := w.Write([]byte("TArrial")); err != nil {
return err
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.TArrial))); err != nil {
return err
}
// t.TSent (uint64) (uint64)
if len("TSent") > cbg.MaxLength {
return xerrors.Errorf("Value in field \"TSent\" was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len("TSent")))); err != nil {
return err
}
if _, err := w.Write([]byte("TSent")); err != nil {
return err
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.TSent))); err != nil {
return err
// t.TSent (int64) (int64)
if t.TSent >= 0 {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, uint64(t.TSent))); err != nil {
return err
}
} else {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajNegativeInt, uint64(-t.TSent)-1)); err != nil {
return err
}
}
return nil
}
@ -247,56 +170,63 @@ func (t *LatencyMessage) UnmarshalCBOR(r io.Reader) error {
if err != nil {
return err
}
if maj != cbg.MajMap {
return fmt.Errorf("cbor input should be of type map")
if maj != cbg.MajArray {
return fmt.Errorf("cbor input should be of type array")
}
if extra > cbg.MaxLength {
return fmt.Errorf("LatencyMessage: map struct too large (%d)", extra)
if extra != 2 {
return fmt.Errorf("cbor input had wrong number of fields")
}
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)
// t.TArrial (int64) (int64)
{
maj, extra, err := cbg.CborReadHeader(br)
var extraI int64
if err != nil {
return err
}
switch name {
// t.TArrial (uint64) (uint64)
case "TArrial":
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
switch maj {
case cbg.MajUnsignedInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 positive overflow")
}
if maj != cbg.MajUnsignedInt {
return fmt.Errorf("wrong type for uint64 field")
case cbg.MajNegativeInt:
extraI = int64(extra)
if extraI < 0 {
return fmt.Errorf("int64 negative oveflow")
}
t.TArrial = uint64(extra)
// t.TSent (uint64) (uint64)
case "TSent":
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajUnsignedInt {
return fmt.Errorf("wrong type for uint64 field")
}
t.TSent = uint64(extra)
extraI = -1 - extraI
default:
return fmt.Errorf("unknown struct field %d: '%s'", i, name)
return fmt.Errorf("wrong type for int64 field: %d", maj)
}
}
t.TArrial = int64(extraI)
}
// t.TSent (int64) (int64)
{
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.TSent = int64(extraI)
}
return nil
}

View File

@ -29,8 +29,8 @@ type HelloMessage struct {
GenesisHash cid.Cid
}
type LatencyMessage struct {
TArrial uint64
TSent uint64
TArrial int64
TSent int64
}
type NewStreamFunc func(context.Context, peer.ID, ...protocol.ID) (inet.Stream, error)
@ -81,8 +81,8 @@ func (hs *Service) HandleStream(s inet.Stream) {
sent := time.Now()
msg := &LatencyMessage{
TArrial: uint64(arrived.UnixNano()),
TSent: uint64(sent.UnixNano()),
TArrial: arrived.UnixNano(),
TSent: sent.UnixNano(),
}
if err := cborutil.WriteCborRPC(s, msg); err != nil {
log.Debugf("error while responding to latency: %v", err)
@ -157,8 +157,8 @@ func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error {
if err == nil {
if lmsg.TArrial != 0 && lmsg.TSent != 0 {
t1 := time.Unix(0, int64(lmsg.TArrial))
t2 := time.Unix(0, int64(lmsg.TSent))
t1 := time.Unix(0, lmsg.TArrial)
t2 := time.Unix(0, lmsg.TSent)
offset := t0.Sub(t1) + t3.Sub(t2)
offset /= 2
log.Infow("time offset", "offset", offset.Seconds(), "peerid", pid.String())