-
Notifications
You must be signed in to change notification settings - Fork 1
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
Abi / codecs (integration sketch) #29
Draft
andreibancioiu
wants to merge
5
commits into
integrate-new-sdk-py
Choose a base branch
from
abi-10
base: integrate-new-sdk-py
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af36cae
Add some test data, some workspace config.
andreibancioiu dc8a80f
Preparatory refactoring.
andreibancioiu eba7adc
Add unit test for SimpleLockEnergyContract.contract_deploy.
andreibancioiu 2ace8c8
GH workflow to run unit tests.
andreibancioiu ab19ba9
Adjust tests.
andreibancioiu 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 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,32 @@ | ||
name: Run tests | ||
|
||
on: | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
run-tests: | ||
name: Run tests on ${{ matrix.os }}, python ${{ matrix.python-version }} | ||
|
||
runs-on: ${{ matrix.os }} | ||
|
||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
python-version: [3.8] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.x' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
pip install -r requirements-dev.txt | ||
- name: Test with pytest | ||
run: | | ||
export PYTHONPATH=. | ||
pytest . |
This file contains 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 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 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 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 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 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,37 @@ | ||
from pathlib import Path | ||
|
||
from contracts.simple_lock_energy_contract import SimpleLockEnergyContract | ||
from testutils.mock_network_provider import MockNetworkProvider | ||
from utils.utils_chain import Account | ||
|
||
testdata_folder = Path(__file__).parent.parent / "testdata" | ||
|
||
|
||
def test_deploy_contract(): | ||
account = Account(pem_file=testdata_folder / "alice.pem") | ||
bytecode_path = testdata_folder / "dummy.wasm" | ||
bytecode = bytecode_path.read_bytes() | ||
network_provider = MockNetworkProvider() | ||
|
||
contract = SimpleLockEnergyContract( | ||
base_token="TEST-987654" | ||
) | ||
|
||
tx_hash, contract_address = contract.contract_deploy( | ||
proxy=network_provider, | ||
deployer=account, | ||
bytecode_path=bytecode_path, | ||
args=[ | ||
"TEST-123456", | ||
"erd1qqqqqqqqqqqqqpgqaxa53w6uk43n6dhyt2la6cd5lyv32qn4396qfsqlnk", | ||
42, | ||
[360, 720, 1440], | ||
[5000, 7000, 8000] | ||
] | ||
) | ||
|
||
assert tx_hash == "cbde33c54afde0a215961568755167c60255a95c70f1a8d91f0b29dc0baa37c2" | ||
assert contract_address == "erd1qqqqqqqqqqqqqpgqak8zt22wl2ph4tswtyc39namqx6ysa2sd8ss4xmlj3" | ||
|
||
tx_on_network = network_provider.get_transaction(tx_hash) | ||
assert tx_on_network.data == f"{bytecode.hex()}@0500@0504@544553542d393837363534@544553542d313233343536@00000000000000000500e9bb48bb5cb5633d36e45abfdd61b4f9191502758974@2a@0168@1388@02d0@1b58@05a0@1f40" |
This file contains 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,14 @@ | ||
{ | ||
"include": ["contracts", "deploy", "events", "utils", "testutils"], | ||
"exclude": ["**/__pycache__"], | ||
"ignore": [], | ||
"defineConstant": { | ||
"DEBUG": true | ||
}, | ||
"venvPath": ".", | ||
"venv": ".venv", | ||
"stubPath": "", | ||
"reportMissingImports": true, | ||
"reportMissingTypeStubs": false, | ||
"reportUnknownParameterType": true | ||
} |
This file contains 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 @@ | ||
pytest |
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
ipykernel | ||
toml | ||
debugpy | ||
multiversx-sdk @ git+https://github.com/multiversx/mx-sdk-py-incubator@main | ||
multiversx-sdk @ git+https://github.com/multiversx/mx-sdk-py-incubator@codecs-init |
This file contains 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,5 @@ | ||
-----BEGIN PRIVATE KEY for erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th----- | ||
NDEzZjQyNTc1ZjdmMjZmYWQzMzE3YTc3ODc3MTIxMmZkYjgwMjQ1ODUwOTgxZTQ4 | ||
YjU4YTRmMjVlMzQ0ZThmOTAxMzk0NzJlZmY2ODg2NzcxYTk4MmYzMDgzZGE1ZDQy | ||
MWYyNGMyOTE4MWU2Mzg4ODIyOGRjODFjYTYwZDY5ZTE= | ||
-----END PRIVATE KEY for erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th----- |
Binary file not shown.
This file contains 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,62 @@ | ||
from typing import Callable, Dict, Optional | ||
|
||
from multiversx_sdk import ProxyNetworkProvider, TransactionComputer | ||
from multiversx_sdk.core.address import Address | ||
from multiversx_sdk.network_providers.network_config import NetworkConfig | ||
from multiversx_sdk.network_providers.transaction_status import \ | ||
TransactionStatus | ||
from multiversx_sdk.network_providers.transactions import ( | ||
ITransaction, TransactionOnNetwork) | ||
|
||
|
||
class MockNetworkProvider(ProxyNetworkProvider): | ||
def __init__(self) -> None: | ||
super().__init__("https://example.multiversx.com") | ||
|
||
self.transactions: Dict[str, TransactionOnNetwork] = {} | ||
self.transaction_computer = TransactionComputer() | ||
|
||
def get_network_config(self) -> NetworkConfig: | ||
network_config = NetworkConfig() | ||
network_config.chain_id = "T" | ||
network_config.gas_per_data_byte = 1500 | ||
network_config.min_gas_limit = 50000 | ||
network_config.min_gas_price = 1000000000 | ||
return network_config | ||
|
||
def mock_update_transaction(self, hash: str, mutate: Callable[[TransactionOnNetwork], None]) -> None: | ||
transaction = self.transactions.get(hash, None) | ||
|
||
if transaction: | ||
mutate(transaction) | ||
|
||
def mock_put_transaction(self, hash: str, transaction: TransactionOnNetwork) -> None: | ||
self.transactions[hash] = transaction | ||
|
||
def get_transaction(self, tx_hash: str, with_process_status: Optional[bool] = False) -> TransactionOnNetwork: | ||
transaction = self.transactions.get(tx_hash, None) | ||
if transaction: | ||
return transaction | ||
|
||
raise Exception("Transaction not found") | ||
|
||
def get_transaction_status(self, tx_hash: str) -> TransactionStatus: | ||
transaction = self.get_transaction(tx_hash) | ||
return transaction.status | ||
|
||
def send_transaction(self, transaction: ITransaction) -> str: | ||
hash = self.transaction_computer.compute_transaction_hash(transaction).hex() | ||
|
||
transaction_on_network = TransactionOnNetwork() | ||
transaction_on_network.hash = hash | ||
transaction_on_network.sender = Address.from_bech32(transaction.sender) | ||
transaction_on_network.receiver = Address.from_bech32(transaction.receiver) | ||
transaction_on_network.value = transaction.value | ||
transaction_on_network.gas_limit = transaction.gas_limit | ||
transaction_on_network.gas_price = transaction.gas_price | ||
transaction_on_network.data = transaction.data.decode("utf-8") | ||
transaction_on_network.signature = transaction.signature.hex() | ||
|
||
self.mock_put_transaction(hash, transaction_on_network) | ||
|
||
return hash |
This file contains 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 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
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.
I don't particularly like relying on this precalculated address rather than fetching it from the transaction itself due to the fact that it doesn't provide any indication whether the deploy really passed. We had too many surprises relying on the precalculated method only to find out too late that the original deploy transaction failed for whatever completely unexpected reason.
In the entire flow of the environment, relying blindly on the address without checking the operation can result in very bad configs.
Checking now would need to separately fetch the final status of the transaction and that is either painfully unreliable or time consuming, so in this case, if I have to have this check anyway because I cannot rely on the address only, I might as well directly rely on the
SCDeploy
event of the transaction itself which is a solid indicator whether it worked or not. If it didn't, program should just crash 'cause there's no reason to continue without it.In the economics of actual code, I know it's time and resource consuming to fetch the tx again from the network after deploying it, but I have to do it in any case due to the above reasons.
The only difference is whether I directly do it at the
atomic
deploy level or should I do this check separately every time I deploy something. Since I don't have cases in which I don't care whether it worked or not, I would rather enforce it at this level and save me the headache of forgetting the check and actually failing.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.
This is unfortunately a no go for me in this state.