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_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 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_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 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_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_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_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_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 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_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_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_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 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_ripemd160(): c = tester.Chain() x = c.contract(ripemd160_code, language='serpent') assert x.main() == [ 0x9c1185a5c5e9fc54612808977ee8f548b2258d31, 0x44d90e2d3714c8663b632fcf0f9d5f22192cc4c8, 0x2a5756a3da3bc6e4c66a65028f43d31a1290bb75, 0x2a5756a3da3bc6e4c66a65028f43d31a1290bb75, 0x9164cab7f680fd7a790080f2e76e049811074349, 0x9164cab7f680fd7a790080f2e76e049811074349 ]
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)
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_data_feeds(): c = tester.Chain() x = c.contract(data_feed_code, sender=tester.k0, language='serpent') o2 = x.get(500) assert o2 == 0 o3 = x.set(500, 19) assert o3 == 1 o4 = x.get(500) assert o4 == 19 o5 = x.set(500, 726, sender=tester.k1) assert o5 == 0 o6 = x.set(500, 726) assert o6 == 1 return c, x
def test_infinite_storage_objects(): c = tester.Chain() x = c.contract(infinite_storage_object_test_code, language='serpent') x.ping() assert 1 == x.query_chessboard(0, 0) assert 2 == x.query_chessboard(0, 1) assert 3 == x.query_chessboard(3, 0) assert [100, 0, 0] == x.query_stats(0) assert [0, 15, 12] == x.query_stats(1) assert 0 == x.query_items(1, 3) assert 0 == x.query_items(0, 2) assert 9 == x.query_items(1, 2) assert [555, 556, 656, 559, 1659, 557, 0, 0, 0, 558, 657, 0, 0, 0, 658] == x.query_person()
def test_sha3(): c = tester.Chain() x = c.contract(sha3_code, language='serpent') assert x.main() == [ 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 - 2**256, 0xc2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b - 2**256, 0x41791102999c339c844880b23950704cc43aa840f3739e365323cda4dfa89e7a, 0x41791102999c339c844880b23950704cc43aa840f3739e365323cda4dfa89e7a, 0xdfded4ed5ac76ba7379cfe7b3b0f53e768dca8d45a34854e649cfc3c18cbd9cd - 2**256, 0xdfded4ed5ac76ba7379cfe7b3b0f53e768dca8d45a34854e649cfc3c18cbd9cd - 2**256 ]
def test_string_logging(): c = tester.Chain() x = c.contract(string_logging_code, language='serpent') o = [] c.head_state.log_listeners.append( lambda f: o.append(x.translator.listen(f))) x.moo() assert o == [{ "_event_type": b"foo", "x": b"bob", "__hash_x": utils.sha3(b"bob"), "y": b"cow", "__hash_y": utils.sha3(b"cow"), "z": b"dog", "__hash_z": utils.sha3(b"dog"), }]
def test_sha256(): c = tester.Chain() x = c.contract(sha256_code, language='serpent') assert x.main() == [ 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 - 2**256, 0xd9147961436944f43cd99d28b2bbddbf452ef872b30c8279e255e7daafc7f946 - 2**256, 0xcd6357efdd966de8c0cb2f876cc89ec74ce35f0968e11743987084bd42fb8944 - 2**256, 0xcd6357efdd966de8c0cb2f876cc89ec74ce35f0968e11743987084bd42fb8944 - 2**256, 0xb393978842a0fa3d3e1470196f098f473f9678e72463cb65ec4ab5581856c2e4 - 2**256, 0xb393978842a0fa3d3e1470196f098f473f9678e72463cb65ec4ab5581856c2e4 - 2**256 ]
def test_ecrecover(): c = tester.Chain() x = c.contract(ecrecover_code, language='serpent') priv = utils.sha3('some big long brainwallet password') pub = bitcoin.privtopub(priv) msghash = utils.sha3('the quick brown fox jumps over the lazy dog') V, R, S = utils.ecsign(msghash, priv) assert bitcoin.ecdsa_raw_verify(msghash, (V, R, S), pub) addr = utils.big_endian_to_int( utils.sha3(bitcoin.encode_pubkey(pub, 'bin')[1:])[12:]) assert utils.big_endian_to_int(utils.privtoaddr(priv)) == addr result = x.test_ecrecover(utils.big_endian_to_int(msghash), V, R, S) assert result == addr
def test_suicider(): c = tester.Chain() x = c.contract(suicider_code, language='serpent') # Run normally: suicide processes, so the attempt to ping the # contract fails x.entry(5, startgas=200000) o2 = x.ping_ten(startgas=200000) assert o2 is None x = c.contract(suicider_code, language='serpent') # Run the suicider in such a way that it suicides in a sub-call, # then runs out of gas, leading to a revert of the suicide and the # storage mutation x.entry(8000, startgas=200000) # Check that the suicide got reverted o2 = x.ping_ten(startgas=200000) assert o2 == 10 # Check that the storage op got reverted o3 = x.ping_storage15(startgas=200000) assert o3 == 20
def test_delegatecall(): c = tester.Chain() c.head_state.block_number = 5555555 x1 = c.contract(""" data v event Happy() def foo(): log(type=Happy) self.v += 1 return(self.v) """, language='serpent') x2 = c.contract(""" extern c1: [foo:[]:int256] data v data callee event Happeh() def set_callee(addr:address): self.callee = addr def bar(): log(type=Happeh) self.callee.foo(call=delegate) def baz(): return(self.v) """, language='serpent') x2.set_callee(x1.address) assert x2.baz() == 0 x2.bar() assert x2.baz() == 1 x2.bar() x2.bar() assert x2.baz() == 3
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_storagevar_fails(): c = tester.Chain() success1, success2, success3, success4, success5, success6 = \ 0, 0, 0, 0, 0, 0 try: c.contract(fail1, language='serpent') except Exception as e: success1 = "Storage variable access not deep enough" in str(e) assert success1 try: c.contract(fail2, language='serpent') except Exception as e: success2 = "Too few array index lookups" in str(e) assert success2 try: c.contract(fail3, language='serpent') except Exception as e: success3 = "Too many array index lookups" in str(e) assert success3 try: c.contract(fail4, language='serpent') except Exception as e: success4 = "Too few array index lookups" in str(e) assert success4 try: c.contract(fail5, language='serpent') except Exception as e: success5 = "Invalid object member" in str(e) assert success5 try: c.contract(fail6, language='serpent') except Exception as e: success6 = "Invalid object member" in str(e) assert success6
def __init__(self, test_string, epoch_length, withdrawal_delay, base_interest_factor, base_penalty_factor): if test_string == '': raise Exception("Please pass in a valid test string") self.test_string = test_string self.genesis = casper_utils.make_casper_genesis( ALLOC, epoch_length, withdrawal_delay, base_interest_factor, base_penalty_factor) self.t = tester.Chain(genesis=self.genesis) self.casper = tester.ABIContract( self.t, casper_utils.casper_abi, self.t.chain.env.config['CASPER_ADDRESS']) self.saved_blocks = dict() self.validators = dict() # Register token handlers self.handlers = dict() self.handlers['B'] = self.mine_blocks self.handlers['J'] = self.join self.handlers['P'] = self.prepare self.handlers['C'] = self.commit self.handlers['S'] = self.save_block self.handlers['R'] = self.revert_to_block self.handlers['H'] = self.check_head_equals_block self.handlers['X'] = self.slash
from biblecoin.tools import tester from biblecoin import opcodes from biblecoin.utils import int_to_big_endian, encode_int32, big_endian_to_int from biblecoin.tools import new_statetest_utils import json import py_pairing c = tester.Chain(env='metropolis') c.head_state.gas_limit = 10**8 kode = """ h: bytes32 def foo(x: bytes <= 192) -> bytes <= 64: o = raw_call(0x0000000000000000000000000000000000000006, x, gas=99999999, outsize=64) self.h = sha3(o) return o """ x1 = c.contract(kode, language='viper') def mk_ecadd_data(p1, p2): if isinstance(p1[0], py_pairing.FQ): p1 = py_pairing.normalize(p1) p1 = (p1[0].n, p1[1].n) if isinstance(p2[0], py_pairing.FQ): p2 = py_pairing.normalize(p2) p2 = (p2[0].n, p2[1].n) return encode_int32(p1[0]) + encode_int32(p1[1]) + \ encode_int32(p2[0]) + encode_int32(p2[1])