def test_solc_installation_as_function_call(monkeypatch, tmpdir, platform, version): if get_platform() != platform: pytest.skip("Wront platform for install script") base_install_path = str(tmpdir.mkdir("temporary-dir")) monkeypatch.setenv('SOLC_BASE_INSTALL_PATH', base_install_path) # sanity check that it's not already installed. executable_path = get_executable_path(version) assert not os.path.exists(executable_path) install_solc(identifier=version, platform=platform) assert os.path.exists(executable_path) monkeypatch.setenv('SOLC_BINARY', executable_path) extract_path = get_extract_path(version) if os.path.exists(extract_path): contains_so_file = any( os.path.basename(path).partition(os.path.extsep)[2] == 'so' for path in os.listdir(extract_path) ) if contains_so_file: monkeypatch.setenv('LD_LIBRARY_PATH', extract_path) actual_version = get_solc_version() expected_version = semantic_version.Spec(version.lstrip('v')) assert actual_version in expected_version
def BAR_SOURCE(): solc_version = get_solc_version() if solc_version in {"0.4.1", "0.4.2", "0.4.4", "0.4.6"}: return b"pragma solidity ^0.4.0;\ncontract Bar { function Bar() {} }" else: raise AssertionError("Unsupported compiler version: {0}".format(solc_version))
def __init__(self, *args, **kwargs): if get_solc_version() not in Spec('>=0.4.11'): raise OSError( "The 'SolcStandardJSONBackend can only be used with solc " "versions >=0.4.11. The SolcCombinedJSONBackend should be used " "for all versions <=0.4.8") super(SolcStandardJSONBackend, self).__init__(*args, **kwargs)
def validate_solc(): if get_solc_version() is None: raise RuntimeError( "Couldn't find the solc in the current $PATH.\n" "Make sure the solidity compiler is installed and available on your $PATH.", ) try: compile_files( [get_contract_path('HumanStandardToken.sol')], 'HumanStandardToken', optimize=False, ) except subprocess.CalledProcessError as e: msg = ( 'The solidity compiler failed to execute. Please make sure that you\n' 'are using the binary version of the compiler (solc-js is not compatible)\n' 'and that the version is >= {}'.format(MIN_REQUIRED_SOLC)) if e.output: msg += ('\n' 'Output: ' + e.output) raise RuntimeError(msg) except OSError as e: msg = ( 'The solidity compiler failed to execute. Please make sure the\n' 'binary is compatible with your architecture and you can execute it.' ) child_traceback = getattr(e, 'child_traceback', None) if child_traceback: msg += ('\n' 'Traceback: ' + child_traceback) raise RuntimeError(msg)
def test_solc_supports_standard_json_interface(): version = get_solc_version() if version in semantic_version.Spec("<0.4.11"): assert not solc_supports_standard_json_interface() else: assert solc_supports_standard_json_interface()
def test_get_solc_version(): version = get_solc_version() assert version major, minor, patch = version.split('.') assert major.isdigit() assert minor.isdigit() assert patch.isdigit()
def get_compiled_registrar_contract(): if is_solc_03x(): compiled_contracts = compile_files([REGISTRAR_V3_SOURCE_PATH]) elif is_solc_04x(): compiled_contracts = compile_files([REGISTRAR_V4_SOURCE_PATH]) else: raise ValueError( "Unsupported version of solc. Found: {0}. Only 0.3.x and 0.4.x " "are supported".format(get_solc_version())) contract_data = compiled_contracts['Registrar'] return contract_data
def __init__(self, *args, **kwargs): if get_solc_version() not in Spec('<=0.4.8'): raise OSError( "The 'SolcCombinedJSONBackend can only be used with solc " "versions <=0.4.8. The SolcStandardJSONBackend should be used " "for all versions >=0.4.9") warn_msg = 'Support for solc <0.4.11 will be dropped in the next populus release' warnings.warn(warn_msg, DeprecationWarning) super(SolcCombinedJSONBackend, self).__init__(*args, **kwargs)
def test_get_solc_version(): raw_version_string = get_solc_version() version, _, commit_sha = raw_version_string.partition('-') assert version assert commit_sha major, minor, patch = version.split('.') assert major.isdigit() assert minor.isdigit() assert patch.isdigit()
def printVersions(): import sys from web3 import __version__ as web3version from solc import get_solc_version from testrpc import __version__ as ethtestrpcversion import pkg_resources pysolcversion = pkg_resources.get_distribution("py-solc").version print("versions: web3 %s, py-solc: %s, solc %s, testrpc %s, python %s" % (web3version, pysolcversion, get_solc_version(), ethtestrpcversion, sys.version.replace("\n", "")))
def get_compiled_registrar_contract(): if is_solc_03x(): compiled_contracts = compile_files([REGISTRAR_V3_SOURCE_PATH]) elif is_solc_04x(): compiled_contracts = compile_files([REGISTRAR_V4_SOURCE_PATH]) else: raise ValueError( "Unsupported version of solc. Found: {0}. Only 0.3.x and 0.4.x " "are supported".format(get_solc_version()) ) contract_data = compiled_contracts['Registrar'] return contract_data
def __init__(self, *args, **kwargs): if get_solc_version() not in Spec('<=0.4.8'): raise OSError( "The 'SolcCombinedJSONBackend can only be used with solc " "versions <=0.4.8. The SolcStandardJSONBackend should be used " "for all versions >=0.4.9" ) warn_msg = 'Support for solc <0.4.11 will be dropped in the next populus release' warnings.warn(warn_msg, DeprecationWarning) super(SolcCombinedJSONBackend, self).__init__(*args, **kwargs)
def get_compiled_registrar_contract(): if is_solc_03x(): registrar_source_path = REGISTRAR_V3_SOURCE_PATH elif is_solc_04x(): registrar_source_path = REGISTRAR_V4_SOURCE_PATH else: raise ValueError( "Unsupported version of solc. Found: {0}. Only 0.3.x and 0.4.x " "are supported".format(get_solc_version())) compiled_contracts = compile_files([registrar_source_path]) contract_data = compiled_contracts['Registrar'] normalized_data = normalize_contract_data(contract_data, {}) return normalized_data
def test_source_code_compilation(): SOURCE = "contract Foo { function Foo() {} function return13() returns (uint) { return 13; } }" output = compile_source(SOURCE) assert output assert 'Foo' in output foo_contract_data = output['Foo'] assert 'code' in foo_contract_data assert 'code_runtime' in foo_contract_data assert 'source' in foo_contract_data assert 'meta' in foo_contract_data assert 'compilerVersion' in foo_contract_data['meta'] # TODO: figure out how to include source. assert foo_contract_data['source'] is None assert foo_contract_data['meta']['compilerVersion'] == get_solc_version()
def test_source_code_compilation(): solc_version_string = get_solc_version_string() solc_version = get_solc_version() if solc_version in {"0.4.1", "0.4.2", "0.4.4", "0.4.6"}: SOURCE = "pragma solidity ^0.4.0;\ncontract Foo { function Foo() {} function return13() returns (uint) { return 13; } }" else: raise AssertionError( "Unsupported compiler version: {0}".format(solc_version)) output = compile_source(SOURCE, optimize=True) assert output assert 'Foo' in output foo_contract_data = output['Foo'] assert 'bin' in foo_contract_data assert 'bin-runtime' in foo_contract_data
def validate_solc(): if get_solc_version() is None: raise RuntimeError( "Couldn't find the solc in the current $PATH.\n" "Make sure the solidity compiler is installed and available on your $PATH.", ) try: compile_files( [CONTRACT_MANAGER.get_contract_path(CONTRACT_HUMAN_STANDARD_TOKEN)], 'HumanStandardToken', optimize=False, ) except subprocess.CalledProcessError as e: msg = ( 'The solidity compiler failed to execute. Please make sure that you\n' 'are using the binary version of the compiler (solc-js is not compatible)\n' 'and that the version is >= {}'.format(MIN_REQUIRED_SOLC) ) if e.output: msg += ( '\n' 'Output: ' + e.output ) raise RuntimeError(msg) except OSError as e: msg = ( 'The solidity compiler failed to execute. Please make sure the\n' 'binary is compatible with your architecture and you can execute it.' ) child_traceback = getattr(e, 'child_traceback', None) if child_traceback: msg += ( '\n' 'Traceback: ' + child_traceback ) raise RuntimeError(msg)
def test_source_files_compilation(contracts_dir): SOURCE = "contract Foo { function Foo() {} function return13() returns (uint) { return 13; } }" source_file_path = os.path.join(contracts_dir, 'Foo.sol') with open(source_file_path, 'w') as source_file: source_file.write(SOURCE) output = compile_files([source_file_path]) assert output assert 'Foo' in output foo_contract_data = output['Foo'] assert 'code' in foo_contract_data assert 'code_runtime' in foo_contract_data assert 'source' in foo_contract_data assert 'meta' in foo_contract_data assert 'compilerVersion' in foo_contract_data['meta'] # TODO: figure out how to include source. assert foo_contract_data['source'] is None assert foo_contract_data['meta']['compilerVersion'] == get_solc_version()
def test_source_files_compilation(contracts_dir): solc_version_string = get_solc_version_string() solc_version = get_solc_version() if solc_version in {"0.4.1", "0.4.2", "0.4.4", "0.4.6"}: SOURCE = "pragma solidity ^0.4.0;\ncontract Foo { function Foo() {} function return13() returns (uint) { return 13; } }" else: raise AssertionError( "Unsupported compiler version: {0}".format(solc_version)) source_file_path = os.path.join(contracts_dir, 'Foo.sol') with open(source_file_path, 'w') as source_file: source_file.write(SOURCE) output = compile_files([source_file_path], optimize=True) assert output assert 'Foo' in output foo_contract_data = output['Foo'] assert 'bin' in foo_contract_data assert 'bin-runtime' in foo_contract_data
def compile_project_contracts(project_dir, contracts_dir, compiler_settings=None): if compiler_settings is None: compiler_settings = {} compiler_settings.setdefault('output_values', DEFAULT_COMPILER_OUTPUT_VALUES) contract_source_paths = find_project_contracts(project_dir, contracts_dir) try: compiled_contracts = compile_files(contract_source_paths, **compiler_settings) except ContractsNotFound: return contract_source_paths, {} solc_version = get_solc_version() contract_meta = get_contract_meta(compiler_settings, solc_version) normalized_compiled_contracts = dict( process_compiler_output(contract_name, contract_data, contract_meta) for contract_name, contract_data in compiled_contracts.items()) return contract_source_paths, normalized_compiled_contracts
def compare_old_solc_version(self) -> None: """ Raise an error if solc version goes down. """ try: current_solc_version = get_solc_version() except FileNotFoundError as ex: raise ContractManagerCompilationError( 'Could not compile the contract. Check that solc is available.', ) from ex precompiled_path = contracts_precompiled_path(self.contracts_version) if not precompiled_path.exists(): warnings.warn( f'Skip comparing solc versions to make sure no downgrade happened.' f'No precompiled data file found in {precompiled_path}.', ) return precompiled_manager = ContractManager(precompiled_path) for contract_name in precompiled_manager.contracts: try: metadata = json.loads( precompiled_manager.contracts[contract_name]['metadata']) old_version = semantic_version.Version( metadata['compiler']['version']) if current_solc_version < old_version: raise ContractManagerCompilationError( f'Solc version went down. ' f'Current version is {current_solc_version}, ' f'old version was {old_version}. If you really want to do this,' f'delete the old precompiled data file from {precompiled_path}', ) except (JSONDecodeError, UnicodeDecodeError) as ex: warnings.warn( f'Comparing old solc version with new one. ' f'Cannot load precompiled smart contract metadata from {contract_name}. ' f'Do not panic, it probably does not exist. {ex}', )
def test_import_remapping(contracts_dir, is_new_key_format, BAR_SOURCE, BAZ_SOURCE): solc_version = get_solc_version() source_file_path = os.path.join(contracts_dir, 'Bar.sol') with open(source_file_path, 'w') as source_file: source_file.write(BAR_SOURCE) source_file_path = os.path.join(contracts_dir, 'Baz.sol') with open(source_file_path, 'w') as source_file: source_file.write(BAZ_SOURCE) output = compile_files( [source_file_path], import_remappings=["contracts={}".format(contracts_dir)]) assert output if is_new_key_format: contact_key = '{0}:Baz'.format(os.path.abspath(source_file_path)) else: contact_key = 'Baz' assert contact_key in output
def __init__(self, settings): proxy_backend_class = get_solc_backend_class_for_version(get_solc_version()) self.proxy_backend = proxy_backend_class(settings)
def solc_version(): return get_solc_version()
def is_new_key_format(): return get_solc_version() in Spec('>=0.4.9')
from hvm.rlp.receipts import ( Receipt, ) from solc import compile_source, compile_files, link_code, get_solc_version from eth_utils import to_int from hvm.utils.address import generate_contract_address from pathlib import Path home = str(Path.home()) os.environ["SOLC_BINARY"] = home + "/.py-solc/solc-v0.4.25/bin/solc" try: get_solc_version() except Exception: print("Solc not found. Installing") from solc import install_solc install_solc('v0.4.25') import pickle from web3 import Web3 from tests.integration_test_helpers import W3_TX_DEFAULTS from hvm.constants import CREATE_CONTRACT_ADDRESS #for testing purposes we will just have the primary private key hardcoded #TODO: save to encrypted json file
load_contract_fixture, load_test_contract_fixture, update_project_config, ) _populus_config_key_value_pairs = ( ('compilation.backend', {"$ref": "compilation.backends.SolcCombinedJSON"}), ) GREETER_SOURCE_PATH = os.path.join(ASSETS_DIR, 'Greeter.sol') pytestmark = pytest.mark.skipif( not get_solc_version() in Spec('<=0.4.8'), reason="Solc compiler not supported for combined json compilation", ) @load_contract_fixture('Math.sol') def test_compiling_project_contracts(project): source_paths, compiled_contracts = compile_project_contracts(project) assert 'contracts/Math.sol' in source_paths assert 'Math' in compiled_contracts contract_data = compiled_contracts['Math'] assert 'bytecode' in contract_data assert 'bytecode_runtime' in contract_data assert 'abi' in contract_data
def is_solc_04x(): version = get_solc_version() major, minor, _ = version.split('.')[:3] return major == '0' and minor == '4'
Requires solc verion 0.4.24 ''' import json import os import sys from typing import List from solc import compile_files, get_solc_version # pylint: disable=E0012 assert sys.version.startswith( '3.7'), 'Require python 3.7 or better' # isort:skip SOLC_VERSION = '0.4.24' solc_version = str(get_solc_version()) assert solc_version.startswith( SOLC_VERSION ), f'Solc version {SOLC_VERSION} ir required but got {solc_version}.' CTR_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'source')) JSN_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'json')) if not os.path.exists(JSN_DIR): os.mkdir(JSN_DIR) def compile_to_json(ctr_paths: List, out_dir: str, complete_out: bool) -> bool: ''' Compile the target sol file, split the respective json key segments into destination files corresponding to source file name. '''
def __init__(self, settings): proxy_backend_class = get_solc_backend_class_for_version( get_solc_version()) self.proxy_backend = proxy_backend_class(settings)
from populus.utils.testing import ( load_contract_fixture, load_test_contract_fixture, update_project_config, ) _populus_config_key_value_pairs = (('compilation.backend', { "$ref": "compilation.backends.SolcCombinedJSON" }), ) GREETER_SOURCE_PATH = os.path.join(ASSETS_DIR, 'Greeter.sol') pytestmark = pytest.mark.skipif( not get_solc_version() in Spec('<=0.4.8'), reason="Solc compiler not supported for combined json compilation", ) @load_contract_fixture('Math.sol') def test_compiling_project_contracts(project): source_paths, compiled_contracts = compile_project_contracts(project) assert 'contracts/Math.sol' in source_paths assert 'Math' in compiled_contracts contract_data = compiled_contracts['Math'] assert 'bytecode' in contract_data assert 'bytecode_runtime' in contract_data assert 'abi' in contract_data
def printVersions(): import sys from web3 import __version__ as web3version from solc import get_solc_version print("versions: web3 %s, solc %s, python %s" % (web3version, get_solc_version(), sys.version.replace("\n", "")))
def test_get_solc_version(): version = get_solc_version() assert isinstance(version, semantic_version.Version)