rename Engine receiver to en
This commit is contained in:
@@ -59,21 +59,21 @@ func NewClient(tpt types.Transport, hdrPrv *prv.Key, no *node.Node,
|
||||
}
|
||||
|
||||
// Start a single thread of the Engine.
|
||||
func (cl *Engine) Start() {
|
||||
func (en *Engine) Start() {
|
||||
for {
|
||||
if cl.handler() {
|
||||
if en.handler() {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (cl *Engine) RegisterConfirmation(hook confirm.Hook,
|
||||
func (en *Engine) RegisterConfirmation(hook confirm.Hook,
|
||||
cnf nonce.ID) {
|
||||
|
||||
if hook == nil {
|
||||
return
|
||||
}
|
||||
cl.Confirms.Add(&confirm.Callback{
|
||||
en.Confirms.Add(&confirm.Callback{
|
||||
ID: cnf,
|
||||
Time: time.Now(),
|
||||
Hook: hook,
|
||||
@@ -82,19 +82,19 @@ func (cl *Engine) RegisterConfirmation(hook confirm.Hook,
|
||||
|
||||
// Cleanup closes and flushes any resources the client opened that require sync
|
||||
// in order to reopen correctly.
|
||||
func (cl *Engine) Cleanup() {
|
||||
func (en *Engine) Cleanup() {
|
||||
// Do cleanup stuff before shutdown.
|
||||
}
|
||||
|
||||
// Shutdown triggers the shutdown of the client and the Cleanup before
|
||||
// finishing.
|
||||
func (cl *Engine) Shutdown() {
|
||||
if cl.ShuttingDown.Load() {
|
||||
func (en *Engine) Shutdown() {
|
||||
if en.ShuttingDown.Load() {
|
||||
return
|
||||
}
|
||||
log.T.C(func() string {
|
||||
return "shutting down client " + cl.Node.AddrPort.String()
|
||||
return "shutting down client " + en.Node.AddrPort.String()
|
||||
})
|
||||
cl.ShuttingDown.Store(true)
|
||||
cl.C.Q()
|
||||
en.ShuttingDown.Store(true)
|
||||
en.C.Q()
|
||||
}
|
||||
|
||||
@@ -10,12 +10,12 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) balance(on *balance.Layer,
|
||||
func (en *Engine) balance(on *balance.Layer,
|
||||
b slice.Bytes, c *slice.Cursor, prev types.Onion) {
|
||||
|
||||
log.T.S(on.ConfID)
|
||||
|
||||
cl.IterateSessions(func(s *traffic.Session) bool {
|
||||
en.IterateSessions(func(s *traffic.Session) bool {
|
||||
if s.ID == on.ID {
|
||||
log.D.F("received balance %x for session %x",
|
||||
on.MilliSatoshi, on.ID)
|
||||
@@ -26,22 +26,22 @@ func (cl *Engine) balance(on *balance.Layer,
|
||||
}
|
||||
return false
|
||||
})
|
||||
pending := cl.PendingResponses.Find(sha256.Single(on.ID[:]))
|
||||
pending := en.PendingResponses.Find(sha256.Single(on.ID[:]))
|
||||
if pending != nil {
|
||||
for i := range pending.Billable {
|
||||
s := cl.FindSession(pending.Billable[i])
|
||||
s := en.FindSession(pending.Billable[i])
|
||||
if s != nil {
|
||||
log.D.Ln(cl.AddrPort.String(), "post acct")
|
||||
log.D.Ln(en.AddrPort.String(), "post acct")
|
||||
if i == 0 {
|
||||
cl.DecSession(s.ID,
|
||||
en.DecSession(s.ID,
|
||||
s.RelayRate*lnwire.MilliSatoshi(len(b)/2)/1024/1024)
|
||||
} else {
|
||||
cl.DecSession(s.ID,
|
||||
en.DecSession(s.ID,
|
||||
s.RelayRate*lnwire.MilliSatoshi(len(b))/1024/1024)
|
||||
}
|
||||
}
|
||||
}
|
||||
cl.PendingResponses.Delete(pending.Hash)
|
||||
en.PendingResponses.Delete(pending.Hash)
|
||||
}
|
||||
cl.Confirms.Confirm(on.ConfID)
|
||||
en.Confirms.Confirm(on.ConfID)
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) confirm(on *confirm.Layer,
|
||||
func (en *Engine) confirm(on *confirm.Layer,
|
||||
b slice.Bytes, c *slice.Cursor, prev types.Onion) {
|
||||
|
||||
// When a confirm arrives check if it is registered for and run
|
||||
// the hook that was registered with it.
|
||||
log.T.S(cl.Confirms)
|
||||
cl.Confirms.Confirm(on.ID)
|
||||
log.T.S(en.Confirms)
|
||||
en.Confirms.Confirm(on.ID)
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) crypt(on *crypt.Layer, b slice.Bytes,
|
||||
func (en *Engine) crypt(on *crypt.Layer, b slice.Bytes,
|
||||
c *slice.Cursor, prev types.Onion) {
|
||||
|
||||
// this is probably an encrypted crypt for us.
|
||||
hdr, _, sess, identity := cl.FindCloaked(on.Cloak)
|
||||
hdr, _, sess, identity := en.FindCloaked(on.Cloak)
|
||||
if hdr == nil {
|
||||
log.T.Ln("no matching key found from cloaked key")
|
||||
return
|
||||
@@ -34,7 +34,7 @@ func (cl *Engine) crypt(on *crypt.Layer, b slice.Bytes,
|
||||
return
|
||||
}
|
||||
|
||||
cl.handleMessage(BudgeUp(b, *c), on)
|
||||
en.handleMessage(BudgeUp(b, *c), on)
|
||||
return
|
||||
}
|
||||
if string(b[*c:][:magicbytes.Len]) == directbalance.MagicString {
|
||||
@@ -47,7 +47,7 @@ func (cl *Engine) crypt(on *crypt.Layer, b slice.Bytes,
|
||||
var balID, confID nonce.ID
|
||||
switch db := on1.(type) {
|
||||
case *directbalance.Layer:
|
||||
log.T.S(cl.AddrPort.String(), db, b[*c:].ToBytes())
|
||||
log.T.S(en.AddrPort.String(), db, b[*c:].ToBytes())
|
||||
balID = db.ID
|
||||
confID = db.ConfID
|
||||
default:
|
||||
@@ -59,17 +59,17 @@ func (cl *Engine) crypt(on *crypt.Layer, b slice.Bytes,
|
||||
}
|
||||
switch fwd := on2.(type) {
|
||||
case *forward.Layer:
|
||||
log.T.S(cl.AddrPort.String(), fwd)
|
||||
log.T.S(en.AddrPort.String(), fwd)
|
||||
o := (&onion.Skins{}).
|
||||
Forward(fwd.AddrPort).
|
||||
Crypt(pub.Derive(hdr), nil, cl.KeySet.Next(), nonce.New(), 0).
|
||||
Crypt(pub.Derive(hdr), nil, en.KeySet.Next(), nonce.New(), 0).
|
||||
Balance(balID, confID, sess.Remaining)
|
||||
rb := onion.Encode(o.Assemble())
|
||||
cl.Send(fwd.AddrPort, rb)
|
||||
// cl.SendOnion(fwd.AddrPort, o)
|
||||
log.D.Ln(cl.AddrPort.String(), "directbalance reply")
|
||||
cl.DecSession(sess.ID,
|
||||
cl.RelayRate*lnwire.MilliSatoshi(len(b)/2+len(rb)/2)/1024/1024)
|
||||
en.Send(fwd.AddrPort, rb)
|
||||
// en.SendOnion(fwd.AddrPort, o)
|
||||
log.D.Ln(en.AddrPort.String(), "directbalance reply")
|
||||
en.DecSession(sess.ID,
|
||||
en.RelayRate*lnwire.MilliSatoshi(len(b)/2+len(rb)/2)/1024/1024)
|
||||
return
|
||||
default:
|
||||
log.T.Ln("dropping directbalance without following " +
|
||||
@@ -78,5 +78,5 @@ func (cl *Engine) crypt(on *crypt.Layer, b slice.Bytes,
|
||||
}
|
||||
return
|
||||
}
|
||||
cl.handleMessage(BudgeUp(b, *c), on)
|
||||
en.handleMessage(BudgeUp(b, *c), on)
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) delay(on *delay.Layer, b slice.Bytes,
|
||||
func (en *Engine) delay(on *delay.Layer, b slice.Bytes,
|
||||
c *slice.Cursor, prev types.Onion) {
|
||||
|
||||
// this is a message to hold the message in the buffer until a duration
|
||||
@@ -19,5 +19,5 @@ func (cl *Engine) delay(on *delay.Layer, b slice.Bytes,
|
||||
select {
|
||||
case <-time.After(on.Duration):
|
||||
}
|
||||
cl.handleMessage(BudgeUp(b, *c), on)
|
||||
en.handleMessage(BudgeUp(b, *c), on)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) exit(ex *exit.Layer, b slice.Bytes,
|
||||
func (en *Engine) exit(ex *exit.Layer, b slice.Bytes,
|
||||
c *slice.Cursor, prev types.Onion) {
|
||||
|
||||
// payload is forwarded to a local port and the result is forwarded
|
||||
@@ -23,28 +23,28 @@ func (cl *Engine) exit(ex *exit.Layer, b slice.Bytes,
|
||||
var result slice.Bytes
|
||||
h := sha256.Single(ex.Bytes)
|
||||
log.T.S(h)
|
||||
if e = cl.SendTo(ex.Port, ex.Bytes); check(e) {
|
||||
if e = en.SendTo(ex.Port, ex.Bytes); check(e) {
|
||||
return
|
||||
}
|
||||
timer := time.NewTicker(time.Second * 5) // todo: timeout/retries etc
|
||||
select {
|
||||
case result = <-cl.ReceiveFrom(ex.Port):
|
||||
case result = <-en.ReceiveFrom(ex.Port):
|
||||
case <-timer.C:
|
||||
}
|
||||
// We need to wrap the result in a message crypt. The client recognises
|
||||
// the context of the response by the hash of the request message.
|
||||
cl.Lock()
|
||||
en.Lock()
|
||||
res := onion.Encode(&response.Layer{
|
||||
Hash: h,
|
||||
Load: cl.Load,
|
||||
Load: en.Load,
|
||||
Bytes: result,
|
||||
})
|
||||
cl.Unlock()
|
||||
en.Unlock()
|
||||
rb := FormatReply(b[*c:c.Inc(crypt.ReverseHeaderLen)],
|
||||
res, ex.Ciphers, ex.Nonces)
|
||||
switch on := prev.(type) {
|
||||
case *crypt.Layer:
|
||||
sess := cl.FindSessionByHeader(on.ToPriv)
|
||||
sess := en.FindSessionByHeader(on.ToPriv)
|
||||
if sess == nil {
|
||||
break
|
||||
}
|
||||
@@ -57,9 +57,9 @@ func (cl *Engine) exit(ex *exit.Layer, b slice.Bytes,
|
||||
out := sess.Services[i].RelayRate *
|
||||
lnwire.MilliSatoshi(len(rb)) / 2 / 1024 / 1024
|
||||
log.D.Ln(sess.AddrPort.String(), "exit send")
|
||||
cl.DecSession(sess.ID, in+out)
|
||||
en.DecSession(sess.ID, in+out)
|
||||
break
|
||||
}
|
||||
}
|
||||
cl.handleMessage(rb, ex)
|
||||
en.handleMessage(rb, ex)
|
||||
}
|
||||
|
||||
@@ -9,25 +9,25 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) forward(on *forward.Layer, b slice.Bytes,
|
||||
func (en *Engine) forward(on *forward.Layer, b slice.Bytes,
|
||||
c *slice.Cursor, prev types.Onion) {
|
||||
|
||||
// forward the whole buffer received onwards. Usually there will be a
|
||||
// crypt.Layer under this which will be unwrapped by the receiver.
|
||||
if on.AddrPort.String() == cl.Node.AddrPort.String() {
|
||||
if on.AddrPort.String() == en.Node.AddrPort.String() {
|
||||
// it is for us, we want to unwrap the next part.
|
||||
cl.handleMessage(BudgeUp(b, *c), on)
|
||||
en.handleMessage(BudgeUp(b, *c), on)
|
||||
} else {
|
||||
switch on1 := prev.(type) {
|
||||
case *crypt.Layer:
|
||||
sess := cl.FindSessionByHeader(on1.ToPriv)
|
||||
sess := en.FindSessionByHeader(on1.ToPriv)
|
||||
if sess != nil {
|
||||
log.D.Ln(on.AddrPort.String(), "forward forward")
|
||||
cl.DecSession(sess.ID,
|
||||
cl.RelayRate*lnwire.MilliSatoshi(len(b))/1024/1024)
|
||||
en.DecSession(sess.ID,
|
||||
en.RelayRate*lnwire.MilliSatoshi(len(b))/1024/1024)
|
||||
}
|
||||
}
|
||||
// we need to forward this message onion.
|
||||
cl.Send(on.AddrPort, b)
|
||||
en.Send(on.AddrPort, b)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,13 +14,13 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) getBalance(on *getbalance.Layer,
|
||||
func (en *Engine) getBalance(on *getbalance.Layer,
|
||||
b slice.Bytes, c *slice.Cursor, prev types.Onion) {
|
||||
|
||||
log.T.S(on)
|
||||
var found bool
|
||||
var bal *balance.Layer
|
||||
cl.IterateSessions(func(s *traffic.Session) bool {
|
||||
en.IterateSessions(func(s *traffic.Session) bool {
|
||||
if s.ID == on.ID {
|
||||
bal = &balance.Layer{
|
||||
ID: on.ID,
|
||||
@@ -41,15 +41,15 @@ func (cl *Engine) getBalance(on *getbalance.Layer,
|
||||
rb = append(rb, slice.NoisePad(714-len(rb))...)
|
||||
switch on1 := prev.(type) {
|
||||
case *crypt.Layer:
|
||||
sess := cl.FindSessionByHeader(on1.ToPriv)
|
||||
sess := en.FindSessionByHeader(on1.ToPriv)
|
||||
if sess != nil {
|
||||
log.D.Ln("getbalance reply")
|
||||
in := sess.RelayRate *
|
||||
lnwire.MilliSatoshi(len(b)) / 2 / 1024 / 1024
|
||||
out := sess.RelayRate *
|
||||
lnwire.MilliSatoshi(len(rb)) / 2 / 1024 / 1024
|
||||
cl.DecSession(sess.ID, in+out)
|
||||
en.DecSession(sess.ID, in+out)
|
||||
}
|
||||
}
|
||||
cl.handleMessage(rb, on)
|
||||
en.handleMessage(rb, on)
|
||||
}
|
||||
|
||||
@@ -9,37 +9,37 @@ import (
|
||||
)
|
||||
|
||||
// response is a payload from an exit message.
|
||||
func (cl *Engine) response(on *response.Layer, b slice.Bytes,
|
||||
func (en *Engine) response(on *response.Layer, b slice.Bytes,
|
||||
cur *slice.Cursor, prev types.Onion) {
|
||||
|
||||
pending := cl.PendingResponses.Find(on.Hash)
|
||||
pending := en.PendingResponses.Find(on.Hash)
|
||||
first := true
|
||||
var rr lnwire.MilliSatoshi
|
||||
if pending != nil {
|
||||
for i := range pending.Billable {
|
||||
if first {
|
||||
first = false
|
||||
s := cl.FindSession(pending.Billable[i])
|
||||
s := en.FindSession(pending.Billable[i])
|
||||
for i := range s.Services {
|
||||
if s.Services[i].Port == pending.Port {
|
||||
rr = s.Services[i].RelayRate
|
||||
}
|
||||
}
|
||||
if s != nil {
|
||||
log.D.Ln(cl.AddrPort.String(), "exit send", i)
|
||||
cl.DecSession(s.ID, rr*lnwire.
|
||||
log.D.Ln(en.AddrPort.String(), "exit send", i)
|
||||
en.DecSession(s.ID, rr*lnwire.
|
||||
MilliSatoshi(len(b)/2)/1024/1024)
|
||||
}
|
||||
continue
|
||||
}
|
||||
s := cl.FindSession(pending.Billable[i])
|
||||
s := en.FindSession(pending.Billable[i])
|
||||
if s != nil {
|
||||
log.D.Ln(cl.AddrPort.String(), "reverse")
|
||||
cl.DecSession(s.ID, s.RelayRate*lnwire.
|
||||
log.D.Ln(en.AddrPort.String(), "reverse")
|
||||
en.DecSession(s.ID, s.RelayRate*lnwire.
|
||||
MilliSatoshi(len(b))/1024/1024)
|
||||
}
|
||||
}
|
||||
pending.Callback(on.Bytes)
|
||||
cl.PendingResponses.Delete(on.Hash)
|
||||
en.PendingResponses.Delete(on.Hash)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,12 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) reverse(on *reverse.Layer, b slice.Bytes,
|
||||
func (en *Engine) reverse(on *reverse.Layer, b slice.Bytes,
|
||||
c *slice.Cursor, prev types.Onion) {
|
||||
|
||||
var e error
|
||||
var on2 types.Onion
|
||||
if on.AddrPort.String() == cl.Node.AddrPort.String() {
|
||||
if on.AddrPort.String() == en.Node.AddrPort.String() {
|
||||
if on2, e = onion.Peel(b, c); check(e) {
|
||||
return
|
||||
}
|
||||
@@ -27,10 +27,10 @@ func (cl *Engine) reverse(on *reverse.Layer, b slice.Bytes,
|
||||
second := first + crypt.ReverseLayerLen
|
||||
last := second + crypt.ReverseLayerLen
|
||||
log.T.Ln("searching for reverse crypt keys")
|
||||
hdr, pld, _, _ := cl.FindCloaked(on1.Cloak)
|
||||
hdr, pld, _, _ := en.FindCloaked(on1.Cloak)
|
||||
if hdr == nil || pld == nil {
|
||||
log.E.F("failed to find key for %s",
|
||||
cl.Node.AddrPort.String())
|
||||
en.Node.AddrPort.String())
|
||||
return
|
||||
}
|
||||
// We need to find the PayloadPub to match.
|
||||
@@ -50,16 +50,16 @@ func (cl *Engine) reverse(on *reverse.Layer, b slice.Bytes,
|
||||
if b[start:start+2].String() != reverse.MagicString {
|
||||
// It's for us!
|
||||
log.D.Ln("handling response")
|
||||
cl.handleMessage(BudgeUp(b, last), on)
|
||||
en.handleMessage(BudgeUp(b, last), on)
|
||||
break
|
||||
}
|
||||
sess := cl.FindSessionByHeader(hdr)
|
||||
sess := en.FindSessionByHeader(hdr)
|
||||
if sess != nil {
|
||||
log.D.Ln(on.AddrPort.String(), "reverse receive")
|
||||
cl.DecSession(sess.ID,
|
||||
cl.RelayRate*lnwire.MilliSatoshi(len(b))/1024/1024)
|
||||
en.DecSession(sess.ID,
|
||||
en.RelayRate*lnwire.MilliSatoshi(len(b))/1024/1024)
|
||||
}
|
||||
cl.handleMessage(BudgeUp(b, start), on)
|
||||
en.handleMessage(BudgeUp(b, start), on)
|
||||
default:
|
||||
// If a reverse is not followed by an onion crypt the
|
||||
// message is incorrectly formed, just drop it.
|
||||
@@ -68,7 +68,7 @@ func (cl *Engine) reverse(on *reverse.Layer, b slice.Bytes,
|
||||
} else {
|
||||
// we need to forward this message onion.
|
||||
log.D.Ln("forwarding reverse")
|
||||
cl.Send(on.AddrPort, b)
|
||||
en.Send(on.AddrPort, b)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,23 +11,23 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) session(on *session.Layer, b slice.Bytes,
|
||||
func (en *Engine) session(on *session.Layer, b slice.Bytes,
|
||||
c *slice.Cursor, prev types.Onion) {
|
||||
|
||||
log.T.C(func() string {
|
||||
return fmt.Sprint("incoming session",
|
||||
spew.Sdump(on.PreimageHash()))
|
||||
})
|
||||
pi := cl.FindPendingPreimage(on.PreimageHash())
|
||||
pi := en.FindPendingPreimage(on.PreimageHash())
|
||||
if pi != nil {
|
||||
// We need to delete this first in case somehow two such
|
||||
// messages arrive at the same time, and we end up with
|
||||
// duplicate sessions.
|
||||
cl.DeletePendingPayment(pi.Preimage)
|
||||
en.DeletePendingPayment(pi.Preimage)
|
||||
log.T.F("Adding session %x\n", pi.ID)
|
||||
cl.AddSession(traffic.NewSession(pi.ID,
|
||||
cl.Node.Peer, pi.Amount, on.Header, on.Payload, on.Hop))
|
||||
cl.handleMessage(BudgeUp(b, *c), on)
|
||||
en.AddSession(traffic.NewSession(pi.ID,
|
||||
en.Node.Peer, pi.Amount, on.Header, on.Payload, on.Hop))
|
||||
en.handleMessage(BudgeUp(b, *c), on)
|
||||
} else {
|
||||
log.T.Ln("dropping session message without payment")
|
||||
}
|
||||
|
||||
@@ -22,23 +22,23 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) handler() (out bool) {
|
||||
func (en *Engine) handler() (out bool) {
|
||||
log.T.C(func() string {
|
||||
return cl.AddrPort.String() +
|
||||
return en.AddrPort.String() +
|
||||
" awaiting message"
|
||||
})
|
||||
var prev types.Onion
|
||||
select {
|
||||
case <-cl.C.Wait():
|
||||
cl.Cleanup()
|
||||
case <-en.C.Wait():
|
||||
en.Cleanup()
|
||||
out = true
|
||||
break
|
||||
case b := <-cl.Node.Receive():
|
||||
cl.handleMessage(b, prev)
|
||||
case p := <-cl.PaymentChan:
|
||||
log.T.S("incoming payment", cl.AddrPort.String(), p)
|
||||
case b := <-en.Node.Receive():
|
||||
en.handleMessage(b, prev)
|
||||
case p := <-en.PaymentChan:
|
||||
log.T.S("incoming payment", en.AddrPort.String(), p)
|
||||
topUp := false
|
||||
cl.IterateSessions(func(s *traffic.Session) bool {
|
||||
en.IterateSessions(func(s *traffic.Session) bool {
|
||||
if s.Preimage == p.Preimage {
|
||||
s.IncSats(p.Amount)
|
||||
topUp = true
|
||||
@@ -49,7 +49,7 @@ func (cl *Engine) handler() (out bool) {
|
||||
return false
|
||||
})
|
||||
if !topUp {
|
||||
cl.AddPendingPayment(p)
|
||||
en.AddPendingPayment(p)
|
||||
log.T.F("awaiting session keys for preimage %x",
|
||||
p.Preimage)
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func (cl *Engine) handler() (out bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (cl *Engine) handleMessage(b slice.Bytes, prev types.Onion) {
|
||||
func (en *Engine) handleMessage(b slice.Bytes, prev types.Onion) {
|
||||
// process received message
|
||||
var on types.Onion
|
||||
var e error
|
||||
@@ -67,35 +67,35 @@ func (cl *Engine) handleMessage(b slice.Bytes, prev types.Onion) {
|
||||
}
|
||||
switch on := on.(type) {
|
||||
case *balance.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.balance(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.balance(on, b, c, prev)
|
||||
case *confirm.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.confirm(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.confirm(on, b, c, prev)
|
||||
case *crypt.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.crypt(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.crypt(on, b, c, prev)
|
||||
case *delay.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.delay(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.delay(on, b, c, prev)
|
||||
case *exit.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.exit(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.exit(on, b, c, prev)
|
||||
case *forward.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.forward(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.forward(on, b, c, prev)
|
||||
case *getbalance.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.getBalance(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.getBalance(on, b, c, prev)
|
||||
case *reverse.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.reverse(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.reverse(on, b, c, prev)
|
||||
case *response.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.response(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.response(on, b, c, prev)
|
||||
case *session.Layer:
|
||||
log.T.C(recLog(on, b, cl))
|
||||
cl.session(on, b, c, prev)
|
||||
log.T.C(recLog(on, b, en))
|
||||
en.session(on, b, c, prev)
|
||||
default:
|
||||
log.I.S("unrecognised packet", b)
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/payment"
|
||||
)
|
||||
|
||||
func (cl *Engine) BuySessions(
|
||||
func (en *Engine) BuySessions(
|
||||
s ...*SessionBuy) (sess []*session.Layer,
|
||||
pmt []*payment.Payment) {
|
||||
|
||||
|
||||
@@ -9,21 +9,21 @@ import (
|
||||
// FindCloaked searches the client identity key and the sessions for a match. It
|
||||
// returns the session as well, though not all users of this function will need
|
||||
// this.
|
||||
func (cl *Engine) FindCloaked(clk cloak.PubKey) (hdr *prv.Key,
|
||||
func (en *Engine) FindCloaked(clk cloak.PubKey) (hdr *prv.Key,
|
||||
pld *prv.Key, sess *traffic.Session, identity bool) {
|
||||
|
||||
var b cloak.Blinder
|
||||
copy(b[:], clk[:cloak.BlindLen])
|
||||
hash := cloak.Cloak(b, cl.Node.IdentityBytes)
|
||||
hash := cloak.Cloak(b, en.Node.IdentityBytes)
|
||||
if hash == clk {
|
||||
log.T.F("encrypted to identity key")
|
||||
hdr = cl.Node.IdentityPrv
|
||||
hdr = en.Node.IdentityPrv
|
||||
// there is no payload key for the node, only in sessions.
|
||||
identity = true
|
||||
return
|
||||
}
|
||||
var i int
|
||||
cl.Node.IterateSessions(func(s *traffic.Session) (stop bool) {
|
||||
en.Node.IterateSessions(func(s *traffic.Session) (stop bool) {
|
||||
hash = cloak.Cloak(b, s.HeaderBytes)
|
||||
if hash == clk {
|
||||
log.T.F("found cloaked key in session %d", i)
|
||||
|
||||
@@ -9,20 +9,20 @@ import (
|
||||
)
|
||||
|
||||
// Send a message to a peer via their AddrPort.
|
||||
func (cl *Engine) Send(addr *netip.AddrPort, b slice.Bytes) {
|
||||
func (en *Engine) Send(addr *netip.AddrPort, b slice.Bytes) {
|
||||
// first search if we already have the node available with connection
|
||||
// open.
|
||||
as := addr.String()
|
||||
for i := range cl.Nodes {
|
||||
if as == cl.Nodes[i].AddrPort.String() {
|
||||
for i := range en.Nodes {
|
||||
if as == en.Nodes[i].AddrPort.String() {
|
||||
log.T.C(func() string {
|
||||
return cl.AddrPort.String() +
|
||||
return en.AddrPort.String() +
|
||||
" sending to " +
|
||||
addr.String() +
|
||||
"\n" +
|
||||
spew.Sdump(b.ToBytes())
|
||||
})
|
||||
cl.Nodes[i].Transport.Send(b)
|
||||
en.Nodes[i].Transport.Send(b)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,15 +6,15 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) SendExit(port uint16, message slice.Bytes,
|
||||
func (en *Engine) SendExit(port uint16, message slice.Bytes,
|
||||
target *traffic.Session, hook func(b slice.Bytes)) {
|
||||
|
||||
hops := []byte{0, 1, 2, 3, 4, 5}
|
||||
s := make(traffic.Sessions, len(hops))
|
||||
s[2] = target
|
||||
se := cl.Select(hops, s)
|
||||
se := en.Select(hops, s)
|
||||
var c traffic.Circuit
|
||||
copy(c[:], se)
|
||||
o := onion.SendExit(port, message, se[len(se)-1], c, cl.KeySet)
|
||||
cl.SendOnion(c[0].AddrPort, o, hook)
|
||||
o := onion.SendExit(port, message, se[len(se)-1], c, en.KeySet)
|
||||
en.SendOnion(c[0].AddrPort, o, hook)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/traffic"
|
||||
)
|
||||
|
||||
func (cl *Engine) SendGetBalance(s *traffic.Session, conf func(cf nonce.ID)) {
|
||||
func (en *Engine) SendGetBalance(s *traffic.Session, conf func(cf nonce.ID)) {
|
||||
var c traffic.Circuit
|
||||
var returns [3]*traffic.Session
|
||||
hops := make([]byte, 0)
|
||||
@@ -15,12 +15,12 @@ func (cl *Engine) SendGetBalance(s *traffic.Session, conf func(cf nonce.ID)) {
|
||||
c[s.Hop] = s
|
||||
hops = append(hops, 5)
|
||||
se := make(traffic.Sessions, len(hops))
|
||||
ss := cl.Payments.Select(hops, se)
|
||||
ss := en.Payments.Select(hops, se)
|
||||
returns[2] = ss[1]
|
||||
confID := nonce.NewID()
|
||||
o := onion.GetBalance(c, int(s.Hop), returns, cl.KeySet, confID)
|
||||
cl.RegisterConfirmation(conf, confID)
|
||||
cl.SendOnion(c[s.Hop].AddrPort, o, nil)
|
||||
o := onion.GetBalance(c, int(s.Hop), returns, en.KeySet, confID)
|
||||
en.RegisterConfirmation(conf, confID)
|
||||
en.SendOnion(c[s.Hop].AddrPort, o, nil)
|
||||
return
|
||||
}
|
||||
var cur byte
|
||||
@@ -34,7 +34,7 @@ func (cl *Engine) SendGetBalance(s *traffic.Session, conf func(cf nonce.ID)) {
|
||||
}
|
||||
se := make(traffic.Sessions, len(hops))
|
||||
se[s.Hop] = s
|
||||
ss := cl.Payments.Select(hops, se)
|
||||
ss := en.Payments.Select(hops, se)
|
||||
// Construct the circuit parameter.
|
||||
for i := range ss {
|
||||
if i > int(s.Hop) {
|
||||
@@ -47,7 +47,7 @@ func (cl *Engine) SendGetBalance(s *traffic.Session, conf func(cf nonce.ID)) {
|
||||
returns[i] = ss[lastIndex+i]
|
||||
}
|
||||
confID := nonce.NewID()
|
||||
o := onion.GetBalance(c, int(s.Hop), returns, cl.KeySet, confID)
|
||||
cl.RegisterConfirmation(conf, confID)
|
||||
cl.SendOnion(c[0].AddrPort, o, nil)
|
||||
o := onion.GetBalance(c, int(s.Hop), returns, en.KeySet, confID)
|
||||
en.RegisterConfirmation(conf, confID)
|
||||
en.SendOnion(c[0].AddrPort, o, nil)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/traffic"
|
||||
)
|
||||
|
||||
func (cl *Engine) SendKeys(sb []*SessionBuy, sess []*session.Layer,
|
||||
func (en *Engine) SendKeys(sb []*SessionBuy, sess []*session.Layer,
|
||||
pmt []*payment.Payment, hook func(hops []*traffic.Session)) {
|
||||
|
||||
if len(sb) != len(sess) || len(sess) != len(pmt) {
|
||||
@@ -39,7 +39,7 @@ func (cl *Engine) SendKeys(sb []*SessionBuy, sess []*session.Layer,
|
||||
s[bu][i].Header, s[bu][i].Payload, byte(i))
|
||||
}
|
||||
// Fill the gaps.
|
||||
se := cl.Select([]byte{5}, make(traffic.Sessions, 1))
|
||||
se := en.Select([]byte{5}, make(traffic.Sessions, 1))
|
||||
cnf := nonce.NewID()
|
||||
// Send the keys.
|
||||
var circuit node.Nodes
|
||||
@@ -53,8 +53,8 @@ func (cl *Engine) SendKeys(sb []*SessionBuy, sess []*session.Layer,
|
||||
}
|
||||
// FIRE!
|
||||
sk := onion.SendKeys(cnf, ss, se[0],
|
||||
circuit, cl.KeySet)
|
||||
cl.RegisterConfirmation(func(cf nonce.ID) {
|
||||
circuit, en.KeySet)
|
||||
en.RegisterConfirmation(func(cf nonce.ID) {
|
||||
log.T.F("confirmed sendkeys id %x", cf)
|
||||
var h []*traffic.Session
|
||||
for i := range circuit {
|
||||
@@ -65,6 +65,6 @@ func (cl *Engine) SendKeys(sb []*SessionBuy, sess []*session.Layer,
|
||||
hook(h)
|
||||
}, cnf)
|
||||
log.T.F("sending out %d session keys", len(buys[bu]))
|
||||
cl.SendOnion(circuit[0].AddrPort, sk, nil)
|
||||
en.SendOnion(circuit[0].AddrPort, sk, nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
func (cl *Engine) SendOnion(ap *netip.AddrPort, o onion.Skins,
|
||||
func (en *Engine) SendOnion(ap *netip.AddrPort, o onion.Skins,
|
||||
responseHook func(b slice.Bytes)) {
|
||||
b := onion.Encode(o.Assemble())
|
||||
var billable, accounted []nonce.ID
|
||||
@@ -33,7 +33,7 @@ func (cl *Engine) SendOnion(ap *netip.AddrPort, o onion.Skins,
|
||||
}
|
||||
switch on := o[i].(type) {
|
||||
case *crypt.Layer:
|
||||
s := cl.FindSessionByHeaderPub(on.ToHeaderPub)
|
||||
s := en.FindSessionByHeaderPub(on.ToHeaderPub)
|
||||
// The last hop needs no accounting as it's us!
|
||||
if i == len(o)-1 {
|
||||
// The session used for the last hop is stored, however.
|
||||
@@ -46,8 +46,8 @@ func (cl *Engine) SendOnion(ap *netip.AddrPort, o onion.Skins,
|
||||
switch on2 := o[i+1].(type) {
|
||||
case *forward.Layer:
|
||||
log.D.Ln("sender:",
|
||||
cl.AddrPort.String(), "send forward")
|
||||
cl.DecSession(s.ID,
|
||||
en.AddrPort.String(), "send forward")
|
||||
en.DecSession(s.ID,
|
||||
s.RelayRate*lnwire.MilliSatoshi(len(b))/1024/1024)
|
||||
accounted = append(accounted, s.ID)
|
||||
case *reverse.Layer:
|
||||
@@ -60,7 +60,7 @@ func (cl *Engine) SendOnion(ap *netip.AddrPort, o onion.Skins,
|
||||
port = on2.Port
|
||||
log.D.Ln("sender:",
|
||||
s.AddrPort.String(), "exit receive")
|
||||
cl.DecSession(s.ID,
|
||||
en.DecSession(s.ID,
|
||||
s.Services[i].RelayRate*lnwire.MilliSatoshi(len(b)/2)/1024/1024)
|
||||
accounted = append(accounted, s.ID)
|
||||
break
|
||||
@@ -70,7 +70,7 @@ func (cl *Engine) SendOnion(ap *netip.AddrPort, o onion.Skins,
|
||||
skip = true
|
||||
case *getbalance.Layer:
|
||||
log.D.Ln("sender: getbalance layer")
|
||||
cl.DecSession(s.ID,
|
||||
en.DecSession(s.ID,
|
||||
s.RelayRate*lnwire.MilliSatoshi(len(b)/2)/1024/1024)
|
||||
last = sha256.Single(s.ID[:])
|
||||
billable = append(billable, s.ID)
|
||||
@@ -80,12 +80,12 @@ func (cl *Engine) SendOnion(ap *netip.AddrPort, o onion.Skins,
|
||||
// the immediate previous layer session needs to be accounted.
|
||||
switch on3 := o[i-1].(type) {
|
||||
case *crypt.Layer:
|
||||
s := cl.FindSessionByHeaderPub(on3.ToHeaderPub)
|
||||
s := en.FindSessionByHeaderPub(on3.ToHeaderPub)
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
log.D.Ln("sender: directbalance layer")
|
||||
cl.DecSession(s.ID,
|
||||
en.DecSession(s.ID,
|
||||
s.RelayRate*lnwire.MilliSatoshi(len(b))/1024/1024)
|
||||
}
|
||||
}
|
||||
@@ -93,8 +93,8 @@ func (cl *Engine) SendOnion(ap *netip.AddrPort, o onion.Skins,
|
||||
if responseHook == nil {
|
||||
responseHook = func(_ slice.Bytes) {}
|
||||
}
|
||||
cl.PendingResponses.Add(last, billable, accounted, ret, port, responseHook)
|
||||
en.PendingResponses.Add(last, billable, accounted, ret, port, responseHook)
|
||||
log.T.Ln("sending out onion")
|
||||
cl.Send(ap, b)
|
||||
en.Send(ap, b)
|
||||
|
||||
}
|
||||
|
||||
@@ -6,15 +6,15 @@ import (
|
||||
"github.com/indra-labs/indra/pkg/traffic"
|
||||
)
|
||||
|
||||
func (cl *Engine) SendPing(c traffic.Circuit, conf func(cf nonce.ID)) {
|
||||
func (en *Engine) SendPing(c traffic.Circuit, conf func(cf nonce.ID)) {
|
||||
|
||||
hops := []byte{0, 1, 2, 3, 4, 5}
|
||||
s := make(traffic.Sessions, len(hops))
|
||||
copy(s, c[:])
|
||||
se := cl.Select(hops, s)
|
||||
se := en.Select(hops, s)
|
||||
copy(c[:], se)
|
||||
confID := nonce.NewID()
|
||||
cl.RegisterConfirmation(conf, confID)
|
||||
o := onion.Ping(confID, se[len(se)-1], c, cl.KeySet)
|
||||
cl.SendOnion(c[0].AddrPort, o, nil)
|
||||
en.RegisterConfirmation(conf, confID)
|
||||
o := onion.Ping(confID, se[len(se)-1], c, en.KeySet)
|
||||
en.SendOnion(c[0].AddrPort, o, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user