def test_namecoin(): k, v, k2, v2 = accounts() blk = b.genesis({v: u.denoms.ether * 1}) code1 = serpent_compile(namecoin_code) tx1 = t.contract(0, gasprice, startgas, 0, code1).sign(k) s, addr = pb.apply_transaction(blk, tx1) snapshot = blk.snapshot() # tx2 tx2 = t.Transaction(1, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 45])) tx2.sign(k) s, o = pb.apply_transaction(blk, tx2) assert serpent.decode_datalist(o) == [1] # tx3 tx3 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 20])).sign(k) s, o = pb.apply_transaction(blk, tx3) assert serpent.decode_datalist(o) == [0] # tx4 tx4 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist(['harry', 60])).sign(k) s, o = pb.apply_transaction(blk, tx4) assert serpent.decode_datalist(o) == [1] blk.revert(snapshot) assert blk.to_dict() assert blk.to_dict()['state'][addr]['code']
def test_data_feeds(): k, v, k2, v2 = accounts() scode3 = ''' if !contract.storage[1000]: contract.storage[1000] = 1 contract.storage[1001] = msg.sender return(0) elif msg.sender == contract.storage[1001] and msg.datasize == 2: contract.storage[msg.data[0]] = msg.data[1] return(1) else: return(contract.storage[msg.data[0]]) ''' code3 = serpent_compile(scode3) logger.debug("AST", serpent.rewrite(serpent.parse(scode3))) blk = b.genesis({v: 10**18, v2: 10**18}) tx10 = t.contract(0, gasprice, startgas, 0, code3).sign(k) s, addr = pb.apply_transaction(blk, tx10) tx11 = t.Transaction(1, gasprice, startgas, addr, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx11) tx12 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_transaction(blk, tx12) assert serpent.decode_datalist(o) == [0] tx13 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist([500, 726])).sign(k) s, o = pb.apply_transaction(blk, tx13) assert serpent.decode_datalist(o) == [1] tx14 = t.Transaction(4, gasprice, startgas, addr, 0, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_transaction(blk, tx14) assert serpent.decode_datalist(o) == [726] return blk, addr
def step_impl(context): word = 'a5b2cc1f54' msg = context.msg = '\x00' * (32 - len(word)) + word tx = transactions.Transaction(1, 100, 10**40, context.contract, 0, msg).sign(context.key) success, context.ans = processblock.apply_transaction(context.gen, tx) assert (success)
def test_gas_price_calculation(): code = [ 'CALLER', 'PUSH1', 69, 'SSTORE', 'PUSH1', 42, 'DUP', 'PUSH1', 16, 'PUSH1', 0, 'CODECOPY', 'PUSH1', 0, 'RETURN', 'STOP', 'PUSH1', 69, 'SLOAD', 'CALLER', 'EQ', 'NOT', 'PUSH1', 42, 'JUMPI', 'CALLDATASIZE', 'PUSH1', 0, 'MLOAD', 'LT', 'NOT', 'PUSH1', 42, 'JUMPI', 'PUSH1', 32, 'PUSH1', 0, 'MLOAD', 'ADD', 'CALLDATALOAD', 'PUSH1', 0, 'MLOAD', 'CALLDATALOAD', 'SSTORE', 'PUSH1', 64, 'PUSH1', 0, 'MLOAD', 'ADD', 'PUSH1', 0, 'MSTORE', 'PUSH1', 9, 'JUMP' ] tx_dict = { 'nonce': 0L, 'startgas': 10000L, 'value': 0L, 'to': '0000000000000000000000000000000000000000', 's': 9504411864285061276187941232604806802531640604671611347567260576513458657555L, 'r': 23362574597211079051662254624411036839077618676481166419446762923566339937125L, 'v': 28L, 'data': '3`EW`*Q`\x10`\x009`\x00\xf2\x00`EV3\x0e\x0f`*Y6`\x00S\n\x0f`*Y` `\x00S\x015`\x00S5W`@`\x00S\x01`\x00T`\tX', 'gasprice': 10000000000000L } assert serpent.compiler.serialize(code) == tx_dict['data'] set_db() block = genesis = blocks.genesis() tx = transactions.Transaction(**tx_dict) assert block.gas_used == 0 success, output = processblock.apply_transaction(block, tx) GAS_USED_CPP = 1001 # as seen for tx0 in block1 in protocol 17 cpp chain assert block.gas_used == GAS_USED_CPP
def test_currency(): k, v, k2, v2 = accounts() scode2 = ''' if !contract.storage[1000]: contract.storage[1000] = 1 contract.storage[0x%s] = 1000 return(1) elif msg.datasize == 1: addr = msg.data[0] return(contract.storage[addr]) else: from = msg.sender fromvalue = contract.storage[from] to = msg.data[0] value = msg.data[1] if fromvalue >= value: contract.storage[from] = fromvalue - value contract.storage[to] = contract.storage[to] + value return(1) else: return(0) ''' % v code2 = serpent_compile(scode2) blk = b.genesis({v: 10**18}) tx4 = t.contract(0, gasprice, startgas, 0, code2).sign(k) s, addr = pb.apply_transaction(blk, tx4) tx5 = t.Transaction(1, gasprice, startgas, addr, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx5) assert serpent.decode_datalist(o) == [1] tx6 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist([v2, 200])).sign(k) s, o = pb.apply_transaction(blk, tx6) assert serpent.decode_datalist(o) == [1] tx7 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist([v2, 900])).sign(k) s, o = pb.apply_transaction(blk, tx7) assert serpent.decode_datalist(o) == [0] tx8 = t.Transaction(4, gasprice, startgas, addr, 0, serpent.encode_datalist([v])).sign(k) s, o = pb.apply_transaction(blk, tx8) assert serpent.decode_datalist(o) == [800] tx9 = t.Transaction(5, gasprice, startgas, addr, 0, serpent.encode_datalist([v2])).sign(k) s, o = pb.apply_transaction(blk, tx9) assert serpent.decode_datalist(o) == [200]
def step_impl(context): word = 342156 word = hex(word)[2:] if len(word) % 2 != 0: word = '0' + word msg = context.msg = '\x00' * (32 - len(word) / 2) + word.decode('hex') tx = transactions.Transaction(1, 100, 10**40, context.contract, 0, msg).sign(context.key) success, context.ans = processblock.apply_transaction(context.gen, tx) assert (success)
def get_transaction(gasprice=0, nonce=0): k, v, k2, v2 = accounts() tx = transactions.Transaction(nonce, gasprice, startgas=10000, to=v2, value=utils.denoms.finney * 10, data='').sign(k) return tx
def test_hedge(): k, v, k2, v2 = accounts() blk, addr = test_data_feeds() scode4 = ''' if !contract.storage[1000]: contract.storage[1000] = msg.sender contract.storage[1002] = msg.value contract.storage[1003] = msg.data[0] return(1) elif !contract.storage[1001]: ethvalue = contract.storage[1002] if msg.value >= ethvalue: contract.storage[1001] = msg.sender othervalue = ethvalue * call(0x%s,[contract.storage[1003]],1) contract.storage[1004] = othervalue contract.storage[1005] = block.timestamp + 86400 return([2,othervalue],2) else: othervalue = contract.storage[1004] ethvalue = othervalue / call(0x%s,contract.storage[1003]) if ethvalue >= contract.balance: send(contract.storage[1000],contract.balance) return(3) elif block.timestamp > contract.storage[1005]: send(contract.storage[1001],contract.balance - ethvalue) send(contract.storage[1000],ethvalue) return(4) else: return(5) ''' % (addr, addr) code4 = serpent_compile(scode4) logger.debug("AST", serpent.rewrite(serpent.parse(scode4))) # important: no new genesis block tx15 = t.contract(5, gasprice, startgas, 0, code4).sign(k) s, addr2 = pb.apply_transaction(blk, tx15) tx16 = t.Transaction(6, gasprice, startgas, addr2, 10**17, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_transaction(blk, tx16) assert serpent.decode_datalist(o) == [1] tx17 = t.Transaction(0, gasprice, startgas, addr2, 10**17, serpent.encode_datalist([500])).sign(k2) s, o = pb.apply_transaction(blk, tx17) assert serpent.decode_datalist(o) == [2, 72600000000000000000L] snapshot = blk.snapshot() tx18 = t.Transaction(7, gasprice, startgas, addr2, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx18) assert serpent.decode_datalist(o) == [5] tx19 = t.Transaction(8, gasprice, startgas, addr, 0, serpent.encode_datalist([500, 300])).sign(k) s, o = pb.apply_transaction(blk, tx19) assert serpent.decode_datalist(o) == [1] tx20 = t.Transaction(9, gasprice, startgas, addr2, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx20) assert serpent.decode_datalist(o) == [3] blk.revert(snapshot) blk.timestamp += 200000 tx21 = t.Transaction(7, gasprice, startgas, addr, 0, serpent.encode_datalist([500, 1452])).sign(k) s, o = pb.apply_transaction(blk, tx21) assert serpent.decode_datalist(o) == [1] tx22 = t.Transaction(8, gasprice, 2000, addr2, 0, '').sign(k) s, o = pb.apply_transaction(blk, tx22) assert serpent.decode_datalist(o) == [4]
def do_test_vm(name): tempdir = tempfile.mktemp() logger.debug('running test:%r', name) params = vm_tests_fixtures()[name] # HOTFIFX # params['pre']['0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6']['balance'] *= 100 pre = params['pre'] execs = params['exec'] callcreates = params['callcreates'] env = params['env'] post = params['post'] check_testdata(env.keys(), [ 'code', 'currentGasLimit', 'currentTimestamp', 'previousHash', 'currentCoinbase', 'currentDifficulty', 'currentNumber' ]) # setup env blk = blocks.Block(prevhash=env['previousHash'].decode('hex'), number=int(env['currentNumber']), coinbase=env['currentCoinbase'], difficulty=int(env['currentDifficulty']), gas_limit=int(env['currentGasLimit']), timestamp=env['currentTimestamp']) # code FIXME WHAT TO DO WITH THIS CODE??? # if isinstance(env['code'], str): # continue # else: # addr = 0 # FIXME # blk.set_code(addr, ''.join(map(chr, env['code']))) # setup state for address, h in pre.items(): check_testdata(h.keys(), ['code', 'nonce', 'balance', 'storage']) blk.set_balance(address, h['balance']) logger.debug('PRE Balance: %r: %r', address, h['balance']) blk._set_acct_item(address, 'nonce', h['nonce']) blk.set_code(address, ''.join(map(chr, h['code']))) assert h['storage'] == { } # FOR NOW test contracts don't seem to persist anything # execute transactions for i, exek in enumerate(execs): sender = exek['address'] # a party that originates a call recvaddr = exek['caller'] tx = transactions.Transaction(nonce=blk._get_acct_item( exek['caller'], 'nonce'), gasprice=int(exek['gasPrice']), startgas=int(exek['gas']), to=recvaddr, value=int(exek['value']), data=exek['data']) tx.sender = sender logger.debug('TX %r > %r v:%r gas:%s @price:%s', sender, recvaddr, tx.value, tx.startgas, tx.gasprice) # capture apply_message calls apply_message_calls = [] orig_apply_msg = processblock.apply_msg def apply_msg_wrapper(_block, _tx, msg): result, gas_remained, data = orig_apply_msg(_block, _tx, msg) apply_message_calls.append( dict(msg=msg, result=result, gas_remained=gas_remained, data=data)) return result, gas_remained, data processblock.apply_msg = apply_msg_wrapper success, output = processblock.apply_transaction(blk, tx) processblock.apply_msg = orig_apply_msg assert success assert len(callcreates) == len(apply_message_calls) # check against callcreates for i, callcreate in enumerate(callcreates): amc = apply_message_calls[i] assert callcreate['data'] == amc['data'] assert callcreate['gasLimit'] == amc['gas_remained'] assert callcreate['value'] == amc['msg'].value # data and out not set in tests yet assert output == params['out'] assert not params['out'] assert not callcreates['data'] # check state for address, h in post.items(): check_testdata(h.keys(), ['code', 'nonce', 'balance', 'storage']) logger.debug('POST: %r %r', address, h['balance']) blk.get_balance(address) == h['balance'] blk._get_acct_item(address, 'nonce') == h['nonce'] map(ord, blk.get_code(recvaddr)) == h['code'] assert storage == { } # FOR NOW test contracts don't seem to persist anything