astreum 0.3.1__py3-none-any.whl → 0.3.16__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- astreum/__init__.py +4 -2
- astreum/communication/handlers/handshake.py +62 -83
- astreum/communication/handlers/object_request.py +176 -0
- astreum/communication/handlers/object_response.py +115 -0
- astreum/communication/handlers/ping.py +6 -20
- astreum/communication/handlers/route_request.py +76 -0
- astreum/communication/handlers/route_response.py +53 -0
- astreum/communication/models/message.py +81 -58
- astreum/communication/models/peer.py +42 -14
- astreum/communication/models/route.py +2 -7
- astreum/communication/processors/__init__.py +0 -0
- astreum/communication/processors/incoming.py +98 -0
- astreum/communication/processors/outgoing.py +20 -0
- astreum/communication/processors/peer.py +59 -0
- astreum/communication/setup.py +39 -76
- astreum/communication/start.py +9 -10
- astreum/communication/util.py +7 -0
- astreum/consensus/start.py +9 -10
- astreum/consensus/validator.py +17 -8
- astreum/consensus/workers/discovery.py +6 -7
- astreum/consensus/workers/validation.py +334 -291
- astreum/consensus/workers/verify.py +8 -10
- astreum/crypto/chacha20poly1305.py +74 -0
- astreum/machine/evaluations/high_evaluation.py +237 -237
- astreum/machine/evaluations/low_evaluation.py +18 -18
- astreum/node.py +29 -7
- astreum/storage/actions/get.py +183 -69
- astreum/storage/actions/set.py +66 -20
- astreum/storage/requests.py +28 -0
- astreum/storage/setup.py +3 -25
- astreum/utils/config.py +76 -0
- {astreum-0.3.1.dist-info → astreum-0.3.16.dist-info}/METADATA +3 -3
- astreum-0.3.16.dist-info/RECORD +72 -0
- astreum/communication/handlers/storage_request.py +0 -81
- astreum-0.3.1.dist-info/RECORD +0 -62
- {astreum-0.3.1.dist-info → astreum-0.3.16.dist-info}/WHEEL +0 -0
- {astreum-0.3.1.dist-info → astreum-0.3.16.dist-info}/licenses/LICENSE +0 -0
- {astreum-0.3.1.dist-info → astreum-0.3.16.dist-info}/top_level.txt +0 -0
|
@@ -1,291 +1,334 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
from
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
from ..models.
|
|
9
|
-
from ..models.
|
|
10
|
-
from ..models.
|
|
11
|
-
from ..
|
|
12
|
-
from
|
|
13
|
-
from ...
|
|
14
|
-
from ...communication.models.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
stop = node._validation_stop_event
|
|
26
|
-
|
|
27
|
-
def _award_validator_reward(block: Block, reward_amount: int) -> None:
|
|
28
|
-
"""Credit the validator account with the provided reward."""
|
|
29
|
-
if reward_amount <= 0:
|
|
30
|
-
return
|
|
31
|
-
accounts = getattr(block, "accounts", None)
|
|
32
|
-
validator_key = getattr(block, "validator_public_key", None)
|
|
33
|
-
if accounts is None or not validator_key:
|
|
34
|
-
|
|
35
|
-
"Skipping validator reward; accounts snapshot or key missing"
|
|
36
|
-
)
|
|
37
|
-
return
|
|
38
|
-
try:
|
|
39
|
-
validator_account = accounts.get_account(
|
|
40
|
-
address=validator_key, node=node
|
|
41
|
-
)
|
|
42
|
-
except Exception:
|
|
43
|
-
|
|
44
|
-
return
|
|
45
|
-
if validator_account is None:
|
|
46
|
-
validator_account = Account.create()
|
|
47
|
-
validator_account.balance += reward_amount
|
|
48
|
-
accounts.set_account(validator_key, validator_account)
|
|
49
|
-
|
|
50
|
-
while not stop.is_set():
|
|
51
|
-
validation_public_key = getattr(node, "validation_public_key", None)
|
|
52
|
-
if not validation_public_key:
|
|
53
|
-
|
|
54
|
-
time.sleep(0.5)
|
|
55
|
-
continue
|
|
56
|
-
|
|
57
|
-
latest_block_hash = getattr(node, "latest_block_hash", None)
|
|
58
|
-
if not isinstance(latest_block_hash, (bytes, bytearray)):
|
|
59
|
-
|
|
60
|
-
time.sleep(0.5)
|
|
61
|
-
continue
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
"Querying current validator for block %s",
|
|
65
|
-
latest_block_hash.hex()
|
|
66
|
-
if isinstance(latest_block_hash, (bytes, bytearray))
|
|
67
|
-
else latest_block_hash,
|
|
68
|
-
)
|
|
69
|
-
try:
|
|
70
|
-
scheduled_validator, _ = current_validator(node, latest_block_hash)
|
|
71
|
-
except Exception as exc:
|
|
72
|
-
|
|
73
|
-
time.sleep(0.5)
|
|
74
|
-
continue
|
|
75
|
-
|
|
76
|
-
if scheduled_validator != validation_public_key:
|
|
77
|
-
expected_hex = (
|
|
78
|
-
scheduled_validator.hex()
|
|
79
|
-
if isinstance(scheduled_validator, (bytes, bytearray))
|
|
80
|
-
else scheduled_validator
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
time.sleep(0.5)
|
|
84
|
-
continue
|
|
85
|
-
|
|
86
|
-
try:
|
|
87
|
-
previous_block = Block.from_atom(node, latest_block_hash)
|
|
88
|
-
except Exception:
|
|
89
|
-
|
|
90
|
-
time.sleep(0.5)
|
|
91
|
-
continue
|
|
92
|
-
|
|
93
|
-
try:
|
|
94
|
-
current_hash = node._validation_transaction_queue.get_nowait()
|
|
95
|
-
queue_empty = False
|
|
96
|
-
except Empty:
|
|
97
|
-
current_hash = None
|
|
98
|
-
queue_empty = True
|
|
99
|
-
|
|
100
|
-
"No pending validation transactions; generating empty block"
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
try:
|
|
104
|
-
accounts_snapshot = Accounts(root_hash=previous_block.accounts_hash)
|
|
105
|
-
except Exception:
|
|
106
|
-
accounts_snapshot = None
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
new_block = Block(
|
|
110
|
-
chain_id=getattr(node, "chain", 0),
|
|
111
|
-
previous_block_hash=latest_block_hash,
|
|
112
|
-
previous_block=previous_block,
|
|
113
|
-
number=(previous_block.number or 0) + 1,
|
|
114
|
-
timestamp=None,
|
|
115
|
-
accounts_hash=previous_block.accounts_hash,
|
|
116
|
-
transactions_total_fees=0,
|
|
117
|
-
transactions_hash=None,
|
|
118
|
-
receipts_hash=None,
|
|
119
|
-
delay_difficulty=None,
|
|
120
|
-
validator_public_key=validation_public_key,
|
|
121
|
-
nonce=0,
|
|
122
|
-
signature=None,
|
|
123
|
-
accounts=accounts_snapshot,
|
|
124
|
-
transactions=[],
|
|
125
|
-
receipts=[],
|
|
126
|
-
)
|
|
127
|
-
|
|
128
|
-
"Creating block #%s extending %s",
|
|
129
|
-
new_block.number,
|
|
130
|
-
(
|
|
131
|
-
node.latest_block_hash.hex()
|
|
132
|
-
if isinstance(node.latest_block_hash, (bytes, bytearray))
|
|
133
|
-
else node.latest_block_hash
|
|
134
|
-
),
|
|
135
|
-
)
|
|
136
|
-
|
|
137
|
-
# we may want to add a timer to process part of the txs only on a slow computer
|
|
138
|
-
total_fees = 0
|
|
139
|
-
while current_hash is not None:
|
|
140
|
-
try:
|
|
141
|
-
total_fees += apply_transaction(node, new_block, current_hash)
|
|
142
|
-
except NotImplementedError:
|
|
143
|
-
tx_hex = (
|
|
144
|
-
current_hash.hex()
|
|
145
|
-
if isinstance(current_hash, (bytes, bytearray))
|
|
146
|
-
else current_hash
|
|
147
|
-
)
|
|
148
|
-
|
|
149
|
-
node._validation_transaction_queue.put(current_hash)
|
|
150
|
-
time.sleep(0.5)
|
|
151
|
-
break
|
|
152
|
-
except Exception:
|
|
153
|
-
tx_hex = (
|
|
154
|
-
current_hash.hex()
|
|
155
|
-
if isinstance(current_hash, (bytes, bytearray))
|
|
156
|
-
else current_hash
|
|
157
|
-
)
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
try:
|
|
161
|
-
current_hash = node._validation_transaction_queue.get_nowait()
|
|
162
|
-
except Empty:
|
|
163
|
-
current_hash = None
|
|
164
|
-
|
|
165
|
-
new_block.transactions_total_fees = total_fees
|
|
166
|
-
reward_amount = total_fees if total_fees > 0 else 1
|
|
167
|
-
if total_fees == 0 and queue_empty:
|
|
168
|
-
|
|
169
|
-
elif total_fees > 0:
|
|
170
|
-
|
|
171
|
-
"Collected %d aster in transaction fees for this block", total_fees
|
|
172
|
-
)
|
|
173
|
-
_award_validator_reward(new_block, reward_amount)
|
|
174
|
-
|
|
175
|
-
# create an atom list of transactions, save the list head hash as the block's transactions_hash
|
|
176
|
-
transactions = new_block.transactions or []
|
|
177
|
-
tx_hashes = [bytes(tx.hash) for tx in transactions if tx.hash]
|
|
178
|
-
head_hash, _ = bytes_list_to_atoms(tx_hashes)
|
|
179
|
-
new_block.transactions_hash = head_hash
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
receipts = new_block.receipts or []
|
|
183
|
-
receipt_atoms = []
|
|
184
|
-
receipt_hashes = []
|
|
185
|
-
for rcpt in receipts:
|
|
186
|
-
receipt_id, atoms = rcpt.to_atom()
|
|
187
|
-
receipt_atoms.extend(atoms)
|
|
188
|
-
receipt_hashes.append(bytes(receipt_id))
|
|
189
|
-
receipts_head, _ = bytes_list_to_atoms(receipt_hashes)
|
|
190
|
-
new_block.receipts_hash = receipts_head
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
account_atoms = []
|
|
194
|
-
if new_block.accounts is not None:
|
|
195
|
-
try:
|
|
196
|
-
account_atoms = new_block.accounts.update_trie(node)
|
|
197
|
-
new_block.accounts_hash = new_block.accounts.root_hash
|
|
198
|
-
|
|
199
|
-
"Updated trie for %d cached accounts",
|
|
200
|
-
len(new_block.accounts._cache),
|
|
201
|
-
)
|
|
202
|
-
except Exception:
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
now = time.time()
|
|
206
|
-
min_allowed = new_block.previous_block.timestamp + 1
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
new_block.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
import time
|
|
5
|
+
from queue import Empty
|
|
6
|
+
from typing import Any, Callable
|
|
7
|
+
|
|
8
|
+
from ..models.account import Account
|
|
9
|
+
from ..models.accounts import Accounts
|
|
10
|
+
from ..models.block import Block
|
|
11
|
+
from ..models.transaction import apply_transaction
|
|
12
|
+
from ..validator import current_validator
|
|
13
|
+
from ...storage.models.atom import bytes_list_to_atoms
|
|
14
|
+
from ...communication.models.message import Message, MessageTopic
|
|
15
|
+
from ...communication.models.ping import Ping
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def make_validation_worker(
|
|
19
|
+
node: Any,
|
|
20
|
+
) -> Callable[[], None]:
|
|
21
|
+
"""Build the validation worker bound to the given node."""
|
|
22
|
+
|
|
23
|
+
def _validation_worker() -> None:
|
|
24
|
+
node.logger.info("Validation worker started")
|
|
25
|
+
stop = node._validation_stop_event
|
|
26
|
+
|
|
27
|
+
def _award_validator_reward(block: Block, reward_amount: int) -> None:
|
|
28
|
+
"""Credit the validator account with the provided reward."""
|
|
29
|
+
if reward_amount <= 0:
|
|
30
|
+
return
|
|
31
|
+
accounts = getattr(block, "accounts", None)
|
|
32
|
+
validator_key = getattr(block, "validator_public_key", None)
|
|
33
|
+
if accounts is None or not validator_key:
|
|
34
|
+
node.logger.debug(
|
|
35
|
+
"Skipping validator reward; accounts snapshot or key missing"
|
|
36
|
+
)
|
|
37
|
+
return
|
|
38
|
+
try:
|
|
39
|
+
validator_account = accounts.get_account(
|
|
40
|
+
address=validator_key, node=node
|
|
41
|
+
)
|
|
42
|
+
except Exception:
|
|
43
|
+
node.logger.exception("Unable to load validator account for reward")
|
|
44
|
+
return
|
|
45
|
+
if validator_account is None:
|
|
46
|
+
validator_account = Account.create()
|
|
47
|
+
validator_account.balance += reward_amount
|
|
48
|
+
accounts.set_account(validator_key, validator_account)
|
|
49
|
+
|
|
50
|
+
while not stop.is_set():
|
|
51
|
+
validation_public_key = getattr(node, "validation_public_key", None)
|
|
52
|
+
if not validation_public_key:
|
|
53
|
+
node.logger.debug("Validation public key unavailable; sleeping")
|
|
54
|
+
time.sleep(0.5)
|
|
55
|
+
continue
|
|
56
|
+
|
|
57
|
+
latest_block_hash = getattr(node, "latest_block_hash", None)
|
|
58
|
+
if not isinstance(latest_block_hash, (bytes, bytearray)):
|
|
59
|
+
node.logger.warning("Missing latest_block_hash; retrying")
|
|
60
|
+
time.sleep(0.5)
|
|
61
|
+
continue
|
|
62
|
+
|
|
63
|
+
node.logger.debug(
|
|
64
|
+
"Querying current validator for block %s",
|
|
65
|
+
latest_block_hash.hex()
|
|
66
|
+
if isinstance(latest_block_hash, (bytes, bytearray))
|
|
67
|
+
else latest_block_hash,
|
|
68
|
+
)
|
|
69
|
+
try:
|
|
70
|
+
scheduled_validator, _ = current_validator(node, latest_block_hash)
|
|
71
|
+
except Exception as exc:
|
|
72
|
+
node.logger.exception("Unable to determine current validator: %s", exc)
|
|
73
|
+
time.sleep(0.5)
|
|
74
|
+
continue
|
|
75
|
+
|
|
76
|
+
if scheduled_validator != validation_public_key:
|
|
77
|
+
expected_hex = (
|
|
78
|
+
scheduled_validator.hex()
|
|
79
|
+
if isinstance(scheduled_validator, (bytes, bytearray))
|
|
80
|
+
else scheduled_validator
|
|
81
|
+
)
|
|
82
|
+
node.logger.debug("Current validator mismatch; expected %s", expected_hex)
|
|
83
|
+
time.sleep(0.5)
|
|
84
|
+
continue
|
|
85
|
+
|
|
86
|
+
try:
|
|
87
|
+
previous_block = Block.from_atom(node, latest_block_hash)
|
|
88
|
+
except Exception:
|
|
89
|
+
node.logger.exception("Unable to load previous block for validation")
|
|
90
|
+
time.sleep(0.5)
|
|
91
|
+
continue
|
|
92
|
+
|
|
93
|
+
try:
|
|
94
|
+
current_hash = node._validation_transaction_queue.get_nowait()
|
|
95
|
+
queue_empty = False
|
|
96
|
+
except Empty:
|
|
97
|
+
current_hash = None
|
|
98
|
+
queue_empty = True
|
|
99
|
+
node.logger.debug(
|
|
100
|
+
"No pending validation transactions; generating empty block"
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
accounts_snapshot = Accounts(root_hash=previous_block.accounts_hash)
|
|
105
|
+
except Exception:
|
|
106
|
+
accounts_snapshot = None
|
|
107
|
+
node.logger.warning("Unable to initialise accounts snapshot for block")
|
|
108
|
+
|
|
109
|
+
new_block = Block(
|
|
110
|
+
chain_id=getattr(node, "chain", 0),
|
|
111
|
+
previous_block_hash=latest_block_hash,
|
|
112
|
+
previous_block=previous_block,
|
|
113
|
+
number=(previous_block.number or 0) + 1,
|
|
114
|
+
timestamp=None,
|
|
115
|
+
accounts_hash=previous_block.accounts_hash,
|
|
116
|
+
transactions_total_fees=0,
|
|
117
|
+
transactions_hash=None,
|
|
118
|
+
receipts_hash=None,
|
|
119
|
+
delay_difficulty=None,
|
|
120
|
+
validator_public_key=validation_public_key,
|
|
121
|
+
nonce=0,
|
|
122
|
+
signature=None,
|
|
123
|
+
accounts=accounts_snapshot,
|
|
124
|
+
transactions=[],
|
|
125
|
+
receipts=[],
|
|
126
|
+
)
|
|
127
|
+
node.logger.debug(
|
|
128
|
+
"Creating block #%s extending %s",
|
|
129
|
+
new_block.number,
|
|
130
|
+
(
|
|
131
|
+
node.latest_block_hash.hex()
|
|
132
|
+
if isinstance(node.latest_block_hash, (bytes, bytearray))
|
|
133
|
+
else node.latest_block_hash
|
|
134
|
+
),
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# we may want to add a timer to process part of the txs only on a slow computer
|
|
138
|
+
total_fees = 0
|
|
139
|
+
while current_hash is not None:
|
|
140
|
+
try:
|
|
141
|
+
total_fees += apply_transaction(node, new_block, current_hash)
|
|
142
|
+
except NotImplementedError:
|
|
143
|
+
tx_hex = (
|
|
144
|
+
current_hash.hex()
|
|
145
|
+
if isinstance(current_hash, (bytes, bytearray))
|
|
146
|
+
else current_hash
|
|
147
|
+
)
|
|
148
|
+
node.logger.warning("Transaction %s unsupported; re-queued", tx_hex)
|
|
149
|
+
node._validation_transaction_queue.put(current_hash)
|
|
150
|
+
time.sleep(0.5)
|
|
151
|
+
break
|
|
152
|
+
except Exception:
|
|
153
|
+
tx_hex = (
|
|
154
|
+
current_hash.hex()
|
|
155
|
+
if isinstance(current_hash, (bytes, bytearray))
|
|
156
|
+
else current_hash
|
|
157
|
+
)
|
|
158
|
+
node.logger.exception("Failed applying transaction %s", tx_hex)
|
|
159
|
+
|
|
160
|
+
try:
|
|
161
|
+
current_hash = node._validation_transaction_queue.get_nowait()
|
|
162
|
+
except Empty:
|
|
163
|
+
current_hash = None
|
|
164
|
+
|
|
165
|
+
new_block.transactions_total_fees = total_fees
|
|
166
|
+
reward_amount = total_fees if total_fees > 0 else 1
|
|
167
|
+
if total_fees == 0 and queue_empty:
|
|
168
|
+
node.logger.debug("Awarding base validator reward of 1 aster")
|
|
169
|
+
elif total_fees > 0:
|
|
170
|
+
node.logger.debug(
|
|
171
|
+
"Collected %d aster in transaction fees for this block", total_fees
|
|
172
|
+
)
|
|
173
|
+
_award_validator_reward(new_block, reward_amount)
|
|
174
|
+
|
|
175
|
+
# create an atom list of transactions, save the list head hash as the block's transactions_hash
|
|
176
|
+
transactions = new_block.transactions or []
|
|
177
|
+
tx_hashes = [bytes(tx.hash) for tx in transactions if tx.hash]
|
|
178
|
+
head_hash, _ = bytes_list_to_atoms(tx_hashes)
|
|
179
|
+
new_block.transactions_hash = head_hash
|
|
180
|
+
node.logger.debug("Block includes %d transactions", len(transactions))
|
|
181
|
+
|
|
182
|
+
receipts = new_block.receipts or []
|
|
183
|
+
receipt_atoms = []
|
|
184
|
+
receipt_hashes = []
|
|
185
|
+
for rcpt in receipts:
|
|
186
|
+
receipt_id, atoms = rcpt.to_atom()
|
|
187
|
+
receipt_atoms.extend(atoms)
|
|
188
|
+
receipt_hashes.append(bytes(receipt_id))
|
|
189
|
+
receipts_head, _ = bytes_list_to_atoms(receipt_hashes)
|
|
190
|
+
new_block.receipts_hash = receipts_head
|
|
191
|
+
node.logger.debug("Block includes %d receipts", len(receipts))
|
|
192
|
+
|
|
193
|
+
account_atoms = []
|
|
194
|
+
if new_block.accounts is not None:
|
|
195
|
+
try:
|
|
196
|
+
account_atoms = new_block.accounts.update_trie(node)
|
|
197
|
+
new_block.accounts_hash = new_block.accounts.root_hash
|
|
198
|
+
node.logger.debug(
|
|
199
|
+
"Updated trie for %d cached accounts",
|
|
200
|
+
len(new_block.accounts._cache),
|
|
201
|
+
)
|
|
202
|
+
except Exception:
|
|
203
|
+
node.logger.exception("Failed to update accounts trie for block")
|
|
204
|
+
|
|
205
|
+
now = time.time()
|
|
206
|
+
min_allowed = new_block.previous_block.timestamp + 1
|
|
207
|
+
nonce_time_seconds = node.nonce_time_ms / 1000.0
|
|
208
|
+
expected_blocktime = now + nonce_time_seconds
|
|
209
|
+
new_block.timestamp = max(int(math.ceil(expected_blocktime)), min_allowed)
|
|
210
|
+
|
|
211
|
+
new_block.delay_difficulty = Block.calculate_delay_difficulty(
|
|
212
|
+
previous_timestamp=previous_block.timestamp,
|
|
213
|
+
current_timestamp=new_block.timestamp,
|
|
214
|
+
previous_difficulty=previous_block.delay_difficulty,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
try:
|
|
218
|
+
nonce_started = time.perf_counter()
|
|
219
|
+
new_block.generate_nonce(difficulty=previous_block.delay_difficulty)
|
|
220
|
+
elapsed_ms = int((time.perf_counter() - nonce_started) * 1000)
|
|
221
|
+
setattr(node, "nonce_time_ms", elapsed_ms)
|
|
222
|
+
node.logger.debug(
|
|
223
|
+
"Found nonce %s for block #%s at difficulty %s",
|
|
224
|
+
new_block.nonce,
|
|
225
|
+
new_block.number,
|
|
226
|
+
new_block.delay_difficulty,
|
|
227
|
+
)
|
|
228
|
+
except Exception:
|
|
229
|
+
node.logger.exception("Failed while searching for block nonce")
|
|
230
|
+
time.sleep(0.5)
|
|
231
|
+
continue
|
|
232
|
+
|
|
233
|
+
# wait until the block timestamp is reached before propagating
|
|
234
|
+
now = time.time()
|
|
235
|
+
if now > (new_block.timestamp + 2):
|
|
236
|
+
node.logger.warning(
|
|
237
|
+
"Skipping block #%s propagation; timestamp %s already elapsed (now=%s)",
|
|
238
|
+
new_block.number,
|
|
239
|
+
new_block.timestamp,
|
|
240
|
+
now,
|
|
241
|
+
)
|
|
242
|
+
continue
|
|
243
|
+
|
|
244
|
+
spread_delay = new_block.timestamp - now
|
|
245
|
+
if spread_delay > 0:
|
|
246
|
+
node.logger.debug(
|
|
247
|
+
"Delaying distribution for %.3fs to reach block timestamp %s",
|
|
248
|
+
spread_delay,
|
|
249
|
+
new_block.timestamp,
|
|
250
|
+
)
|
|
251
|
+
time.sleep(spread_delay)
|
|
252
|
+
|
|
253
|
+
# atomize block
|
|
254
|
+
new_block_hash, new_block_atoms = new_block.to_atom()
|
|
255
|
+
# put as own latest block hash
|
|
256
|
+
node.latest_block_hash = new_block_hash
|
|
257
|
+
node.latest_block = new_block
|
|
258
|
+
node.logger.info(
|
|
259
|
+
"Created block #%s with hash %s (%d atoms)",
|
|
260
|
+
new_block.number,
|
|
261
|
+
new_block_hash.hex(),
|
|
262
|
+
len(new_block_atoms),
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
# ping peers in the validation route to update their records
|
|
267
|
+
if node.validation_route and node.outgoing_queue and node.peers:
|
|
268
|
+
route_peers = {
|
|
269
|
+
peer_key
|
|
270
|
+
for bucket in getattr(node.validation_route, "buckets", {}).values()
|
|
271
|
+
for peer_key in bucket
|
|
272
|
+
}
|
|
273
|
+
if route_peers:
|
|
274
|
+
ping_payload = Ping(
|
|
275
|
+
is_validator=True,
|
|
276
|
+
latest_block=new_block_hash,
|
|
277
|
+
).to_bytes()
|
|
278
|
+
|
|
279
|
+
for peer_key in route_peers:
|
|
280
|
+
peer_hex = (
|
|
281
|
+
peer_key.hex()
|
|
282
|
+
if isinstance(peer_key, (bytes, bytearray))
|
|
283
|
+
else peer_key
|
|
284
|
+
)
|
|
285
|
+
peer = node.get_peer(peer_key)
|
|
286
|
+
if peer is None:
|
|
287
|
+
node.logger.debug(
|
|
288
|
+
"Skipping validator ping to peer %s; peer not found",
|
|
289
|
+
peer_hex,
|
|
290
|
+
)
|
|
291
|
+
continue
|
|
292
|
+
address = getattr(peer, "address", None)
|
|
293
|
+
if not address:
|
|
294
|
+
node.logger.debug(
|
|
295
|
+
"Skipping validator ping to %s; address missing",
|
|
296
|
+
peer_hex,
|
|
297
|
+
)
|
|
298
|
+
continue
|
|
299
|
+
try:
|
|
300
|
+
ping_msg = Message(
|
|
301
|
+
topic=MessageTopic.PING,
|
|
302
|
+
content=ping_payload,
|
|
303
|
+
sender=node.relay_public_key,
|
|
304
|
+
)
|
|
305
|
+
ping_msg.encrypt(peer.shared_key_bytes)
|
|
306
|
+
node.outgoing_queue.put((ping_msg.to_bytes(), address))
|
|
307
|
+
node.logger.debug(
|
|
308
|
+
"Queued validator ping to %s (%s)",
|
|
309
|
+
address,
|
|
310
|
+
peer_key.hex()
|
|
311
|
+
if isinstance(peer_key, (bytes, bytearray))
|
|
312
|
+
else peer_key,
|
|
313
|
+
)
|
|
314
|
+
except Exception:
|
|
315
|
+
node.logger.exception("Failed queueing validator ping to %s", address)
|
|
316
|
+
|
|
317
|
+
# upload block atoms
|
|
318
|
+
for block_atom in new_block_atoms:
|
|
319
|
+
atom_id = block_atom.object_id()
|
|
320
|
+
node._hot_storage_set(key=atom_id, value=block_atom)
|
|
321
|
+
|
|
322
|
+
# upload receipt atoms
|
|
323
|
+
for receipt_atom in receipt_atoms:
|
|
324
|
+
atom_id = receipt_atom.object_id()
|
|
325
|
+
node._hot_storage_set(key=atom_id, value=receipt_atom)
|
|
326
|
+
|
|
327
|
+
# upload account atoms
|
|
328
|
+
for account_atom in account_atoms:
|
|
329
|
+
atom_id = account_atom.object_id()
|
|
330
|
+
node._hot_storage_set(key=atom_id, value=account_atom)
|
|
331
|
+
|
|
332
|
+
node.logger.info("Validation worker stopped")
|
|
333
|
+
|
|
334
|
+
return _validation_worker
|