Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,23 @@ class Block:
decode=lambda raw: decode_model_sequence(lambda: SignedTxnInBlock, raw),
),
)


def __post_init__(self) -> None:
# populates genesis id and hash on transactions if required to ensure
# tx id's are correct
genesis_id = self.header.genesis_id
genesis_hash = self.header.genesis_hash
set_frozen_field = object.__setattr__
for txn_in_block in self.payset or []:
txn = txn_in_block.signed_transaction.signed_transaction.transaction

if txn_in_block.has_genesis_id and txn.genesis_id is None:
set_frozen_field(txn, "genesis_id", genesis_id)

# the following assumes that Consensus.RequireGenesisHash is true
# so assigns genesis hash unless explicitly set to False
if txn_in_block.has_genesis_hash is not False and txn.genesis_hash is None:
set_frozen_field(txn, "genesis_hash", genesis_hash)

@dataclass(slots=True)
class BlockResponse:
Expand Down
17 changes: 17 additions & 0 deletions src/algokit_algod_client/models/_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,23 @@ class Block:
),
)

def __post_init__(self) -> None:
# populates genesis id and hash on transactions if required to ensure
# tx id's are correct
genesis_id = self.header.genesis_id
genesis_hash = self.header.genesis_hash
set_frozen_field = object.__setattr__
for txn_in_block in self.payset or []:
txn = txn_in_block.signed_transaction.signed_transaction.txn

if txn_in_block.has_genesis_id and txn.genesis_id is None:
set_frozen_field(txn, "genesis_id", genesis_id)

# the following assumes that Consensus.RequireGenesisHash is true
# so assigns genesis hash unless explicitly set to False
if txn_in_block.has_genesis_hash is not False and txn.genesis_hash is None:
set_frozen_field(txn, "genesis_hash", genesis_hash)


@dataclass(slots=True)
class BlockResponse:
Expand Down
42 changes: 42 additions & 0 deletions tests/modules/algod_client/manual/test_tx_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import base64
import dataclasses

from algokit_transact import Transaction
from algokit_utils import AlgorandClient, AlgoAmount, AssetCreateParams


def test_block_tx_id_matches_submitted_id() -> None:
localnet = AlgorandClient.default_localnet()
algod = localnet.client.algod

sender = localnet.account.random()
localnet.account.ensure_funded(sender, localnet.account.localnet_dispenser(), AlgoAmount(micro_algo=1_000_000))

txns = (
localnet.new_group()
.add_asset_create(
AssetCreateParams(sender=sender.addr, static_fee=AlgoAmount(micro_algo=2000), total=100_000_000)
)
.send()
)
txn = txns.transactions[0]
group_id = base64.b64decode(txns.group_id)
tx_id = txns.tx_ids[0]
confirmation = algod.pending_transaction_information(tx_id)
confirmed_round = confirmation.confirmed_round
assert confirmed_round is not None, "expected a confirmed round"
assert confirmation.txn.txn.tx_id() == tx_id, "expected confirmation tx_id to match submitted tx_id"
assert confirmation.txn.txn.group == group_id, "expected group_id to match"

block = algod.get_block(confirmed_round)
assert block.block.payset is not None, "expected non null payset"
(block_txn,) = block.block.payset
txn_fields = {f.name: getattr(txn, f.name) for f in dataclasses.fields(Transaction)}
block_txn_fields = {
f.name: getattr(block_txn.signed_transaction.signed_transaction.txn, f.name)
for f in dataclasses.fields(Transaction)
}
assert txn_fields == block_txn_fields

assert block_txn.signed_transaction.signed_transaction.txn.group == group_id, "expected group_id to match"
assert block_txn.signed_transaction.signed_transaction.txn.tx_id() == tx_id, "expected block tx_id to match"
Loading