service made plural for easier database key

This commit is contained in:
херетик
2023-06-16 12:25:22 +01:00
parent 7091559bda
commit 70008ab8c0
4 changed files with 48 additions and 31 deletions

View File

@@ -11,7 +11,7 @@ import (
"github.com/indra-labs/indra/pkg/onions/adaddress"
"github.com/indra-labs/indra/pkg/onions/adintro"
"github.com/indra-labs/indra/pkg/onions/adpeer"
"github.com/indra-labs/indra/pkg/onions/adservice"
"github.com/indra-labs/indra/pkg/onions/adservices"
"github.com/indra-labs/indra/pkg/onions/ont"
"github.com/indra-labs/indra/pkg/onions/reg"
"github.com/indra-labs/indra/pkg/util/splice"
@@ -82,11 +82,11 @@ func (ng *Engine) HandleAd(p *pubsub.Message, ctx context.Context) (e error) {
_ = intr
}
case *adservice.Ad:
case *adservices.Ad:
log.D.Ln("received", reflect.TypeOf(c), "from gossip network")
if serv, ok := c.(*adservice.Ad); !ok {
if serv, ok := c.(*adservices.Ad); !ok {
return fmt.Errorf(ErrWrongTypeDecode,
adservice.Magic, reflect.TypeOf(c).String())
adservices.Magic, reflect.TypeOf(c).String())
} else {
// If we got to here now we can add to the PeerStore.
log.D.S("new ad for service:", c)

View File

@@ -55,7 +55,7 @@ func TestEngine_PeerStore(t *testing.T) {
t.FailNow()
}
//time.Sleep(time.Second)
//newServiceAd := adservice.NewServiceAd(nonce.NewID(),
//newServiceAd := adservices.NewServiceAd(nonce.NewID(),
// engines[0].Manager.GetLocalNodeIdentityPrv(),
// 20000, 54321, time.Now().Add(time.Hour*24*7))
//ss := splice.New(newServiceAd.Len())

View File

@@ -1,4 +1,4 @@
package adservice
package adservices
import (
"fmt"
@@ -24,12 +24,15 @@ var (
)
const (
Magic = "svad"
Len = adproto.Len +
slice.Uint16Len +
slice.Uint32Len
Magic = "svad"
ServiceLen = slice.Uint16Len + slice.Uint32Len
)
type Service struct {
Port uint16
RelayRate uint32
}
// Ad stores a specification for the fee rate and the service port, which
// must be a well known port to match with a type of service, eg 80 for web, 53
// for DNS, etc. These are also attached to the PeerAd entry via concatenating
@@ -37,19 +40,18 @@ const (
// signals to stop scanning for more subsequent values.
type Ad struct {
adproto.Ad
Port uint16
RelayRate uint32
Services []Service
}
var _ coding.Codec = &Ad{}
// NewServiceAd ...
func NewServiceAd(id nonce.ID, key *crypto.Prv, relayRate uint32, port uint16,
func NewServiceAd(id nonce.ID, key *crypto.Prv, services []Service,
expiry time.Time) (sv *Ad) {
s := splice.New(adintro.Len)
k := crypto.DerivePub(key)
ServiceSplice(s, id, k, relayRate, port, expiry)
ServiceSplice(s, id, k, services, expiry)
hash := sha256.Single(s.GetUntil(s.GetCursor()))
var e error
var sign crypto.SigBytes
@@ -63,17 +65,22 @@ func NewServiceAd(id nonce.ID, key *crypto.Prv, relayRate uint32, port uint16,
Expiry: time.Now().Add(adproto.TTL),
Sig: sign,
},
RelayRate: relayRate,
Port: port,
Services: services,
}
return
}
func (x *Ad) Decode(s *splice.Splice) (e error) {
var i, count uint32
s.ReadID(&x.ID).
ReadPubkey(&x.Key).
ReadUint16(&x.Port).
ReadUint32(&x.RelayRate).
ReadUint32(&count)
x.Services = make([]Service, count)
for ; i < count; i++ {
s.ReadUint16(&x.Services[i].Port).
ReadUint32(&x.Services[i].RelayRate)
}
s.
ReadTime(&x.Expiry).
ReadSignature(&x.Sig)
return
@@ -88,7 +95,7 @@ func (x *Ad) GetOnion() interface{} { return nil }
func (x *Ad) Gossip(sm *sess.Manager, c qu.C) {}
func (x *Ad) Len() int { return Len }
func (x *Ad) Len() int { return adproto.Len + len(x.Services)*ServiceLen + slice.Uint32Len }
func (x *Ad) Magic() string { return "" }
@@ -115,7 +122,7 @@ func (x *Ad) Splice(s *splice.Splice) {
}
func (x *Ad) SpliceNoSig(s *splice.Splice) {
ServiceSplice(s, x.ID, x.Key, x.RelayRate, x.Port, x.Expiry)
ServiceSplice(s, x.ID, x.Key, x.Services, x.Expiry)
}
func (x *Ad) Validate() (valid bool) {
@@ -132,14 +139,18 @@ func (x *Ad) Validate() (valid bool) {
return false
}
func ServiceSplice(s *splice.Splice, id nonce.ID, key *crypto.Pub, relayRate uint32, port uint16, expiry time.Time) {
func ServiceSplice(s *splice.Splice, id nonce.ID, key *crypto.Pub, services []Service, expiry time.Time) {
s.Magic(Magic).
ID(id).
Pubkey(key).
Uint16(port).
Uint32(relayRate).
Time(expiry)
Uint32(uint32(len(services)))
for i := range services {
s.
Uint16(services[i].Port).
Uint32(services[i].RelayRate)
}
s.Time(expiry)
}
func init() { reg.Register(Magic, serviceAdGen) }

View File

@@ -1,4 +1,4 @@
package adservice
package adservices
import (
"github.com/indra-labs/indra"
@@ -19,7 +19,7 @@ func TestServiceAd(t *testing.T) {
var e error
pr, _, _ := crypto.NewSigner()
id := nonce.NewID()
sv := NewServiceAd(id, pr, 20000, 80, time.Now().Add(time.Hour))
sv := NewServiceAd(id, pr, []Service{{80, 50000},{443, 50000}}, time.Now().Add(time.Hour))
log.D.S("service", sv)
s := splice.New(sv.Len())
if e = sv.Encode(s); fails(e) {
@@ -42,13 +42,19 @@ func TestServiceAd(t *testing.T) {
t.Error("did not unwrap expected type")
t.FailNow()
}
if svcAd.RelayRate != sv.RelayRate {
t.Errorf("relay rate did not decode correctly")
if len(sv.Services) != len(svcAd.Services) {
t.Errorf("number of services incorrectly decoded")
t.FailNow()
}
if svcAd.Port != sv.Port {
t.Errorf("port did not decode correctly")
t.FailNow()
for i := range sv.Services {
if svcAd.Services[i].RelayRate != sv.Services[i].RelayRate {
t.Errorf("relay rate did not decode correctly")
t.FailNow()
}
if svcAd.Services[i].Port != sv.Services[i].Port {
t.Errorf("port did not decode correctly")
t.FailNow()
}
}
if !svcAd.Key.Equals(crypto.DerivePub(pr)) {
t.Errorf("public key did not decode correctly")