/
node.go
143 lines (127 loc) · 2.87 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package internal
import (
"log"
"sync"
"time"
"github.com/AutoRoute/node/types"
)
// A Node includes various functions which are called by Server but shouldn't be publicly exposed.
type Node struct {
router *Router
l *sync.Mutex
id PrivateKey
outgoing chan types.Packet
receipt_buffer []types.PacketHash
receipt_ticker <-chan time.Time
payment_ticker <-chan time.Time
m types.Money
quit chan bool
}
func NewNode(pk PrivateKey, m types.Money, receipt_ticker <-chan time.Time, payment_ticker <-chan time.Time, route_logger Logger) *Node {
n := &Node{
NewRouter(pk.PublicKey(), route_logger),
&sync.Mutex{},
pk,
make(chan types.Packet),
nil,
receipt_ticker,
payment_ticker,
m,
make(chan bool),
}
go n.receivePackets()
go n.sendReceipts()
go n.sendPayments()
return n
}
func (n *Node) receivePackets() {
for {
select {
case p, ok := <-n.router.Packets():
if !ok {
return
}
n.l.Lock()
n.receipt_buffer = append(n.receipt_buffer, p.Hash())
n.l.Unlock()
n.outgoing <- p
case <-n.quit:
return
}
}
}
func (n *Node) sendReceipts() {
for {
select {
case <-n.receipt_ticker:
n.l.Lock()
if len(n.receipt_buffer) > 0 {
r := CreateMerkleReceipt(n.id, n.receipt_buffer)
n.receipt_buffer = nil
n.router.SendReceipt(r)
}
n.l.Unlock()
case <-n.quit:
return
}
}
}
func (n *Node) sendPayments() {
for {
select {
case <-n.payment_ticker:
n.l.Lock()
for _, c := range n.router.Connections() {
owed := n.router.OutgoingDebt(c.Key().Hash())
log.Printf("Owe %x %d", c.Key().Hash(), owed)
if owed > 0 {
log.Printf("Sending payment to %s", c.OtherMetaData().Payment_Address)
p, err := n.m.MakePayment(owed, c.OtherMetaData().Payment_Address)
if err != nil {
log.Printf("Failed to make a payment to %x (%x) : %v",
c.Key().Hash(), c.OtherMetaData().Payment_Address, err)
break
}
go n.router.RecordPayment(c.Key().Hash(), owed, p)
}
}
n.l.Unlock()
case <-n.quit:
return
}
}
}
func (n *Node) SendPacket(p types.Packet) error {
return n.router.SendPacket(p)
}
func (n *Node) Packets() <-chan types.Packet {
return n.outgoing
}
func (n *Node) Close() error {
close(n.quit)
return nil
}
func (n *Node) GetNewAddress() string {
address, c, err := n.m.GetNewAddress()
if err != nil {
log.Fatal("Failed to get payment address: ", err)
}
n.router.Ledger.AddAddress(address, c)
return address
}
func (n *Node) GetAddress() PublicKey {
return n.id.PublicKey()
}
func (n *Node) GetNodeAddress() types.NodeAddress {
return n.GetAddress().Hash()
}
func (n *Node) IsReachable(addr types.NodeAddress) bool {
_, err := n.router.FindPossibleDests(addr, "")
return err == nil
}
func (n *Node) AddConnection(c Connection) {
n.router.AddConnection(c)
}
func (n *Node) ID() PrivateKey {
return n.id
}