Skip to content

Commit b40aae9

Browse files
Start using nox and pre-commit, linted code
1 parent e58a375 commit b40aae9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+649
-206
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ cython_debug/
142142
benchmarks
143143

144144
errors
145-
tmp
145+
tmp

.pre-commit-config.yaml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Skip execution of one or more hooks using the SKIP environment variable:
2+
# $ SKIP=pylint git commit -m "foo"
3+
# $ SKIP=mypy,pylint pre-commit run --all-files
4+
#
5+
# If want to disable all hooks while committing, use the --no-verify/-n option:
6+
# $ git commit -n -m "foo"
7+
8+
repos:
9+
- repo: https://github.com/pre-commit/pre-commit-hooks
10+
rev: v4.4.0
11+
hooks:
12+
- id: trailing-whitespace
13+
- id: end-of-file-fixer
14+
- id: mixed-line-ending
15+
- id: check-case-conflict
16+
- id: check-yaml
17+
- id: check-merge-conflict
18+
- id: check-vcs-permalinks
19+
- id: detect-private-key
20+
- id: debug-statements
21+
- id: check-docstring-first
22+
- id: check-toml
23+
- repo: https://github.com/DavidAnson/markdownlint-cli2
24+
rev: v0.8.1
25+
hooks:
26+
- id: markdownlint-cli2-fix
27+
name: markdownlint-cli2-fix (in place fixes)
28+
- repo: https://github.com/leoll2/copyright_notice_precommit
29+
rev: 0.1.1
30+
hooks:
31+
- id: copyright-notice
32+
args: [--notice=COPYRIGHT]
33+
files: python
34+
- repo: https://github.com/asottile/pyupgrade
35+
rev: v3.10.1
36+
hooks:
37+
- id: pyupgrade
38+
args: [--py38-plus]
39+
# exclude: *fixtures
40+
- repo: https://github.com/PyCQA/autoflake
41+
rev: v2.2.1
42+
hooks:
43+
- id: autoflake
44+
args:
45+
- --in-place
46+
- --expand-star-imports
47+
- --remove-all-unused-imports
48+
- --ignore-init-module-imports
49+
- --remove-duplicate-keys
50+
- --remove-unused-variables
51+
- --remove-rhs-for-unused-variables
52+
- repo: https://github.com/PyCQA/isort
53+
rev: 5.12.0
54+
hooks:
55+
- id: isort
56+
name: isort (black profile, in place fixes)
57+
args: ["--profile", "black", "--filter-files"]
58+
- repo: https://github.com/DanielNoord/pydocstringformatter
59+
rev: v0.7.3
60+
hooks:
61+
- id: pydocstringformatter
62+
- repo: https://github.com/asottile/yesqa
63+
rev: v1.5.0
64+
hooks:
65+
- id: yesqa
66+
- repo: https://github.com/psf/black
67+
rev: 23.9.1
68+
hooks:
69+
- id: black
70+
name: black (in place fixes)
71+
# args: [--diff, --check]
72+
# It is recommended to specify the latest version of Python
73+
# supported by your project here, or alternatively use
74+
# pre-commit's default_language_version, see
75+
# https://pre-commit.com/#top_level-default_language_version
76+
# language_version: python3.11
77+
- repo: https://github.com/PyCQA/flake8
78+
rev: 6.1.0
79+
hooks:
80+
- id: flake8
81+
additional_dependencies: [Flake8-pyproject]
82+
- repo: https://github.com/PyCQA/bandit
83+
rev: 1.7.5
84+
hooks:
85+
- id: bandit
86+
name: bandit (checksig)
87+
exclude: ".*test.*"
88+
language: python
89+
types: [python]
90+
- repo: https://github.com/PyCQA/bandit
91+
rev: 1.7.5
92+
hooks:
93+
- id: bandit
94+
name: bandit (tests)
95+
# for the test folder disable
96+
# B101, Test for use of assert
97+
args: ["--skip", "B101,B311"]
98+
exclude: checksig
99+
language: python
100+
types: [python]
101+
- repo: https://github.com/pre-commit/mirrors-mypy
102+
rev: v1.5.1
103+
hooks:
104+
- id: mypy

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# TODO
2+
13
- Add bloom filter messages
24
- Add address store system. stop using dns for finding peers
35
- Improve time spent while connecting when starting to download blocks

btclib_node/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def stop_handler(signal, frame):
6464
self.rpc_manager = RpcManager(self, self.rpc_port)
6565

6666
def run(self):
67-
6867
self.logger.info("Starting main loop")
6968

7069
if self.p2p_port:

btclib_node/block_db/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def serialize(self):
8383

8484
class BlockDB:
8585
def __init__(self, data_dir, logger):
86-
8786
self.logger = logger
8887

8988
self.data_dir = data_dir / "blocks"
@@ -127,7 +126,7 @@ def __find_block_file(self):
127126
else:
128127
filename = f"{self.file_index:06d}.blk"
129128
file_metadata = self.files[filename]
130-
if file_metadata.size > 128 * 1000 ** 2: # 128MB
129+
if file_metadata.size > 128 * 1000**2: # 128MB
131130
new_file = True
132131
if new_file:
133132
self.file_index += 1

btclib_node/chains.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def create_genesis(time, nonce, difficulty, version, reward):
1515
[
1616
"FFFF001D",
1717
b"\x04",
18-
"The Times 03/Jan/2009 Chancellor on brink of second bailout for banks".encode(),
18+
b"The Times 03/Jan/2009 Chancellor on brink of second bailout for banks",
1919
]
2020
)
2121
script_pub_key = script.serialize(
@@ -80,7 +80,7 @@ def __init__(self):
8080
"seed.bitcoin.wiz.biz",
8181
]
8282
self.genesis = create_genesis(
83-
1231006505, 2083236893, 0x1D00FFFF, 1, 50 * 10 ** 8
83+
1231006505, 2083236893, 0x1D00FFFF, 1, 50 * 10**8
8484
)
8585
self.flags = [
8686
(170061, "P2SH"),
@@ -106,7 +106,7 @@ def __init__(self):
106106
"testnet-seed.bluematt.me",
107107
]
108108
self.genesis = create_genesis(
109-
1296688602, 414098458, 0x1D00FFFF, 1, 50 * 10 ** 8
109+
1296688602, 414098458, 0x1D00FFFF, 1, 50 * 10**8
110110
)
111111
self.flags = [
112112
(395, "P2SH"),
@@ -126,7 +126,7 @@ def __init__(self):
126126
self.port = 38333
127127
self.magic = "0a03cf40" # default signet
128128
self.addresses = ["178.128.221.177"]
129-
self.genesis = create_genesis(1598918400, 52613770, 0x1E0377AE, 1, 50 * 10 ** 8)
129+
self.genesis = create_genesis(1598918400, 52613770, 0x1E0377AE, 1, 50 * 10**8)
130130
self.flags = [
131131
(0, "P2SH"),
132132
(0, "DERSIG"),
@@ -145,7 +145,7 @@ def __init__(self):
145145
self.port = 18444
146146
self.magic = "fabfb5da"
147147
self.addresses = []
148-
self.genesis = create_genesis(1296688602, 2, 0x207FFFFF, 1, 50 * 10 ** 8)
148+
self.genesis = create_genesis(1296688602, 2, 0x207FFFFF, 1, 50 * 10**8)
149149
self.flags = [
150150
(0, "P2SH"),
151151
(0, "DERSIG"),

btclib_node/chainstate/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import plyvel
2-
from btclib.tx.tx_in import OutPoint
3-
from btclib.tx.tx_out import TxOut
4-
5-
from btclib_node.block_db import RevBlock
62

73
from .block_index import BlockIndex
84
from .utxo_index import UtxoIndex

btclib_node/chainstate/block_index.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from collections import deque
33
from dataclasses import dataclass
44

5-
import plyvel
65
from btclib import var_int
76
from btclib.block import BlockHeader
87
from btclib.utils import bytesio_from_binarydata
@@ -12,7 +11,7 @@
1211
def calculate_work(header):
1312
target = int.from_bytes(header.bits[-3:], "big")
1413
exp = pow(256, (header.bits[0] - 3))
15-
return int(256 ** 32 / target / exp)
14+
return int(256**32 / target / exp)
1615

1716

1817
class BlockStatus(enum.IntEnum):
@@ -49,7 +48,6 @@ def serialize(self):
4948

5049
class BlockIndex:
5150
def __init__(self, parent_db, chain, logger):
52-
5351
self.logger = logger
5452

5553
self.db = parent_db
@@ -188,7 +186,6 @@ def add_headers(self, headers):
188186
added = False # flag that signals if there is a new header in this message
189187
current_work = self.get_block_info(self.active_chain[-1]).chainwork
190188
for header in headers:
191-
192189
# TODO: validate timestamp and difficulty
193190

194191
header_hash = header.hash

btclib_node/chainstate/utxo_index.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import plyvel
21
from btclib.tx.tx_in import OutPoint
32
from btclib.tx.tx_out import TxOut
43

@@ -7,7 +6,6 @@
76

87
class UtxoIndex:
98
def __init__(self, parent_db, logger):
10-
119
self.db = parent_db
1210

1311
self.removed_utxos = set()
@@ -16,7 +14,6 @@ def __init__(self, parent_db, logger):
1614
self.logger = logger
1715

1816
def add_block(self, block):
19-
2017
removed = []
2118
added = []
2219
complete_transactions = []
@@ -27,13 +24,11 @@ def add_block(self, block):
2724
added.append(out_point)
2825

2926
for tx in block.transactions[1:]:
30-
3127
tx_id = tx.id
3228

3329
prev_outputs = []
3430

3531
for tx_in in tx.vin:
36-
3732
prevout_bytes = tx_in.prev_out.serialize(check_validity=False)
3833

3934
if prevout_bytes in self.removed_utxos:
@@ -68,7 +63,6 @@ def add_block(self, block):
6863

6964
def apply_rev_block(self, rev_block):
7065
for out_point in rev_block.to_remove:
71-
7266
out_point_bytes = out_point.serialize(check_validity=False)
7367

7468
if out_point_bytes in self.removed_utxos:

btclib_node/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
"""Expception classes"""
1+
"""Expception classes."""
22

33

44
class MissingPrevoutError(ValueError):
5-
pass
5+
pass

0 commit comments

Comments
 (0)