Skip to content

Commit

Permalink
stprf and keyreg snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-p committed Sep 9, 2024
1 parent 3a6d01f commit bb11cf9
Show file tree
Hide file tree
Showing 4 changed files with 4,250 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/algokit_subscriber_py/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ def process_extra_fields(
return {
**transaction,
'arc28_events': arc28_events if len(arc28_events) > 0 else None,
'balance_changes': balance_changes if len(balance_changes) > 0 else None,
'balance_changes': balance_changes,
'inner-txns': inner_txns if len(inner_txns) > 0 else None,
'filters_matched': transaction.get('filters_matched') or None,
}
Expand Down
24 changes: 22 additions & 2 deletions src/algokit_subscriber_py/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,26 @@ def get_tx_id_from_block_transaction(
class TransactionInBlockWithChildOffset(TransactionInBlock):
get_child_offset: NotRequired[Callable[[], int]]

def convert_bytes_to_base64(obj):
"""
Recursively iterate over a nested dict and convert any bytes values to base64 strings.
Args:
obj: The object to convert (can be a dict, list, or any other type)
Returns:
The object with all bytes values converted to base64 strings
"""
if isinstance(obj, dict):
return {key: convert_bytes_to_base64(value) for key, value in obj.items()}
elif isinstance(obj, list):
return [convert_bytes_to_base64(item) for item in obj]
elif isinstance(obj, bytes):
return base64.b64encode(obj).decode('utf-8')
else:
return obj


def get_indexer_transaction_from_algod_transaction( # noqa: C901
t: TransactionInBlock | TransactionInBlockWithChildOffset, filter_name: str | None = None
) -> SubscribedTransaction:
Expand Down Expand Up @@ -551,7 +571,7 @@ def get_child_offset() -> int:
"ln-proven-weight": state_proof_message["P"],
"voters-commitment": state_proof_message["v"],
},
"state-proof-type": transaction.sprf_type,
"state-proof-type": transaction.sprf_type or 0,
}

if block_transaction.get("dt") is not None:
Expand All @@ -576,7 +596,7 @@ def get_child_offset() -> int:
for ibt in block_transaction["dt"].get("itx", []) # type: ignore[union-attr]
]

return result
return convert_bytes_to_base64(result)

except Exception as e:
logger.error(
Expand Down
180 changes: 87 additions & 93 deletions tests/test_transform_keyreg.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from algokit_utils.beta.algorand_client import AlgorandClient
from algokit_subscriber_py.types.transaction import TransactionType

from .transactions import get_subscribed_transactions_for_test
from algokit_subscriber_py.types.subscription import BalanceChangeRole
from .transactions import remove_none_values, get_subscribed_transactions_for_test

KEYREG_ROUND = 34418662
KEYREG_TXN_ID = "LSTIW7IBLO4SFPLFAI45WAV3NPXYPX6RWPTZ5KYDL3NX2LTJFXNA"
Expand Down Expand Up @@ -36,51 +36,50 @@ def test_keyreg_from_indexer(algorand_mainnet: AlgorandClient) -> None:
# https://allo.info/tx/LSTIW7IBLO4SFPLFAI45WAV3NPXYPX6RWPTZ5KYDL3NX2LTJFXNA
assert txn['id'] == KEYREG_TXN_ID

# TODO: Perform snapshot assertion
# assert txn == {
# "arc28_events": None,
# "balance_changes": [
# {
# "address": "HQQRVWPYAHABKCXNMZRG242Z5GWFTJMRO63HDCLF23ZWCT3IPQXIGQ2KGY",
# "amount": -1000,
# "asset_id": 0,
# "roles": [
# "Sender",
# ],
# },
# ],
# "close-rewards": 0,
# "closing-amount": 0,
# "confirmed-round": 34418662,
# "fee": 1000,
# "filters_matched": [
# "default",
# ],
# "first-valid": 34418595,
# "genesis-hash": "wGHE2Pwdvd7S12BL5FaOP20EGYesN73ktiC1qzkkit8=",
# "genesis-id": "mainnet-v1.0",
# "id": "LSTIW7IBLO4SFPLFAI45WAV3NPXYPX6RWPTZ5KYDL3NX2LTJFXNA",
# "inner-txns": None,
# "intra-round-offset": 54,
# "keyreg-transaction": {
# "non-participation": False,
# "selection-participation-key": "Fsp1QLE/fXpmq5fsk/bWP8P1+H8n30bMD3X7hPdk/GU=",
# "state-proof-key": "Qld9eu3U/OhHohBMF4atWbKbDQB5NGO2vPl5sZ9q9yHssmrbnQIOlhujP3vaSdFXqstnzD77Z85yrlfxJFfu+g==",
# "vote-first-valid": 34300000,
# "vote-key-dilution": 2450,
# "vote-last-valid": 40300000,
# "vote-participation-key": "yUR+nfHtSb2twOaprEXrnYjkhbFMBtmXW9D8x+/ROBg=",
# },
# "last-valid": 34419595,
# "receiver-rewards": 0,
# "round-time": 1702579204,
# "sender": "HQQRVWPYAHABKCXNMZRG242Z5GWFTJMRO63HDCLF23ZWCT3IPQXIGQ2KGY",
# "sender-rewards": 0,
# "signature": {
# "sig": "zs+8H5J4hXmmKk36uEupgupE5Filw/xMae0ox5c7yuHM4jYVPLPBYHLOdPapguScPzuz0Lney/+V9MFrKLj9Dw==",
# },
# "tx-type": "keyreg",
# }
assert txn == {
"arc28_events": None,
"balance_changes": [
{
"address": "HQQRVWPYAHABKCXNMZRG242Z5GWFTJMRO63HDCLF23ZWCT3IPQXIGQ2KGY",
"amount": -1000,
"asset_id": 0,
"roles": [
BalanceChangeRole.Sender,
],
},
],
"close-rewards": 0,
"closing-amount": 0,
"confirmed-round": 34418662,
"fee": 1000,
"filters_matched": [
"default",
],
"first-valid": 34418595,
"genesis-hash": "wGHE2Pwdvd7S12BL5FaOP20EGYesN73ktiC1qzkkit8=",
"genesis-id": "mainnet-v1.0",
"id": "LSTIW7IBLO4SFPLFAI45WAV3NPXYPX6RWPTZ5KYDL3NX2LTJFXNA",
"inner-txns": None,
"intra-round-offset": 54,
"keyreg-transaction": {
"non-participation": False,
"selection-participation-key": "Fsp1QLE/fXpmq5fsk/bWP8P1+H8n30bMD3X7hPdk/GU=",
"state-proof-key": "Qld9eu3U/OhHohBMF4atWbKbDQB5NGO2vPl5sZ9q9yHssmrbnQIOlhujP3vaSdFXqstnzD77Z85yrlfxJFfu+g==",
"vote-first-valid": 34300000,
"vote-key-dilution": 2450,
"vote-last-valid": 40300000,
"vote-participation-key": "yUR+nfHtSb2twOaprEXrnYjkhbFMBtmXW9D8x+/ROBg=",
},
"last-valid": 34419595,
"receiver-rewards": 0,
"round-time": 1702579204,
"sender": "HQQRVWPYAHABKCXNMZRG242Z5GWFTJMRO63HDCLF23ZWCT3IPQXIGQ2KGY",
"sender-rewards": 0,
"signature": {
"sig": "zs+8H5J4hXmmKk36uEupgupE5Filw/xMae0ox5c7yuHM4jYVPLPBYHLOdPapguScPzuz0Lney/+V9MFrKLj9Dw==",
},
"tx-type": "keyreg",
}


def test_keyreg_from_algod(algorand_mainnet: AlgorandClient) -> None:
Expand All @@ -105,49 +104,44 @@ def test_keyreg_from_algod(algorand_mainnet: AlgorandClient) -> None:
txn = txns["subscribed_transactions"][0]
# https://allo.info/tx/LSTIW7IBLO4SFPLFAI45WAV3NPXYPX6RWPTZ5KYDL3NX2LTJFXNA
assert txn['id'] == KEYREG_TXN_ID

# TODO: Perform snapshot assertion
# assert txn == {
# "arc28_events": None,
# "balance_changes": [
# {
# "address": "HQQRVWPYAHABKCXNMZRG242Z5GWFTJMRO63HDCLF23ZWCT3IPQXIGQ2KGY",
# "amount": -1000,
# "asset_id": 0,
# "roles": [
# "Sender",
# ],
# },
# ],
# "close-rewards": 0,
# "closing-amount": 0,
# "confirmed-round": 34418662,
# "fee": 1000,
# "filters_matched": [
# "default",
# ],
# "first-valid": 34418595,
# "genesis-hash": "wGHE2Pwdvd7S12BL5FaOP20EGYesN73ktiC1qzkkit8=",
# "genesis-id": "mainnet-v1.0",
# "id": "LSTIW7IBLO4SFPLFAI45WAV3NPXYPX6RWPTZ5KYDL3NX2LTJFXNA",
# "inner-txns": None,
# "intra-round-offset": 54,
# "keyreg-transaction": {
# "non-participation": False,
# "selection-participation-key": "Fsp1QLE/fXpmq5fsk/bWP8P1+H8n30bMD3X7hPdk/GU=",
# "state-proof-key": "Qld9eu3U/OhHohBMF4atWbKbDQB5NGO2vPl5sZ9q9yHssmrbnQIOlhujP3vaSdFXqstnzD77Z85yrlfxJFfu+g==",
# "vote-first-valid": 34300000,
# "vote-key-dilution": 2450,
# "vote-last-valid": 40300000,
# "vote-participation-key": "yUR+nfHtSb2twOaprEXrnYjkhbFMBtmXW9D8x+/ROBg=",
# },
# "last-valid": 34419595,
# "receiver-rewards": 0,
# "round-time": 1702579204,
# "sender": "HQQRVWPYAHABKCXNMZRG242Z5GWFTJMRO63HDCLF23ZWCT3IPQXIGQ2KGY",
# "sender-rewards": 0,
# "signature": {
# "sig": "zs+8H5J4hXmmKk36uEupgupE5Filw/xMae0ox5c7yuHM4jYVPLPBYHLOdPapguScPzuz0Lney/+V9MFrKLj9Dw==",
# },
# "tx-type": "keyreg",
# }
assert remove_none_values(txn) == {
"balance_changes": [
{
"address": "HQQRVWPYAHABKCXNMZRG242Z5GWFTJMRO63HDCLF23ZWCT3IPQXIGQ2KGY",
"amount": -1000,
"asset_id": 0,
"roles": [
BalanceChangeRole.Sender,
],
},
],
"confirmed-round": 34418662,
"fee": 1000,
"filters_matched": [
"default",
],
"first-valid": 34418595,
"genesis-hash": "wGHE2Pwdvd7S12BL5FaOP20EGYesN73ktiC1qzkkit8=",
"genesis-id": "mainnet-v1.0",
"id": "LSTIW7IBLO4SFPLFAI45WAV3NPXYPX6RWPTZ5KYDL3NX2LTJFXNA",
"intra-round-offset": 54,
"keyreg-transaction": {
"non-participation": False,
"selection-participation-key": "Fsp1QLE/fXpmq5fsk/bWP8P1+H8n30bMD3X7hPdk/GU=",
"state-proof-key": "Qld9eu3U/OhHohBMF4atWbKbDQB5NGO2vPl5sZ9q9yHssmrbnQIOlhujP3vaSdFXqstnzD77Z85yrlfxJFfu+g==",
"vote-first-valid": 34300000,
"vote-key-dilution": 2450,
"vote-last-valid": 40300000,
"vote-participation-key": "yUR+nfHtSb2twOaprEXrnYjkhbFMBtmXW9D8x+/ROBg=",
},
"last-valid": 34419595,
"lease": "",
"note": "",
"round-time": 1702579204,
"sender": "HQQRVWPYAHABKCXNMZRG242Z5GWFTJMRO63HDCLF23ZWCT3IPQXIGQ2KGY",
# TODO: Investigate why signature is in the snapshot
# "signature": {
# "sig": "zs+8H5J4hXmmKk36uEupgupE5Filw/xMae0ox5c7yuHM4jYVPLPBYHLOdPapguScPzuz0Lney/+V9MFrKLj9Dw==",
# },
"tx-type": "keyreg",
}
Loading

0 comments on commit bb11cf9

Please sign in to comment.