From e0842ff596bf643f1f53595adf3739ea949be9b1 Mon Sep 17 00:00:00 2001 From: Jan Segre Date: Tue, 19 Aug 2025 19:49:04 +0200 Subject: [PATCH] fix(nano): state API regression after ctx.address refactor --- hathor/nanocontracts/resources/state.py | 5 ++-- tests/resources/nanocontracts/test_state.py | 30 ++++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/hathor/nanocontracts/resources/state.py b/hathor/nanocontracts/resources/state.py index d44362368..d782c62f7 100644 --- a/hathor/nanocontracts/resources/state.py +++ b/hathor/nanocontracts/resources/state.py @@ -204,9 +204,8 @@ def render_GET(self, request: 'Request') -> bytes: fields[field] = NCValueErrorResponse(errmsg='field not found') continue - if type(value) is bytes: - value = value.hex() - fields[field] = NCValueSuccessResponse(value=value) + json_value = field_nc_type.value_to_json(value) + fields[field] = NCValueSuccessResponse(value=json_value) # Call view methods. runner.disable_call_trace() # call trace is not required for calling view methods. diff --git a/tests/resources/nanocontracts/test_state.py b/tests/resources/nanocontracts/test_state.py index de5f86312..f7fabee38 100644 --- a/tests/resources/nanocontracts/test_state.py +++ b/tests/resources/nanocontracts/test_state.py @@ -12,7 +12,16 @@ from hathor.nanocontracts.catalog import NCBlueprintCatalog from hathor.nanocontracts.method import Method from hathor.nanocontracts.resources import NanoContractStateResource -from hathor.nanocontracts.types import Address, NCActionType, NCDepositAction, Timestamp, TokenUid +from hathor.nanocontracts.types import ( + Address, + CallerId, + ContractId, + NCActionType, + NCDepositAction, + Timestamp, + TokenUid, + VertexId, +) from hathor.nanocontracts.utils import sign_openssl from hathor.simulator.utils import add_new_block from hathor.transaction import Transaction, TxInput @@ -40,12 +49,19 @@ class MyBlueprint(Blueprint): address_details: dict[Address, dict[str, Amount]] bytes_field: bytes dict_with_bytes: dict[bytes, str] + last_caller_id: CallerId + last_bet_address: Address + last_vertex_id: VertexId + self_contract_id: ContractId @public def initialize(self, ctx: Context, token_uid: TokenUid, date_last_bet: Timestamp) -> None: self.token_uid = token_uid self.date_last_bet = date_last_bet self.total = 0 + self.last_caller_id = ctx.caller_id + self.self_contract_id = ContractId(ctx.vertex.hash) + self.last_vertex_id = VertexId(ctx.vertex.hash) @public(allow_deposit=True) def bet(self, ctx: Context, address: Address, score: str) -> None: @@ -58,6 +74,9 @@ def bet(self, ctx: Context, address: Address, score: str) -> None: else: partial[score] += action.amount self.address_details[address] = partial + self.last_bet_address = address + self.last_caller_id = ctx.caller_id + self.last_vertex_id = VertexId(ctx.vertex.hash) encoded_score = score.encode() self.bytes_field = encoded_score @@ -302,6 +321,10 @@ def test_success(self): (b'fields[]', b'token_uid'), (b'fields[]', b'total'), (b'fields[]', b'date_last_bet'), + (b'fields[]', b'last_caller_id'), + (b'fields[]', b'last_bet_address'), + (b'fields[]', b'last_vertex_id'), + (b'fields[]', b'self_contract_id'), (b'fields[]', address_param.encode()), (b'fields[]', b'bytes_field'), (b'fields[]', dict_with_bytes_param.encode()), @@ -312,9 +335,14 @@ def test_success(self): fields2 = data2['fields'] self.assertEqual(data2['blueprint_id'], self.bet_id.hex()) self.assertEqual(data2['blueprint_name'], 'MyBlueprint') + self.assertEqual(len(data2['fields']), 10) self.assertEqual(fields2['token_uid'], {'value': settings.HATHOR_TOKEN_UID.hex()}) self.assertEqual(fields2['total'], {'value': 10**11}) self.assertEqual(fields2['date_last_bet'], {'value': date_last_bet}) + self.assertEqual(fields2['last_caller_id'], {'value': address_b58}) + self.assertEqual(fields2['last_bet_address'], {'value': address_b58}) + self.assertEqual(fields2['last_vertex_id'], {'value': nc_bet.hash.hex()}) + self.assertEqual(fields2['self_contract_id'], {'value': nc.hash.hex()}) self.assertEqual(len(fields2[address_param]), 1) # TODO: RE-IMPLEMENT SUPPORT FOR THIS # FIXME