Skip to content

Commit 90d857d

Browse files
committed
feat: update algopy.op.Block with fields added in AVM 11
1 parent d7ad774 commit 90d857d

File tree

3 files changed

+103
-19
lines changed

3 files changed

+103
-19
lines changed

src/_algopy_testing/context_helpers/ledger_context.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import typing
44
from collections import defaultdict
55

6+
import algosdk.constants
7+
68
from _algopy_testing.constants import MAX_BOX_SIZE
79
from _algopy_testing.models.account import Account
810
from _algopy_testing.primitives.uint64 import UInt64
@@ -31,7 +33,7 @@ def __init__(self) -> None:
3133
self._account_data = defaultdict[str, AccountContextData](get_empty_account)
3234
self._app_data: dict[int, ApplicationContextData] = {}
3335
self._asset_data: dict[int, AssetFields] = {}
34-
self._blocks: dict[int, dict[str, int]] = {}
36+
self._blocks: dict[int, dict[str, int | bytes | str]] = {}
3537
self._global_fields: GlobalFields = get_default_global_fields()
3638

3739
self._asset_id = iter(range(1001, 2**64))
@@ -365,19 +367,49 @@ def box_exists(
365367
boxes = self._get_app_data(app).boxes
366368
return _as_box_key(key) in boxes
367369

368-
def set_block(
369-
self, index: int, seed: algopy.UInt64 | int, timestamp: algopy.UInt64 | int
370+
def set_block( # noqa: PLR0913
371+
self,
372+
index: int,
373+
seed: algopy.UInt64 | int,
374+
timestamp: algopy.UInt64 | int,
375+
bonus: algopy.UInt64 | int = 0,
376+
branch: algopy.Bytes | bytes = b"",
377+
fee_sink: algopy.Account | str = algosdk.constants.ZERO_ADDRESS,
378+
fees_collected: algopy.UInt64 | int = 0,
379+
proposer: algopy.Account | str = algosdk.constants.ZERO_ADDRESS,
380+
proposer_payout: algopy.UInt64 | int = 0,
381+
protocol: algopy.Bytes | bytes = b"",
382+
txn_counter: algopy.UInt64 | int = 0,
370383
) -> None:
371384
"""Set block content.
372385
373386
Args:
374387
index (int): The block index.
375388
seed (algopy.UInt64 | int): The block seed.
376389
timestamp (algopy.UInt64 | int): The block timestamp.
390+
bonus (algopy.UInt64 | int): The block bonus.
391+
branch (algopy.Bytes | bytes): The block branch.
392+
fee_sink (algopy.Account | str): The block fee sink.
393+
fees_collected (algopy.UInt64 | int): The fess collected.
394+
proposer (algopy.Account | str): The block proposer.
395+
proposer_payout (algopy.UInt64 | int): The block proposer payout.
396+
protocol (algopy.Bytes | bytes): The block protocol.
397+
txn_counter (algopy.UInt64 | int): The block transaction counter.
377398
"""
378-
self._blocks[index] = {"seed": int(seed), "timestamp": int(timestamp)}
379-
380-
def get_block_content(self, index: int, key: str) -> int:
399+
self._blocks[index] = {
400+
"seed": int(seed),
401+
"timestamp": int(timestamp),
402+
"bonus": int(bonus),
403+
"branch": as_bytes(branch),
404+
"fee_sink": str(fee_sink),
405+
"fees_collected": int(fees_collected),
406+
"proposer": str(proposer),
407+
"proposer_payout": int(proposer_payout),
408+
"protocol": as_bytes(protocol),
409+
"txn_counter": int(txn_counter),
410+
}
411+
412+
def get_block_content(self, index: int, key: str) -> int | bytes | str:
381413
"""Get block content.
382414
383415
Args:

src/_algopy_testing/op/block.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
from __future__ import annotations
22

3+
import typing
34
from typing import TYPE_CHECKING
45

56
from _algopy_testing import op
67
from _algopy_testing.context_helpers import lazy_context
7-
from _algopy_testing.primitives import UInt64
8+
from _algopy_testing.models.account import Account
9+
from _algopy_testing.primitives import Bytes, UInt64
810

911
if TYPE_CHECKING:
12+
from collections.abc import Callable
13+
1014
import algopy
1115

1216

13-
class Block:
14-
@staticmethod
15-
def blk_seed(a: algopy.UInt64 | int, /) -> algopy.Bytes:
17+
_T = typing.TypeVar("_T")
18+
19+
20+
def _make_block_method(
21+
field: str, conv: Callable[[typing.Any], _T]
22+
) -> Callable[[algopy.UInt64 | int], _T]:
23+
def _read_block(index: algopy.UInt64 | int) -> _T:
1624
try:
17-
index = int(a)
18-
return op.itob(lazy_context.ledger.get_block_content(index, "seed"))
25+
return conv(lazy_context.ledger.get_block_content(int(index), field))
1926
except KeyError as e:
20-
raise KeyError(f"Block {a} not set") from e
27+
raise KeyError(f"Block {index} not set") from e
2128

29+
return _read_block
30+
31+
32+
class Block:
2233
@staticmethod
23-
def blk_timestamp(a: algopy.UInt64 | int, /) -> algopy.UInt64:
34+
def blk_seed(index: algopy.UInt64 | int) -> Bytes:
2435
try:
25-
index = int(a)
26-
return UInt64(lazy_context.ledger.get_block_content(index, "timestamp"))
36+
value = lazy_context.ledger.get_block_content(int(index), "seed")
2737
except KeyError as e:
28-
raise KeyError(f"Block {a} not set") from e
38+
raise KeyError(f"Block {index} not set") from e
39+
else:
40+
assert isinstance(value, int), "expected int for blk_seed"
41+
return op.itob(value)
42+
43+
blk_timestamp = _make_block_method("timestamp", UInt64)
44+
blk_bonus = _make_block_method("bonus", UInt64)
45+
blk_proposer = _make_block_method("proposer", Account)
46+
blk_fees_collected = _make_block_method("fees_collected", UInt64)
47+
blk_txn_counter = _make_block_method("txn_counter", UInt64)
48+
blk_proposer_payout = _make_block_method("proposer_payout", UInt64)
49+
blk_branch = _make_block_method("branch", Bytes)
50+
blk_protocol = _make_block_method("protocol", Bytes)
51+
blk_fee_sink = _make_block_method("fee_sink", Account)

tests/test_op.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,13 +938,42 @@ def test_itxn_ops(context: AlgopyTestContext) -> None:
938938
assert hasattr(appl_itxn, "created_app")
939939

940940

941-
def test_blk_seed_existing_block(context: AlgopyTestContext) -> None:
941+
def test_blk_existing_block(context: AlgopyTestContext) -> None:
942942
block_index = 42
943943
block_seed = 123
944-
context.ledger.set_block(block_index, block_seed, 1234567890)
944+
sink = context.any.account()
945+
fees = context.any.uint64()
946+
bonus = context.any.uint64()
947+
proposer = context.any.account()
948+
payout = context.any.uint64()
949+
protocol = context.any.bytes()
950+
branch = context.any.bytes()
951+
counter = context.any.uint64()
952+
context.ledger.set_block(
953+
block_index,
954+
seed=block_seed,
955+
timestamp=1234567890,
956+
fee_sink=sink,
957+
fees_collected=fees,
958+
proposer=proposer,
959+
proposer_payout=payout,
960+
protocol=protocol,
961+
txn_counter=counter,
962+
bonus=bonus,
963+
branch=branch,
964+
)
945965
result = op.Block.blk_seed(algopy.UInt64(block_index))
946966
assert op.btoi(result) == block_seed
947967

968+
assert op.Block.blk_fee_sink(block_index) == sink
969+
assert op.Block.blk_fees_collected(block_index) == fees
970+
assert op.Block.blk_proposer(block_index) == proposer
971+
assert op.Block.blk_proposer_payout(block_index) == payout
972+
assert op.Block.blk_protocol(block_index) == protocol
973+
assert op.Block.blk_txn_counter(block_index) == counter
974+
assert op.Block.blk_bonus(block_index) == bonus
975+
assert op.Block.blk_branch(block_index) == branch
976+
948977

949978
@pytest.mark.usefixtures("context")
950979
def test_blk_seed_missing_block() -> None:

0 commit comments

Comments
 (0)