2017-02-11 08:46:00 +01:00
|
|
|
#!/usr/bin/env python3
|
2023-09-10 16:10:08 +02:00
|
|
|
# Copyright (c) 2014-2021 The Bitcoin Core developers
|
2014-10-28 03:10:59 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2014-07-24 13:52:57 +02:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
'''
|
|
|
|
Script to generate list of seed nodes for chainparams.cpp.
|
|
|
|
|
|
|
|
This script expects two text files in the directory that is passed as an
|
|
|
|
argument:
|
|
|
|
|
|
|
|
nodes_main.txt
|
|
|
|
nodes_test.txt
|
|
|
|
|
2018-06-16 15:21:01 +02:00
|
|
|
These files must consist of lines in the format
|
2014-07-24 13:52:57 +02:00
|
|
|
|
|
|
|
<ip>:<port>
|
|
|
|
[<ipv6>]:<port>
|
2023-09-10 16:10:08 +02:00
|
|
|
<onion>.onion:<port>
|
2014-07-24 13:52:57 +02:00
|
|
|
|
|
|
|
The output will be two data structures with the peers in binary format:
|
|
|
|
|
2023-09-10 16:10:08 +02:00
|
|
|
static const uint8_t chainparams_seed_{main,test}[]={
|
2014-07-24 13:52:57 +02:00
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
These should be pasted into `src/chainparamsseeds.h`.
|
|
|
|
'''
|
2017-02-11 08:46:00 +01:00
|
|
|
|
2014-07-24 13:52:57 +02:00
|
|
|
from base64 import b32decode
|
2023-09-10 16:10:08 +02:00
|
|
|
from enum import Enum
|
|
|
|
import struct
|
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
|
|
|
import sys
|
|
|
|
import os
|
2014-07-24 13:52:57 +02:00
|
|
|
import re
|
|
|
|
|
2023-09-10 16:10:08 +02:00
|
|
|
class BIP155Network(Enum):
|
|
|
|
IPV4 = 1
|
|
|
|
IPV6 = 2
|
2023-09-20 13:01:53 +02:00
|
|
|
TORV2 = 3 # no longer supported
|
2023-09-10 16:10:08 +02:00
|
|
|
TORV3 = 4
|
|
|
|
I2P = 5
|
|
|
|
CJDNS = 6
|
|
|
|
|
|
|
|
def name_to_bip155(addr):
|
|
|
|
'''Convert address string to BIP155 (networkID, addr) tuple.'''
|
|
|
|
if addr.endswith('.onion'):
|
2014-07-24 13:52:57 +02:00
|
|
|
vchAddr = b32decode(addr[0:-6], True)
|
2023-09-20 13:01:53 +02:00
|
|
|
if len(vchAddr) == 35:
|
|
|
|
assert vchAddr[34] == 3
|
2023-09-10 16:10:08 +02:00
|
|
|
return (BIP155Network.TORV3, vchAddr[:32])
|
2023-09-20 13:01:53 +02:00
|
|
|
elif len(vchAddr) == 10:
|
|
|
|
return (BIP155Network.TORV2, vchAddr)
|
2023-09-10 16:10:08 +02:00
|
|
|
else:
|
Merge #12987: tests/tools: Enable additional Python flake8 rules for automatic linting via Travis
643aad17fa Enable additional flake8 rules (practicalswift)
f020aca297 Minor Python cleanups to make flake8 pass with the new rules enabled (practicalswift)
Pull request description:
Enabled rules:
```
* E242: tab after ','
* E266: too many leading '#' for block comment
* E401: multiple imports on one line
* E402: module level import not at top of file
* E701: multiple statements on one line (colon)
* E901: SyntaxError: invalid syntax
* E902: TokenError: EOF in multi-line string
* F821: undefined name 'Foo'
* W293: blank line contains whitespace
* W606: 'async' and 'await' are reserved keywords starting with Python 3.7
```
Note to reviewers:
* In general we don't allow whitespace cleanups to existing code, but in order to allow for enabling Travis checking for these rules a few smaller whitespace cleanups had to made as part of this PR.
* Use [this `?w=1` link](https://github.com/bitcoin/bitcoin/pull/12987/files?w=1) to show a diff without whitespace changes.
Before this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
5 E266 too many leading '#' for block comment
4 E401 multiple imports on one line
6 E402 module level import not at top of file
5 E701 multiple statements on one line (colon)
1 F812 list comprehension redefines 'n' from line 159
4 F821 undefined name 'ConnectionRefusedError'
28 W293 blank line contains whitespace
```
After this commit:
```
$ flake8 -qq --statistics --ignore=B,C,E,F,I,N,W --select=E112,E113,E115,E116,E125,E131,E133,E223,E224,E242,E266,E271,E272,E273,E274,E275,E304,E306,E401,E402,E502,E701,E702,E703,E714,E721,E741,E742,E743,F401,E901,E902,F402,F404,F406,F407,F601,F602,F621,F622,F631,F701,F702,F703,F704,F705,F706,F707,F811,F812,F821,F822,F823,F831,F841,W292,W293,W504,W601,W602,W603,W604,W605,W606 .
$
```
Tree-SHA512: fc7d5e752298a50d4248afc620ee2c173135b4ca008e48e02913ac968e5a24a5fd5396926047ec62f1d580d537434ccae01f249bb2f3338fa59dc630bf97ca7a
Signed-off-by: pasta <pasta@dashboost.org>
2018-04-16 17:49:49 +02:00
|
|
|
raise ValueError('Invalid onion %s' % vchAddr)
|
2014-07-24 13:52:57 +02:00
|
|
|
elif '.' in addr: # IPv4
|
2023-09-10 16:10:08 +02:00
|
|
|
return (BIP155Network.IPV4, bytes((int(x) for x in addr.split('.'))))
|
2024-05-27 11:24:31 +02:00
|
|
|
elif ':' in addr: # IPv6 or CJDNS
|
2014-07-24 13:52:57 +02:00
|
|
|
sub = [[], []] # prefix, suffix
|
|
|
|
x = 0
|
|
|
|
addr = addr.split(':')
|
|
|
|
for i,comp in enumerate(addr):
|
|
|
|
if comp == '':
|
|
|
|
if i == 0 or i == (len(addr)-1): # skip empty component at beginning or end
|
|
|
|
continue
|
|
|
|
x += 1 # :: skips to suffix
|
|
|
|
assert(x < 2)
|
|
|
|
else: # two bytes per component
|
|
|
|
val = int(comp, 16)
|
|
|
|
sub[x].append(val >> 8)
|
|
|
|
sub[x].append(val & 0xff)
|
|
|
|
nullbytes = 16 - len(sub[0]) - len(sub[1])
|
|
|
|
assert((x == 0 and nullbytes == 0) or (x == 1 and nullbytes > 0))
|
2024-05-27 11:24:31 +02:00
|
|
|
addr_bytes = bytes(sub[0] + ([0] * nullbytes) + sub[1])
|
|
|
|
if addr_bytes[0] == 0xfc:
|
|
|
|
# Assume that seeds with fc00::/8 addresses belong to CJDNS,
|
|
|
|
# not to the publicly unroutable "Unique Local Unicast" network, see
|
|
|
|
# RFC4193: https://datatracker.ietf.org/doc/html/rfc4193#section-8
|
|
|
|
return (BIP155Network.CJDNS, addr_bytes)
|
|
|
|
else:
|
|
|
|
return (BIP155Network.IPV6, addr_bytes)
|
2014-07-24 13:52:57 +02:00
|
|
|
else:
|
|
|
|
raise ValueError('Could not parse address %s' % addr)
|
|
|
|
|
2023-09-10 16:10:08 +02:00
|
|
|
def parse_spec(s):
|
|
|
|
'''Convert endpoint string to BIP155 (networkID, addr, port) tuple.'''
|
2021-09-10 02:45:16 +02:00
|
|
|
match = re.match(r'\[([0-9a-fA-F:]+)\](?::([0-9]+))?$', s)
|
2014-07-24 13:52:57 +02:00
|
|
|
if match: # ipv6
|
|
|
|
host = match.group(1)
|
|
|
|
port = match.group(2)
|
2015-06-23 21:31:47 +02:00
|
|
|
elif s.count(':') > 1: # ipv6, no port
|
|
|
|
host = s
|
|
|
|
port = ''
|
2014-07-24 13:52:57 +02:00
|
|
|
else:
|
|
|
|
(host,_,port) = s.partition(':')
|
|
|
|
|
|
|
|
if not port:
|
2023-09-10 16:10:08 +02:00
|
|
|
port = 0
|
2014-07-24 13:52:57 +02:00
|
|
|
else:
|
|
|
|
port = int(port)
|
|
|
|
|
2023-09-10 16:10:08 +02:00
|
|
|
host = name_to_bip155(host)
|
2014-07-24 13:52:57 +02:00
|
|
|
|
2023-09-20 13:01:53 +02:00
|
|
|
if host[0] == BIP155Network.TORV2:
|
|
|
|
return None # TORV2 is no longer supported, so we ignore it
|
|
|
|
else:
|
|
|
|
return host + (port, )
|
2014-07-24 13:52:57 +02:00
|
|
|
|
2023-09-10 16:10:08 +02:00
|
|
|
def ser_compact_size(l):
|
|
|
|
r = b""
|
|
|
|
if l < 253:
|
|
|
|
r = struct.pack("B", l)
|
|
|
|
elif l < 0x10000:
|
|
|
|
r = struct.pack("<BH", 253, l)
|
|
|
|
elif l < 0x100000000:
|
|
|
|
r = struct.pack("<BI", 254, l)
|
|
|
|
else:
|
|
|
|
r = struct.pack("<BQ", 255, l)
|
|
|
|
return r
|
|
|
|
|
|
|
|
def bip155_serialize(spec):
|
|
|
|
'''
|
|
|
|
Serialize (networkID, addr, port) tuple to BIP155 binary format.
|
|
|
|
'''
|
|
|
|
r = b""
|
|
|
|
r += struct.pack('B', spec[0].value)
|
|
|
|
r += ser_compact_size(len(spec[1]))
|
|
|
|
r += spec[1]
|
|
|
|
r += struct.pack('>H', spec[2])
|
|
|
|
return r
|
|
|
|
|
|
|
|
def process_nodes(g, f, structname):
|
|
|
|
g.write('static const uint8_t %s[] = {\n' % structname)
|
2014-07-24 13:52:57 +02:00
|
|
|
for line in f:
|
|
|
|
comment = line.find('#')
|
|
|
|
if comment != -1:
|
|
|
|
line = line[0:comment]
|
|
|
|
line = line.strip()
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
|
2023-09-10 16:10:08 +02:00
|
|
|
spec = parse_spec(line)
|
2023-09-20 13:01:53 +02:00
|
|
|
if spec is None: # ignore this entry (e.g. no longer supported addresses like TORV2)
|
|
|
|
continue
|
2023-09-10 16:10:08 +02:00
|
|
|
blob = bip155_serialize(spec)
|
|
|
|
hoststr = ','.join(('0x%02x' % b) for b in blob)
|
|
|
|
g.write(f' {hoststr},\n')
|
|
|
|
g.write('};\n')
|
2014-07-24 13:52:57 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv)<2:
|
|
|
|
print(('Usage: %s <path_to_nodes_txt>' % sys.argv[0]), file=sys.stderr)
|
2017-08-28 22:53:34 +02:00
|
|
|
sys.exit(1)
|
2014-07-24 13:52:57 +02:00
|
|
|
g = sys.stdout
|
|
|
|
indir = sys.argv[1]
|
2020-11-08 15:45:32 +01:00
|
|
|
g.write('#ifndef BITCOIN_CHAINPARAMSSEEDS_H\n')
|
|
|
|
g.write('#define BITCOIN_CHAINPARAMSSEEDS_H\n')
|
2014-10-28 03:10:59 +01:00
|
|
|
g.write('/**\n')
|
2015-04-05 23:56:58 +02:00
|
|
|
g.write(' * List of fixed seed nodes for the dash network\n')
|
2015-06-23 21:31:47 +02:00
|
|
|
g.write(' * AUTOGENERATED by contrib/seeds/generate-seeds.py\n')
|
2014-10-28 03:10:59 +01:00
|
|
|
g.write(' *\n')
|
2023-09-10 16:10:08 +02:00
|
|
|
g.write(' * Each line contains a BIP155 serialized (networkID, addr, port) tuple.\n')
|
2014-10-28 03:10:59 +01:00
|
|
|
g.write(' */\n')
|
2018-06-16 15:21:01 +02:00
|
|
|
with open(os.path.join(indir,'nodes_main.txt'), 'r', encoding="utf8") as f:
|
2023-09-10 16:10:08 +02:00
|
|
|
process_nodes(g, f, 'chainparams_seed_main')
|
2014-07-24 13:52:57 +02:00
|
|
|
g.write('\n')
|
2018-06-16 15:21:01 +02:00
|
|
|
with open(os.path.join(indir,'nodes_test.txt'), 'r', encoding="utf8") as f:
|
2023-09-10 16:10:08 +02:00
|
|
|
process_nodes(g, f, 'chainparams_seed_test')
|
2020-11-08 15:45:32 +01:00
|
|
|
g.write('#endif // BITCOIN_CHAINPARAMSSEEDS_H\n')
|
2018-06-16 15:21:01 +02:00
|
|
|
|
2014-07-24 13:52:57 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|