astreum 0.2.21__py3-none-any.whl → 0.2.23__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.

@@ -1,80 +1,34 @@
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
1
  from __future__ import annotations
12
-
13
2
  from typing import Dict, Optional, Callable
14
-
15
3
  from .patricia import PatriciaTrie
16
4
  from .account import Account
17
5
 
18
-
19
6
  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
7
  def __init__(
28
8
  self,
29
9
  root_hash: Optional[bytes] = None,
30
- *,
31
- get_node_fn: Optional[Callable[[bytes], Optional[bytes]]] = None,
10
+ global_get_fn: Optional[Callable[[bytes], Optional[bytes]]] = None,
32
11
  ) -> 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
12
+ self._global_get_fn = global_get_fn
13
+ self._trie = PatriciaTrie(node_get=global_get_fn, root_hash=root_hash)
45
14
  self._cache: Dict[bytes, Account] = {}
46
15
 
47
- # ------------------------------------------------------------
48
- # public introspection
49
- # ------------------------------------------------------------
50
16
  @property
51
17
  def root_hash(self) -> Optional[bytes]:
52
- """Current trie root (updates automatically after puts)."""
53
18
  return self._trie.root_hash
54
19
 
55
- # ------------------------------------------------------------
56
- # account access – read‑only for now
57
- # ------------------------------------------------------------
58
20
  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
21
  if address in self._cache:
70
22
  return self._cache[address]
71
23
 
72
- # trie lookup (raw body_hash)
73
24
  body_hash: Optional[bytes] = self._trie.get(address)
74
25
  if body_hash is None:
75
26
  return None
76
27
 
77
- # wrap in Account and cache
78
- acc = Account(body_hash, get_node_fn=self._remote_get)
28
+ acc = Account(body_hash, get_node_fn=self._global_get_fn)
79
29
  self._cache[address] = acc
80
30
  return acc
31
+
32
+ def set_account(self, address: bytes, account: Account) -> None:
33
+ self._cache[address] = account
34
+ self._trie.put(address, account.body_hash())
astreum/models/block.py CHANGED
@@ -1,6 +1,10 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from typing import List, Dict, Any, Optional, Union
4
+
5
+ from astreum.models.account import Account
6
+ from astreum.models.accounts import Accounts
7
+ from astreum.models.patricia import PatriciaTrie
4
8
  from ..crypto import ed25519
5
9
  from .merkle import MerkleTree
6
10
 
@@ -128,3 +132,39 @@ class Block:
128
132
  return True
129
133
  except Exception:
130
134
  return False
135
+
136
+ @classmethod
137
+ def genesis(cls, validator_addr: bytes) -> "Block":
138
+ # 1 . validator-stakes sub-trie
139
+ stake_trie = PatriciaTrie()
140
+ stake_trie.put(validator_addr, (1).to_bytes(32, "big"))
141
+ stake_root = stake_trie.root_hash
142
+
143
+ # 2 . build the two Account bodies
144
+ validator_acct = Account.create(balance=0, data=b"", nonce=0)
145
+ treasury_acct = Account.create(balance=1, data=stake_root, nonce=0)
146
+
147
+ # 3 . global Accounts structure
148
+ accts = Accounts()
149
+ accts.set_account(validator_addr, validator_acct)
150
+ accts.set_account(b"\x11" * 32, treasury_acct)
151
+ accounts_hash = accts.root_hash
152
+
153
+ # 4 . constant body fields for genesis
154
+ body_kwargs = dict(
155
+ number = 0,
156
+ prev_block_hash = b"\x00" * 32,
157
+ timestamp = 0,
158
+ accounts_hash = accounts_hash,
159
+ transactions_total_fees = 0,
160
+ transaction_limit = 0,
161
+ transactions_root_hash = b"\x00" * 32,
162
+ delay_difficulty = 0,
163
+ delay_output = b"",
164
+ delay_proof = b"",
165
+ validator_pk = validator_addr,
166
+ signature = b"",
167
+ )
168
+
169
+ # 5 . build and return the block
170
+ return cls.create(**body_kwargs)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: astreum
3
- Version: 0.2.21
3
+ Version: 0.2.23
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,13 +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
15
- astreum/models/block.py,sha256=YIY3qLneFM7OooY29khLDEs3NIEA7y8VhlXitflkxTo,4564
14
+ astreum/models/accounts.py,sha256=aFSEWlq6zRf65-KGAdNGqEJyNVY3fpKhx8y1vU6sgSc,1164
15
+ astreum/models/block.py,sha256=3OyAAqw853aLnnWogYdkxOg_WXNEsfn2g8jrFEip2aY,6141
16
16
  astreum/models/merkle.py,sha256=merV3rx2iRfzvglV6gNusrJf7OMbcVV854T-DUWCC64,6733
17
17
  astreum/models/patricia.py,sha256=ohmXrcaz7Ae561tyC4u4iPOkQPkKr8N0IWJek4upFIg,13392
18
18
  astreum/models/transaction.py,sha256=yBarvRK2ybMAHHEQPZpubGO7gms4U9k093xQGNQHQ4Q,3043
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,,
19
+ astreum-0.2.23.dist-info/licenses/LICENSE,sha256=gYBvRDP-cPLmTyJhvZ346QkrYW_eleke4Z2Yyyu43eQ,1089
20
+ astreum-0.2.23.dist-info/METADATA,sha256=itKxfzkJrO8JcpMVCW3nDA_8NdtdndEj9zKJaoT5GGc,5478
21
+ astreum-0.2.23.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
22
+ astreum-0.2.23.dist-info/top_level.txt,sha256=1EG1GmkOk3NPmUA98FZNdKouhRyget-KiFiMk0i2Uz0,8
23
+ astreum-0.2.23.dist-info/RECORD,,