Skip to content
Merged
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
2 changes: 1 addition & 1 deletion hathor/conf/localnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ REWARD_SPEND_MIN_BLOCKS: 1
CHECKPOINTS: []

ENABLE_NANO_CONTRACTS: 'enabled'
NC_ON_CHAIN_BLUEPRINT_RESTRICTED: False
NC_ON_CHAIN_BLUEPRINT_RESTRICTED: false

extends: testnet.yml
19 changes: 8 additions & 11 deletions hathor/nanocontracts/resources/nc_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from hathor.nanocontracts.types import BlueprintId, VertexId
from hathor.transaction import Transaction
from hathor.transaction.storage.exceptions import TransactionDoesNotExist
from hathor.util import bytes_from_hex, collect_n, not_none
from hathor.util import bytes_from_hex, not_none
from hathor.utils.api import ErrorResponse, QueryParams, Response


Expand Down Expand Up @@ -138,11 +138,13 @@ def render_GET(self, request: Request) -> bytes:
)
iter_nc_ids = iter_getter2(tx_start=ref_tx)

nc_id_txs, has_more = collect_n(iter_nc_ids, params.count)
nc_txs = []
for nc_id in nc_id_txs:
item = self._get_nc_creation_item(nc_id)
if item is not None:
nc_txs: list[NCCreationItem] = []
has_more = False
for nc_id in iter_nc_ids:
if len(nc_txs) >= params.count:
has_more = True
break
if item := self._get_nc_creation_item(nc_id):
nc_txs.append(item)

response = NCCreationResponse(
Expand Down Expand Up @@ -202,11 +204,6 @@ def _get_nc_creation_item(self, nc_id: bytes) -> NCCreationItem | None:
created_at=created_at,
)

def _get_nc_creation_item_strict(self, nc_id: bytes) -> NCCreationItem:
tx = self._get_nc_creation_item(nc_id)
assert tx is not None
return tx


class NCCreationParams(QueryParams):
before: str | None
Expand Down
63 changes: 62 additions & 1 deletion hathor_tests/resources/nanocontracts/test_nc_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

from typing import Any

from hathor import Blueprint, Context, public
from hathor.nanocontracts.resources.nc_creation import NCCreationResource
from hathor.nanocontracts.types import BlueprintId, VertexId
from hathor.nanocontracts.utils import load_builtin_blueprint_for_ocb
from hathor.transaction import Transaction
from hathor.transaction import Block, Transaction
from hathor.transaction.nc_execution_state import NCExecutionState
from hathor_tests import unittest
from hathor_tests.dag_builder.builder import TestDAGBuilder
from hathor_tests.nanocontracts import test_blueprints
Expand All @@ -26,6 +28,16 @@
from hathor_tests.utils import get_genesis_key


class MyBlueprint(Blueprint):
@public
def initialize(self, ctx: Context) -> None:
pass

@public
def create_child(self, ctx: Context) -> None:
self.syscall.setup_new_contract(self.syscall.get_blueprint_id(), salt=b'1').initialize()


class NCCreationResourceTest(_BaseResourceTest._ResourceTest):

def setUp(self):
Expand Down Expand Up @@ -568,3 +580,52 @@ async def test_non_hex_pagination(self) -> None:
success=False,
error='Invalid "before" or "after": abc'
)

async def test_contract_create_contract(self) -> None:
blueprint_id = BlueprintId(self.rng.randbytes(32))
self.manager.tx_storage.nc_catalog.blueprints[blueprint_id] = MyBlueprint

dag_builder = TestDAGBuilder.from_manager(self.manager)
artifacts = dag_builder.build_from_str(f'''
blockchain genesis b[1..12]
b10 < dummy

nc1.nc_id = "{blueprint_id.hex()}"
nc1.nc_method = initialize()
nc1 <-- b11

nc2.nc_id = nc1
nc2.nc_method = create_child()
nc2 <-- b12
''')

artifacts.propagate_with(self.manager)
b11, b12 = artifacts.get_typed_vertices(('b11', 'b12'), Block)
nc1, nc2, = artifacts.get_typed_vertices(('nc1', 'nc2'), Transaction)

assert nc1.get_metadata().first_block == b11.hash
assert nc1.get_metadata().nc_execution == NCExecutionState.SUCCESS
assert nc2.get_metadata().first_block == b12.hash
assert nc2.get_metadata().nc_execution == NCExecutionState.SUCCESS

response = await self.web.get('creation')
data = response.json_value()

# Contracts created by contracts are currently not supported by the API and are simply omitted.
assert data == dict(
success=True,
before=None,
after=None,
count=10,
has_more=False,
nc_creation_txs=[
dict(
nano_contract_id=nc1.hash_hex,
blueprint_id=blueprint_id.hex(),
blueprint_name='MyBlueprint',
last_tx_timestamp=nc2.timestamp,
total_txs=2,
created_at=nc1.timestamp,
),
],
)
Loading