Removes Ethereumconfig, inject web providers into blockchain classes.

pull/224/merge^2
Kieran Prasch 2018-04-13 15:23:20 -07:00
parent 041438a320
commit d7e93f0ae3
4 changed files with 22 additions and 32 deletions

View File

@ -1,7 +1,7 @@
import random
from abc import ABC
from nkms.config.configs import EthereumConfig
from nkms.blockchain.eth.interfaces import Provider
class TheBlockchain(ABC):
@ -26,7 +26,7 @@ class TheBlockchain(ABC):
class IsAlreadyRunning(RuntimeError):
pass
def __init__(self, config: EthereumConfig):
def __init__(self, provider=Provider()):
"""
Configures a populus project and connects to blockchain.network.
Transaction timeouts specified measured in seconds.
@ -41,7 +41,7 @@ class TheBlockchain(ABC):
raise TheBlockchain.IsAlreadyRunning(message)
TheBlockchain.__instance = self
self.config = config
self.provider = provider
@classmethod
def get(cls):
@ -67,7 +67,7 @@ class TheBlockchain(ABC):
if timeout is None:
timeout = self._default_timeout
result = self.config.wait.for_receipt(txhash, timeout=timeout)
result = self.provider.web3.wait.for_receipt(txhash, timeout=timeout)
return result
@ -79,10 +79,10 @@ class TesterBlockchain(TheBlockchain):
def wait_time(self, wait_hours, step=50):
"""Wait the specified number of wait_hours by comparing block timestamps."""
end_timestamp = self.config.web3.eth.getBlock(
self._chain.web3.eth.blockNumber).timestamp + wait_hours * 60 * 60
while self._chain.web3.eth.getBlock(self._chain.web3.eth.blockNumber).timestamp < end_timestamp:
self._chain.wait.for_block(self._chain.web3.eth.blockNumber + step)
end_timestamp = self.provider.web3.eth.getBlock(
self.provider.web3.eth.blockNumber).timestamp + wait_hours * 60 * 60
while self.provider.web3.eth.getBlock(self.provider.web3.eth.blockNumber).timestamp < end_timestamp:
self.provider.web3.wait.for_block(self.provider.web3.eth.blockNumber + step)
def spawn_miners(self, miner_agent, addresses: list, locktime: int, random_amount=False) -> list():
"""
@ -107,7 +107,7 @@ class TesterBlockchain(TheBlockchain):
def _global_airdrop(self, token_agent, amount: int):
"""Airdrops from creator address to all other addresses!"""
_creator, *addresses = self._chain.web3.eth.accounts
_creator, *addresses = self.provider.web3.eth.accounts
def txs():
for address in addresses:

View File

@ -63,8 +63,8 @@ class ContractDeployer:
rules = (
(self.is_armed is True, 'Contract not armed'),
(self.is_deployed is not True, 'Contract already deployed'),
(self.blockchain._chain.provider.are_contract_dependencies_available(self._contract_name),
'Blockchain contract dependencies unmet'),
# (self.blockchain.provider.are_contract_dependencies_available(self._contract_name),
# 'Blockchain contract dependencies unmet'),
)
@ -89,7 +89,7 @@ class ContractDeployer:
raise self.ContractDeploymentError(message)
# http: // populus.readthedocs.io / en / latest / chain.contracts.html # checking-availability-of-contracts
available = bool(self.blockchain._chain.provider.are_contract_dependencies_available(self._contract_name))
available = bool(self.blockchain.provider.are_contract_dependencies_available(self._contract_name))
if not available:
raise self.ContractDeploymentError('Contract is not available')
@ -98,9 +98,9 @@ class ContractDeployer:
def _wrap_government(self, dispatcher_contract: Contract, target_contract: Contract) -> Contract:
# Wrap the contract
wrapped_contract = self.blockchain._chain.web3.eth.contract(target_contract.abi,
dispatcher_contract.address,
ContractFactoryClass=Contract)
wrapped_contract = self.blockchain.provider.web3.eth.contract(target_contract.abi,
dispatcher_contract.address,
ContractFactoryClass=Contract)
return wrapped_contract
def arm(self, fail_on_abort=True) -> None:
@ -156,7 +156,7 @@ class NuCypherKMSTokenDeployer(ContractDeployer, NuCypherTokenConfig):
def __init__(self, blockchain):
super().__init__(blockchain=blockchain)
self._creator = self.blockchain.config.provider.get_accounts()[0] # TODO: make swappable
self._creator = self.blockchain.provider.web3.eth.accounts[0] # TODO: make swappable
def deploy(self) -> str:
"""
@ -169,7 +169,7 @@ class NuCypherKMSTokenDeployer(ContractDeployer, NuCypherTokenConfig):
is_ready, _disqualifications = self.check_ready_to_deploy(fail=True)
assert is_ready
the_nucypher_token_contract, deployment_txhash = self.blockchain._chain.provider.deploy_contract(
the_nucypher_token_contract, deployment_txhash = self.blockchain.provider.deploy_contract(
self._contract_name,
deploy_args=[self.saturation],
deploy_transaction={'from': self._creator})
@ -196,7 +196,7 @@ class DispatcherDeployer(ContractDeployer):
def deploy(self) -> str:
dispatcher_contract, txhash = self.blockchain._chain.provider.deploy_contract(
dispatcher_contract, txhash = self.blockchain.provider.deploy_contract(
'Dispatcher', deploy_args=[self.target_contract.address],
deploy_transaction={'from': self.token_agent.origin})
@ -241,7 +241,7 @@ class MinerEscrowDeployer(ContractDeployer, NuCypherMinerConfig):
origin_args = {'from': self.token_agent.origin}
# 1 - Deploy #
the_escrow_contract, deploy_txhash = self.blockchain._chain.provider.\
the_escrow_contract, deploy_txhash = self.blockchain.provider.\
deploy_contract(self._contract_name,
deploy_args=deploy_args,
deploy_transaction=origin_args)
@ -300,7 +300,7 @@ class PolicyManagerDeployer(ContractDeployer):
assert is_ready
# Creator deploys the policy manager
the_policy_manager_contract, deploy_txhash = self.blockchain._chain.provider.deploy_contract(
the_policy_manager_contract, deploy_txhash = self.blockchain.provider.deploy_contract(
self._contract_name,
deploy_args=[self.miner_escrow_deployer.contract_address],
deploy_transaction={'from': self.token_deployer.origin})
@ -360,7 +360,7 @@ class UserEscrowDeployer(ContractDeployer):
self.policy_deployer.contract_address],
deploy_transaction = {'from': self.token_deployer.contract_address}
the_user_escrow_contract, deploy_txhash = self.blockchain._chain.provider.deploy_contract(
the_user_escrow_contract, deploy_txhash = self.blockchain.provider.deploy_contract(
self._contract_name,
deploy_args=deployment_args,
deploy_transaction=deploy_transaction)

View File

@ -1,7 +1,7 @@
import json
import os
from pathlib import Path
from typing import Tuple, ClassVar, Dict
from typing import Tuple, ClassVar, Dict, Union
from eth_tester import EthereumTester, PyEVMBackend
from web3 import Web3, EthereumTesterProvider

View File

@ -11,16 +11,6 @@ class KMSConfigurationError(RuntimeError):
pass
class EthereumConfig:
"""
TODO: Unmock
"""
def __init__(self, registrar=None, provider=None):
self.registrar = registrar
self.provider = provider
class StakeConfig:
# __minimum_stake_amount = 0 # TODO
# __minimum_stake_duration = 0