astreum 0.2.61__py3-none-any.whl → 0.3.9__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 +16 -7
- astreum/{_communication → communication}/__init__.py +3 -3
- astreum/communication/handlers/handshake.py +89 -0
- astreum/communication/handlers/object_request.py +176 -0
- astreum/communication/handlers/object_response.py +115 -0
- astreum/communication/handlers/ping.py +34 -0
- astreum/communication/handlers/route_request.py +76 -0
- astreum/communication/handlers/route_response.py +53 -0
- astreum/communication/models/__init__.py +0 -0
- astreum/communication/models/message.py +124 -0
- astreum/communication/models/peer.py +51 -0
- astreum/{_communication → communication/models}/route.py +7 -12
- astreum/communication/processors/__init__.py +0 -0
- astreum/communication/processors/incoming.py +98 -0
- astreum/communication/processors/outgoing.py +20 -0
- astreum/communication/setup.py +166 -0
- astreum/communication/start.py +37 -0
- astreum/{_communication → communication}/util.py +7 -0
- astreum/consensus/__init__.py +20 -0
- astreum/consensus/genesis.py +66 -0
- astreum/consensus/models/__init__.py +0 -0
- astreum/consensus/models/account.py +84 -0
- astreum/consensus/models/accounts.py +72 -0
- astreum/consensus/models/block.py +364 -0
- astreum/{_consensus → consensus/models}/chain.py +7 -7
- astreum/{_consensus → consensus/models}/fork.py +8 -8
- astreum/consensus/models/receipt.py +98 -0
- astreum/{_consensus → consensus/models}/transaction.py +76 -78
- astreum/{_consensus → consensus}/setup.py +18 -50
- astreum/consensus/start.py +67 -0
- astreum/consensus/validator.py +95 -0
- astreum/{_consensus → consensus}/workers/discovery.py +19 -1
- astreum/consensus/workers/validation.py +307 -0
- astreum/{_consensus → consensus}/workers/verify.py +29 -2
- astreum/crypto/chacha20poly1305.py +74 -0
- astreum/machine/__init__.py +20 -0
- astreum/machine/evaluations/__init__.py +0 -0
- astreum/{_lispeum → machine/evaluations}/high_evaluation.py +237 -236
- astreum/machine/evaluations/low_evaluation.py +281 -0
- astreum/machine/evaluations/script_evaluation.py +27 -0
- astreum/machine/models/__init__.py +0 -0
- astreum/machine/models/environment.py +31 -0
- astreum/{_lispeum → machine/models}/expression.py +36 -8
- astreum/machine/tokenizer.py +90 -0
- astreum/node.py +78 -767
- astreum/storage/__init__.py +7 -0
- astreum/storage/actions/get.py +183 -0
- astreum/storage/actions/set.py +178 -0
- astreum/{_storage → storage/models}/atom.py +55 -57
- astreum/{_storage/patricia.py → storage/models/trie.py} +227 -203
- astreum/storage/requests.py +28 -0
- astreum/storage/setup.py +22 -15
- astreum/utils/config.py +48 -0
- {astreum-0.2.61.dist-info → astreum-0.3.9.dist-info}/METADATA +27 -26
- astreum-0.3.9.dist-info/RECORD +71 -0
- astreum/_communication/message.py +0 -101
- astreum/_communication/peer.py +0 -23
- astreum/_communication/setup.py +0 -322
- astreum/_consensus/__init__.py +0 -20
- astreum/_consensus/account.py +0 -95
- astreum/_consensus/accounts.py +0 -38
- astreum/_consensus/block.py +0 -311
- astreum/_consensus/genesis.py +0 -72
- astreum/_consensus/receipt.py +0 -136
- astreum/_consensus/workers/validation.py +0 -125
- astreum/_lispeum/__init__.py +0 -16
- astreum/_lispeum/environment.py +0 -13
- astreum/_lispeum/low_evaluation.py +0 -123
- astreum/_lispeum/tokenizer.py +0 -22
- astreum/_node.py +0 -198
- astreum/_storage/__init__.py +0 -7
- astreum/_storage/setup.py +0 -35
- astreum/format.py +0 -75
- astreum/models/block.py +0 -441
- astreum/models/merkle.py +0 -205
- astreum/models/patricia.py +0 -393
- astreum/storage/object.py +0 -68
- astreum-0.2.61.dist-info/RECORD +0 -57
- /astreum/{models → communication/handlers}/__init__.py +0 -0
- /astreum/{_communication → communication/models}/ping.py +0 -0
- /astreum/{_consensus → consensus}/workers/__init__.py +0 -0
- /astreum/{_lispeum → machine/models}/meter.py +0 -0
- /astreum/{_lispeum → machine}/parser.py +0 -0
- {astreum-0.2.61.dist-info → astreum-0.3.9.dist-info}/WHEEL +0 -0
- {astreum-0.2.61.dist-info → astreum-0.3.9.dist-info}/licenses/LICENSE +0 -0
- {astreum-0.2.61.dist-info → astreum-0.3.9.dist-info}/top_level.txt +0 -0
astreum/_communication/setup.py
DELETED
|
@@ -1,322 +0,0 @@
|
|
|
1
|
-
import socket, threading
|
|
2
|
-
from datetime import datetime, timezone
|
|
3
|
-
from queue import Queue
|
|
4
|
-
from typing import Tuple, Optional
|
|
5
|
-
from cryptography.hazmat.primitives.asymmetric import ed25519
|
|
6
|
-
from cryptography.hazmat.primitives import serialization
|
|
7
|
-
from cryptography.hazmat.primitives.asymmetric.x25519 import (
|
|
8
|
-
X25519PrivateKey,
|
|
9
|
-
X25519PublicKey,
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
from typing import TYPE_CHECKING
|
|
13
|
-
if TYPE_CHECKING:
|
|
14
|
-
from .. import Node
|
|
15
|
-
|
|
16
|
-
from . import Route, Message
|
|
17
|
-
from .message import MessageTopic
|
|
18
|
-
from .peer import Peer
|
|
19
|
-
from .ping import Ping
|
|
20
|
-
from .util import address_str_to_host_and_port
|
|
21
|
-
|
|
22
|
-
def load_x25519(hex_key: Optional[str]) -> X25519PrivateKey:
|
|
23
|
-
"""DH key for relaying (always X25519)."""
|
|
24
|
-
return
|
|
25
|
-
|
|
26
|
-
def load_ed25519(hex_key: Optional[str]) -> Optional[ed25519.Ed25519PrivateKey]:
|
|
27
|
-
"""Signing key for validation (Ed25519), or None if absent."""
|
|
28
|
-
return ed25519.Ed25519PrivateKey.from_private_bytes(bytes.fromhex(hex_key)) \
|
|
29
|
-
if hex_key else None
|
|
30
|
-
|
|
31
|
-
def make_routes(
|
|
32
|
-
relay_pk: X25519PublicKey,
|
|
33
|
-
val_sk: Optional[ed25519.Ed25519PrivateKey]
|
|
34
|
-
) -> Tuple[Route, Optional[Route]]:
|
|
35
|
-
"""Peer route (DH pubkey) + optional validation route (ed pubkey)."""
|
|
36
|
-
peer_rt = Route(relay_pk)
|
|
37
|
-
val_rt = Route(val_sk.public_key()) if val_sk else None
|
|
38
|
-
return peer_rt, val_rt
|
|
39
|
-
|
|
40
|
-
def setup_outgoing(
|
|
41
|
-
use_ipv6: bool
|
|
42
|
-
) -> Tuple[socket.socket, Queue, threading.Thread]:
|
|
43
|
-
fam = socket.AF_INET6 if use_ipv6 else socket.AF_INET
|
|
44
|
-
sock = socket.socket(fam, socket.SOCK_DGRAM)
|
|
45
|
-
q = Queue()
|
|
46
|
-
thr = threading.Thread(target=lambda: None, daemon=True)
|
|
47
|
-
thr.start()
|
|
48
|
-
return sock, q, thr
|
|
49
|
-
|
|
50
|
-
def make_maps():
|
|
51
|
-
"""Empty lookup maps: peers and addresses."""
|
|
52
|
-
return
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def process_incoming_messages(node: "Node") -> None:
|
|
56
|
-
"""Process incoming messages (placeholder)."""
|
|
57
|
-
while True:
|
|
58
|
-
try:
|
|
59
|
-
data, addr = node.incoming_queue.get()
|
|
60
|
-
except Exception as exc:
|
|
61
|
-
print(f"Error taking from incoming queue: {exc}")
|
|
62
|
-
continue
|
|
63
|
-
|
|
64
|
-
try:
|
|
65
|
-
message = Message.from_bytes(data)
|
|
66
|
-
except Exception as exc:
|
|
67
|
-
print(f"Error decoding message: {exc}")
|
|
68
|
-
continue
|
|
69
|
-
|
|
70
|
-
if message.handshake:
|
|
71
|
-
sender_key = message.sender
|
|
72
|
-
|
|
73
|
-
try:
|
|
74
|
-
sender_public_key_bytes = sender_key.public_bytes(
|
|
75
|
-
encoding=serialization.Encoding.Raw,
|
|
76
|
-
format=serialization.PublicFormat.Raw,
|
|
77
|
-
)
|
|
78
|
-
except Exception as exc:
|
|
79
|
-
print(f"Error extracting sender key bytes: {exc}")
|
|
80
|
-
continue
|
|
81
|
-
|
|
82
|
-
# Normalize remote address (IPv6 tuples may be 4 elements)
|
|
83
|
-
try:
|
|
84
|
-
host, port = addr[0], int(addr[1])
|
|
85
|
-
except Exception:
|
|
86
|
-
continue
|
|
87
|
-
address_key = (host, port)
|
|
88
|
-
|
|
89
|
-
old_key_bytes = node.addresses.get(address_key)
|
|
90
|
-
node.addresses[address_key] = sender_public_key_bytes
|
|
91
|
-
|
|
92
|
-
if old_key_bytes is None:
|
|
93
|
-
# brand-new address -> brand-new peer
|
|
94
|
-
try:
|
|
95
|
-
peer = Peer(node.relay_secret_key, sender_key)
|
|
96
|
-
except Exception:
|
|
97
|
-
continue
|
|
98
|
-
peer.address = address_key
|
|
99
|
-
|
|
100
|
-
node.peers[sender_public_key_bytes] = peer
|
|
101
|
-
node.peer_route.add_peer(sender_public_key_bytes, peer)
|
|
102
|
-
|
|
103
|
-
response = Message(handshake=True, sender=node.relay_public_key)
|
|
104
|
-
node.outgoing_queue.put((response.to_bytes(), address_key))
|
|
105
|
-
continue
|
|
106
|
-
|
|
107
|
-
elif old_key_bytes == sender_public_key_bytes:
|
|
108
|
-
# existing mapping with same key -> nothing to change
|
|
109
|
-
peer = node.peers.get(sender_public_key_bytes)
|
|
110
|
-
if peer is not None:
|
|
111
|
-
peer.address = address_key
|
|
112
|
-
|
|
113
|
-
else:
|
|
114
|
-
# address reused with a different key -> replace peer
|
|
115
|
-
node.peers.pop(old_key_bytes, None)
|
|
116
|
-
try:
|
|
117
|
-
node.peer_route.remove_peer(old_key_bytes)
|
|
118
|
-
except Exception:
|
|
119
|
-
pass
|
|
120
|
-
try:
|
|
121
|
-
peer = Peer(node.relay_secret_key, sender_key)
|
|
122
|
-
except Exception:
|
|
123
|
-
continue
|
|
124
|
-
peer.address = address_key
|
|
125
|
-
|
|
126
|
-
node.peers[sender_public_key_bytes] = peer
|
|
127
|
-
node.peer_route.add_peer(sender_public_key_bytes, peer)
|
|
128
|
-
|
|
129
|
-
match message.topic:
|
|
130
|
-
case MessageTopic.PING:
|
|
131
|
-
try:
|
|
132
|
-
host, port = addr[0], int(addr[1])
|
|
133
|
-
except Exception:
|
|
134
|
-
continue
|
|
135
|
-
address_key = (host, port)
|
|
136
|
-
sender_public_key_bytes = node.addresses.get(address_key)
|
|
137
|
-
if sender_public_key_bytes is None:
|
|
138
|
-
continue
|
|
139
|
-
peer = node.peers.get(sender_public_key_bytes)
|
|
140
|
-
if peer is None:
|
|
141
|
-
continue
|
|
142
|
-
try:
|
|
143
|
-
ping = Ping.from_bytes(message.content)
|
|
144
|
-
except Exception as exc:
|
|
145
|
-
print(f"Error decoding ping: {exc}")
|
|
146
|
-
continue
|
|
147
|
-
|
|
148
|
-
peer.timestamp = datetime.now(timezone.utc)
|
|
149
|
-
peer.latest_block = ping.latest_block
|
|
150
|
-
|
|
151
|
-
validation_route = node.validation_route
|
|
152
|
-
if validation_route is None:
|
|
153
|
-
continue
|
|
154
|
-
if ping.is_validator:
|
|
155
|
-
try:
|
|
156
|
-
validation_route.add_peer(sender_public_key_bytes)
|
|
157
|
-
except Exception:
|
|
158
|
-
pass
|
|
159
|
-
else:
|
|
160
|
-
try:
|
|
161
|
-
validation_route.remove_peer(sender_public_key_bytes)
|
|
162
|
-
except Exception:
|
|
163
|
-
pass
|
|
164
|
-
case MessageTopic.OBJECT_REQUEST:
|
|
165
|
-
pass
|
|
166
|
-
case MessageTopic.OBJECT_RESPONSE:
|
|
167
|
-
pass
|
|
168
|
-
case MessageTopic.ROUTE_REQUEST:
|
|
169
|
-
pass
|
|
170
|
-
case MessageTopic.ROUTE_RESPONSE:
|
|
171
|
-
pass
|
|
172
|
-
case MessageTopic.TRANSACTION:
|
|
173
|
-
if node.validation_secret_key is None:
|
|
174
|
-
continue
|
|
175
|
-
node._validation_transaction_queue.put(message.content)
|
|
176
|
-
|
|
177
|
-
case MessageTopic.STORAGE_REQUEST:
|
|
178
|
-
payload = message.content
|
|
179
|
-
if len(payload) < 32:
|
|
180
|
-
continue
|
|
181
|
-
|
|
182
|
-
atom_id = payload[:32]
|
|
183
|
-
provider_bytes = payload[32:]
|
|
184
|
-
if not provider_bytes:
|
|
185
|
-
continue
|
|
186
|
-
|
|
187
|
-
try:
|
|
188
|
-
provider_str = provider_bytes.decode("utf-8")
|
|
189
|
-
except UnicodeDecodeError:
|
|
190
|
-
continue
|
|
191
|
-
|
|
192
|
-
try:
|
|
193
|
-
host, port = addr[0], int(addr[1])
|
|
194
|
-
except Exception:
|
|
195
|
-
continue
|
|
196
|
-
address_key = (host, port)
|
|
197
|
-
sender_key_bytes = node.addresses.get(address_key)
|
|
198
|
-
if sender_key_bytes is None:
|
|
199
|
-
continue
|
|
200
|
-
|
|
201
|
-
try:
|
|
202
|
-
local_key_bytes = node.relay_public_key.public_bytes(
|
|
203
|
-
encoding=serialization.Encoding.Raw,
|
|
204
|
-
format=serialization.PublicFormat.Raw,
|
|
205
|
-
)
|
|
206
|
-
except Exception:
|
|
207
|
-
continue
|
|
208
|
-
|
|
209
|
-
def xor_distance(target: bytes, key: bytes) -> int:
|
|
210
|
-
return int.from_bytes(
|
|
211
|
-
bytes(a ^ b for a, b in zip(target, key)),
|
|
212
|
-
byteorder="big",
|
|
213
|
-
signed=False,
|
|
214
|
-
)
|
|
215
|
-
|
|
216
|
-
self_distance = xor_distance(atom_id, local_key_bytes)
|
|
217
|
-
|
|
218
|
-
try:
|
|
219
|
-
closest_peer = node.peer_route.closest_peer_for_hash(atom_id)
|
|
220
|
-
except Exception:
|
|
221
|
-
closest_peer = None
|
|
222
|
-
|
|
223
|
-
if (
|
|
224
|
-
closest_peer is not None
|
|
225
|
-
and closest_peer.public_key_bytes != sender_key_bytes
|
|
226
|
-
):
|
|
227
|
-
closest_distance = xor_distance(atom_id, closest_peer.public_key_bytes)
|
|
228
|
-
if closest_distance < self_distance:
|
|
229
|
-
target_addr = closest_peer.address
|
|
230
|
-
if target_addr is not None and target_addr != addr:
|
|
231
|
-
node.outgoing_queue.put((message.to_bytes(), target_addr))
|
|
232
|
-
continue
|
|
233
|
-
|
|
234
|
-
node.storage_index[atom_id] = provider_str.strip()
|
|
235
|
-
|
|
236
|
-
case _:
|
|
237
|
-
continue
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
def populate_incoming_messages(node: "Node") -> None:
|
|
241
|
-
"""Receive UDP packets and feed the incoming queue (placeholder)."""
|
|
242
|
-
while True:
|
|
243
|
-
try:
|
|
244
|
-
data, addr = node.incoming_socket.recvfrom(4096)
|
|
245
|
-
node.incoming_queue.put((data, addr))
|
|
246
|
-
except Exception as exc:
|
|
247
|
-
print(f"Error populating incoming queue: {exc}")
|
|
248
|
-
|
|
249
|
-
def communication_setup(node: "Node", config: dict):
|
|
250
|
-
node.use_ipv6 = config.get('use_ipv6', False)
|
|
251
|
-
|
|
252
|
-
# key loading
|
|
253
|
-
node.relay_secret_key = load_x25519(config.get('relay_secret_key'))
|
|
254
|
-
node.validation_secret_key = load_ed25519(config.get('validation_secret_key'))
|
|
255
|
-
|
|
256
|
-
# derive pubs + routes
|
|
257
|
-
node.relay_public_key = node.relay_secret_key.public_key()
|
|
258
|
-
node.validation_public_key = (
|
|
259
|
-
node.validation_secret_key.public_key().public_bytes(
|
|
260
|
-
encoding=serialization.Encoding.Raw,
|
|
261
|
-
format=serialization.PublicFormat.Raw,
|
|
262
|
-
)
|
|
263
|
-
if node.validation_secret_key
|
|
264
|
-
else None
|
|
265
|
-
)
|
|
266
|
-
node.peer_route, node.validation_route = make_routes(
|
|
267
|
-
node.relay_public_key,
|
|
268
|
-
node.validation_secret_key
|
|
269
|
-
)
|
|
270
|
-
|
|
271
|
-
# sockets + queues + threads
|
|
272
|
-
incoming_port = config.get('incoming_port', 7373)
|
|
273
|
-
fam = socket.AF_INET6 if node.use_ipv6 else socket.AF_INET
|
|
274
|
-
node.incoming_socket = socket.socket(fam, socket.SOCK_DGRAM)
|
|
275
|
-
if node.use_ipv6:
|
|
276
|
-
node.incoming_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
|
|
277
|
-
node.incoming_socket.bind(("::" if node.use_ipv6 else "0.0.0.0", incoming_port or 0))
|
|
278
|
-
node.incoming_port = node.incoming_socket.getsockname()[1]
|
|
279
|
-
node.incoming_queue = Queue()
|
|
280
|
-
node.incoming_populate_thread = threading.Thread(
|
|
281
|
-
target=populate_incoming_messages,
|
|
282
|
-
args=(node,),
|
|
283
|
-
daemon=True,
|
|
284
|
-
)
|
|
285
|
-
node.incoming_process_thread = threading.Thread(
|
|
286
|
-
target=process_incoming_messages,
|
|
287
|
-
args=(node,),
|
|
288
|
-
daemon=True,
|
|
289
|
-
)
|
|
290
|
-
node.incoming_populate_thread.start()
|
|
291
|
-
node.incoming_process_thread.start()
|
|
292
|
-
|
|
293
|
-
(node.outgoing_socket,
|
|
294
|
-
node.outgoing_queue,
|
|
295
|
-
node.outgoing_thread
|
|
296
|
-
) = setup_outgoing(node.use_ipv6)
|
|
297
|
-
|
|
298
|
-
# other workers & maps
|
|
299
|
-
node.object_request_queue = Queue()
|
|
300
|
-
node.peer_manager_thread = threading.Thread(
|
|
301
|
-
target=node._relay_peer_manager,
|
|
302
|
-
daemon=True
|
|
303
|
-
)
|
|
304
|
-
node.peer_manager_thread.start()
|
|
305
|
-
|
|
306
|
-
node.peers, node.addresses = {}, {} # peers: Dict[bytes,Peer], addresses: Dict[(str,int),bytes]
|
|
307
|
-
latest_hash = getattr(node, "latest_block_hash", None)
|
|
308
|
-
if not isinstance(latest_hash, (bytes, bytearray)) or len(latest_hash) != 32:
|
|
309
|
-
node.latest_block_hash = bytes(32)
|
|
310
|
-
else:
|
|
311
|
-
node.latest_block_hash = bytes(latest_hash)
|
|
312
|
-
|
|
313
|
-
# bootstrap pings
|
|
314
|
-
for addr in config.get('bootstrap', []):
|
|
315
|
-
try:
|
|
316
|
-
host, port = address_str_to_host_and_port(addr) # type: ignore[arg-type]
|
|
317
|
-
except Exception:
|
|
318
|
-
continue
|
|
319
|
-
|
|
320
|
-
handshake_message = Message(handshake=True, sender=node.relay_public_key)
|
|
321
|
-
|
|
322
|
-
node.outgoing_queue.put((handshake_message.to_bytes(), (host, port)))
|
astreum/_consensus/__init__.py
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
from .account import Account
|
|
2
|
-
from .accounts import Accounts
|
|
3
|
-
from .block import Block
|
|
4
|
-
from .chain import Chain
|
|
5
|
-
from .fork import Fork
|
|
6
|
-
from .receipt import Receipt
|
|
7
|
-
from .transaction import Transaction
|
|
8
|
-
from .setup import consensus_setup
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
__all__ = [
|
|
12
|
-
"Block",
|
|
13
|
-
"Chain",
|
|
14
|
-
"Fork",
|
|
15
|
-
"Receipt",
|
|
16
|
-
"Transaction",
|
|
17
|
-
"Account",
|
|
18
|
-
"Accounts",
|
|
19
|
-
"consensus_setup",
|
|
20
|
-
]
|
astreum/_consensus/account.py
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from dataclasses import dataclass, field
|
|
4
|
-
from typing import Any, List, Optional, Tuple
|
|
5
|
-
|
|
6
|
-
from .._storage.atom import Atom, ZERO32
|
|
7
|
-
from .._storage.patricia import PatriciaTrie
|
|
8
|
-
from ..utils.integer import bytes_to_int, int_to_bytes
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
@dataclass
|
|
12
|
-
class Account:
|
|
13
|
-
balance: int
|
|
14
|
-
code: bytes
|
|
15
|
-
counter: int
|
|
16
|
-
data_hash: bytes
|
|
17
|
-
data: PatriciaTrie
|
|
18
|
-
hash: bytes = ZERO32
|
|
19
|
-
body_hash: bytes = ZERO32
|
|
20
|
-
atoms: List[Atom] = field(default_factory=list)
|
|
21
|
-
|
|
22
|
-
@classmethod
|
|
23
|
-
def create(cls, balance: int = 0, data_hash: bytes = ZERO32, code: bytes = ZERO32, counter: int = 0) -> "Account":
|
|
24
|
-
account = cls(
|
|
25
|
-
balance=int(balance),
|
|
26
|
-
code=bytes(code),
|
|
27
|
-
counter=int(counter),
|
|
28
|
-
data_hash=bytes(data_hash),
|
|
29
|
-
data=PatriciaTrie(root_hash=bytes(data_hash)),
|
|
30
|
-
)
|
|
31
|
-
account.to_atom()
|
|
32
|
-
return account
|
|
33
|
-
|
|
34
|
-
@classmethod
|
|
35
|
-
def from_atom(cls, node: Any, account_id: bytes) -> "Account":
|
|
36
|
-
storage_get = node.storage_get
|
|
37
|
-
|
|
38
|
-
type_atom = storage_get(account_id)
|
|
39
|
-
if type_atom is None or type_atom.data != b"account":
|
|
40
|
-
raise ValueError("not an account (type mismatch)")
|
|
41
|
-
|
|
42
|
-
def _read_atom(atom_id: Optional[bytes]) -> Optional[Atom]:
|
|
43
|
-
if not atom_id or atom_id == ZERO32:
|
|
44
|
-
return None
|
|
45
|
-
return storage_get(atom_id)
|
|
46
|
-
|
|
47
|
-
balance_atom = _read_atom(type_atom.next)
|
|
48
|
-
if balance_atom is None:
|
|
49
|
-
raise ValueError("malformed account (balance missing)")
|
|
50
|
-
|
|
51
|
-
code_atom = _read_atom(balance_atom.next)
|
|
52
|
-
if code_atom is None:
|
|
53
|
-
raise ValueError("malformed account (code missing)")
|
|
54
|
-
|
|
55
|
-
counter_atom = _read_atom(code_atom.next)
|
|
56
|
-
if counter_atom is None:
|
|
57
|
-
raise ValueError("malformed account (counter missing)")
|
|
58
|
-
|
|
59
|
-
data_atom = _read_atom(counter_atom.next)
|
|
60
|
-
if data_atom is None:
|
|
61
|
-
raise ValueError("malformed account (data missing)")
|
|
62
|
-
|
|
63
|
-
account = cls.create(
|
|
64
|
-
balance=bytes_to_int(balance_atom.data),
|
|
65
|
-
data_hash=data_atom.data,
|
|
66
|
-
counter=bytes_to_int(counter_atom.data),
|
|
67
|
-
code=code_atom.data,
|
|
68
|
-
)
|
|
69
|
-
if account.hash != account_id:
|
|
70
|
-
raise ValueError("account hash mismatch while decoding")
|
|
71
|
-
return account
|
|
72
|
-
|
|
73
|
-
def to_atom(self) -> Tuple[bytes, List[Atom]]:
|
|
74
|
-
# Build a single forward chain: account -> balance -> code -> counter -> data.
|
|
75
|
-
data_atom = Atom.from_data(data=bytes(self.data_hash))
|
|
76
|
-
counter_atom = Atom.from_data(
|
|
77
|
-
data=int_to_bytes(self.counter),
|
|
78
|
-
next_hash=data_atom.object_id(),
|
|
79
|
-
)
|
|
80
|
-
code_atom = Atom.from_data(
|
|
81
|
-
data=bytes(self.code),
|
|
82
|
-
next_hash=counter_atom.object_id(),
|
|
83
|
-
)
|
|
84
|
-
balance_atom = Atom.from_data(
|
|
85
|
-
data=int_to_bytes(self.balance),
|
|
86
|
-
next_hash=code_atom.object_id(),
|
|
87
|
-
)
|
|
88
|
-
type_atom = Atom.from_data(data=b"account", next_hash=balance_atom.object_id())
|
|
89
|
-
|
|
90
|
-
atoms = [data_atom, counter_atom, code_atom, balance_atom, type_atom]
|
|
91
|
-
account_hash = type_atom.object_id()
|
|
92
|
-
self.hash = account_hash
|
|
93
|
-
self.body_hash = account_hash
|
|
94
|
-
self.atoms = atoms
|
|
95
|
-
return account_hash, list(atoms)
|
astreum/_consensus/accounts.py
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from typing import Any, Dict, Optional
|
|
4
|
-
|
|
5
|
-
from .._storage.patricia import PatriciaTrie
|
|
6
|
-
from .account import Account
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class Accounts:
|
|
10
|
-
def __init__(
|
|
11
|
-
self,
|
|
12
|
-
root_hash: Optional[bytes] = None,
|
|
13
|
-
) -> None:
|
|
14
|
-
self._trie = PatriciaTrie(root_hash=root_hash)
|
|
15
|
-
self._cache: Dict[bytes, Account] = {}
|
|
16
|
-
|
|
17
|
-
@property
|
|
18
|
-
def root_hash(self) -> Optional[bytes]:
|
|
19
|
-
return self._trie.root_hash
|
|
20
|
-
|
|
21
|
-
def get_account(self, address: bytes, node: Optional[Any] = None) -> Optional[Account]:
|
|
22
|
-
cached = self._cache.get(address)
|
|
23
|
-
if cached is not None:
|
|
24
|
-
return cached
|
|
25
|
-
|
|
26
|
-
if node is None:
|
|
27
|
-
raise ValueError("Accounts requires a node reference for trie access")
|
|
28
|
-
|
|
29
|
-
account_id: Optional[bytes] = self._trie.get(node, address)
|
|
30
|
-
if account_id is None:
|
|
31
|
-
return None
|
|
32
|
-
|
|
33
|
-
account = Account.from_atom(node, account_id)
|
|
34
|
-
self._cache[address] = account
|
|
35
|
-
return account
|
|
36
|
-
|
|
37
|
-
def set_account(self, address: bytes, account: Account) -> None:
|
|
38
|
-
self._cache[address] = account
|