-
Notifications
You must be signed in to change notification settings - Fork 6
/
creep.go
130 lines (110 loc) · 2.85 KB
/
creep.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
package main
import (
"log"
"net"
"sync"
"time"
"github.com/Qitmeer/qitmeer/core/blockdag"
"github.com/Qitmeer/qitmeer/core/message"
"github.com/Qitmeer/qitmeer/core/protocol"
"github.com/Qitmeer/qitmeer/p2p/connmgr"
"github.com/Qitmeer/qitmeer/p2p/peer"
)
func creep() {
defer globalWg.Done()
onaddr := make(chan struct{})
verack := make(chan struct{})
newestGSFunc := func() (gs *blockdag.GraphState, err error) {
gs = blockdag.NewGraphState()
gs.GetTips().Add(activeNetParams.GenesisHash)
gs.SetTotal(1)
return gs, err
}
onAddrFunc := func(p *peer.Peer, msg *message.MsgAddr) {
n := make([]net.IP, 0, len(msg.AddrList))
for _, addr := range msg.AddrList {
n = append(n, addr.IP)
}
added := manager.AddAddresses(n)
log.Printf("Peer %v sent %v addresses, %d new",
p.Addr(), len(msg.AddrList), added)
onaddr <- struct{}{}
}
onVerAckFunc := func(p *peer.Peer, msg *message.MsgVerAck) {
log.Printf("Adding peer %v with services %v",
p.NA().IP.String(), p.Services())
verack <- struct{}{}
}
messageListener := peer.MessageListeners{
OnAddr: onAddrFunc,
OnVerAck: onVerAckFunc,
}
peerConfig := peer.Config{
NewestGS: newestGSFunc,
UserAgentName: "qitmeer-seeder",
UserAgentVersion: "0.3.1",
UserAgentComments: []string{"qitmeer", "seeder"},
ChainParams: activeNetParams,
DisableRelayTx: true,
Services: protocol.Full,
ProtocolVersion: protocol.ProtocolVersion,
Listeners: messageListener,
}
var wg sync.WaitGroup
for {
ips := manager.Addresses()
if len(ips) == 0 {
log.Printf("No stale addresses -- sleeping for %v",
defaultAddressTimeout)
time.Sleep(defaultAddressTimeout)
continue
}
wg.Add(len(ips))
for _, ip := range ips {
go func(ip net.IP) {
defer wg.Done()
host := net.JoinHostPort(ip.String(),
activeNetParams.DefaultPort)
p, err := peer.NewOutboundPeer(&peerConfig, host)
if err != nil {
log.Printf("NewOutboundPeer on %v: %v",
host, err)
return
}
manager.Attempt(ip)
conn, err := net.DialTimeout("tcp", p.Addr(),
defaultNodeTimeout)
if err != nil {
return
}
c := connmgr.NewConnReq()
c.SetConn(conn)
p.AssociateConnection(c)
// Wait for the verack message or timeout in case of
// failure.
select {
case <-verack:
// Mark this peer as a good node.
manager.Good(p.NA().IP, p.Services())
// Ask peer for some addresses.
p.QueueMessage(message.NewMsgGetAddr(), nil)
case <-time.After(defaultNodeTimeout):
log.Printf("verack timeout on peer %v",
p.Addr())
p.Disconnect()
return
}
select {
case <-onaddr:
case <-time.After(defaultNodeTimeout):
log.Printf("getaddr timeout on peer %v",
p.Addr())
p.Disconnect()
return
}
p.Disconnect()
}(ip)
}
wg.Wait()
}
}