-
Notifications
You must be signed in to change notification settings - Fork 0
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
Python in local network deployment and tests #20
Merged
Merged
Changes from 24 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
03896c6
Deploy on Python (WIP)
vsuharnikov 7fe81c6
Fixes
vsuharnikov 34aac8a
Asserts in tests
vsuharnikov 0921ad5
Fix
vsuharnikov 493946a
Fix
vsuharnikov 0009726
Remove the setup directory
vsuharnikov 10ee93b
Updated README.md
vsuharnikov 6c70ae4
Test with multiple e2c transfers
vsuharnikov 7640030
Test with multiple c2e transfers
vsuharnikov e7560a9
Fixed transfer-c2e.py balances comparison
vsuharnikov 801552d
Cleanup
vsuharnikov 7ac7131
Cleanup
vsuharnikov 052caf7
BaseTransfer
vsuharnikov 57cb51c
Renaming
vsuharnikov 30fd707
Up
vsuharnikov 29ac45f
Logs in file
vsuharnikov c54bd5e
Force Accounts to depend on Network to guarantee right network addres…
vsuharnikov bca8e78
Up
vsuharnikov aab750f
pyproject.toml instead of setup.py
vsuharnikov fcf6d72
Test accounts instead of miners
vsuharnikov 6914dd3
Moving functions to unit0-examples, better logs
vsuharnikov a6b5e3f
Accounts in ExtendedNetwork
vsuharnikov 90525aa
Decimals instead of float, logs improvements
vsuharnikov ee96e95
Removed files
vsuharnikov ba9219e
Fixes
vsuharnikov 5d106b1
Issue and transfer chain contract tokens to run tests in any order
vsuharnikov 9c651d6
Run basic tests
vsuharnikov 05007d6
Fixed tests
vsuharnikov 369d5f5
local-network: removed logs directory, run test with docker compose p…
vsuharnikov d78da29
Up
vsuharnikov ef95673
Logging from unit0-examples
vsuharnikov e442f55
Test improvements
vsuharnikov 40fbd8a
Docker compose context for BlockScout
vsuharnikov 0fdcbde
Merge github/main
vsuharnikov 38bb073
Fix
vsuharnikov aab9817
Review fixes
vsuharnikov 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
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,2 +1,4 @@ | ||
node_modules | ||
logs | ||
.venv/ | ||
.solc-select/ | ||
__pycache__/ | ||
*.egg-info/ |
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,6 @@ | ||
node_modules | ||
logs | ||
.solc-select | ||
**/*.js | ||
.venv/ | ||
.solc-select/ | ||
__pycache__/ | ||
build/ | ||
setup/ | ||
*.egg-info/ |
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,14 +1,17 @@ | ||
FROM node:18-slim | ||
FROM python:3.12-slim | ||
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
python3 \ | ||
python3-pip \ | ||
curl \ | ||
&& pip3 install solc-select --break-system-packages \ | ||
git \ | ||
gcc \ | ||
libc6-dev \ | ||
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* | ||
|
||
WORKDIR /home/node/app | ||
COPY package.json package-lock.json /home/node/app/ | ||
RUN npm i | ||
WORKDIR /usr/src/app | ||
|
||
COPY local/__init__.py ./local/ | ||
COPY pyproject.toml ./ | ||
|
||
RUN pip install --no-cache-dir --editable . | ||
|
||
CMD ["/bin/bash", "deploy-run.sh"] |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,4 +4,4 @@ DIR="$(cd "$(dirname "$0")" && pwd)" | |
cd "${DIR}" || exit | ||
|
||
./bridge-compile.sh | ||
npm run deploy | ||
python -B deploy.py |
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,71 @@ | ||
#!/usr/bin/env python | ||
from time import sleep | ||
|
||
from units_network import waves | ||
from units_network.chain_contract import HexStr | ||
from units_network.node import Node | ||
|
||
from local.common import configure_script_logger | ||
from local.network import get_local | ||
|
||
log = configure_script_logger("main") | ||
network = get_local() | ||
|
||
|
||
node = Node() | ||
min_peers = len(network.cl_miners) - 1 | ||
while True: | ||
r = node.connected_peers() | ||
if len(r) >= min_peers: | ||
break | ||
|
||
log.info(f"Wait for {min_peers} peers, now: {r}") | ||
sleep(2) | ||
|
||
log.info(f"Chain contract address: {network.cl_chain_contract.oracleAddress}") | ||
|
||
script_info = network.cl_chain_contract.oracleAcc.scriptInfo() | ||
if script_info["script"] is None: | ||
log.info("Set chain contract script") | ||
|
||
with open("setup/waves/main.ride", "r", encoding="utf-8") as file: | ||
source = file.read() | ||
r = network.cl_chain_contract.setScript(source) | ||
waves.force_success(log, r, "Can not set the chain contract script") | ||
|
||
if not network.cl_chain_contract.isContractSetup(): | ||
log.info("Call ChainContract.setup") | ||
el_genesis_block = network.w3.eth.get_block(0) | ||
|
||
assert "hash" in el_genesis_block | ||
el_genesis_block_hash = HexStr(el_genesis_block["hash"].to_0x_hex()) | ||
|
||
log.info(f"Genesis block hash: {el_genesis_block_hash}") | ||
|
||
r = network.cl_chain_contract.setup(el_genesis_block_hash) | ||
waves.force_success(log, r, "Can not setup the chain contract") | ||
|
||
|
||
r = network.cl_chain_contract.evaluate("allMiners") | ||
joined_miners = [] | ||
for entry in r["result"]["value"]: | ||
joined_miners.append(entry["value"]) | ||
log.info(f"Miners: {joined_miners}") | ||
|
||
for miner in network.cl_miners: | ||
if miner.account.address not in joined_miners: | ||
log.info(f"Call ChainContract.join by miner f{miner.account.address}") | ||
r = network.cl_chain_contract.join(miner.account, miner.el_reward_address_hex) | ||
waves.force_success( | ||
log, | ||
r, | ||
f"{miner.account.address} can not join the chain contract", | ||
wait=False, | ||
) | ||
|
||
while True: | ||
r = network.w3.eth.get_block("latest") | ||
if "number" in r and r["number"] >= 1: | ||
break | ||
log.info("Wait for at least one block on EL") | ||
sleep(3) |
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
#!/usr/bin/env bash | ||
|
||
if [ ! -d "$PWD/.venv" ]; then | ||
echo "Create a virtual environment" | ||
python3 -m venv .venv --prompt local-network | ||
source .venv/bin/activate | ||
|
||
if [[ "$(uname)" == "Darwin" && "$(uname -m)" == "arm64" ]]; then | ||
# Otherwise python-axolotl-curve25519 won't compile | ||
export CC=gcc | ||
fi | ||
|
||
echo "Install dependencies" | ||
# --no-cache-dir is useful during development and local units-network in dependencies | ||
pip install --editable . | ||
fi | ||
|
||
echo "Done." |
Empty file.
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,74 @@ | ||
import os | ||
from dataclasses import dataclass | ||
from decimal import Decimal | ||
from functools import cached_property | ||
|
||
from eth_account.signers.local import LocalAccount | ||
from pywaves import pw | ||
from units_network import units | ||
from units_network.chain_contract import common_utils | ||
from web3.types import Wei | ||
|
||
|
||
@dataclass() | ||
class BaseTransfer: | ||
el_account: LocalAccount | ||
cl_account: pw.Address | ||
raw_amount: Decimal | ||
|
||
@cached_property | ||
def wei_amount(self) -> Wei: | ||
return units.raw_to_wei(self.raw_amount) | ||
|
||
@cached_property | ||
def waves_atomic_amount(self) -> int: | ||
return units.raw_to_waves_atomic(self.raw_amount) | ||
|
||
|
||
@dataclass() | ||
class C2ETransfer(BaseTransfer): | ||
@property | ||
def from_account(self) -> pw.Address: | ||
return self.cl_account | ||
|
||
@property | ||
def to_account(self) -> LocalAccount: | ||
return self.el_account | ||
|
||
def __repr__(self) -> str: | ||
return f"C2E(from={self.cl_account.address}, to={self.el_account.address}, {self.raw_amount} UNIT0)" | ||
|
||
|
||
@dataclass() | ||
class E2CTransfer(BaseTransfer): | ||
@property | ||
def from_account(self) -> LocalAccount: | ||
return self.el_account | ||
|
||
@property | ||
def to_account(self) -> pw.Address: | ||
return self.cl_account | ||
|
||
def __repr__(self) -> str: | ||
return f"E2C(from={self.el_account.address}, to={self.cl_account.address}, {self.raw_amount} UNIT0)" | ||
|
||
|
||
_INSIDE_DOCKER = None | ||
|
||
|
||
def in_docker() -> bool: | ||
global _INSIDE_DOCKER | ||
if _INSIDE_DOCKER is None: | ||
try: | ||
os.stat("/.dockerenv") | ||
_INSIDE_DOCKER = True | ||
except FileNotFoundError: | ||
_INSIDE_DOCKER = False | ||
return _INSIDE_DOCKER | ||
|
||
|
||
def configure_script_logger(name: str): | ||
file = None | ||
if in_docker(): | ||
file = "/var/log/deploy/deploy.log" | ||
return common_utils.configure_script_logger(name, file) |
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,91 @@ | ||
from dataclasses import dataclass | ||
from functools import cached_property | ||
from typing import List, Optional | ||
|
||
from eth_account.signers.local import LocalAccount | ||
from pywaves import pw | ||
from units_network import networks | ||
from units_network.chain_contract import ChainContract, HexStr | ||
from units_network.networks import Network, NetworkSettings | ||
from web3 import Account | ||
|
||
from local.common import in_docker | ||
|
||
|
||
def get_waves_api_url(n: int) -> str: | ||
return f"http://wavesnode-{n}:6869" if in_docker() else f"http://127.0.0.1:{n}6869" | ||
|
||
|
||
def get_ec_api_url(n: int) -> str: | ||
return f"http://ec-{n}:8545" if in_docker() else f"http://127.0.0.1:{n}8545" | ||
|
||
|
||
@dataclass | ||
class Miner: | ||
account: pw.Address | ||
el_reward_address_hex: HexStr | ||
|
||
|
||
class ExtendedNetwork(Network): | ||
@cached_property | ||
def cl_chain_contract(self) -> ChainContract: | ||
return ChainContract(seed="devnet-1", nonce=2) | ||
|
||
@cached_property | ||
def cl_miners(self) -> List[Miner]: | ||
return [ | ||
Miner( | ||
account=pw.Address( | ||
seed="devnet-1", | ||
nonce=0, | ||
), | ||
el_reward_address_hex=HexStr( | ||
"0x7dbcf9c6c3583b76669100f9be3caf6d722bc9f9" | ||
), | ||
), | ||
Miner( | ||
account=pw.Address( | ||
seed="devnet-2", | ||
nonce=0, | ||
), | ||
el_reward_address_hex=HexStr( | ||
"0xcf0b9e13fdd593f4ca26d36afcaa44dd3fdccbed" | ||
), | ||
), | ||
] | ||
|
||
@cached_property | ||
def cl_rich_accounts(self) -> List[pw.Address]: | ||
return [pw.Address(seed="devnet-0", nonce=n) for n in range(0, 2)] | ||
|
||
@cached_property | ||
def el_rich_accounts(self) -> List[LocalAccount]: | ||
return [ | ||
Account.from_key( | ||
"0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63" | ||
), | ||
Account.from_key( | ||
"0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f" | ||
), | ||
] | ||
|
||
|
||
local_net = NetworkSettings( | ||
name="LocalNet", | ||
chain_id_str="D", | ||
cl_node_api_url=get_waves_api_url(1), | ||
el_node_api_url=get_ec_api_url(1), | ||
chain_contract_address="3FdaanzgX4roVgHevhq8L8q42E7EZL9XTQr", | ||
) | ||
|
||
|
||
_NETWORK: Optional[ExtendedNetwork] = None | ||
|
||
|
||
def get_local() -> ExtendedNetwork: | ||
global _NETWORK | ||
if _NETWORK is None: | ||
networks.prepare(local_net) | ||
_NETWORK = ExtendedNetwork(local_net) | ||
|
||
return _NETWORK |
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.
devnet-0
sodevnet-3
can be a miner