From ae649089587e4d0507ec78369606c13cbcb213b7 Mon Sep 17 00:00:00 2001 From: Justin C Presley Date: Sun, 8 Oct 2023 20:54:22 -0500 Subject: [PATCH] vector resides in appParams --- pkg/svs/common.go | 1 + pkg/svs/state_vector.go | 253 +++++++++++++++++++++------------- pkg/svs/twostate_core.go | 6 +- test/svs/state_vector_test.go | 16 +-- 4 files changed, 170 insertions(+), 106 deletions(-) diff --git a/pkg/svs/common.go b/pkg/svs/common.go index 78b0707..a58e847 100644 --- a/pkg/svs/common.go +++ b/pkg/svs/common.go @@ -31,6 +31,7 @@ type CoreState int32 const ( Steady CoreState = 0 Suppression CoreState = 1 + Shaking CoreState = 2 ) type Status int diff --git a/pkg/svs/state_vector.go b/pkg/svs/state_vector.go index f96cb8b..8903b87 100644 --- a/pkg/svs/state_vector.go +++ b/pkg/svs/state_vector.go @@ -1,7 +1,6 @@ package svs import ( - "errors" "strconv" "strings" @@ -16,7 +15,7 @@ type StateVector interface { Len() int Total() uint64 Entries() *om.OrderedMap[uint64] - ToComponent(bool) enc.Component + Encode(bool) enc.Wire } type stateVector struct { @@ -27,11 +26,11 @@ func NewStateVector() StateVector { return stateVector{entries: om.New[uint64](om.LatestEntriesFirst)} } -func ParseStateVector(comp enc.Component, formal bool) (ret StateVector, err error) { +func ParseStateVector(reader enc.ParseReader, formal bool) (StateVector, error) { if formal { - return parseFormalStateVector(comp) + return parseFormalStateVector(reader) } else { - return parseInformalStateVector(comp) + return parseInformalStateVector(reader) } } @@ -76,23 +75,37 @@ func (sv stateVector) Entries() *om.OrderedMap[uint64] { return sv.entries } -func (sv stateVector) ToComponent(formal bool) enc.Component { - return enc.Component{ - Typ: TypeVector, - Val: sv.encodeVector(formal), - } -} - -func (sv stateVector) encodeVector(formal bool) []byte { +func (sv stateVector) Encode(formal bool) enc.Wire { if formal { tl, ls := sv.formalEncodingLengths() - buf := make([]byte, tl) - sv.formalEncodeInto(buf, ls) - return buf + // length + e := TypeVector.EncodingLength() + e += enc.TLNum(tl).EncodingLength() + e += tl + // space + wire := make(enc.Wire, 1) + wire[0] = make([]byte, e) + buf := wire[0] + // encode + off := TypeVector.EncodeInto(buf) + off += enc.TLNum(tl).EncodeInto(buf[off:]) + sv.formalEncodeInto(buf[off:], ls) + return wire } else { - buf := make([]byte, sv.informalEncodingLength()) - sv.informalEncodeInto(buf) - return buf + tl := sv.informalEncodingLength() + // length + e := TypeVector.EncodingLength() + e += enc.TLNum(tl).EncodingLength() + e += tl + // space + wire := make(enc.Wire, 1) + wire[0] = make([]byte, e) + buf := wire[0] + // encode + off := TypeVector.EncodeInto(buf) + off += enc.TLNum(tl).EncodeInto(buf[off:]) + sv.informalEncodeInto(buf[off:]) + return wire } } @@ -178,103 +191,153 @@ func (sv stateVector) informalEncodeInto(buf []byte) int { return pos } -func parseFormalStateVector(comp enc.Component) (ret StateVector, err error) { - defer func() { - if recover() != nil { - ret = NewStateVector() - err = errors.New("encoding.ParseStatevector: buffer length invalid") - } - }() +func parseFormalStateVector(reader enc.ParseReader) (StateVector, error) { + if reader == nil { + return NewStateVector(), enc.ErrBufferOverflow + } var ( - source enc.Name - seqno enc.Nat - length, typ enc.TLNum - buf = comp.Val - pos, temp int + source enc.Name + seqno enc.Nat + l, t enc.TLNum + b enc.Buffer + end int + err error + ret StateVector = NewStateVector() ) - // verify type - if comp.Typ != TypeVector { - return NewStateVector(), errors.New("encoding.ParseStatevector: incorrect tlv type") + // vector + t, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + if t != TypeVector { + return ret, enc.ErrUnrecognizedField{TypeNum: t} + } + l, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} } - // decode components - ret = NewStateVector() - for pos < len(buf) { + if reader.Length()-reader.Pos() < int(l) { + return ret, enc.ErrFailToParse{TypeNum: t} + } + // entries + end = int(l) + for reader.Pos() < end { // entry - typ, temp = enc.ParseTLNum(buf[pos:]) - pos += temp - if typ != TypeEntry { - return NewStateVector(), errors.New("encoding.ParseStatevector: incorrect tlv type") + t, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + if t != TypeEntry { + return ret, enc.ErrUnrecognizedField{TypeNum: t} + } + _, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} } - _, temp = enc.ParseTLNum(buf[pos:]) - pos += temp // source - typ, temp = enc.ParseTLNum(buf[pos:]) - pos += temp - if typ != enc.TypeName { - return NewStateVector(), errors.New("encoding.ParseStatevector: incorrect tlv type") + t, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + if t != enc.TypeName { + return ret, enc.ErrUnrecognizedField{TypeNum: t} + } + l, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + source, err = enc.ReadName(reader.Delegate(int(l))) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} } - length, temp = enc.ParseTLNum(buf[pos:]) - pos += temp - source, _ = enc.ReadName(enc.NewBufferReader(buf[pos : pos+int(length)])) - pos += int(length) // seqno - typ, temp = enc.ParseTLNum(buf[pos:]) - pos += temp - if typ != TypeEntrySeqno { - return NewStateVector(), errors.New("encoding.ParseStatevector: incorrect tlv type") + t, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + if t != TypeEntrySeqno { + return ret, enc.ErrUnrecognizedField{TypeNum: t} } - length, temp = enc.ParseTLNum(buf[pos:]) - pos += temp - seqno, _ = enc.ParseNat(buf[pos : pos+int(length)]) - pos += int(length) - // add the entry + l, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + b, err = reader.ReadBuf(int(l)) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + seqno, _ = enc.ParseNat(b) + // add ret.Set(source.String(), source, uint64(seqno), true) } return ret, nil } -func parseInformalStateVector(comp enc.Component) (ret StateVector, err error) { - defer func() { - if recover() != nil { - ret = NewStateVector() - err = errors.New("encoding.ParseStatevector: buffer length invalid") - } - }() +func parseInformalStateVector(reader enc.ParseReader) (StateVector, error) { + if reader == nil { + return NewStateVector(), enc.ErrBufferOverflow + } var ( - source enc.Name - seqno enc.Nat - length, typ enc.TLNum - buf = comp.Val - pos, temp int + source enc.Name + seqno enc.Nat + l, t enc.TLNum + b enc.Buffer + end int + err error + ret StateVector = NewStateVector() ) - // verify type - if comp.Typ != TypeVector { - return NewStateVector(), errors.New("encoding.ParseStatevector: incorrect tlv type") + // vector + t, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + if t != TypeVector { + return ret, enc.ErrUnrecognizedField{TypeNum: t} + } + l, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + if reader.Length()-reader.Pos() < int(l) { + return ret, enc.ErrFailToParse{TypeNum: t} } - // decode components - ret = NewStateVector() - for pos < len(buf) { + // entries + end = int(l) + for reader.Pos() < end { // source - typ, temp = enc.ParseTLNum(buf[pos:]) - pos += temp - if typ != enc.TypeName { - return NewStateVector(), errors.New("encoding.ParseStatevector: incorrect tlv type") + t, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + if t != enc.TypeName { + return ret, enc.ErrUnrecognizedField{TypeNum: t} + } + l, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + source, err = enc.ReadName(reader.Delegate(int(l))) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} } - length, temp = enc.ParseTLNum(buf[pos:]) - pos += temp - source, _ = enc.ReadName(enc.NewBufferReader(buf[pos : pos+int(length)])) - pos += int(length) // seqno - typ, temp = enc.ParseTLNum(buf[pos:]) - pos += temp - if typ != TypeEntrySeqno { - return NewStateVector(), errors.New("encoding.ParseStatevector: incorrect tlv type") + t, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + if t != TypeEntrySeqno { + return ret, enc.ErrUnrecognizedField{TypeNum: t} + } + l, err = enc.ReadTLNum(reader) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} + } + b, err = reader.ReadBuf(int(l)) + if err != nil { + return ret, enc.ErrFailToParse{TypeNum: t, Err: err} } - length, temp = enc.ParseTLNum(buf[pos:]) - pos += temp - seqno, _ = enc.ParseNat(buf[pos : pos+int(length)]) - pos += int(length) - // add the entry + seqno, _ = enc.ParseNat(b) + // add ret.Set(source.String(), source, uint64(seqno), true) } return ret, nil diff --git a/pkg/svs/twostate_core.go b/pkg/svs/twostate_core.go index 1b57cc2..92ea6a9 100644 --- a/pkg/svs/twostate_core.go +++ b/pkg/svs/twostate_core.go @@ -122,7 +122,7 @@ func (c *twoStateCore) FeedInterest(interest ndn.Interest, rawInterest enc.Wire, func (c *twoStateCore) onInterest(interest ndn.Interest, rawInterest enc.Wire, sigCovered enc.Wire, reply ndn.ReplyFunc, deadline time.Time) { // TODO: VERIFY THE INTEREST - incomingVector, err := ParseStateVector(interest.Name()[len(interest.Name())-2], c.formal) + incomingVector, err := ParseStateVector(enc.NewWireReader(interest.AppParam()), c.formal) if err != nil { c.logger.Warnf("Received unparsable statevector: %+v", err) return @@ -159,10 +159,10 @@ func (c *twoStateCore) sendInterest() { // TODO: SIGN THE INTEREST WITH AUTHENTICATABLE KEY // WARNING: SHA SIGNER PROVIDES NOTHING (signature only includes the appParams) & IS ONLY PLACEHOLDER c.vectorMtx.Lock() - name := append(c.syncPrefix, c.vector.ToComponent(c.formal)) + appP := c.vector.Encode(c.formal) c.vectorMtx.Unlock() wire, _, finalName, err := c.app.Spec().MakeInterest( - name, c.intCfg, enc.Wire{}, sec.NewSha256IntSigner(c.app.Timer()), + c.syncPrefix, c.intCfg, appP, sec.NewSha256IntSigner(c.app.Timer()), ) if err != nil { c.logger.Errorf("Unable to make Sync Interest: %+v", err) diff --git a/test/svs/state_vector_test.go b/test/svs/state_vector_test.go index 0bae511..64c9e50 100644 --- a/test/svs/state_vector_test.go +++ b/test/svs/state_vector_test.go @@ -47,8 +47,8 @@ func TestStateVectorFormalEncodeDecode(t *testing.T) { sv.Set("/one", n, 1, true) n, _ = enc.NameFromStr("/two") sv.Set("/two", n, 2, true) - comp := sv.ToComponent(true) - nsv, _ := svs.ParseStateVector(comp, true) + wire := sv.Encode(true) + nsv, _ := svs.ParseStateVector(enc.NewWireReader(wire), true) assert.Equal(t, uint64(1), nsv.Get("/one")) assert.Equal(t, uint64(2), nsv.Get("/two")) assert.Equal(t, uint64(3), nsv.Total()) @@ -62,8 +62,8 @@ func TestStateVectorInformalEncodeDecode(t *testing.T) { sv.Set("/one", n, 1, true) n, _ = enc.NameFromStr("/two") sv.Set("/two", n, 2, true) - comp := sv.ToComponent(false) - nsv, _ := svs.ParseStateVector(comp, false) + wire := sv.Encode(false) + nsv, _ := svs.ParseStateVector(enc.NewWireReader(wire), false) assert.Equal(t, uint64(1), nsv.Get("/one")) assert.Equal(t, uint64(2), nsv.Get("/two")) assert.Equal(t, uint64(3), nsv.Total()) @@ -72,8 +72,8 @@ func TestStateVectorInformalEncodeDecode(t *testing.T) { } func TestStateVectorFormalDecodeStatic(t *testing.T) { - comp, _ := enc.ComponentFromBytes([]byte{201, 24, 202, 10, 7, 5, 8, 3, 111, 110, 101, 204, 1, 1, 202, 10, 7, 5, 8, 3, 116, 119, 111, 204, 1, 2}) - sv, _ := svs.ParseStateVector(comp, true) + wire := enc.Wire{[]byte{201, 24, 202, 10, 7, 5, 8, 3, 111, 110, 101, 204, 1, 1, 202, 10, 7, 5, 8, 3, 116, 119, 111, 204, 1, 2}} + sv, _ := svs.ParseStateVector(enc.NewWireReader(wire), true) assert.Equal(t, uint64(1), sv.Get("/one")) assert.Equal(t, uint64(2), sv.Get("/two")) assert.Equal(t, uint64(3), sv.Total()) @@ -81,8 +81,8 @@ func TestStateVectorFormalDecodeStatic(t *testing.T) { } func TestStateVectorInformalDecodeStatic(t *testing.T) { - comp, _ := enc.ComponentFromBytes([]byte{201, 20, 7, 5, 8, 3, 111, 110, 101, 204, 1, 1, 7, 5, 8, 3, 116, 119, 111, 204, 1, 2}) - sv, _ := svs.ParseStateVector(comp, false) + wire := enc.Wire{[]byte{201, 20, 7, 5, 8, 3, 111, 110, 101, 204, 1, 1, 7, 5, 8, 3, 116, 119, 111, 204, 1, 2}} + sv, _ := svs.ParseStateVector(enc.NewWireReader(wire), false) assert.Equal(t, uint64(1), sv.Get("/one")) assert.Equal(t, uint64(2), sv.Get("/two")) assert.Equal(t, uint64(3), sv.Total())