Skip to content

Commit

Permalink
Upgrade hmt-escrow to latest version of web3 and py-evm using trinity
Browse files Browse the repository at this point in the history
  • Loading branch information
uivlis committed Jul 30, 2020
1 parent 6267f43 commit 5c0b729
Show file tree
Hide file tree
Showing 7 changed files with 1,509 additions and 35 deletions.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ ENV PYTHONUNBUFFERED True
WORKDIR /work
RUN apt-get update -y && \
apt-get install -y automake bash black build-essential curl git jq libffi-dev libgmp-dev libtool mypy nodejs npm \
pandoc pkg-config python3-boto python3-dev python3-pip
pandoc pkg-config python3-boto python3-dev python3-pip libsnappy-dev

COPY package.json /work/
RUN npm install

COPY requirements.txt /work/
RUN pip3 install -r requirements.txt
# Pin to specific version that's guaranteed to work
RUN pip3 install pipenv
COPY Pipfile Pipfile.lock /work/
RUN pipenv install --system --deploy --pre

ENV SOLC_VERSION="v0.6.2"
RUN python3 -m solcx.install ${SOLC_VERSION}
Expand Down
19 changes: 19 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
trinity = {git = "https://github.com/ethereum/trinity.git", ref = "master"}
py-solc-x = {git = "https://github.com/iamdefinitelyahuman/py-solc-x.git", ref = "master"}
boto3 = "*"
hmt-basemodels = "*"
Sphinx = {git = "https://github.com/sphinx-doc/sphinx.git", ref = "master"}

[requires]
python_version = "3.8"

[pipenv]
allow_prereleases = true
1,448 changes: 1,448 additions & 0 deletions Pipfile.lock

Large diffs are not rendered by default.

34 changes: 21 additions & 13 deletions hmt_escrow/eth_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import unittest

from solcx import compile_files
from web3 import Web3, HTTPProvider, EthereumTesterProvider
from web3 import Web3
from web3.providers import HTTPProvider
from web3.providers.eth_tester import EthereumTesterProvider
from web3.types import TxReceipt
from eth_typing import Address, ChecksumAddress, HexAddress, HexStr
from web3.contract import Contract
from web3.middleware import geth_poa_middleware
from web3.utils.transactions import wait_for_transaction_receipt
from web3._utils.transactions import wait_for_transaction_receipt
from hmt_escrow.kvstore_abi import abi as kvstore_abi
from typing import Dict, List, Tuple, Optional, Any

Expand Down Expand Up @@ -53,13 +57,13 @@ def get_w3() -> Web3:
endpoint = os.getenv("HMT_ETH_SERVER", "http://localhost:8545")
if not endpoint:
LOG.error("Using EthereumTesterProvider as we have no HMT_ETH_SERVER")
provider = HTTPProvider(endpoint) if endpoint else EthereumTesterProvider
provider = HTTPProvider(endpoint) if endpoint else EthereumTesterProvider()
w3 = Web3(provider)
w3.middleware_stack.inject(geth_poa_middleware, layer=0)
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
return w3


def handle_transaction(txn_func, *args, **kwargs) -> AttributeDict:
def handle_transaction(txn_func, *args, **kwargs) -> TxReceipt:
"""Handles a transaction that updates the contract state by locally
signing, building, sending the transaction and returning a transaction
receipt.
Expand Down Expand Up @@ -119,7 +123,7 @@ def get_hmtoken(hmtoken_addr=HMTOKEN_ADDR) -> Contract:
"""Retrieve the HMToken contract from a given address.
>>> type(get_hmtoken())
<class 'web3.utils.datatypes.Contract'>
<class 'web3._utils.datatypes.Contract'>
Returns:
Contract: returns the HMToken solidity contract.
Expand Down Expand Up @@ -148,7 +152,7 @@ def get_escrow(escrow_addr: str) -> Contract:
>>> job.launch(rep_oracle_pub_key)
True
>>> type(get_escrow(job.job_contract.address))
<class 'web3.utils.datatypes.Contract'>
<class 'web3._utils.datatypes.Contract'>
Args:
escrow_addr (str): an ethereum address of the escrow contract.
Expand All @@ -162,11 +166,14 @@ def get_escrow(escrow_addr: str) -> Contract:
contract_interface = get_contract_interface(
"{}/Escrow.sol:Escrow".format(CONTRACT_FOLDER)
)
escrow = w3.eth.contract(address=escrow_addr, abi=contract_interface["abi"])
escrow = w3.eth.contract(
address=ChecksumAddress(HexAddress(HexStr(escrow_addr))),
abi=contract_interface["abi"],
)
return escrow


def get_factory(factory_addr: Optional[str]) -> Contract:
def get_factory(factory_addr: str) -> Contract:
"""Retrieve the EscrowFactory contract from a given address.
>>> credentials = {
Expand All @@ -175,7 +182,7 @@ def get_factory(factory_addr: Optional[str]) -> Contract:
... }
>>> job = Job(credentials=credentials, escrow_manifest=manifest)
>>> type(get_factory(job.factory_contract.address))
<class 'web3.utils.datatypes.Contract'>
<class 'web3._utils.datatypes.Contract'>
Args:
factory_addr (str): the ethereum address of the Escrow contract.
Expand All @@ -189,7 +196,8 @@ def get_factory(factory_addr: Optional[str]) -> Contract:
"{}/EscrowFactory.sol:EscrowFactory".format(CONTRACT_FOLDER)
)
escrow_factory = w3.eth.contract(
address=factory_addr, abi=contract_interface["abi"]
address=ChecksumAddress(HexAddress(HexStr(factory_addr))),
abi=contract_interface["abi"],
)
return escrow_factory

Expand Down Expand Up @@ -220,7 +228,7 @@ def deploy_factory(gas: int = GAS_LIMIT, **credentials) -> str:
txn_info = {"gas_payer": gas_payer, "gas_payer_priv": gas_payer_priv, "gas": gas}
txn_receipt = handle_transaction(txn_func, *func_args, **txn_info)
contract_addr = txn_receipt["contractAddress"]
return contract_addr
return str(contract_addr)


def get_pub_key_from_addr(wallet_addr: str) -> bytes:
Expand Down Expand Up @@ -274,7 +282,7 @@ def get_pub_key_from_addr(wallet_addr: str) -> bytes:
return bytes(addr_pub_key, encoding="utf-8")


def set_pub_key_at_addr(pub_key: str) -> Dict[str, Any]:
def set_pub_key_at_addr(pub_key: str) -> TxReceipt:
"""
Given a public key, this function will use the eth-kvstore to reach out to the blockchain
and set the key `hmt_pub_key` on the callers kvstore collection of values, equivalent to the
Expand Down
27 changes: 15 additions & 12 deletions hmt_escrow/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from web3 import Web3
from web3.contract import Contract
from web3.types import Wei
from eth_keys import keys
from eth_utils import decode_hex

Expand Down Expand Up @@ -61,7 +62,7 @@ def status(escrow_contract: Contract, gas_payer: str, gas: int = GAS_LIMIT) -> E
"""
status_ = escrow_contract.functions.getStatus().call(
{"from": gas_payer, "gas": gas}
{"from": gas_payer, "gas": Wei(gas)}
)
return Status(status_ + 1)

Expand Down Expand Up @@ -95,7 +96,7 @@ def manifest_url(
"""
return escrow_contract.functions.getManifestUrl().call(
{"from": gas_payer, "gas": gas}
{"from": gas_payer, "gas": Wei(gas)}
)


Expand Down Expand Up @@ -128,15 +129,15 @@ def manifest_hash(
"""
return escrow_contract.functions.getManifestHash().call(
{"from": gas_payer, "gas": gas}
{"from": gas_payer, "gas": Wei(gas)}
)


def is_trusted_handler(
escrow_contract: Contract, handler_addr: str, gas_payer: str, gas: int = GAS_LIMIT
) -> bool:
return escrow_contract.functions.isTrustedHandler(handler_addr).call(
{"from": gas_payer, "gas": gas}
{"from": gas_payer, "gas": Wei(gas)}
)


Expand All @@ -152,7 +153,9 @@ def launcher(escrow_contract: Contract, gas_payer: str, gas: int = GAS_LIMIT) ->
str: returns the address of who launched the job.
"""
return escrow_contract.functions.getLauncher().call({"from": gas_payer, "gas": gas})
return escrow_contract.functions.getLauncher().call(
{"from": gas_payer, "gas": Wei(gas)}
)


class Job:
Expand Down Expand Up @@ -996,7 +999,7 @@ def balance(self, gas: int = GAS_LIMIT) -> int:
"""
return self.job_contract.functions.getBalance().call(
{"from": self.gas_payer, "gas": gas}
{"from": self.gas_payer, "gas": Wei(gas)}
)

def manifest(self, priv_key: bytes) -> Dict:
Expand Down Expand Up @@ -1094,7 +1097,7 @@ def final_results(self, priv_key: bytes, gas: int = GAS_LIMIT) -> Dict:
"""
final_results_url = self.job_contract.functions.getFinalResultsUrl().call(
{"from": self.gas_payer, "gas": gas}
{"from": self.gas_payer, "gas": Wei(gas)}
)
return download(final_results_url, priv_key)

Expand Down Expand Up @@ -1256,7 +1259,7 @@ def _factory_contains_escrow(
"""
factory_contract = get_factory(factory_addr)
return factory_contract.functions.hasEscrow(escrow_addr).call(
{"from": self.gas_payer, "gas": gas}
{"from": self.gas_payer, "gas": Wei(gas)}
)

def _init_factory(
Expand All @@ -1276,7 +1279,7 @@ def _init_factory(
... }
>>> job = Job(credentials, manifest)
>>> type(job.factory_contract)
<class 'web3.utils.datatypes.Contract'>
<class 'web3._utils.datatypes.Contract'>
Initializing a new Job instance with a factory address succeeds.
>>> factory_addr = deploy_factory(**credentials)
Expand All @@ -1303,7 +1306,7 @@ def _init_factory(
raise Exception("Unable to get address from factory")

if not factory:
factory = get_factory(factory_addr)
factory = get_factory(str(factory_addr))
return factory

def _bulk_paid(self, gas: int = GAS_LIMIT) -> int:
Expand Down Expand Up @@ -1340,7 +1343,7 @@ def _bulk_paid(self, gas: int = GAS_LIMIT) -> int:
"""
return self.job_contract.functions.getBulkPaid().call(
{"from": self.gas_payer, "gas": gas}
{"from": self.gas_payer, "gas": Wei(gas)}
)

def _last_escrow_addr(self, gas: int = GAS_LIMIT) -> str:
Expand All @@ -1367,7 +1370,7 @@ def _last_escrow_addr(self, gas: int = GAS_LIMIT) -> str:
"""
return self.factory_contract.functions.getLastEscrow().call(
{"from": self.gas_payer, "gas": gas}
{"from": self.gas_payer, "gas": Wei(gas)}
)

def _create_escrow(self, trusted_handlers=[], gas: int = GAS_LIMIT) -> bool:
Expand Down
6 changes: 0 additions & 6 deletions requirements.txt

This file was deleted.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"Programming Language :: Python",
],
packages=setuptools.find_packages() + ["contracts", "migrations"],
install_requires=["py-solc-x", "web3", "hmt-basemodels", "boto3"],
install_requires=["py-solc-x", "trinity", "hmt-basemodels", "boto3", "sphinx"],
)

0 comments on commit 5c0b729

Please sign in to comment.