-
Notifications
You must be signed in to change notification settings - Fork 45
tests(nano): add basic structures and resources tests [part 14] #1291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| from hathor.conf import HathorSettings | ||
| from hathor.crypto.util import decode_address | ||
| from hathor.manager import HathorManager | ||
| from hathor.nanocontracts import Context | ||
| from hathor.nanocontracts.blueprint import Blueprint | ||
| from hathor.nanocontracts.nc_exec_logs import NCLogConfig | ||
| from hathor.nanocontracts.storage import NCBlockStorage, NCMemoryStorageFactory | ||
| from hathor.nanocontracts.storage.backends import MemoryNodeTrieStore | ||
| from hathor.nanocontracts.storage.patricia_trie import PatriciaTrie | ||
| from hathor.nanocontracts.types import Address, BlueprintId, ContractId, NCAction, TokenUid, VertexId | ||
| from hathor.nanocontracts.vertex_data import VertexData | ||
| from hathor.transaction import BaseTransaction, Transaction | ||
| from hathor.util import not_none | ||
| from hathor.wallet import KeyPair | ||
| from tests import unittest | ||
| from tests.nanocontracts.utils import TestRunner | ||
|
|
||
| settings = HathorSettings() | ||
|
|
||
|
|
||
| class BlueprintTestCase(unittest.TestCase): | ||
| use_memory_storage = True | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| self.manager = self.build_manager() | ||
| self.rng = self.manager.rng | ||
| self.wallet = self.manager.wallet | ||
| self.reactor = self.manager.reactor | ||
| self.nc_catalog = self.manager.tx_storage.nc_catalog | ||
|
|
||
| self.htr_token_uid = settings.HATHOR_TOKEN_UID | ||
| self.runner = self.build_runner() | ||
| self.now = int(self.reactor.seconds()) | ||
|
|
||
| self._token_index = 1 | ||
|
|
||
| def build_manager(self) -> HathorManager: | ||
| """Create a HathorManager instance.""" | ||
| return self.create_peer('testnet', nc_indices=True, nc_log_config=NCLogConfig.FAILED, wallet_index=True) | ||
|
|
||
| def register_blueprint_class(self, blueprint_id: BlueprintId, blueprint_class: type[Blueprint]) -> None: | ||
| """Register a blueprint class with a given id, allowing contracts to be created from it.""" | ||
| assert blueprint_id not in self.nc_catalog.blueprints | ||
| self.nc_catalog.blueprints[blueprint_id] = blueprint_class | ||
|
|
||
| def build_runner(self) -> TestRunner: | ||
| """Create a Runner instance.""" | ||
| nc_storage_factory = NCMemoryStorageFactory() | ||
| store = MemoryNodeTrieStore() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use RocksDB? |
||
| block_trie = PatriciaTrie(store) | ||
| block_storage = NCBlockStorage(block_trie) | ||
| return TestRunner( | ||
| self.manager.tx_storage, nc_storage_factory, block_storage, settings=self._settings, reactor=self.reactor | ||
| ) | ||
|
|
||
| def gen_random_token_uid(self) -> TokenUid: | ||
| """Generate a random token UID (32 bytes).""" | ||
| token = self._token_index.to_bytes(32, byteorder='big', signed=False) | ||
| self._token_index += 1 | ||
| return TokenUid(token) | ||
|
|
||
| def gen_random_address(self) -> Address: | ||
| """Generate a random wallet address.""" | ||
| address, _ = self.gen_random_address_with_key() | ||
| return address | ||
|
|
||
| def gen_random_address_with_key(self) -> tuple[Address, KeyPair]: | ||
| """Generate a random wallet address with its key.""" | ||
| password = self.rng.randbytes(12) | ||
| key = KeyPair.create(password) | ||
| address_b58 = key.address | ||
| address_bytes = decode_address(not_none(address_b58)) | ||
| return Address(address_bytes), key | ||
|
|
||
| def gen_random_contract_id(self) -> ContractId: | ||
| """Generate a random contract id.""" | ||
| return ContractId(VertexId(self.rng.randbytes(32))) | ||
|
|
||
| def gen_random_blueprint_id(self) -> BlueprintId: | ||
| """Generate a random contract id.""" | ||
| return BlueprintId(self.rng.randbytes(32)) | ||
|
|
||
| def get_genesis_tx(self) -> Transaction: | ||
| """Return a genesis transaction.""" | ||
| genesis = self.manager.tx_storage.get_all_genesis() | ||
| tx = list(tx for tx in genesis if isinstance(tx, Transaction))[0] | ||
| return tx | ||
|
|
||
| def create_context( | ||
| self, | ||
| actions: list[NCAction] | None = None, | ||
| vertex: BaseTransaction | VertexData | None = None, | ||
| address: Address | None = None, | ||
| timestamp: int | None = None, | ||
| ) -> Context: | ||
| """Create a Context instance with optional values or defaults.""" | ||
| return Context( | ||
| actions=actions if actions is not None else [], | ||
| vertex=vertex or self.get_genesis_tx(), | ||
| address=address or self.gen_random_address(), | ||
| timestamp=timestamp or self.now, | ||
| ) | ||
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of receiving a mandatory
blueprint_id, we could generate one, use it, and return it. Most tests won't need to pass this argument.