Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

enable test_solidity.py #865

Merged
merged 1 commit into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,50 @@

from ethereum.utils import encode_hex

from ethereum import tester
from ethereum.tools import tester
from ethereum import utils
from ethereum import _solidity
from ethereum._solidity import get_solidity
import ethereum.config as config
from ethereum.tools import _solidity
from ethereum.tools._solidity import get_solidity

SOLIDITY_AVAILABLE = get_solidity() is not None
CONTRACTS_DIR = path.join(path.dirname(__file__), 'contracts')

skip_if_no_solidity = pytest.mark.skipif(
not SOLIDITY_AVAILABLE,
reason='solc compiler not available')

def bytecode_is_generated(cinfo, cname):
return 'code' in cinfo[cname] and len(cinfo[cname]['code']) > 10


@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
reason='solc compiler not available')
def test_library_from_file():
state = tester.state()
state.env.config['HOMESTEAD_FORK_BLKNUM'] = 0 # enable CALLCODE opcode

library = state.abi_contract(
None,
path=path.join(CONTRACTS_DIR, 'seven_library.sol'),
language='solidity',
)

libraries = {
'SevenLibrary': encode_hex(library.address),
}
contract = state.abi_contract(
None,
path=path.join(CONTRACTS_DIR, 'seven_contract.sol'),
libraries=libraries,
language='solidity',
)

# pylint: disable=no-member
assert library.seven() == 7
assert contract.test() == 7


@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
reason='solc compiler not available')
@skip_if_no_solidity
def test_library_from_code():
with open(path.join(CONTRACTS_DIR, 'seven_library.sol')) as handler:
library_code = handler.read()

with open(path.join(CONTRACTS_DIR, 'seven_contract_without_import.sol')) as handler:
contract_code = handler.read()

state = tester.state()
state.env.config['HOMESTEAD_FORK_BLKNUM'] = 0 # enable CALLCODE opcode
state = tester.Chain()
env = config.Env()
env.config['HOMESTEAD_FORK_BLKNUM'] = 0 # enable CALLCODE opcode

library = state.abi_contract(
library_code,
path=None,
library = state.contract(
sourcecode=library_code,
language='solidity',
)

libraries = {
'SevenLibrary': encode_hex(library.address),
}
contract = state.abi_contract(
contract_code,
path=None,
contract = state.contract(
sourcecode=contract_code,
libraries=libraries,
language='solidity',
)

# pylint: disable=no-member
assert library.seven() == 7
assert contract.test() == 7


Expand Down Expand Up @@ -129,17 +103,16 @@ def test_symbols():
) == 'beef1111111111111111111111111111111111111111cafe'


@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
reason='solc compiler not available')
@skip_if_no_solidity
def test_interop():
serpent_contract = """
extern solidity: [sub2:[]:i]
extern solidity: [sub2:[]:i]

def main(a):
return(a.sub2() * 2)
def main(a):
return(a.sub2() * 2)

def sub1():
return(5)
def sub1():
return(5)
"""

solidity_contract = """
Expand All @@ -158,9 +131,13 @@ def sub1():
}
"""

state = tester.state()
serpent_abi = state.abi_contract(serpent_contract)
solidity_abi = state.abi_contract(
state = tester.Chain()

serpent_abi = state.contract(
serpent_contract,
language='serpent')

solidity_abi = state.contract(
solidity_contract,
language='solidity') # should be zoo
solidity_address = utils.encode_hex(solidity_abi.address)
Expand All @@ -171,12 +148,11 @@ def sub1():

assert solidity_abi.sub2() == 7
assert solidity_abi.sub3(utils.encode_hex(
solidity_abi.address)) == solidity_address
solidity_abi.address)) == '0x' + solidity_address
assert solidity_abi.main(serpent_abi.address) == 10


@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
reason='solc compiler not available')
@skip_if_no_solidity
def test_constructor():
constructor_contract = """
contract testme {
Expand All @@ -190,19 +166,19 @@ def test_constructor():
}
"""

state = tester.state()
contract = state.abi_contract(
state = tester.Chain()

contract = state.contract(
constructor_contract,
language='solidity',
constructor_parameters=(2, ),
args=(2, ),
)

# pylint: disable=no-member
assert contract.getValue() == 2


@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
reason='solc compiler not available')
@skip_if_no_solidity
def test_solidity_compile_rich():
compile_rich_contract = """
contract contract_add {
Expand Down Expand Up @@ -239,8 +215,7 @@ def test_solidity_compile_rich():
} == {'subtract7', 'subtract42'}


@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
reason='solc compiler not available')
@skip_if_no_solidity
def test_abi_contract():
one_contract = """
contract foo {
Expand All @@ -253,17 +228,16 @@ def test_abi_contract():
}
"""

state = tester.state()
contract = state.abi_contract(one_contract, language='solidity')
state = tester.Chain()
contract = state.contract(one_contract, language='solidity')

# pylint: disable=no-member
assert contract.seven() == 7
assert contract.mul2(2) == 4
assert contract.mul2(-2) == -4


@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
reason='solc compiler not available')
@skip_if_no_solidity
def test_extra_args():
src = """
contract foo {
Expand Down
10 changes: 9 additions & 1 deletion ethereum/tools/_solidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ def solc_arguments(libraries=None, combined='bin,abi',
'--combined-json', combined,
]

def str_of(address):
"""cast address to string. py2/3 compatability. """
try:
return address.decode('utf8')
except AttributeError:
return address


if optimize:
args.append('--optimize')

Expand All @@ -70,7 +78,7 @@ def solc_arguments(libraries=None, combined='bin,abi',
if libraries is not None and len(libraries):
addresses = [
'{name}:{address}'.format(
name=name, address=address.decode('utf8'))
name=name, address=str_of(address))
for name, address in libraries.items()
]
args.extend([
Expand Down
5 changes: 3 additions & 2 deletions ethereum/tools/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def last_gas_used(self, with_tx=False):
self.head_state.receipts[-2].gas_used
return diff - (not with_tx) * self.last_tx.intrinsic_gas_used

def contract(self, sourcecode, args=[], sender=k0, value=0,
def contract(self, sourcecode, args=[], sender=k0, value=0, libraries=None,
language=None, l=None, startgas=STARTGAS, gasprice=GASPRICE):
assert not (l and language)
language = l or language
Expand All @@ -238,7 +238,8 @@ def contract(self, sourcecode, args=[], sender=k0, value=0,
interface = compiler.mk_full_signature(sourcecode)
ct = ContractTranslator(interface)
code = compiler.compile(
sourcecode) + (ct.encode_constructor_arguments(args) if args else b'')
sourcecode, libraries=libraries
) + (ct.encode_constructor_arguments(args) if args else b'')
addr = self.tx(
sender=sender,
to=b'',
Expand Down