more compact addresses ad and starting with updated mock
This commit is contained in:
@@ -24,7 +24,7 @@ var (
|
||||
)
|
||||
|
||||
const (
|
||||
Magic = "svad"
|
||||
Magic = "adad"
|
||||
AddrLen = splice.AddrLen
|
||||
)
|
||||
|
||||
@@ -69,10 +69,10 @@ func New(id nonce.ID, key *crypto.Prv, addrs []*netip.AddrPort,
|
||||
|
||||
// Decode a splice.Splice's next bytes into an Ad.
|
||||
func (x *Ad) Decode(s *splice.Splice) (e error) {
|
||||
var i, count uint32
|
||||
var i, count uint16
|
||||
s.ReadID(&x.ID).
|
||||
ReadPubkey(&x.Key).
|
||||
ReadUint32(&count)
|
||||
ReadUint16(&count)
|
||||
x.Addresses = make([]*netip.AddrPort, count)
|
||||
for ; i < count; i++ {
|
||||
addy := &netip.AddrPort{}
|
||||
@@ -96,7 +96,9 @@ func (x *Ad) Unwrap() interface{} { return nil }
|
||||
|
||||
// Len returns the length of bytes required to encode the Ad, based on the number
|
||||
// of Addresses inside it.
|
||||
func (x *Ad) Len() int { return ad.Len + len(x.Addresses)*AddrLen + slice.Uint32Len + 2 }
|
||||
func (x *Ad) Len() int {
|
||||
return ad.Len + len(x.Addresses)*(1+AddrLen) + slice.Uint16Len
|
||||
}
|
||||
|
||||
// Magic bytes that identify this message
|
||||
func (x *Ad) Magic() string { return Magic }
|
||||
@@ -152,7 +154,7 @@ func Splice(s *splice.Splice, id nonce.ID, key *crypto.Pub,
|
||||
s.Magic(Magic).
|
||||
ID(id).
|
||||
Pubkey(key).
|
||||
Uint32(uint32(len(addrs)))
|
||||
Uint16(uint16(len(addrs)))
|
||||
for i := range addrs {
|
||||
s.AddrPort(addrs[i])
|
||||
}
|
||||
|
||||
@@ -73,10 +73,12 @@ func New(id nonce.ID, key *crypto.Prv, services []Service,
|
||||
|
||||
// Decode an Ad out of the next bytes of a splice.Splice.
|
||||
func (x *Ad) Decode(s *splice.Splice) (e error) {
|
||||
var i, count uint32
|
||||
log.D.S("decoding")
|
||||
var i, count uint16
|
||||
s.ReadID(&x.ID).
|
||||
ReadPubkey(&x.Key).
|
||||
ReadUint32(&count)
|
||||
ReadUint16(&count)
|
||||
log.D.Ln("found services:", count)
|
||||
x.Services = make([]Service, count)
|
||||
for ; i < count; i++ {
|
||||
s.ReadUint16(&x.Services[i].Port).
|
||||
@@ -155,7 +157,7 @@ func ServiceSplice(s *splice.Splice, id nonce.ID, key *crypto.Pub, services []Se
|
||||
s.Magic(Magic).
|
||||
ID(id).
|
||||
Pubkey(key).
|
||||
Uint32(uint32(len(services)))
|
||||
Uint16(uint16(len(services)))
|
||||
for i := range services {
|
||||
s.
|
||||
Uint16(services[i].Port).
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestServiceAd(t *testing.T) {
|
||||
var e error
|
||||
pr, _, _ := crypto.NewSigner()
|
||||
id := nonce.NewID()
|
||||
sv := New(id, pr, []Service{{80, 50000}, {443, 50000}}, time.Now().Add(time.Hour))
|
||||
sv := New(id, pr, []Service{{80, 62346}, {443, 42216}}, time.Now().Add(time.Hour))
|
||||
log.D.S("service", sv)
|
||||
s := splice.New(sv.Len())
|
||||
if e = sv.Encode(s); fails(e) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,6 @@ package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/indra-labs/indra/pkg/crypto"
|
||||
"github.com/indra-labs/indra/pkg/crypto/nonce"
|
||||
"github.com/indra-labs/indra/pkg/engine/node"
|
||||
@@ -10,11 +9,8 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/engine/tpt"
|
||||
"github.com/indra-labs/indra/pkg/engine/transport"
|
||||
log2 "github.com/indra-labs/indra/pkg/proc/log"
|
||||
"github.com/indra-labs/indra/pkg/util/multi"
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
"net/netip"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -22,12 +18,6 @@ var (
|
||||
fails = log.E.Chk
|
||||
)
|
||||
|
||||
// CreateNMockCircuits creates an arbitrary number of mock circuits from the given specification.
|
||||
func CreateNMockCircuits(nCirc int, nReturns int,
|
||||
ctx context.Context) (cl []*Engine, e error) {
|
||||
return createNMockCircuits(false, nCirc, nReturns, ctx)
|
||||
}
|
||||
|
||||
// CreateNMockCircuitsWithSessions creates an arbitrary number of mock circuits
|
||||
// from the given specification, with an arbitrary number of mock sessions.
|
||||
func CreateNMockCircuitsWithSessions(nCirc int, nReturns int,
|
||||
@@ -91,51 +81,3 @@ func createNMockCircuits(inclSessions bool, nCircuits int,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CreateMockEngine creates an indra Engine with a random localhost listener.
|
||||
func CreateMockEngine(seed, dataPath string) (ng *Engine, cancel func(), e error) {
|
||||
defer func(f *error) {
|
||||
if *f != nil {
|
||||
fails(os.RemoveAll(dataPath))
|
||||
}
|
||||
}(&e)
|
||||
var ctx context.Context
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
var keys []*crypto.Keys
|
||||
var k *crypto.Keys
|
||||
if k, e = crypto.GenerateKeys(); fails(e) {
|
||||
return
|
||||
}
|
||||
keys = append(keys, k)
|
||||
var l *transport.Listener
|
||||
if l, e = transport.NewListener([]string{seed},
|
||||
[]string{transport.LocalhostZeroIPv4TCP}, dataPath, k, ctx,
|
||||
transport.DefaultMTU); fails(e) {
|
||||
return
|
||||
}
|
||||
if l == nil {
|
||||
cancel()
|
||||
return nil, nil, errors.New("got nil listener")
|
||||
}
|
||||
sa := transport.GetHostAddress(l.Host)
|
||||
var ap netip.AddrPort
|
||||
var ma multiaddr.Multiaddr
|
||||
if ma, e = multiaddr.NewMultiaddr(sa); fails(e) {
|
||||
return
|
||||
}
|
||||
if ap, e = multi.AddrToAddrPort(ma); fails(e) {
|
||||
return
|
||||
}
|
||||
var nod *node.Node
|
||||
if nod, _ = node.NewNode([]*netip.AddrPort{&ap}, k, nil, 50000); fails(e) {
|
||||
return
|
||||
}
|
||||
if ng, e = New(Params{
|
||||
Transport: transport.NewDuplexByteChan(transport.ConnBufs),
|
||||
Listener: l,
|
||||
Keys: k,
|
||||
Node: nod,
|
||||
}); fails(e) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
88
pkg/engine/mockengine.go
Normal file
88
pkg/engine/mockengine.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/indra-labs/indra/pkg/crypto"
|
||||
"github.com/indra-labs/indra/pkg/engine/node"
|
||||
"github.com/indra-labs/indra/pkg/engine/transport"
|
||||
"github.com/indra-labs/indra/pkg/util/multi"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
"net/netip"
|
||||
"os"
|
||||
)
|
||||
|
||||
// CreateMockEngine creates an indra Engine with a random localhost listener.
|
||||
func CreateMockEngine(seed, dataPath string) (ng *Engine, cancel func(), e error) {
|
||||
defer func(f *error) {
|
||||
if *f != nil {
|
||||
fails(os.RemoveAll(dataPath))
|
||||
}
|
||||
}(&e)
|
||||
var ctx context.Context
|
||||
ctx, cancel = context.WithCancel(context.Background())
|
||||
var keys []*crypto.Keys
|
||||
var k *crypto.Keys
|
||||
if k, e = crypto.GenerateKeys(); fails(e) {
|
||||
return
|
||||
}
|
||||
keys = append(keys, k)
|
||||
var l *transport.Listener
|
||||
if l, e = transport.NewListener([]string{seed},
|
||||
[]string{transport.LocalhostZeroIPv4TCP, transport.LocalhostZeroIPv6TCP}, dataPath, k, ctx,
|
||||
transport.DefaultMTU); fails(e) {
|
||||
return
|
||||
}
|
||||
if l == nil {
|
||||
cancel()
|
||||
return nil, nil, errors.New("got nil listener")
|
||||
}
|
||||
sa := transport.GetHostAddress(l.Host)
|
||||
var ap netip.AddrPort
|
||||
var ma multiaddr.Multiaddr
|
||||
if ma, e = multiaddr.NewMultiaddr(sa); fails(e) {
|
||||
return
|
||||
}
|
||||
if ap, e = multi.AddrToAddrPort(ma); fails(e) {
|
||||
return
|
||||
}
|
||||
var nod *node.Node
|
||||
if nod, _ = node.NewNode([]*netip.AddrPort{&ap}, k, nil, 50000); fails(e) {
|
||||
return
|
||||
}
|
||||
if ng, e = New(Params{
|
||||
Transport: transport.NewDuplexByteChan(transport.ConnBufs),
|
||||
Listener: l,
|
||||
Keys: k,
|
||||
Node: nod,
|
||||
}); fails(e) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CreateAndStartMockEngines(n int) (engines []*Engine, cleanup func(), e error) {
|
||||
cleanup = func() {}
|
||||
var seed string
|
||||
for i := 0; i < n; i++ {
|
||||
dataPath, err := os.MkdirTemp(os.TempDir(), "badger")
|
||||
if err != nil {
|
||||
cleanup()
|
||||
return
|
||||
}
|
||||
var eng *Engine
|
||||
if eng, _, e = CreateMockEngine(seed, dataPath); fails(e) {
|
||||
cleanup()
|
||||
return
|
||||
}
|
||||
engines = append(engines, eng)
|
||||
if i == 0 {
|
||||
seed = transport.GetHostAddress(eng.Listener.Host)
|
||||
}
|
||||
cleanup = func() {
|
||||
cleanup()
|
||||
fails(os.RemoveAll(dataPath))
|
||||
}
|
||||
go eng.Start()
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -94,6 +94,7 @@ func (ng *Engine) HandleAd(p *pubsub.Message) (e error) {
|
||||
if e = c.Decode(s); fails(e) {
|
||||
return
|
||||
}
|
||||
log.D.S("decoded", c)
|
||||
var ok bool
|
||||
switch c.(type) {
|
||||
case *addresses.Ad:
|
||||
@@ -174,6 +175,7 @@ func (ng *Engine) HandleAd(p *pubsub.Message) (e error) {
|
||||
}
|
||||
case *services.Ad:
|
||||
log.D.Ln("received", reflect.TypeOf(c), "from gossip network")
|
||||
log.D.S("message", c)
|
||||
var sa *services.Ad
|
||||
if sa, ok = c.(*services.Ad); !ok {
|
||||
return fmt.Errorf(ErrWrongTypeDecode,
|
||||
|
||||
@@ -11,46 +11,29 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/multi"
|
||||
"github.com/indra-labs/indra/pkg/util/splice"
|
||||
"net/netip"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/indra-labs/indra/pkg/engine/transport"
|
||||
log2 "github.com/indra-labs/indra/pkg/proc/log"
|
||||
)
|
||||
|
||||
func pauza() { time.Sleep(time.Second / 4) }
|
||||
|
||||
func TestEngine_PeerStore(t *testing.T) {
|
||||
if indra.CI == "false" {
|
||||
log2.SetLogLevel(log2.Debug)
|
||||
log2.SetLogLevel(log2.Trace)
|
||||
}
|
||||
const nTotal = 26
|
||||
var cancel func()
|
||||
const nTotal = 10
|
||||
var e error
|
||||
var engines []*Engine
|
||||
var seed string
|
||||
for i := 0; i < nTotal; i++ {
|
||||
dataPath, err := os.MkdirTemp(os.TempDir(), "badger")
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
}
|
||||
var eng *Engine
|
||||
if eng, cancel, e = CreateMockEngine(seed, dataPath); fails(e) {
|
||||
return
|
||||
}
|
||||
engines = append(engines, eng)
|
||||
if i == 0 {
|
||||
seed = transport.GetHostAddress(eng.Listener.Host)
|
||||
}
|
||||
defer os.RemoveAll(dataPath)
|
||||
go eng.Start()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
engines, _, e = CreateAndStartMockEngines(nTotal)
|
||||
adz := engines[0].Listener.Host.Addrs()
|
||||
addrs := make([]*netip.AddrPort, len(adz))
|
||||
for i := range adz {
|
||||
addy, _ := multi.AddrToAddrPort(adz[i])
|
||||
addrs[i] = &addy
|
||||
}
|
||||
pauza()
|
||||
newAddressAd := addresses.New(nonce.NewID(),
|
||||
engines[0].Mgr().GetLocalNodeIdentityPrv(),
|
||||
addrs,
|
||||
@@ -62,7 +45,7 @@ func TestEngine_PeerStore(t *testing.T) {
|
||||
if e = engines[0].SendAd(sa.GetAll()); fails(e) {
|
||||
t.FailNow()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
pauza()
|
||||
newIntroAd := intro.New(nonce.NewID(),
|
||||
engines[0].Mgr().GetLocalNodeIdentityPrv(),
|
||||
engines[0].Mgr().GetLocalNode().Identity.Pub,
|
||||
@@ -75,7 +58,7 @@ func TestEngine_PeerStore(t *testing.T) {
|
||||
if e = engines[0].SendAd(si.GetAll()); fails(e) {
|
||||
t.FailNow()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
pauza()
|
||||
newLoadAd := load.New(nonce.NewID(),
|
||||
engines[0].Mgr().GetLocalNodeIdentityPrv(),
|
||||
17,
|
||||
@@ -87,7 +70,7 @@ func TestEngine_PeerStore(t *testing.T) {
|
||||
if e = engines[0].SendAd(sl.GetAll()); fails(e) {
|
||||
t.FailNow()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
pauza()
|
||||
newPeerAd := peer.New(nonce.NewID(),
|
||||
engines[0].Mgr().GetLocalNodeIdentityPrv(),
|
||||
20000,
|
||||
@@ -100,10 +83,10 @@ func TestEngine_PeerStore(t *testing.T) {
|
||||
if e = engines[0].SendAd(sp.GetAll()); fails(e) {
|
||||
t.FailNow()
|
||||
}
|
||||
time.Sleep(time.Second * 1)
|
||||
pauza()
|
||||
newServiceAd := services.New(nonce.NewID(),
|
||||
engines[0].Mgr().GetLocalNodeIdentityPrv(),
|
||||
[]services.Service{{20000, 54321}},
|
||||
[]services.Service{{20000, 54321}, {10000, 42221}},
|
||||
time.Now().Add(time.Hour*24*7))
|
||||
ss := splice.New(newServiceAd.Len())
|
||||
if e = newServiceAd.Encode(ss); fails(e) {
|
||||
@@ -112,9 +95,9 @@ func TestEngine_PeerStore(t *testing.T) {
|
||||
if e = engines[0].SendAd(ss.GetAll()); fails(e) {
|
||||
t.FailNow()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
cancel()
|
||||
pauza()
|
||||
for i := range engines {
|
||||
engines[i].Shutdown()
|
||||
}
|
||||
pauza()
|
||||
}
|
||||
|
||||
@@ -35,6 +35,10 @@ const (
|
||||
// tests.
|
||||
LocalhostZeroIPv4TCP = "/ip4/127.0.0.1/tcp/0"
|
||||
|
||||
// LocalhostZeroIPv6TCP is the default localhost to bind to any address. Used in
|
||||
// tests.
|
||||
LocalhostZeroIPv6TCP = "/ip6/::1/tcp/0"
|
||||
|
||||
// LocalhostZeroIPv4QUIC - Don't use. Buffer problems on linux and fails on CI.
|
||||
// LocalhostZeroIPv4QUIC = "/ip4/127.0.0.1/udp/0/quic"
|
||||
|
||||
@@ -198,10 +202,10 @@ func (l *Listener) ProtocolsAvailable() (p protocols.NetworkProtocols) {
|
||||
return protocols.IP4 | protocols.IP6
|
||||
}
|
||||
for _, v := range l.Host.Addrs() {
|
||||
if _, e := v.ValueForProtocol(multiaddr.P_IP4); fails(e) {
|
||||
if _, e := v.ValueForProtocol(multiaddr.P_IP4); e != nil {
|
||||
p &= protocols.IP4
|
||||
}
|
||||
if _, e := v.ValueForProtocol(multiaddr.P_IP4); fails(e) {
|
||||
if _, e := v.ValueForProtocol(multiaddr.P_IP4); e != nil {
|
||||
p &= protocols.IP6
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func AddrToAddrPort(ma multiaddr.Multiaddr) (ap netip.AddrPort, e error) {
|
||||
}
|
||||
var addrStr string
|
||||
var is6 bool
|
||||
if addrStr, e = ma.ValueForProtocol(multiaddr.P_IP4); fails(e) {
|
||||
if addrStr, e = ma.ValueForProtocol(multiaddr.P_IP4); e != nil {
|
||||
if addrStr, e = ma.ValueForProtocol(multiaddr.P_IP6); fails(e) {
|
||||
return
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user