moved services and payments out of engine

This commit is contained in:
херетик
2023-04-17 08:34:45 +01:00
parent 2e57869cbb
commit eab52203dc
14 changed files with 64 additions and 49 deletions

View File

@@ -94,7 +94,8 @@ func (x *Balance) Handle(s *splice.Splice, p Onion,
return
}
func (x *Balance) Account(res *Data, sm *SessionManager, s *SessionData, last bool) (skip bool, sd *SessionData) {
func (x *Balance) Account(res *Data, sm *SessionManager,
s *SessionData, last bool) (skip bool, sd *SessionData) {
res.ID = x.ID
return

View File

@@ -7,6 +7,7 @@ import (
"git-indra.lan/indra-labs/indra/pkg/crypto"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
"git-indra.lan/indra-labs/indra/pkg/splice"
"git-indra.lan/indra-labs/indra/pkg/util/cryptorand"
)
@@ -23,7 +24,7 @@ type KnownIntros map[crypto.PubBytes]*Intro
type LocalHiddenService struct {
Prv *crypto.Prv
CurrentIntros []*Intro
*Service
*services.Service
}
type HiddenServices map[crypto.PubBytes]*LocalHiddenService
@@ -45,7 +46,7 @@ func NewHiddenrouting() *HiddenRouting {
}
}
func (hr *HiddenRouting) AddHiddenService(svc *Service, key *crypto.Prv,
func (hr *HiddenRouting) AddHiddenService(svc *services.Service, key *crypto.Prv,
in *Intro, addr string) {
pk := crypto.DerivePub(key).ToBytes()

View File

@@ -7,27 +7,21 @@ import (
"git-indra.lan/indra-labs/indra/pkg/crypto"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/engine/payments"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
"git-indra.lan/indra-labs/indra/pkg/engine/transport"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
)
type Service struct {
Port uint16
RelayRate int
transport.Transport
}
type Services []*Service
// Node is a representation of a messaging counterparty.
type Node struct {
ID nonce.ID
sync.Mutex
AddrPort *netip.AddrPort
Identity *crypto.Keys
RelayRate int // Base relay price mSAT/Mb.
Services Services // Services offered by this peer.
PaymentChan
RelayRate int // Base relay price mSAT/Mb.
Services services.Services // Services offered by this peer.
payments.Chan
Transport transport.Transport
}
@@ -46,17 +40,17 @@ func NewNode(addr *netip.AddrPort, idPrv *crypto.Prv, tpt transport.Transport,
id = nonce.NewID()
n = &Node{
ID: id,
AddrPort: addr,
Identity: crypto.MakeKeys(idPrv),
RelayRate: relayRate,
PaymentChan: make(PaymentChan, PaymentChanBuffers),
Transport: tpt,
ID: id,
AddrPort: addr,
Identity: crypto.MakeKeys(idPrv),
RelayRate: relayRate,
Chan: make(payments.Chan, PaymentChanBuffers),
Transport: tpt,
}
return
}
func (n *Node) AddService(s *Service) (e error) {
func (n *Node) AddService(s *services.Service) (e error) {
n.Lock()
defer n.Unlock()
for i := range n.Services {
@@ -84,7 +78,7 @@ func (n *Node) DeleteService(port uint16) {
}
}
func (n *Node) FindService(port uint16) (service *Service) {
func (n *Node) FindService(port uint16) (svc *services.Service) {
n.Lock()
defer n.Unlock()
for i := range n.Services {

View File

@@ -8,6 +8,7 @@ import (
"git-indra.lan/indra-labs/indra/pkg/crypto"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
"git-indra.lan/indra-labs/indra/pkg/splice"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
)
@@ -45,7 +46,7 @@ func (ng *Engine) SendGetBalance(alice, bob *SessionData, hook Callback) {
func (ng *Engine) SendHiddenService(id nonce.ID, key *crypto.Prv,
expiry time.Time, alice, bob *SessionData,
svc *Service, hook Callback) (in *Intro) {
svc *services.Service, hook Callback) (in *Intro) {
hops := StandardCircuit()
s := make(Sessions, len(hops))

View File

@@ -11,12 +11,14 @@ import (
"git-indra.lan/indra-labs/indra/pkg/crypto"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/crypto/sha256"
"git-indra.lan/indra-labs/indra/pkg/engine/payments"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
)
type SessionManager struct {
nodes []*Node
PendingPayments PendingPayments
PendingPayments payments.PendingPayments
Sessions
SessionCache
sync.Mutex
@@ -25,7 +27,7 @@ type SessionManager struct {
func NewSessionManager() *SessionManager {
return &SessionManager{
SessionCache: make(SessionCache),
PendingPayments: make(PendingPayments, 0),
PendingPayments: make(payments.PendingPayments, 0),
}
}
@@ -277,9 +279,9 @@ func (sm *SessionManager) NodesLen() int {
// GetLocalNode returns the engine's local Node.
func (sm *SessionManager) GetLocalNode() *Node { return sm.nodes[0] }
// GetLocalNodePaymentChan returns the engine's local Node PaymentChan.
func (sm *SessionManager) GetLocalNodePaymentChan() PaymentChan {
return sm.nodes[0].PaymentChan
// GetLocalNodePaymentChan returns the engine's local Node Chan.
func (sm *SessionManager) GetLocalNodePaymentChan() payments.Chan {
return sm.nodes[0].Chan
}
func (sm *SessionManager) GetLocalNodeAddress() (addr *netip.AddrPort) {
@@ -315,7 +317,7 @@ func (sm *SessionManager) ReceiveToLocalNode(port uint16) <-chan slice.Bytes {
return sm.GetLocalNode().ReceiveFrom(port)
}
func (sm *SessionManager) AddServiceToLocalNode(s *Service) (e error) {
func (sm *SessionManager) AddServiceToLocalNode(s *services.Service) (e error) {
sm.Lock()
defer sm.Unlock()
return sm.GetLocalNode().AddService(s)
@@ -466,7 +468,7 @@ func (sc SessionCache) Add(s *SessionData) SessionCache {
// PendingPayment accessors. For the same reason as the sessions, pending
// payments need to be accessed only with the node's mutex locked.
func (sm *SessionManager) AddPendingPayment(np *Payment) {
func (sm *SessionManager) AddPendingPayment(np *payments.Payment) {
sm.Lock()
defer sm.Unlock()
log.D.F("%s adding pending payment %s for %v",
@@ -479,12 +481,12 @@ func (sm *SessionManager) DeletePendingPayment(preimage sha256.Hash) {
defer sm.Unlock()
sm.PendingPayments = sm.PendingPayments.Delete(preimage)
}
func (sm *SessionManager) FindPendingPayment(id nonce.ID) (pp *Payment) {
func (sm *SessionManager) FindPendingPayment(id nonce.ID) (pp *payments.Payment) {
sm.Lock()
defer sm.Unlock()
return sm.PendingPayments.Find(id)
}
func (sm *SessionManager) FindPendingPreimage(pi sha256.Hash) (pp *Payment) {
func (sm *SessionManager) FindPendingPreimage(pi sha256.Hash) (pp *payments.Payment) {
log.T.F("searching preimage %s", pi)
sm.Lock()
defer sm.Unlock()

View File

@@ -77,7 +77,7 @@ func (ng *Engine) BuyNewSessions(amount lnwire.MilliSatoshi,
var pendingConfirms int
for i := range nodes {
confirmChans[i] = nodes[i].
PaymentChan.Send(amount, s[i])
Chan.Send(amount, s[i].ID, s[i].PreimageHash())
pendingConfirms++
}
var success bool

View File

@@ -120,7 +120,7 @@ func (ng *Engine) Handler() (out bool) {
case b := <-ng.ReceiveToLocalNode(0):
s := splice.Load(b, slice.NewCursor())
ng.HandleMessage(s, prev)
case p := <-ng.GetLocalNode().PaymentChan.Receive():
case p := <-ng.GetLocalNode().Chan.Receive():
log.D.F("incoming payment for %s: %v", p.ID, p.Amount)
topUp := false
ng.IterateSessions(func(s *SessionData) bool {
@@ -147,7 +147,7 @@ func (ng *Engine) Handler() (out bool) {
out:
for {
select {
case <-ng.GetLocalNode().PaymentChan.Receive():
case <-ng.GetLocalNode().Chan.Receive():
log.D.Ln("discarding payments while in pause")
case <-ng.ReceiveToLocalNode(0):
log.D.Ln("discarding messages while in pause")

View File

@@ -13,6 +13,7 @@ import (
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/crypto/sha256"
"git-indra.lan/indra-labs/indra/pkg/engine/coding"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
"git-indra.lan/indra-labs/indra/pkg/util/cryptorand"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
@@ -98,7 +99,7 @@ func TestClient_SendExit(t *testing.T) {
const port = 3455
sim := NewByteChan(0)
for i := range clients {
e = clients[i].AddServiceToLocalNode(&Service{
e = clients[i].AddServiceToLocalNode(&services.Service{
Port: port,
Transport: sim,
RelayRate: 58000,

View File

@@ -12,6 +12,7 @@ import (
"git-indra.lan/indra-labs/indra/pkg/crypto"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
"git-indra.lan/indra-labs/indra/pkg/util/cryptorand"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
@@ -91,7 +92,7 @@ func TestEngine_SendHiddenService(t *testing.T) {
returner = returnHops[0]
wg.Add(1)
counter.Inc()
svc := &Service{
svc := &services.Service{
Port: 2345,
RelayRate: 43523,
Transport: NewByteChan(64),

View File

@@ -12,6 +12,7 @@ import (
"git-indra.lan/indra-labs/indra/pkg/crypto"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/engine/coding"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
"git-indra.lan/indra-labs/indra/pkg/util/cryptorand"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
@@ -149,7 +150,7 @@ func TestEngine_SendIntroQuery(t *testing.T) {
})
}
returner = returnHops[0]
svc := &Service{
svc := &services.Service{
Port: 2345,
RelayRate: 43523,
Transport: NewByteChan(64),

View File

@@ -11,6 +11,7 @@ import (
"git-indra.lan/indra-labs/indra/pkg/crypto"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
"git-indra.lan/indra-labs/indra/pkg/util/cryptorand"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
@@ -117,7 +118,7 @@ func TestEngine_Message(t *testing.T) {
}
wg.Add(1)
counter.Inc()
svc := &Service{
svc := &services.Service{
Port: 2345,
RelayRate: 43523,
Transport: NewByteChan(64),

View File

@@ -1,4 +1,4 @@
package engine
package payments
import (
"git-indra.lan/indra-labs/lnd/lnd/lnwire"
@@ -14,24 +14,24 @@ type Payment struct {
ConfirmChan chan bool
}
type PaymentChan chan *Payment
type Chan chan *Payment
// Send a payment on the PaymentChan.
func (pc PaymentChan) Send(amount lnwire.MilliSatoshi,
s *Session) (confirmChan chan bool) {
// Send a payment on the Chan.
func (pc Chan) Send(amount lnwire.MilliSatoshi,
id nonce.ID, preimage sha256.Hash, ) (confirmChan chan bool) {
confirmChan = make(chan bool)
pc <- &Payment{
ID: s.ID,
Preimage: s.PreimageHash(),
ID: id,
Preimage: preimage,
Amount: amount,
ConfirmChan: confirmChan,
}
return
}
// Receive waits on receiving a Payment on a PaymentChan.
func (pc PaymentChan) Receive() <-chan *Payment { return pc }
// Receive waits on receiving a Payment on a Chan.
func (pc Chan) Receive() <-chan *Payment { return pc }
type PendingPayments []*Payment

View File

@@ -12,6 +12,7 @@ import (
"git-indra.lan/indra-labs/indra/pkg/crypto"
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
"git-indra.lan/indra-labs/indra/pkg/engine/services"
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
"git-indra.lan/indra-labs/indra/pkg/util/cryptorand"
"git-indra.lan/indra-labs/indra/pkg/util/slice"
@@ -118,7 +119,7 @@ func TestEngine_Route(t *testing.T) {
}
wg.Add(1)
counter.Inc()
svc := &Service{
svc := &services.Service{
Port: localPort,
RelayRate: 43523,
Transport: NewByteChan(64),

View File

@@ -0,0 +1,11 @@
package services
import "git-indra.lan/indra-labs/indra/pkg/engine/transport"
type Service struct {
Port uint16
RelayRate int
transport.Transport
}
type Services []*Service