def test_cli_select_chain_helper(project_dir, write_project_file, stdin, expected): project = Project() project.config['chains.local_a.chain.class'] = 'populus.chain.TesterChain' project.config[ 'chains.local_a.web3.provider.class'] = 'web3.providers.ipc.IPCProvider' project.config['chains.local_a.web3.provider.settings.ipc_path'] = ( get_geth_ipc_path( get_local_chain_datadir(project.project_dir, 'local_a'))) project.config['chains.local_b.chain.class'] = 'populus.chain.TesterChain' project.config[ 'chains.local_b.web3.provider.class'] = 'web3.providers.ipc.IPCProvider' project.config['chains.local_b.web3.provider.settings.ipc_path'] = ( get_geth_ipc_path( get_local_chain_datadir(project.project_dir, 'local_b'))) project.config['chains.local_c.chain.class'] = 'populus.chain.TesterChain' project.config[ 'chains.local_c.web3.provider.class'] = 'web3.providers.ipc.IPCProvider' project.config['chains.local_c.web3.provider.settings.ipc_path'] = ( get_geth_ipc_path( get_local_chain_datadir(project.project_dir, 'local_c'))) project.write_config() @click.command() def wrapper(): chain_name = select_chain(project) print("~~{0}~~".format(chain_name)) runner = CliRunner() result = runner.invoke(wrapper, [], input=stdin) assert result.exit_code == 0 assert expected in result.output
def local_chain(project_dir): project = Project() project.config['chains.local.chain.class'] = 'populus.chain.LocalGethChain' project.config['chains.local.web3.provider.class'] = 'web3.providers.ipc.IPCProvider' project.config['chains.local.contracts.backends.Memory.$ref'] = 'contracts.backends.Memory' project.write_config() chain = project.get_chain('local') return chain
def local_chain(project_dir): project = Project() project.config['chains.local.chain.class'] = 'populus.chain.LocalGethChain' project.config[ 'chains.local.web3.provider.class'] = 'web3.providers.ipc.IPCProvider' project.config['chains.local.web3.provider.settings.ipc_path'] = ( get_geth_ipc_path(get_local_chain_datadir(project.project_dir, 'local'))) project.write_config() chain = project.get_chain('local') return chain
def test_chain_web3_is_preconfigured_with_default_from(project_dir, write_project_file): write_project_file('populus.ini', '[chain:local]') project = Project() chain = project.get_chain('local') default_account = force_text( create_new_account(chain.geth.data_dir, b'a-test-password')) project.config.set('chain:local', 'default_account', default_account) project.write_config() with chain: web3 = chain.web3 assert len(web3.eth.accounts) == 2 assert web3.eth.defaultAccount == default_account assert web3.eth.coinbase != default_account
def test_project_local_chain_ipc(project_dir): project = Project() project.config['chains.local.chain.class'] = 'populus.chain.LocalGethChain' project.config[ 'chains.local.web3.provider.class'] = 'web3.providers.ipc.IPCProvider' project.write_config() chain = project.get_chain('local') with chain as running_local_chain: web3 = running_local_chain.web3 assert web3.version.node.startswith('Geth') running_local_chain.wait.for_block(block_number=1, timeout=180) block_1 = web3.eth.getBlock(1) assert block_1['hash'] != MAINNET_BLOCK_1_HASH assert block_1['hash'] != TESTNET_BLOCK_1_HASH assert block_1['miner'] == web3.eth.coinbase
def test_initializing_no_choices(project_dir, write_project_file): write_project_file('populus.ini', "[chain:local_a]") project = Project() with project.get_chain('local_a') as chain: project.config.set('chain:local_a', 'deploy_from', chain.web3.eth.coinbase) project.write_config() runner = CliRunner() result = runner.invoke( main, ['chain', 'init', 'local_a'], ) assert result.exit_code == 0, result.output + str(result.exception) updated_project = Project() assert 'registrar' in updated_project.config.chains['local_a'] assert 'deploy_from' in updated_project.config.chains['local_a']
def test_chain_web3_is_preconfigured_with_default_from(project_dir, write_project_file): write_project_file('populus.ini', '[chain:local]') project = Project() chain = project.get_chain('local') default_account = force_text( create_new_account(chain.geth.data_dir, b'a-test-password') ) project.config.set('chain:local', 'default_account', default_account) project.write_config() with chain: web3 = chain.web3 assert len(web3.eth.accounts) == 2 assert web3.eth.defaultAccount == default_account assert web3.eth.coinbase != default_account
def test_external_rpc_chain(project_dir, write_project_file): project = Project() with project.get_chain('testrpc') as chain: web3 = chain.web3 registrar = chain.registrar project.config[ 'chains.external.chain.class'] = 'populus.chain.ExternalChain' project.config['chains.external.registrar'] = registrar.address project.config[ 'chains.external.web3.provider.class'] = 'web3.providers.rpc.HTTPProvider' project.config[ 'chains.external.web3.provider.settings.endpoint_uri'] = 'http://127.0.0.1:{0}'.format( chain.port) project.write_config() with project.get_chain('external') as external_chain: ext_web3 = external_chain.web3 ext_registrar = external_chain.registrar assert ext_web3.eth.coinbase == web3.eth.coinbase assert registrar.address == ext_registrar.address
def test_external_ipc_chain(project_dir, write_project_file): project = Project() with project.get_chain('temp') as chain: web3 = chain.web3 chain.wait.for_unlock(timeout=30) registrar = chain.registrar project.config[ 'chains.external.chain.class'] = 'populus.chain.ExternalChain' project.config['chains.external.registrar'] = registrar.address project.config[ 'chains.external.web3.provider.class'] = 'web3.providers.ipc.IPCProvider' project.config[ 'chains.external.web3.provider.settings.ipc_path'] = chain.geth.ipc_path project.write_config() with project.get_chain('external') as external_chain: ext_web3 = external_chain.web3 ext_registrar = external_chain.registrar assert ext_web3.eth.coinbase == web3.eth.coinbase assert registrar.address == ext_registrar.address
def test_project_local_chain_rpc(project_dir): project = Project() rpc_port = str(get_open_port()) project.config['chains.local.chain.class'] = 'populus.chain.LocalGethChain' project.config['chains.local.chain.settings.rpc_port'] = rpc_port project.config[ 'chains.local.web3.provider.class'] = 'web3.providers.rpc.HTTPProvider' project.config[ 'chains.local.web3.provider.settings.endpoint_uri'] = "http://127.0.0.1:{0}".format( rpc_port) project.write_config() chain = project.get_chain('local') with chain as running_local_chain: web3 = running_local_chain.web3 assert web3.version.node.startswith('Geth') running_local_chain.wait.for_block(block_number=1, timeout=180) block_1 = web3.eth.getBlock(1) assert block_1['hash'] != MAINNET_BLOCK_1_HASH assert block_1['hash'] != TESTNET_BLOCK_1_HASH assert block_1['miner'] == web3.eth.coinbase
def test_migrate_cmd(project_dir, write_project_file, MATH): class MigrationA(migrations.Migration): migration_id = '0001_deploy_math' dependencies = [] operations = [ migrations.DeployContract('Math'), ] compiled_contracts = { 'Math': MATH, } class MigrationB(migrations.Migration): migration_id = '0002_increment' dependencies = ['0001_deploy_math'] operations = [ migrations.TransactContract( contract_name='Math', method_name='increment', arguments=[3], contract_address=migrations.Address.defer(key='contract/Math'), timeout=30, ), ] compiled_contracts = { 'Math': MATH, } write_project_file('contracts/Math.sol', MATH['source']) write_project_file('migrations/__init__.py') migration_0001_path = os.path.join( project_dir, 'migrations', '0001_deploy_math.py', ) with open(migration_0001_path, 'w') as migration_0001: write_migration(migration_0001, MigrationA) migration_0002_path = os.path.join( project_dir, 'migrations', '0002_increment.py', ) with open(migration_0002_path, 'w') as migration_0002: write_migration(migration_0002, MigrationB) write_project_file('populus.ini', '[chain:local_a]') project = Project() # sanity assert len(project.migrations) == 2 with project.get_chain('local_a') as chain: chain.wait.for_unlock(chain.web3.eth.coinbase, timeout=30) project.config.set('chain:local_a', 'deploy_from', chain.web3.eth.coinbase) RegistrarFactory = get_registrar(web3=chain.web3) deploy_transaction_hash = RegistrarFactory.deploy() registrar_address = chain.wait.for_contract_address(deploy_transaction_hash, timeout=60) project.config.set('chain:local_a', 'registrar', registrar_address) project.write_config() runner = CliRunner() result = runner.invoke(main, ['migrate', 'local_a']) assert result.exit_code == 0, result.output + str(result.exception) with project.get_chain('local_a') as chain: registrar = chain.registrar math_address = registrar.call().getAddress('contract/Math') Math = chain.contract_factories.Math(address=math_address) assert Math.call().counter() == 3
def test_migrate_cmd(project_dir, write_project_file, MATH): class MigrationA(migrations.Migration): migration_id = '0001_deploy_math' dependencies = [] operations = [ migrations.DeployContract('Math'), ] compiled_contracts = { 'Math': MATH, } class MigrationB(migrations.Migration): migration_id = '0002_increment' dependencies = ['0001_deploy_math'] operations = [ migrations.TransactContract( contract_name='Math', method_name='increment', arguments=[3], contract_address=migrations.Address.defer(key='contract/Math'), timeout=30, ), ] compiled_contracts = { 'Math': MATH, } write_project_file('contracts/Math.sol', MATH['source']) write_project_file('migrations/__init__.py') migration_0001_path = os.path.join( project_dir, 'migrations', '0001_deploy_math.py', ) with open(migration_0001_path, 'w') as migration_0001: write_migration(migration_0001, MigrationA) migration_0002_path = os.path.join( project_dir, 'migrations', '0002_increment.py', ) with open(migration_0002_path, 'w') as migration_0002: write_migration(migration_0002, MigrationB) write_project_file('populus.ini', '[chain:local_a]') project = Project() # sanity assert len(project.migrations) == 2 with project.get_chain('local_a') as chain: chain.wait.for_unlock(chain.web3.eth.coinbase, timeout=30) project.config.set('chain:local_a', 'deploy_from', chain.web3.eth.coinbase) RegistrarFactory = get_registrar(web3=chain.web3) deploy_transaction_hash = RegistrarFactory.deploy() registrar_address = chain.wait.for_contract_address( deploy_transaction_hash, timeout=60) project.config.set('chain:local_a', 'registrar', registrar_address) project.write_config() runner = CliRunner() result = runner.invoke(main, ['migrate', 'local_a']) assert result.exit_code == 0, result.output + str(result.exception) with project.get_chain('local_a') as chain: registrar = chain.registrar math_address = registrar.call().getAddress('contract/Math') Math = chain.contract_factories.Math(address=math_address) assert Math.call().counter() == 3