def reset_to_genesis(self): from ethereum.tools import tester self.evm = tester.Chain() self.evm = tester.Chain(alloc={ account: {'balance': to_wei(1000000, 'ether')} for account in tester.accounts }) self.evm.extra_accounts = {}
def t(): tester.s = tester.Chain( {account: { 'balance': 10**30 } for account in tester.accounts}) return tester
def setUp(self): chain = tester.Chain() bn256g2 = chain.contract(open('BN256G2.sol').read().replace( 'internal', 'public'), language='solidity') self.contractJ = contractWrapperJ(bn256g2) self.contract = contractWrapper(bn256g2)
def init_chain_and_casper(): genesis = casper_utils.make_casper_genesis(ALLOC, EPOCH_LENGTH, 100, 0.02, 0.002) t = tester.Chain(genesis=genesis) casper = tester.ABIContract(t, casper_utils.casper_abi, t.chain.config['CASPER_ADDRESS']) return t, casper
def test_chain(alloc={}, genesis_gas_limit=9999999, min_gas_limit=5000, startgas=3141592): # alloc alloc[tester.a0] = {'balance': 100000 * utils.denoms.ether} for i in range(9): alloc[utils.int_to_addr(i)] = {'balance': 1} # genesis header = { "number": 0, "gas_limit": genesis_gas_limit, "gas_used": 0, "timestamp": 1467446877, "difficulty": 1, "uncles_hash": '0x' + utils.encode_hex(utils.sha3(rlp.encode([]))) } genesis = mk_basic_state(alloc, header, tester.get_env(None)) # tester tester.languages['viper'] = compiler.Compiler() tester.STARTGAS = startgas chain = tester.Chain(alloc=alloc, genesis=genesis) chain.chain.env.config['MIN_GAS_LIMIT'] = min_gas_limit chain.mine(1) return chain
def chain(): # Create the simulated blockchain # s = tester.state() chain = tester.Chain() chain.mine() tester.gas_limit = 3141592 return chain
def setUp(self): config.config_metropolis['BLOCK_GAS_LIMIT'] = 2**60 self.chain = t.Chain(env=config.Env(config=config.config_metropolis)) self.chain.mine() self.verifier_contract = deploy_solidity_contract_with_args( self.chain, { 'LibSubmarine.sol': { 'urls': ['contract/LibSubmarine.sol'] }, 'SafeMath.sol': { 'urls': ['contract/SafeMath.sol'] }, 'proveth/ProvethVerifier.sol': { 'urls': ['contract/proveth/ProvethVerifier.sol'] }, 'proveth/RLP.sol': { 'urls': ['contract/proveth/RLP.sol'] } }, os.path.abspath(os.getcwd()), 'LibSubmarine.sol', 'LibSubmarine', 10**7, args=[REVEAL_DEPOSIT, CHALLENGE_PERIOD_LENGTH])
def test_returnten(): c = tester.Chain() open(filename, 'w').write(mul2_code) x = c.contract(returnten_code, language='serpent') o1 = c.tx(tester.k0, x.address, 0) os.remove(filename) assert utils.big_endian_to_int(o1) == 10
def test_with(): c = tester.Chain() x = c.contract(with_code, language='serpent') assert x.f1() == [5, 7, 8, 5] assert x.f2() == 2 assert x.f3() == 7 assert x.f4() == [5, 7, 5, 5]
def test_abi_logging(): c = tester.Chain() x = c.contract(abi_logging_code, language='serpent') o = [] c.head_state.log_listeners.append( lambda f: o.append(x.translator.listen(f))) x.test_rabbit(3) assert o == [{"_event_type": b"rabbit", "x": 3}] o.pop() x.test_frog(5) assert o == [{"_event_type": b"frog", "y": 5}] o.pop() x.test_moose(7, b"nine", 11, [13, 15, 17]) assert o == [{ "_event_type": b"moose", "a": 7, "b": b"nine", "c": 11, "d": [13, 15, 17] }] o.pop() x.test_chicken(tester.a0) assert o == [{ "_event_type": b"chicken", "m": "0x" + utils.encode_hex(tester.a0) }] o.pop()
def test_crowdfund(): c = tester.Chain() x = c.contract(crowdfund_code, language='serpent') # Create a campaign with id 100 x.create_campaign(100, 45, 100000, 2) # Create a campaign with id 200 x.create_campaign(200, 48, 100000, 2) # Make some contributions x.contribute(100, value=1, sender=tester.k1) assert 1 == x.progress_report(100) x.contribute(200, value=30000, sender=tester.k2) x.contribute(100, value=59049, sender=tester.k3) assert 59050 == x.progress_report(100) x.contribute(200, value=70001, sender=tester.k4) # Expect the 100001 units to be delivered to the destination # account for campaign 2 assert 100001 == c.head_state.get_balance(utils.int_to_addr(48)) mida1 = c.head_state.get_balance(tester.a1) mida3 = c.head_state.get_balance(tester.a3) # Mine 5 blocks to expire the campaign c.mine(5) # Ping the campaign after expiry x.contribute(100, value=1) # Expect refunds assert mida1 + 1 == c.head_state.get_balance(tester.a1) assert mida3 + 59049 == c.head_state.get_balance(tester.a3)
def identiy_tester(): tester.s = tester.Chain() from viper import compiler tester.languages['viper'] = compiler.Compiler() contract_code = open('contracts/identity/erc_735.v.py').read() tester.c = tester.s.contract(contract_code, language='viper', args=[]) return tester
def test_sort(): c = tester.Chain() x = c.contract(sort_code, language='serpent') assert x.sort([9]) == [9] assert x.sort([9, 5]) == [5, 9] assert x.sort([9, 3, 5]) == [3, 5, 9] assert x.sort([80, 234, 112, 112, 29]) == [29, 80, 112, 112, 234]
def setUp(self): config.config_metropolis['BLOCK_GAS_LIMIT'] = 2**60 self.chain = t.Chain(env=config.Env(config=config.config_metropolis)) self.chain.mine() contract_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), '..')) self.verifier_contract = deploy_solidity_contract( self.chain, { 'ProvethVerifier.sol': { 'urls': [os.path.join(contract_dir, 'ProvethVerifier.sol')] }, 'RLP.sol': { 'urls': [os.path.join(contract_dir, 'RLP.sol')] }, 'ExposedProvethVerifier.sol': { 'urls': [os.path.join(contract_dir, 'ExposedProvethVerifier.sol')] }, }, contract_dir, 'ExposedProvethVerifier.sol', 'ExposedProvethVerifier', 10**7, ) self.rpc_cache = {}
def test_callcode(): c = tester.Chain() open(filename3, 'w').write(add1_code) x = c.contract(callcode_test_code, language='serpent') c.mine(1) o1 = c.tx(tester.k0, x.address, 0) os.remove(filename3) assert utils.big_endian_to_int(o1) == 64
def test_reverter(): c = tester.Chain() x = c.contract(reverter_code, value=10**15, language='serpent') x.entry() assert c.head_state.get_storage_data(x.address, 8080) == 4040 assert c.head_state.get_balance(utils.int_to_addr(5050)) == 9 assert c.head_state.get_storage_data(x.address, 8081) == 0 assert c.head_state.get_balance(utils.int_to_addr(5051)) == 0
def ethtester(): tester.chain = tester.Chain() tester.accounts = [] for i in range(10): address = getattr(tester, 'a{0}'.format(i)) key = getattr(tester, 'k{0}'.format(i)) tester.accounts.append(EthereumAccount(address_to_hex(address), key)) return tester
def test_abi_address_output(): c = tester.Chain() x = c.contract(abi_address_output_test_code, language='serpent') x.register(123, b'1212121212121212121212121212121212121212') x.register(123, b'3434343434343434343434343434343434343434') x.register(125, b'5656565656565656565656565656565656565656') assert x.get_address(123) == '0x1212121212121212121212121212121212121212' assert x.get_address(125) == '0x5656565656565656565656565656565656565656'
def test_saveload2(): c = tester.Chain() x = c.contract(saveload_code2, language='serpent') c.tx(tester.k0, x.address, 0) assert bitcoin.encode(c.head_state.get_storage_data(x.address, 0), 256) == b'01ab' + b'\x00' * 28 assert bitcoin.encode(c.head_state.get_storage_data(x.address, 1), 256) == b'01ab' + b'\x00' * 28
def test_inner_abi_address_output(): c = tester.Chain() open(filename5, 'w').write(abi_address_output_test_code) x = c.contract(abi_address_caller_code, language='serpent') x.register(123, b'1212121212121212121212121212121212121212') x.register(123, b'3434343434343434343434343434343434343434') x.register(125, b'5656565656565656565656565656565656565656') assert x.get_address(123) == '0x1212121212121212121212121212121212121212' assert x.get_address(125) == '0x5656565656565656565656565656565656565656'
def test_type_system_fails(): c = tester.Chain() success7 = False try: c.contract(fail7, language='serpent') except Exception as e: success7 = "Please specify maximum" in str(e) assert success7
def test_get_blockhashes_from_hash(): test_chain = tester.Chain() test_chain.mine(5) blockhashes = test_chain.chain.get_blockhashes_from_hash( test_chain.chain.get_block_by_number(5).hash, 2, ) assert len(blockhashes) == 2
def test_evm(): evm_code = serpent.compile(serpent_code) translator = abi.ContractTranslator( serpent.mk_full_signature(serpent_code)) data = translator.encode('main', [2, 5]) c = tester.Chain() x = c.contract(evm_code, l='evm') o = translator.decode('main', c.tx(tester.k0, x, 0, data)) assert o == [32]
def setUp(self): self.chain = t.Chain() self.chain.mine(number_of_blocks=2) with open(AUCTION_CONTRACT, 'r') as f: code = f.read() self.auction_contract = self.chain.contract( code, language='solidity', value=0, startgas=10**6)
def test_saveload(): c = tester.Chain() x = c.contract(saveload_code, language='serpent') o = x.kall() assert o[0] == 0x73697220626f62616c6f7420746f207468652072657363756520212131213121, bitcoin.encode(o[0], 16) assert o[1] == 0x2131213100000000000000000000000000000000000000000000000000000000, bitcoin.encode(o[1], 16) assert o[2] == 0x73697220626f62616c6f7420746f207468652072657363756520212131213121, bitcoin.encode(o[2], 16) assert o[3] == 0x2131213100000000000000000000000000000000000000000000000000000000, bitcoin.encode(o[3], 16) assert o[4] == 0x73697220626f62616c6f7420746f207468652072657363756520212131213121, bitcoin.encode(o[4], 16) assert o[5] == 0x2100000000000000000000000000000000000000000000000000000000000000, bitcoin.encode(o[5], 16)
def test_ripemd160(): c = tester.Chain() x = c.contract(ripemd160_code, language='serpent') assert x.main() == [ 0x9c1185a5c5e9fc54612808977ee8f548b2258d31, 0x44d90e2d3714c8663b632fcf0f9d5f22192cc4c8, 0x2a5756a3da3bc6e4c66a65028f43d31a1290bb75, 0x2a5756a3da3bc6e4c66a65028f43d31a1290bb75, 0x9164cab7f680fd7a790080f2e76e049811074349, 0x9164cab7f680fd7a790080f2e76e049811074349]
def tester(): tester = t tester.s = t.Chain() tester.s.head_state.gas_limit = 10**9 tester.languages['viper'] = compiler.Compiler() contract_code = open('examples/stock/company.v.py').read() tester.company_address = t.a0 # Company with 1000 shares @ 10^6 wei / share tester.c = tester.s.contract(contract_code, language='viper', \ args=[tester.company_address, 1000, 10**6]) return tester
def test_currency(): c = tester.Chain() x = c.contract(currency_code, sender=tester.k0, language='serpent') o1 = x.send(tester.a2, 200) assert o1 == 1 o2 = x.send(tester.a2, 900) assert o2 == 0 o3 = x.query(tester.a0) assert o3 == 800 o4 = x.query(tester.a2) assert o4 == 200
def test_namecoin(): c = tester.Chain() x = c.contract(namecoin_code, language='serpent') o1 = x.main("george", 45) assert o1 == 1 o2 = x.main("george", 20) assert o2 == 0 o3 = x.main("harry", 60) assert o3 == 1 assert c.head_state.to_dict()
def test_calls(): c = tester.Chain() x = c.contract(calltest_code, language='serpent') x.main() assert 12345 == x.get(1) assert 23456 == x.get(2) assert 34567 == x.get(3) x.first(4, 5, 6, 7, 8) assert 45678 == x.get(1) x.second(5, 6, 7, 8, 9) assert 56789 == x.get(2)