Skip to content

Commit 27322cd

Browse files
committed
Merge #16999: net: 0.19 seeds update
0218171 contrib: Remove invalid nodes from seeds list (Wladimir J. van der Laan) 3b09f2b net: 0.19 hardcoded seeds update (Wladimir J. van der Laan) 801d341 contrib: makeseeds: More fancy output (Wladimir J. van der Laan) ed76299 contrib: makeseeds: Limit per network, instead of total (Wladimir J. van der Laan) c254a9e contrib: makeseeds: dedup by ip,port (Wladimir J. van der Laan) 3314d87 contrib: makeseeds: Factor out ASN lookup (Wladimir J. van der Laan) 301c2b1 contrib: makeseeds: Improve logging and filtering (Wladimir J. van der Laan) Pull request description: - contrib: Improve makeseeds script - net: 0.19 hardcoded seeds update Sources: - http://bitcoin.sipa.be/seeds.txt.gz (Sipa) - https://github.com/bitcoin/bitcoin/files/3671913/dnsseed.dump.tar.gz (Sjors) Output: ``` Initial: IPv4 418690, IPv6 55861, Onion 2747 Skip entries with invalid address: IPv4 418690, IPv6 55861, Onion 2747 After removing duplicates: IPv4 409220, IPv6 54028, Onion 2717 Skip entries from suspicious hosts: IPv4 409219, IPv6 54028, Onion 2717 Enforce minimal number of blocks: IPv4 106719, IPv6 46342, Onion 2621 Require service bit 1: IPv4 106384, IPv6 46241, Onion 2542 Require minimum uptime: IPv4 5300, IPv6 1153, Onion 201 Require a known and recent user agent: IPv4 4642, IPv6 1060, Onion 141 Filter out hosts with multiple bitcoin ports: IPv4 4642, IPv6 1060, Onion 141 Look up ASNs and limit results, both per ASN and globally: IPv4 464, IPv6 48, Onion 141 ``` ACKs for top commit: Sjors: ACK 0218171. I also checked that `chainparamsseeds.h` is generated from `nodes_main.txt`. Sounds like we should look at this script a bit more outside release moments :-) Tree-SHA512: c1f5795fe88d14800c4da918387368d51e85f4319f2ce3c0359851d041767e2883f32b1da371bba22bd5f0b442ac3e5ea7d685c233ad2cc4045c930f973b0aa2
2 parents 752debd + 0218171 commit 27322cd

File tree

3 files changed

+1519
-2502
lines changed

3 files changed

+1519
-2502
lines changed

contrib/seeds/makeseeds.py

Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@
3030
PATTERN_IPV4 = re.compile(r"^((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})):(\d+)$")
3131
PATTERN_IPV6 = re.compile(r"^\[([0-9a-z:]+)\]:(\d+)$")
3232
PATTERN_ONION = re.compile(r"^([abcdefghijklmnopqrstuvwxyz234567]{16}\.onion):(\d+)$")
33-
PATTERN_AGENT = re.compile(r"^(/Satoshi:0.14.(0|1|2|99)/|/Satoshi:0.15.(0|1|2|99)|/Satoshi:0.16.(0|1|2|99)/)$")
33+
PATTERN_AGENT = re.compile(
34+
r"^/Satoshi:("
35+
r"0.14.(0|1|2|3|99)|"
36+
r"0.15.(0|1|2|99)|"
37+
r"0.16.(0|1|2|3|99)|"
38+
r"0.17.(0|0.1|1|2|99)|"
39+
r"0.18.(0|1|99)|"
40+
r"0.19.99"
41+
r")")
3442

3543
def parseline(line):
3644
sline = line.split()
@@ -99,78 +107,119 @@ def parseline(line):
99107
'sortkey': sortkey,
100108
}
101109

110+
def dedup(ips):
111+
'''deduplicate by address,port'''
112+
d = {}
113+
for ip in ips:
114+
d[ip['ip'],ip['port']] = ip
115+
return list(d.values())
116+
102117
def filtermultiport(ips):
103118
'''Filter out hosts with more nodes per IP'''
104119
hist = collections.defaultdict(list)
105120
for ip in ips:
106121
hist[ip['sortkey']].append(ip)
107122
return [value[0] for (key,value) in list(hist.items()) if len(value)==1]
108123

124+
def lookup_asn(net, ip):
125+
'''
126+
Look up the asn for an IP (4 or 6) address by querying cymry.com, or None
127+
if it could not be found.
128+
'''
129+
try:
130+
if net == 'ipv4':
131+
ipaddr = ip
132+
prefix = '.origin'
133+
else: # http://www.team-cymru.com/IP-ASN-mapping.html
134+
res = str() # 2001:4860:b002:23::68
135+
for nb in ip.split(':')[:4]: # pick the first 4 nibbles
136+
for c in nb.zfill(4): # right padded with '0'
137+
res += c + '.' # 2001 4860 b002 0023
138+
ipaddr = res.rstrip('.') # 2.0.0.1.4.8.6.0.b.0.0.2.0.0.2.3
139+
prefix = '.origin6'
140+
141+
asn = int([x.to_text() for x in dns.resolver.query('.'.join(
142+
reversed(ipaddr.split('.'))) + prefix + '.asn.cymru.com',
143+
'TXT').response.answer][0].split('\"')[1].split(' ')[0])
144+
return asn
145+
except Exception:
146+
sys.stderr.write('ERR: Could not resolve ASN for "' + ip + '"\n')
147+
return None
148+
109149
# Based on Greg Maxwell's seed_filter.py
110-
def filterbyasn(ips, max_per_asn, max_total):
150+
def filterbyasn(ips, max_per_asn, max_per_net):
111151
# Sift out ips by type
112152
ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']]
113153
ips_onion = [ip for ip in ips if ip['net'] == 'onion']
114154

115-
# Filter IPv46 by ASN
155+
# Filter IPv46 by ASN, and limit to max_per_net per network
116156
result = []
117-
asn_count = {}
157+
net_count = collections.defaultdict(int)
158+
asn_count = collections.defaultdict(int)
118159
for ip in ips_ipv46:
119-
if len(result) == max_total:
120-
break
121-
try:
122-
if ip['net'] == 'ipv4':
123-
ipaddr = ip['ip']
124-
prefix = '.origin'
125-
else: # http://www.team-cymru.com/IP-ASN-mapping.html
126-
res = str() # 2001:4860:b002:23::68
127-
for nb in ip['ip'].split(':')[:4]: # pick the first 4 nibbles
128-
for c in nb.zfill(4): # right padded with '0'
129-
res += c + '.' # 2001 4860 b002 0023
130-
ipaddr = res.rstrip('.') # 2.0.0.1.4.8.6.0.b.0.0.2.0.0.2.3
131-
prefix = '.origin6'
132-
133-
asn = int([x.to_text() for x in dns.resolver.query('.'.join(
134-
reversed(ipaddr.split('.'))) + prefix + '.asn.cymru.com',
135-
'TXT').response.answer][0].split('\"')[1].split(' ')[0])
136-
if asn not in asn_count:
137-
asn_count[asn] = 0
138-
if asn_count[asn] == max_per_asn:
139-
continue
140-
asn_count[asn] += 1
141-
result.append(ip)
142-
except:
143-
sys.stderr.write('ERR: Could not resolve ASN for "' + ip['ip'] + '"\n')
144-
145-
# Add back Onions
146-
result.extend(ips_onion)
160+
if net_count[ip['net']] == max_per_net:
161+
continue
162+
asn = lookup_asn(ip['net'], ip['ip'])
163+
if asn is None or asn_count[asn] == max_per_asn:
164+
continue
165+
asn_count[asn] += 1
166+
net_count[ip['net']] += 1
167+
result.append(ip)
168+
169+
# Add back Onions (up to max_per_net)
170+
result.extend(ips_onion[0:max_per_net])
147171
return result
148172

173+
def ip_stats(ips):
174+
hist = collections.defaultdict(int)
175+
for ip in ips:
176+
if ip is not None:
177+
hist[ip['net']] += 1
178+
179+
return '%6d %6d %6d' % (hist['ipv4'], hist['ipv6'], hist['onion'])
180+
149181
def main():
150182
lines = sys.stdin.readlines()
151183
ips = [parseline(line) for line in lines]
152184

153-
# Skip entries with valid address.
185+
print('\x1b[7m IPv4 IPv6 Onion Pass \x1b[0m', file=sys.stderr)
186+
print('%s Initial' % (ip_stats(ips)), file=sys.stderr)
187+
# Skip entries with invalid address.
154188
ips = [ip for ip in ips if ip is not None]
189+
print('%s Skip entries with invalid address' % (ip_stats(ips)), file=sys.stderr)
190+
# Skip duplicattes (in case multiple seeds files were concatenated)
191+
ips = dedup(ips)
192+
print('%s After removing duplicates' % (ip_stats(ips)), file=sys.stderr)
155193
# Skip entries from suspicious hosts.
156194
ips = [ip for ip in ips if ip['ip'] not in SUSPICIOUS_HOSTS]
195+
print('%s Skip entries from suspicious hosts' % (ip_stats(ips)), file=sys.stderr)
157196
# Enforce minimal number of blocks.
158197
ips = [ip for ip in ips if ip['blocks'] >= MIN_BLOCKS]
198+
print('%s Enforce minimal number of blocks' % (ip_stats(ips)), file=sys.stderr)
159199
# Require service bit 1.
160200
ips = [ip for ip in ips if (ip['service'] & 1) == 1]
161-
# Require at least 50% 30-day uptime.
162-
ips = [ip for ip in ips if ip['uptime'] > 50]
201+
print('%s Require service bit 1' % (ip_stats(ips)), file=sys.stderr)
202+
# Require at least 50% 30-day uptime for clearnet, 10% for onion.
203+
req_uptime = {
204+
'ipv4': 50,
205+
'ipv6': 50,
206+
'onion': 10,
207+
}
208+
ips = [ip for ip in ips if ip['uptime'] > req_uptime[ip['net']]]
209+
print('%s Require minimum uptime' % (ip_stats(ips)), file=sys.stderr)
163210
# Require a known and recent user agent.
164211
ips = [ip for ip in ips if PATTERN_AGENT.match(ip['agent'])]
212+
print('%s Require a known and recent user agent' % (ip_stats(ips)), file=sys.stderr)
165213
# Sort by availability (and use last success as tie breaker)
166214
ips.sort(key=lambda x: (x['uptime'], x['lastsuccess'], x['ip']), reverse=True)
167215
# Filter out hosts with multiple bitcoin ports, these are likely abusive
168216
ips = filtermultiport(ips)
217+
print('%s Filter out hosts with multiple bitcoin ports' % (ip_stats(ips)), file=sys.stderr)
169218
# Look up ASNs and limit results, both per ASN and globally.
170219
ips = filterbyasn(ips, MAX_SEEDS_PER_ASN, NSEEDS)
220+
print('%s Look up ASNs and limit results per ASN and per net' % (ip_stats(ips)), file=sys.stderr)
171221
# Sort the results by IP address (for deterministic output).
172222
ips.sort(key=lambda x: (x['net'], x['sortkey']))
173-
174223
for ip in ips:
175224
if ip['net'] == 'ipv6':
176225
print('[%s]:%i' % (ip['ip'], ip['port']))

0 commit comments

Comments
 (0)