switched getbalance to uniform, anonymous style

This commit is contained in:
херетик
2023-02-07 15:26:39 +00:00
parent eb2ec4c44c
commit b31491a7af
12 changed files with 70 additions and 260 deletions

3
go.mod
View File

@@ -11,6 +11,7 @@ require (
github.com/docker/cli v20.10.22+incompatible
github.com/docker/docker v20.10.22+incompatible
github.com/gookit/color v1.5.2
github.com/indra-labs/indra v0.1.9
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/libp2p/go-libp2p v0.24.2
github.com/libp2p/go-libp2p-kad-dht v0.20.0
@@ -25,7 +26,7 @@ require (
require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/Microsoft/hcsshim v0.9.6 // indirect
github.com/aead/siphash v1.0.1 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect

4
go.sum
View File

@@ -67,6 +67,8 @@ github.com/Microsoft/go-winio v0.4.17-0.20210324224401-5516f17a5958/go.mod h1:JP
github.com/Microsoft/go-winio v0.4.17/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
github.com/Microsoft/hcsshim v0.8.6/go.mod h1:Op3hHsoHPAvb6lceZHDtd9OkTew38wNoXnJs8iY7rUg=
github.com/Microsoft/hcsshim v0.8.7-0.20190325164909-8abdbb8205e4/go.mod h1:Op3hHsoHPAvb6lceZHDtd9OkTew38wNoXnJs8iY7rUg=
github.com/Microsoft/hcsshim v0.8.7/go.mod h1:OHd7sQqRFrYd3RmSgbgji+ctCwkbq2wbEYNSzOYtcBQ=
@@ -581,6 +583,8 @@ github.com/imdario/mergo v0.3.10/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/indra-labs/indra v0.1.9 h1:IgjDWVCiS4uSrWQe3LL8kCPMHx5kSPBqmJeTupxi2Pk=
github.com/indra-labs/indra v0.1.9/go.mod h1:tbAsGhLx2/RaFsGhhp7YSwBu6SuPxeeBKF8Lp+mnKtM=
github.com/indra-labs/lnd v0.15.5-beta h1:moLiD+/bBIDpkdZWGuDgm7+CoC8U0p8jrPbyQ9wlw3A=
github.com/indra-labs/lnd v0.15.5-beta/go.mod h1:iddXZqwzGmXtsq0h6bx/JMOuweedbphKjP3ZsUA8hyk=
github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj6+M=

View File

@@ -2,9 +2,9 @@ package onion
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"git-indra.lan/indra-labs/indra"
"git-indra.lan/indra-labs/indra/pkg/crypto/key/ecdh"
"git-indra.lan/indra-labs/indra/pkg/crypto/key/prv"
@@ -15,7 +15,6 @@ import (
"git-indra.lan/indra-labs/indra/pkg/onion/layers/confirm"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/crypt"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/delay"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/directbalance"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/exit"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/forward"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/getbalance"
@@ -64,11 +63,6 @@ func Peel(b slice.Bytes, c *slice.Cursor) (on types.Onion, e error) {
if e = on.Decode(b, c); check(e) {
return
}
case directbalance.MagicString:
on = &directbalance.Layer{}
if e = on.Decode(b, c); check(e) {
return
}
case exit.MagicString:
on = &exit.Layer{}
if e = on.Decode(b, c); check(e) {

View File

@@ -1,53 +0,0 @@
package directbalance
import (
"git-indra.lan/indra-labs/indra"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/magicbytes"
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
"git-indra.lan/indra-labs/indra/pkg/types"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
)
const (
MagicString = "db"
Len = magicbytes.Len + 2*nonce.IDLen
)
var (
log = log2.GetLogger(indra.PathBase)
check = log.E.Chk
Magic = slice.Bytes(MagicString)
_ types.Onion = &Layer{}
)
// Layer getbalance messages are a request to return the sats balance of the
// session the message is embedded in.
type Layer struct {
nonce.ID
ConfID nonce.ID
types.Onion
}
func (x *Layer) Inner() types.Onion { return x.Onion }
func (x *Layer) Insert(o types.Onion) { x.Onion = o }
func (x *Layer) Len() int {
return Len + x.Onion.Len()
}
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
copy(b[*c:c.Inc(nonce.IDLen)], x.ID[:])
copy(b[*c:c.Inc(nonce.IDLen)], x.ConfID[:])
x.Onion.Encode(b, c)
}
func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
if len(b[*c:]) < Len-magicbytes.Len {
return magicbytes.TooShort(len(b[*c:]), Len-magicbytes.Len,
string(Magic))
}
copy(x.ID[:], b[*c:c.Inc(nonce.IDLen)])
copy(x.ConfID[:], b[*c:c.Inc(nonce.IDLen)])
return
}

View File

@@ -77,11 +77,11 @@ func SendExit(port uint16, payload slice.Bytes, id nonce.ID,
// Purchase header bytes and to generate the ciphers provided in the Purchase
// message to encrypt the Session that is returned.
//
// The OnionSkin key, its cloaked public key counterpart used in the ToHeaderPub field of
// the Purchase message preformed header bytes, but the Ciphers provided in the
// Purchase message, for encrypting the Session to be returned, uses the Payload
// key, along with the public key found in the encrypted crypt of the header for
// the Reverse relay.
// The OnionSkin key, its cloaked public key counterpart used in the ToHeaderPub
// field of the Purchase message preformed header bytes, but the Ciphers
// provided in the Purchase message, for encrypting the Session to be returned,
// uses the Payload key, along with the public key found in the encrypted crypt
// of the header for the Reverse relay.
//
// This message's last crypt is a Confirmation, which allows the client to know
// that the keys were successfully delivered.
@@ -109,63 +109,27 @@ func SendKeys(id nonce.ID, s [5]*session.Layer,
}
// GetBalance sends out a request in a similar way to SendExit except the node
// being queried can be any of the 5 and the return path is always a further two
// hops until the client.
//
// First and last hop sessions are just directly queried, the second and exit
// pass forwards, and if it's the second last one the hops are reverse.
func GetBalance(s traffic.Circuit, target int, returns [3]*traffic.Session,
ks *signer.KeySet, id nonce.ID) (o Skins) {
// being queried can be any of the 5.
func GetBalance(id, confID nonce.ID, client *traffic.Session,
s traffic.Circuit, ks *signer.KeySet) Skins {
if target == 0 || target == 4 {
n := GenNonces(2)
o = o.ForwardCrypt(s[target], ks.Next(), n[0]).
DirectBalance(s[target].ID, id).
Forward(returns[2].AddrPort)
return
}
n := GenNonces(target + 1 + 3)
var returnNonces [3]nonce.IV
copy(returnNonces[:], n[len(n)-1-3:])
var prvs [3]*prv.Key
for i := range prvs {
prvs[i] = ks.Next()
}
var pubs [3]*pub.Key
for i := range returns {
pubs[i] = returns[i].PayloadPub
}
if target == 3 {
// If it is the second last hop we can save on hops by reversing the
// path.
} else {
}
for i := 0; i <= target; i++ {
o = o.ForwardCrypt(s[i], ks.Next(), n[i])
}
o = o.GetBalance(s[target].ID, id, prvs, pubs, returnNonces)
for i := range returns {
o = o.ReverseCrypt(returns[i], prvs[i], n[i+target], 0)
}
return
}
func NewGetBalance(id nonce.ID, circuit traffic.Circuit, ret *traffic.Session,
ks *signer.KeySet) (o Skins) {
n := GenNonces(6)
retNonces := [3]nonce.IV{n[3], n[4], n[5]}
prvs := [3]*prv.Key{circuit[3].PayloadPrv, circuit[4].PayloadPrv,
ret.PayloadPrv}
pubs := [3]*pub.Key{circuit[3].PayloadPub, circuit[4].PayloadPub,
ret.PayloadPub}
o = o.ForwardCrypt(circuit[0], ks.Next(), n[0]).
ForwardCrypt(circuit[1], ks.Next(), n[1]).
GetBalance(id, circuit[2].ID, prvs, pubs, retNonces).
ReverseCrypt(circuit[3], ks.Next(), n[0], 3).
ReverseCrypt(circuit[4], ks.Next(), n[0], 2).
ReverseCrypt(ret, ks.Next(), n[0], 1)
return
var retNonces [3]nonce.IV
copy(retNonces[:], n[3:])
var pubs [3]*pub.Key
pubs[0] = s[3].PayloadPub
pubs[1] = s[4].PayloadPub
pubs[2] = client.PayloadPub
return Skins{}.
ReverseCrypt(s[0], ks.Next(), n[0], 3).
ReverseCrypt(s[1], ks.Next(), n[1], 2).
ReverseCrypt(s[2], ks.Next(), n[2], 1).
GetBalance(id, confID, prvs, pubs, retNonces).
ReverseCrypt(s[3], prvs[0], n[3], 0).
ReverseCrypt(s[4], prvs[1], n[4], 0).
ReverseCrypt(client, prvs[2], n[5], 0)
}

View File

@@ -3,9 +3,9 @@ package onion
import (
"net/netip"
"time"
"git-indra.lan/indra-labs/lnd/lnd/lnwire"
"git-indra.lan/indra-labs/indra/pkg/crypto/key/prv"
"git-indra.lan/indra-labs/indra/pkg/crypto/key/pub"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
@@ -13,7 +13,6 @@ import (
"git-indra.lan/indra-labs/indra/pkg/onion/layers/confirm"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/crypt"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/delay"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/directbalance"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/exit"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/forward"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/getbalance"
@@ -32,18 +31,18 @@ var os = &noop.Layer{}
func (o Skins) ForwardCrypt(s *traffic.Session, k *prv.Key,
n nonce.IV) Skins {
return o.Forward(s.AddrPort).Crypt(s.HeaderPub, s.PayloadPub, k, n, 0)
}
func (o Skins) ReverseCrypt(s *traffic.Session, k *prv.Key, n nonce.IV, seq int) Skins {
return o.Reverse(s.AddrPort).Crypt(s.HeaderPub, s.PayloadPub, k, n, seq)
}
func (o Skins) ForwardSession(s *traffic.Node,
k *prv.Key, n nonce.IV, sess *session.Layer) Skins {
return o.Forward(s.AddrPort).
Crypt(s.IdentityPub, nil, k, n, 0).
Session(sess)
@@ -51,7 +50,7 @@ func (o Skins) ForwardSession(s *traffic.Node,
func (o Skins) Balance(id, confID nonce.ID,
amt lnwire.MilliSatoshi) Skins {
return append(o, &balance.Layer{
ID: id,
ConfID: confID,
@@ -67,13 +66,9 @@ func (o Skins) Delay(d time.Duration) Skins {
return append(o, &delay.Layer{Duration: d, Onion: os})
}
func (o Skins) DirectBalance(id, confID nonce.ID) Skins {
return append(o, &directbalance.Layer{ID: id, ConfID: confID, Onion: os})
}
func (o Skins) Exit(port uint16, prvs [3]*prv.Key, pubs [3]*pub.Key,
nonces [3]nonce.IV, id nonce.ID, payload slice.Bytes) Skins {
return append(o, &exit.Layer{
Port: port,
Ciphers: GenCiphers(prvs, pubs),
@@ -94,7 +89,7 @@ func (o Skins) Forward(addr *netip.AddrPort) Skins {
func (o Skins) GetBalance(id, confID nonce.ID, prvs [3]*prv.Key,
pubs [3]*pub.Key, nonces [3]nonce.IV) Skins {
return append(o, &getbalance.Layer{
ID: id,
ConfID: confID,
@@ -105,7 +100,7 @@ func (o Skins) GetBalance(id, confID nonce.ID, prvs [3]*prv.Key,
}
func (o Skins) Crypt(toHdr, toPld *pub.Key, from *prv.Key, n nonce.IV, seq int) Skins {
return append(o, &crypt.Layer{
Seq: seq,
ToHeaderPub: toHdr,

View File

@@ -36,9 +36,12 @@ func TestClient_SendSessionKeys(t *testing.T) {
}
var wg sync.WaitGroup
var counter atomic.Int32
quit := qu.T()
go func() {
select {
case <-time.After(time.Second * 2):
case <-quit:
return
}
for i := 0; i < int(counter.Load()); i++ {
wg.Done()
@@ -59,6 +62,7 @@ func TestClient_SendSessionKeys(t *testing.T) {
counter.Dec()
}
wg.Wait()
quit.Q()
for j := range clients[0].SessionCache {
log.D.F("%d %s %v", i, j, clients[0].SessionCache[j])
}
@@ -226,6 +230,7 @@ out:
wg.Add(1)
clients[0].SendGetBalance(clients[0].Sessions[i],
func(cf nonce.ID, b slice.Bytes) {
log.I.Ln("success")
wg.Done()
})
select {

View File

@@ -18,14 +18,21 @@ func (eng *Engine) balance(on *balance.Layer,
for i := range pending.Billable {
s := eng.FindSession(pending.Billable[i])
if s != nil {
if i == 0 {
eng.DecSession(s.ID,
s.RelayRate*lnwire.MilliSatoshi(len(b)/2)/1024/1024,
true, "balance1")
} else {
eng.DecSession(s.ID,
s.RelayRate*lnwire.MilliSatoshi(len(b))/1024/1024,
true, "balance2")
switch {
case i < 2:
in := s.RelayRate * lnwire.MilliSatoshi(
pending.SentSize) / 1024 / 1024
eng.DecSession(s.ID, in, true, "reverse")
case i == 2:
in := s.RelayRate * lnwire.MilliSatoshi(
pending.SentSize/2) / 1024 / 1024
out := s.RelayRate * lnwire.MilliSatoshi(
len(b)/2) / 1024 / 1024
eng.DecSession(s.ID, in+out, true, "getbalance")
case i > 2:
out := s.RelayRate * lnwire.MilliSatoshi(
len(b)) / 1024 / 1024
eng.DecSession(s.ID, out, true, "reverse")
}
}
}

View File

@@ -1,15 +1,7 @@
package relay
import (
"git-indra.lan/indra-labs/lnd/lnd/lnwire"
"git-indra.lan/indra-labs/indra/pkg/crypto/key/pub"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/onion"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/balance"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/crypt"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/directbalance"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/forward"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/magicbytes"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/session"
"git-indra.lan/indra-labs/indra/pkg/types"
@@ -20,7 +12,7 @@ func (eng *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 := eng.FindCloaked(on.Cloak)
hdr, _, _, identity := eng.FindCloaked(on.Cloak)
if hdr == nil {
log.T.Ln("no matching key found from cloaked key")
return
@@ -33,52 +25,8 @@ func (eng *Engine) crypt(on *crypt.Layer, b slice.Bytes,
" no following session")
return
}
eng.handleMessage(BudgeUp(b, *c), on)
return
}
if string(b[*c:][:magicbytes.Len]) == directbalance.MagicString {
var on1, on2 types.Onion
var e error
if on1, e = onion.Peel(b, c); check(e) {
return
}
var balID, confID nonce.ID
switch db := on1.(type) {
case *directbalance.Layer:
balID = db.ID
confID = db.ConfID
default:
log.T.Ln("malformed/truncated onion")
return
}
if on2, e = onion.Peel(b, c); check(e) {
return
}
switch fwd := on2.(type) {
case *forward.Layer:
o := (&onion.Skins{}).
Forward(fwd.AddrPort).
Crypt(pub.Derive(hdr), nil, eng.KeySet.Next(), nonce.New(), 0).
Balance(balID, confID, sess.Remaining)
oo := o.Assemble()
// This is a little more complicated as we need to decrement the
// amount before sending out the balance.
eng.DecSession(sess.ID,
(eng.GetLocalNodeRelayRate()*lnwire.MilliSatoshi(len(b)+oo.
Len())/2)/1024/1024,
false, "directbalance")
o[2].(*balance.Layer).MilliSatoshi = sess.Remaining
rb := onion.Encode(oo)
eng.Send(fwd.AddrPort, rb)
// eng.SendOnion(fwd.AddrPort, o)
return
default:
log.T.Ln("dropping directbalance without following " +
"forward")
return
}
return
}
eng.handleMessage(BudgeUp(b, *c), on)
}

View File

@@ -6,46 +6,15 @@ import (
"git-indra.lan/indra-labs/indra/pkg/traffic"
)
func (eng *Engine) SendGetBalance(s *traffic.Session, conf Callback) {
func (eng *Engine) SendGetBalance(target *traffic.Session, hook Callback) {
hops := []byte{0, 1, 2, 3, 4, 5}
s := make(traffic.Sessions, len(hops))
s[2] = target
se := eng.SelectHops(hops, s)
var c traffic.Circuit
var returns [3]*traffic.Session
hops := make([]byte, 0)
if s.Hop == 0 || s.Hop == 4 {
hops = append(hops, s.Hop)
c[s.Hop] = s
hops = append(hops, 5)
se := make(traffic.Sessions, len(hops))
ss := eng.SessionManager.SelectHops(hops, se)
returns[2] = ss[1]
confID := nonce.NewID()
o := onion.GetBalance(c, int(s.Hop), returns, eng.KeySet, confID)
eng.SendOnion(c[s.Hop].AddrPort, o, conf, 0)
return
}
var cur byte
for i := 0; i < int(s.Hop); i++ {
hops = append(hops, cur)
cur++
}
hops = append(hops, s.Hop)
for i := 3; i < 6; i++ {
hops = append(hops, byte(i))
}
se := make(traffic.Sessions, len(hops))
se[s.Hop] = s
ss := eng.SessionManager.SelectHops(hops, se)
// Construct the circuit parameter.
for i := range ss {
if i > int(s.Hop) {
break
}
c[i] = ss[i]
}
lastIndex := len(hops) - 3
for i := range returns {
returns[i] = ss[lastIndex+i]
}
copy(c[:], se)
confID := nonce.NewID()
o := onion.GetBalance(c, int(s.Hop), returns, eng.KeySet, confID)
eng.SendOnion(c[0].AddrPort, o, conf, 0)
o := onion.GetBalance(se[2].ID, confID, se[5], c, eng.KeySet)
log.D.Ln("sending out exit onion")
eng.SendOnion(c[0].AddrPort, o, hook, 0)
}

View File

@@ -11,7 +11,6 @@ import (
"git-indra.lan/indra-labs/indra/pkg/onion/layers/balance"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/confirm"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/crypt"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/directbalance"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/exit"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/forward"
"git-indra.lan/indra-labs/indra/pkg/onion/layers/getbalance"
@@ -81,33 +80,10 @@ func (eng *Engine) SendOnion(ap *netip.AddrPort, o onion.Skins,
last = on2.ID
skip = true
case *getbalance.Layer:
postAcct = append(postAcct,
func() {
eng.DecSession(s.ID,
s.RelayRate*lnwire.MilliSatoshi(len(b)/2)/1024/1024,
true, "getbalance")
})
last = s.ID
billable = append(billable, s.ID)
skip = true
}
case *directbalance.Layer:
// the immediate previous layer session needs to be accounted.
switch on3 := o[i-1].(type) {
case *crypt.Layer:
s := eng.FindSessionByHeaderPub(on3.ToHeaderPub)
if s == nil {
return
}
postAcct = append(postAcct,
func() {
eng.DecSession(s.ID,
s.RelayRate*lnwire.MilliSatoshi(len(b)/2)/1024/1024,
true, "directbalance")
})
billable = append(billable, s.ID)
last = on.ID
}
case *confirm.Layer:
last = on.ID
case *balance.Layer:

View File

@@ -31,7 +31,7 @@ func CreateNMockCircuits(inclSessions bool, nCircuits int) (cl []*Engine, e erro
if i == 0 {
local = true
}
nodes[i], _ = traffic.NewNode(addr, idPub, idPrv, transports[i], 180000,
nodes[i], _ = traffic.NewNode(addr, idPub, idPrv, transports[i], 20480,
local)
if cl[i], e = NewEngine(transports[i], idPrv, nodes[i], nil); check(e) {
return