Create and unwrap Ping message type
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/Indra-Labs/indra"
|
||||
"github.com/Indra-Labs/indra/pkg/ifc"
|
||||
"github.com/Indra-Labs/indra/pkg/key/prv"
|
||||
"github.com/Indra-Labs/indra/pkg/key/pub"
|
||||
"github.com/Indra-Labs/indra/pkg/nonce"
|
||||
log2 "github.com/cybriq/proc/pkg/log"
|
||||
@@ -25,20 +26,25 @@ var (
|
||||
type Node struct {
|
||||
nonce.ID
|
||||
*netip.AddrPort
|
||||
HeaderKey, PayloadKey *pub.Key
|
||||
HeaderKey, PayloadKey *pub.Key
|
||||
HeaderPriv, PayloadPriv *prv.Key
|
||||
ifc.Transport
|
||||
}
|
||||
|
||||
// New creates a new Node. net.IP is optional if the counterparty is not in
|
||||
// direct connection.
|
||||
func New(ip *netip.AddrPort, fwd, rtn *pub.Key, tpt ifc.Transport) (n *Node, id nonce.ID) {
|
||||
func New(ip *netip.AddrPort, hdr, pld *pub.Key, hdrPriv, pldPriv *prv.Key,
|
||||
tpt ifc.Transport) (n *Node, id nonce.ID) {
|
||||
|
||||
id = nonce.NewID()
|
||||
n = &Node{
|
||||
ID: id,
|
||||
AddrPort: ip,
|
||||
Transport: tpt,
|
||||
HeaderKey: fwd,
|
||||
PayloadKey: rtn,
|
||||
ID: id,
|
||||
AddrPort: ip,
|
||||
Transport: tpt,
|
||||
HeaderKey: hdr,
|
||||
PayloadKey: pld,
|
||||
HeaderPriv: hdrPriv,
|
||||
PayloadPriv: pldPriv,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func TestNodes_FindByID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodes_FindByIP(t *testing.T) {
|
||||
func TestNodes_FindByAddrPort(t *testing.T) {
|
||||
n := NewNodes()
|
||||
const nNodes = 10000
|
||||
for i := 0; i < nNodes; i++ {
|
||||
|
||||
@@ -2,11 +2,14 @@ package testutils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
|
||||
"github.com/Indra-Labs/indra"
|
||||
"github.com/Indra-Labs/indra/pkg/key/prv"
|
||||
"github.com/Indra-Labs/indra/pkg/key/pub"
|
||||
"github.com/Indra-Labs/indra/pkg/sha256"
|
||||
"github.com/Indra-Labs/indra/pkg/slice"
|
||||
log2 "github.com/cybriq/proc/pkg/log"
|
||||
)
|
||||
|
||||
@@ -37,3 +40,16 @@ func GenerateTestKeyPairs() (sp, rp *prv.Key, sP, rP *pub.Key, e error) {
|
||||
rP = pub.Derive(rp)
|
||||
return
|
||||
}
|
||||
|
||||
func GenerateRandomAddrPortIPv4() (ap *netip.AddrPort) {
|
||||
a := netip.AddrPort{}
|
||||
b := make([]byte, 7)
|
||||
_, e := rand.Read(b)
|
||||
if check(e) {
|
||||
log.E.Ln(e)
|
||||
}
|
||||
port := slice.DecodeUint16(b[5:7])
|
||||
str := fmt.Sprintf("%d.%d.%d.%d:%d", b[1], b[2], b[3], b[4], port)
|
||||
a, e = netip.ParseAddrPort(str)
|
||||
return &a
|
||||
}
|
||||
|
||||
@@ -21,10 +21,11 @@ import (
|
||||
// an increment of their liveness score. By using this scheme, when nodes are
|
||||
// offline their scores will fall to zero after a time whereas live nodes will
|
||||
// have steadily increasing scores from successful pings.
|
||||
func Ping(id nonce.ID, client node.Node, hop [3]node.Node,
|
||||
set signer.KeySet) types.Onion {
|
||||
func Ping(id nonce.ID, client *node.Node, hop [3]*node.Node,
|
||||
set *signer.KeySet) types.Onion {
|
||||
|
||||
return OnionSkins{}.
|
||||
Forward(hop[0].AddrPort).
|
||||
OnionSkin(address.FromPubKey(hop[0].HeaderKey), set.Next()).
|
||||
Forward(hop[1].AddrPort).
|
||||
OnionSkin(address.FromPubKey(hop[1].HeaderKey), set.Next()).
|
||||
|
||||
172
pkg/wire/onion_test.go
Normal file
172
pkg/wire/onion_test.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package wire
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/Indra-Labs/indra/pkg/key/pub"
|
||||
"github.com/Indra-Labs/indra/pkg/key/signer"
|
||||
"github.com/Indra-Labs/indra/pkg/node"
|
||||
"github.com/Indra-Labs/indra/pkg/nonce"
|
||||
"github.com/Indra-Labs/indra/pkg/slice"
|
||||
"github.com/Indra-Labs/indra/pkg/testutils"
|
||||
"github.com/Indra-Labs/indra/pkg/types"
|
||||
"github.com/Indra-Labs/indra/pkg/wire/confirmation"
|
||||
"github.com/Indra-Labs/indra/pkg/wire/forward"
|
||||
"github.com/Indra-Labs/indra/pkg/wire/layer"
|
||||
log2 "github.com/cybriq/proc/pkg/log"
|
||||
)
|
||||
|
||||
func TestPing(t *testing.T) {
|
||||
log2.CodeLoc = true
|
||||
_, ks, e := signer.New()
|
||||
if check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var hop [3]*node.Node
|
||||
for i := range hop {
|
||||
prv1, prv2 := GetTwoPrvKeys(t)
|
||||
pub1, pub2 := pub.Derive(prv1), pub.Derive(prv2)
|
||||
var n nonce.ID
|
||||
hop[i], n = node.New(testutils.GenerateRandomAddrPortIPv4(),
|
||||
pub1, pub2, prv1, prv2, nil)
|
||||
_ = n
|
||||
}
|
||||
cprv1, cprv2 := GetTwoPrvKeys(t)
|
||||
cpub1, cpub2 := pub.Derive(cprv1), pub.Derive(cprv2)
|
||||
var n nonce.ID
|
||||
var client *node.Node
|
||||
client, n = node.New(testutils.GenerateRandomAddrPortIPv4(),
|
||||
cpub1, cpub2, cprv1, cprv2, nil)
|
||||
on := Ping(n, client, hop, ks)
|
||||
b := EncodeOnion(on)
|
||||
c := slice.NewCursor()
|
||||
|
||||
var ok bool
|
||||
var on0 types.Onion
|
||||
if on0, e = PeelOnion(b, c); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var f0 *forward.OnionSkin
|
||||
if f0, ok = on0.(*forward.OnionSkin); !ok {
|
||||
t.Error("did not unwrap expected type", reflect.TypeOf(f0))
|
||||
t.FailNow()
|
||||
}
|
||||
if hop[0].AddrPort.String() != f0.AddrPort.String() {
|
||||
t.Errorf("failed to unwrap expected: '%s', got '%s'",
|
||||
hop[0].AddrPort.String(), f0.AddrPort.String())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
var on1 types.Onion
|
||||
if on1, e = PeelOnion(b, c); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var l0 *layer.OnionSkin
|
||||
if l0, ok = on1.(*layer.OnionSkin); !ok {
|
||||
t.Error("did not unwrap expected type", reflect.TypeOf(l0))
|
||||
t.FailNow()
|
||||
}
|
||||
l0.Decrypt(hop[0].HeaderPriv, b, c)
|
||||
var on2 types.Onion
|
||||
if on2, e = PeelOnion(b, c); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var f1 *forward.OnionSkin
|
||||
if f1, ok = on2.(*forward.OnionSkin); !ok {
|
||||
t.Error("did not unwrap expected type", reflect.TypeOf(on2))
|
||||
t.FailNow()
|
||||
}
|
||||
if hop[1].AddrPort.String() != f1.AddrPort.String() {
|
||||
t.Errorf("failed to unwrap expected: '%s', got '%s'",
|
||||
hop[1].AddrPort.String(), f1.AddrPort.String())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
var on3 types.Onion
|
||||
if on3, e = PeelOnion(b, c); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var l1 *layer.OnionSkin
|
||||
if l1, ok = on3.(*layer.OnionSkin); !ok {
|
||||
t.Error("did not unwrap expected type", reflect.TypeOf(l1))
|
||||
t.FailNow()
|
||||
}
|
||||
l1.Decrypt(hop[1].HeaderPriv, b, c)
|
||||
var on4 types.Onion
|
||||
if on4, e = PeelOnion(b, c); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var f2 *forward.OnionSkin
|
||||
if f2, ok = on4.(*forward.OnionSkin); !ok {
|
||||
t.Error("did not unwrap expected type", reflect.TypeOf(on2))
|
||||
t.FailNow()
|
||||
}
|
||||
if hop[2].AddrPort.String() != f2.AddrPort.String() {
|
||||
t.Errorf("failed to unwrap expected: '%s', got '%s'",
|
||||
hop[2].AddrPort.String(), f2.AddrPort.String())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
var on5 types.Onion
|
||||
if on5, e = PeelOnion(b, c); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var l2 *layer.OnionSkin
|
||||
if l2, ok = on5.(*layer.OnionSkin); !ok {
|
||||
t.Error("did not unwrap expected type", reflect.TypeOf(l1))
|
||||
t.FailNow()
|
||||
}
|
||||
l2.Decrypt(hop[2].HeaderPriv, b, c)
|
||||
var on6 types.Onion
|
||||
if on6, e = PeelOnion(b, c); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var f3 *forward.OnionSkin
|
||||
if f3, ok = on6.(*forward.OnionSkin); !ok {
|
||||
t.Error("did not unwrap expected type", reflect.TypeOf(on2))
|
||||
t.FailNow()
|
||||
}
|
||||
if client.AddrPort.String() != f3.AddrPort.String() {
|
||||
t.Errorf("failed to unwrap expected: '%s', got '%s'",
|
||||
client.AddrPort.String(), f3.AddrPort.String())
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
var on7 types.Onion
|
||||
if on7, e = PeelOnion(b, c); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var l3 *layer.OnionSkin
|
||||
if l3, ok = on7.(*layer.OnionSkin); !ok {
|
||||
t.Error("did not unwrap expected type", reflect.TypeOf(l1))
|
||||
t.FailNow()
|
||||
}
|
||||
l3.Decrypt(client.HeaderPriv, b, c)
|
||||
var on8 types.Onion
|
||||
if on8, e = PeelOnion(b, c); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
var co *confirmation.OnionSkin
|
||||
if co, ok = on8.(*confirmation.OnionSkin); !ok {
|
||||
t.Error("did not unwrap expected type", reflect.TypeOf(on8))
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if co.ID != n {
|
||||
t.Error("did not unwrap expected confirmation nonce")
|
||||
t.FailNow()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,11 +10,11 @@ var (
|
||||
// GitRef is the gitref, as in refs/heads/branchname.
|
||||
GitRef = "refs/heads/main"
|
||||
// ParentGitCommit is the commit hash of the parent HEAD.
|
||||
ParentGitCommit = "f28a26ba8f93a306617a498ce6a3a4f033ea9c35"
|
||||
ParentGitCommit = "308ecfa7e65d2326509706e8f55da0a20bee008e"
|
||||
// BuildTime stores the time when the current binary was built.
|
||||
BuildTime = "2022-12-27T12:27:13Z"
|
||||
BuildTime = "2022-12-27T14:09:57Z"
|
||||
// SemVer lists the (latest) git tag on the build.
|
||||
SemVer = "v0.0.238"
|
||||
SemVer = "v0.0.239"
|
||||
// PathBase is the path base returned from runtime caller.
|
||||
PathBase = "/home/loki/src/github.com/Indra-Labs/indra/"
|
||||
// Major is the major number from the tag.
|
||||
@@ -22,7 +22,7 @@ var (
|
||||
// Minor is the minor number from the tag.
|
||||
Minor = 0
|
||||
// Patch is the patch version number from the tag.
|
||||
Patch = 238
|
||||
Patch = 239
|
||||
)
|
||||
|
||||
// Version returns a pretty printed version information string.
|
||||
|
||||
Reference in New Issue
Block a user