Skip to content

Commit

Permalink
vector resides in appParams
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin C Presley committed Oct 9, 2023
1 parent c08acc8 commit ae64908
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 106 deletions.
1 change: 1 addition & 0 deletions pkg/svs/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type CoreState int32
const (
Steady CoreState = 0
Suppression CoreState = 1
Shaking CoreState = 2
)

type Status int
Expand Down
253 changes: 158 additions & 95 deletions pkg/svs/state_vector.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package svs

import (
"errors"
"strconv"
"strings"

Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/svs/twostate_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions test/svs/state_vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -72,17 +72,17 @@ 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())
assert.Equal(t, int(2), sv.Len())
}

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())
Expand Down

0 comments on commit ae64908

Please sign in to comment.