Merge pull request #617 from KPrasch/testerchain

High-level TesterBlockchain network bootstrap method
pull/640/head
Justin Holmes 2018-12-19 13:51:32 -05:00 committed by GitHub
commit e4f70bf6ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 52 deletions

9
.gitignore vendored
View File

@ -23,6 +23,10 @@ chains
.ethash
nucypher_cli/examples/examples-runtime-cruft/*
nucypher_cli/examples/finnegans-wake.txt
examples/heartbeat_demo/*.json
examples/heartbeat_demo/*.msgpack
examples/heartbeat_demo/doctor-files/
examples/heartbeat_demo/alicia-files/
mypy_reports/
reports/
test-*
@ -32,7 +36,4 @@ variables*.yml
*ansible/*.retry
*ansible/inventory/*
.env
examples/heartbeat_demo/*.json
examples/heartbeat_demo/*.msgpack
examples/heartbeat_demo/doctor-files/
examples/heartbeat_demo/alicia-files/
/tests/metrics/results/

View File

@ -14,6 +14,8 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
"""
from twisted.logger import Logger
from web3.middleware import geth_poa_middleware

View File

@ -14,10 +14,23 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
"""
import os
from functools import partial
from twisted.logger import Logger
from constant_sorrow.constants import NO_BLOCKCHAIN_AVAILABLE
from typing import List
from typing import List, Tuple, Dict
from nucypher.blockchain.eth.actors import Deployer
from nucypher.blockchain.eth.agents import NucypherTokenAgent, MinerAgent, PolicyAgent, EthereumContractAgent
from nucypher.blockchain.eth.constants import DISPATCHER_SECRET_LENGTH
from nucypher.blockchain.eth.interfaces import BlockchainDeployerInterface
from nucypher.blockchain.eth.registry import InMemoryEthereumContractRegistry
from nucypher.blockchain.eth.sol.compile import SolidityCompiler
from nucypher.config.constants import CONTRACT_ROOT
from umbral.keys import UmbralPrivateKey
from web3.middleware import geth_poa_middleware
@ -48,10 +61,15 @@ class TesterBlockchain(Blockchain):
Blockchain subclass with additional test utility methods and options.
"""
_PROVIDER_URI = 'tester://pyevm'
_instance = NO_BLOCKCHAIN_AVAILABLE
_test_account_cache = list()
_default_test_accounts = 10
def __init__(self, test_accounts=None, poa=True, airdrop=False, *args, **kwargs):
if test_accounts is None:
test_accounts = self._default_test_accounts
def __init__(self, test_accounts=None, poa=True, airdrop=True, *args, **kwargs):
super().__init__(*args, **kwargs)
self.log = Logger("test-blockchain") # type: Logger
@ -143,3 +161,24 @@ class TesterBlockchain(Blockchain):
self.interface.w3.eth.web3.testing.timeTravel(timestamp=end_timestamp)
self.interface.w3.eth.web3.testing.mine(1)
self.log.info("Time traveled to {}".format(end_timestamp))
@classmethod
def connect(cls, *args, **kwargs) -> 'TesterBlockchain':
interface = BlockchainDeployerInterface(provider_uri=cls._PROVIDER_URI,
compiler=SolidityCompiler(test_contract_dir=CONTRACT_ROOT),
registry=InMemoryEthereumContractRegistry())
testerchain = TesterBlockchain(interface=interface, *args, **kwargs)
return testerchain
@classmethod
def bootstrap_network(cls) -> Tuple['TesterBlockchain', Dict[str, EthereumContractAgent]]:
testerchain = cls.connect()
origin = testerchain.interface.w3.eth.accounts[0]
deployer = Deployer(blockchain=testerchain, deployer_address=origin, bare=True)
random_deployment_secret = partial(os.urandom, DISPATCHER_SECRET_LENGTH)
_txhashes, agents = deployer.deploy_network_contracts(miner_secret=random_deployment_secret(),
policy_secret=random_deployment_secret())
return testerchain, agents

View File

@ -115,43 +115,6 @@ class AnalyzeGas:
globalLogPublisher.addObserver(json_observer)
globalLogPublisher.addObserver(self)
def connect_to_blockchain(self) -> TesterBlockchain:
print("Deploying Blockchain...")
solidity_compiler = SolidityCompiler(test_contract_dir=self.CONTRACT_DIR)
memory_registry = InMemoryEthereumContractRegistry()
interface = BlockchainDeployerInterface(provider_uri=self.PROVIDER_URI, compiler=solidity_compiler,
registry=memory_registry)
testerchain = TesterBlockchain(interface=interface, test_accounts=self.TEST_ACCOUNTS, airdrop=False)
return testerchain
@staticmethod
def deploy_contracts(testerchain: TesterBlockchain) -> None:
print("Deploying Contracts...")
origin = testerchain.interface.w3.eth.accounts[0]
deployer = Deployer(blockchain=testerchain, deployer_address=origin, bare=True)
_txhashes, _agents = deployer.deploy_network_contracts(miner_secret=os.urandom(DISPATCHER_SECRET_LENGTH),
policy_secret=os.urandom(DISPATCHER_SECRET_LENGTH))
@staticmethod
def connect_to_contracts(testerchain: TesterBlockchain) -> Tuple[NucypherTokenAgent, MinerAgent, PolicyAgent]:
print("Connecting...")
token_agent = NucypherTokenAgent(blockchain=testerchain)
miner_agent = MinerAgent(blockchain=testerchain)
policy_agent = PolicyAgent(blockchain=testerchain)
return token_agent, miner_agent, policy_agent
def bootstrap_network(self) -> Tuple[TesterBlockchain, List[str]]:
print("Bootstrapping testing network...")
testerchain = self.connect_to_blockchain()
self.deploy_contracts(testerchain=testerchain)
return testerchain, testerchain.interface.w3.eth.accounts
def estimate_gas(analyzer: AnalyzeGas = None) -> None:
"""
@ -165,25 +128,29 @@ def estimate_gas(analyzer: AnalyzeGas = None) -> None:
#
# Setup
#
if AnalyzeGas is None:
if analyzer is None:
analyzer = AnalyzeGas()
# Logger
log = Logger(AnalyzeGas.LOG_NAME)
# Blockchain
testerchain, accounts = analyzer.bootstrap_network()
testerchain, agents = TesterBlockchain.bootstrap_network()
web3 = testerchain.interface.w3
# Contracts
token_agent, miner_agent, policy_agent = analyzer.connect_to_contracts(testerchain=testerchain)
token_functions = token_agent.contract.functions
miner_functions = miner_agent.contract.functions
policy_functions = policy_agent.contract.functions
# Accounts
origin, ursula1, ursula2, ursula3, alice1, *everyone_else = testerchain.interface.w3.eth.accounts
# Contracts
token_agent = NucypherTokenAgent(blockchain=testerchain)
miner_agent = MinerAgent(blockchain=testerchain)
policy_agent = PolicyAgent(blockchain=testerchain)
# Contract Callers
token_functions = token_agent.contract.functions
miner_functions = miner_agent.contract.functions
policy_functions = policy_agent.contract.functions
analyzer.start_collection()
print("********* Estimating Gas *********")