astreum 0.2.19__py3-none-any.whl → 0.2.21__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.
Potentially problematic release.
This version of astreum might be problematic. Click here for more details.
- astreum/models/accounts.py +80 -0
- astreum/models/patricia.py +19 -3
- {astreum-0.2.19.dist-info → astreum-0.2.21.dist-info}/METADATA +1 -1
- {astreum-0.2.19.dist-info → astreum-0.2.21.dist-info}/RECORD +7 -6
- {astreum-0.2.19.dist-info → astreum-0.2.21.dist-info}/WHEEL +0 -0
- {astreum-0.2.19.dist-info → astreum-0.2.21.dist-info}/licenses/LICENSE +0 -0
- {astreum-0.2.19.dist-info → astreum-0.2.21.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""Account‐state management built on a PatriciaTrie.
|
|
2
|
+
|
|
3
|
+
Only the *initial skeleton* is provided here per current request:
|
|
4
|
+
• __init__: wrap an existing trie root (or start empty).
|
|
5
|
+
• get_account: look up an Account object, using a cache so multiple
|
|
6
|
+
balance/nonce updates in one block operate on the same instance.
|
|
7
|
+
|
|
8
|
+
Additional mutation helpers (set_account, change_balance, etc.) will be
|
|
9
|
+
added next, once confirmed.
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from typing import Dict, Optional, Callable
|
|
14
|
+
|
|
15
|
+
from .patricia import PatriciaTrie
|
|
16
|
+
from .account import Account
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Accounts:
|
|
20
|
+
"""Light wrapper around a PatriciaTrie (address → body_hash) plus
|
|
21
|
+
an in‑memory cache of Account objects currently being worked on.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
# ------------------------------------------------------------
|
|
25
|
+
# construction
|
|
26
|
+
# ------------------------------------------------------------
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
root_hash: Optional[bytes] = None,
|
|
30
|
+
*,
|
|
31
|
+
get_node_fn: Optional[Callable[[bytes], Optional[bytes]]] = None,
|
|
32
|
+
) -> None:
|
|
33
|
+
"""Wrap an existing state trie *or* start empty (root_hash=None).
|
|
34
|
+
|
|
35
|
+
`get_node_fn` (optional) is a callback that retrieves raw node bytes
|
|
36
|
+
when the underlying PatriciaTrie encounters an unknown hash – useful
|
|
37
|
+
for disk or network-backed light clients.
|
|
38
|
+
"""
|
|
39
|
+
self._remote_get = get_node_fn
|
|
40
|
+
# Instantiate the trie; we pass the external fetcher straight in.
|
|
41
|
+
# PatriciaTrie is expected to accept `node_get` and `root_hash`.
|
|
42
|
+
self._trie = PatriciaTrie(node_get=get_node_fn, root_hash=root_hash)
|
|
43
|
+
|
|
44
|
+
# Working‑set cache: address → Account instance
|
|
45
|
+
self._cache: Dict[bytes, Account] = {}
|
|
46
|
+
|
|
47
|
+
# ------------------------------------------------------------
|
|
48
|
+
# public introspection
|
|
49
|
+
# ------------------------------------------------------------
|
|
50
|
+
@property
|
|
51
|
+
def root_hash(self) -> Optional[bytes]:
|
|
52
|
+
"""Current trie root (updates automatically after puts)."""
|
|
53
|
+
return self._trie.root_hash
|
|
54
|
+
|
|
55
|
+
# ------------------------------------------------------------
|
|
56
|
+
# account access – read‑only for now
|
|
57
|
+
# ------------------------------------------------------------
|
|
58
|
+
def get_account(self, address: bytes) -> Optional[Account]:
|
|
59
|
+
"""Return an *Account* for `address`, or *None* if it doesn't exist.
|
|
60
|
+
|
|
61
|
+
• First check the in‑memory cache (so repeat reads/updates reuse the
|
|
62
|
+
same object).
|
|
63
|
+
• Otherwise look up `body_hash` in the PatriciaTrie.
|
|
64
|
+
• If found, create a lightweight `Account` wrapper, cache it, and
|
|
65
|
+
return it. The Account is initialised with the same external
|
|
66
|
+
node‑fetcher so its own Merkle lookups can go remote if needed.
|
|
67
|
+
"""
|
|
68
|
+
# cache hit → hot path
|
|
69
|
+
if address in self._cache:
|
|
70
|
+
return self._cache[address]
|
|
71
|
+
|
|
72
|
+
# trie lookup (raw body_hash)
|
|
73
|
+
body_hash: Optional[bytes] = self._trie.get(address)
|
|
74
|
+
if body_hash is None:
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
# wrap in Account and cache
|
|
78
|
+
acc = Account(body_hash, get_node_fn=self._remote_get)
|
|
79
|
+
self._cache[address] = acc
|
|
80
|
+
return acc
|
astreum/models/patricia.py
CHANGED
|
@@ -195,9 +195,12 @@ class PatriciaTrie:
|
|
|
195
195
|
|
|
196
196
|
# 4.3 – matched entire key → update value
|
|
197
197
|
if key_pos == total_bits:
|
|
198
|
-
|
|
198
|
+
old_hash = node.hash()
|
|
199
199
|
node.value = value
|
|
200
|
+
self._invalidate_hash(node)
|
|
200
201
|
new_hash = node.hash()
|
|
202
|
+
if new_hash != old_hash:
|
|
203
|
+
self.nodes.pop(old_hash, None)
|
|
201
204
|
self.nodes[new_hash] = node
|
|
202
205
|
self._bubble(stack, new_hash)
|
|
203
206
|
return
|
|
@@ -233,12 +236,12 @@ class PatriciaTrie:
|
|
|
233
236
|
value: bytes,
|
|
234
237
|
stack: List[Tuple[PatriciaNode, bytes, int]],
|
|
235
238
|
) -> None:
|
|
236
|
-
# key_pos points to routing bit; leaf stores the rest after that bit
|
|
237
239
|
tail_len = len(key) * 8 - (key_pos + 1)
|
|
238
240
|
tail_bits, tail_len = self._bit_slice(key, key_pos + 1, tail_len)
|
|
239
241
|
leaf = self._make_node(tail_bits, tail_len, value, None, None)
|
|
240
242
|
|
|
241
|
-
|
|
243
|
+
old_parent_hash = parent.hash()
|
|
244
|
+
|
|
242
245
|
if dir_bit:
|
|
243
246
|
parent.child_1 = leaf.hash()
|
|
244
247
|
else:
|
|
@@ -246,9 +249,12 @@ class PatriciaTrie:
|
|
|
246
249
|
|
|
247
250
|
self._invalidate_hash(parent)
|
|
248
251
|
new_parent_hash = parent.hash()
|
|
252
|
+
if new_parent_hash != old_parent_hash:
|
|
253
|
+
self.nodes.pop(old_parent_hash, None)
|
|
249
254
|
self.nodes[new_parent_hash] = parent
|
|
250
255
|
self._bubble(stack, new_parent_hash)
|
|
251
256
|
|
|
257
|
+
|
|
252
258
|
def _split_and_insert(
|
|
253
259
|
self,
|
|
254
260
|
node: PatriciaNode,
|
|
@@ -277,10 +283,14 @@ class PatriciaTrie:
|
|
|
277
283
|
lcp + 1, # start *after* divergence bit
|
|
278
284
|
node.key_len - lcp - 1 # may be zero
|
|
279
285
|
)
|
|
286
|
+
old_node_hash = node.hash()
|
|
287
|
+
|
|
280
288
|
node.key = old_suffix_bits
|
|
281
289
|
node.key_len = old_suffix_len
|
|
282
290
|
self._invalidate_hash(node)
|
|
283
291
|
new_node_hash = node.hash()
|
|
292
|
+
if new_node_hash != old_node_hash:
|
|
293
|
+
self.nodes.pop(old_node_hash, None)
|
|
284
294
|
self.nodes[new_node_hash] = node
|
|
285
295
|
|
|
286
296
|
# ➍—new leaf for the key being inserted (unchanged)
|
|
@@ -341,15 +351,21 @@ class PatriciaTrie:
|
|
|
341
351
|
"""
|
|
342
352
|
while stack:
|
|
343
353
|
parent, old_hash, dir_bit = stack.pop()
|
|
354
|
+
|
|
344
355
|
if dir_bit == 0:
|
|
345
356
|
parent.child_0 = new_hash
|
|
346
357
|
else:
|
|
347
358
|
parent.child_1 = new_hash
|
|
359
|
+
|
|
348
360
|
self._invalidate_hash(parent)
|
|
349
361
|
new_hash = parent.hash()
|
|
362
|
+
if new_hash != old_hash:
|
|
363
|
+
self.nodes.pop(old_hash, None)
|
|
350
364
|
self.nodes[new_hash] = parent
|
|
365
|
+
|
|
351
366
|
self.root_hash = new_hash
|
|
352
367
|
|
|
368
|
+
|
|
353
369
|
def _bit_slice(
|
|
354
370
|
self,
|
|
355
371
|
buf: bytes,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: astreum
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.21
|
|
4
4
|
Summary: Python library to interact with the Astreum blockchain and its Lispeum virtual machine.
|
|
5
5
|
Author-email: "Roy R. O. Okello" <roy@stelar.xyz>
|
|
6
6
|
Project-URL: Homepage, https://github.com/astreum/lib
|
|
@@ -11,12 +11,13 @@ astreum/lispeum/parser.py,sha256=jQRzZYvBuSg8t_bxsbt1-WcHaR_LPveHNX7Qlxhaw-M,116
|
|
|
11
11
|
astreum/lispeum/tokenizer.py,sha256=J-I7MEd0r2ZoVqxvRPlu-Afe2ZdM0tKXXhf1R4SxYTo,1429
|
|
12
12
|
astreum/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
13
|
astreum/models/account.py,sha256=sHujGSwtV13rvOGJ5LZXuMrJ4F9XUdvyuWKz-zJ9lkE,2986
|
|
14
|
+
astreum/models/accounts.py,sha256=-To0l_COBa9A4hA1zNVvtxCbnKOT4O0bcj_CUMfXQFo,3283
|
|
14
15
|
astreum/models/block.py,sha256=YIY3qLneFM7OooY29khLDEs3NIEA7y8VhlXitflkxTo,4564
|
|
15
16
|
astreum/models/merkle.py,sha256=merV3rx2iRfzvglV6gNusrJf7OMbcVV854T-DUWCC64,6733
|
|
16
|
-
astreum/models/patricia.py,sha256=
|
|
17
|
+
astreum/models/patricia.py,sha256=ohmXrcaz7Ae561tyC4u4iPOkQPkKr8N0IWJek4upFIg,13392
|
|
17
18
|
astreum/models/transaction.py,sha256=yBarvRK2ybMAHHEQPZpubGO7gms4U9k093xQGNQHQ4Q,3043
|
|
18
|
-
astreum-0.2.
|
|
19
|
-
astreum-0.2.
|
|
20
|
-
astreum-0.2.
|
|
21
|
-
astreum-0.2.
|
|
22
|
-
astreum-0.2.
|
|
19
|
+
astreum-0.2.21.dist-info/licenses/LICENSE,sha256=gYBvRDP-cPLmTyJhvZ346QkrYW_eleke4Z2Yyyu43eQ,1089
|
|
20
|
+
astreum-0.2.21.dist-info/METADATA,sha256=naZ-ipusDSvUJg4oNgtVFC2F8oMCrHu0R6ge66lU1cQ,5478
|
|
21
|
+
astreum-0.2.21.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
22
|
+
astreum-0.2.21.dist-info/top_level.txt,sha256=1EG1GmkOk3NPmUA98FZNdKouhRyget-KiFiMk0i2Uz0,8
|
|
23
|
+
astreum-0.2.21.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|