def test_genesis_db(): k, v, k2, v2 = accounts() set_db() blk = blocks.genesis({v: utils.denoms.ether * 1}) db_store(blk) blk2 = blocks.genesis({v: utils.denoms.ether * 1}) blk3 = blocks.genesis() assert blk == blk2 assert blk != blk3 set_db() blk2 = blocks.genesis({v: utils.denoms.ether * 1}) blk3 = blocks.genesis() assert blk == blk2 assert blk != blk3
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_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 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 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_genesis_state_root(genesis_fixture): # https://ethereum.etherpad.mozilla.org/12 set_db() genesis = blocks.genesis() for k, v in blocks.GENESIS_INITIAL_ALLOC.items(): assert genesis.get_balance(k) == v assert genesis.state_root.encode( 'hex') == genesis_fixture['genesis_state_root']
def test_genesis_hash(genesis_fixture): set_db() genesis = blocks.genesis() """ YP: https://raw.githubusercontent.com/ethereum/latexpaper/master/Paper.tex 0256 , SHA3RLP(), 0160 , stateRoot, 0256 , 2**22 , 0, 0, 1000000, 0, 0, (), SHA3(42), (), () Where 0256 refers to the parent and state and transaction root hashes, a 256-bit hash which is all zeroes; 0160 refers to the coinbase address, a 160-bit hash which is all zeroes; 2**22 refers to the difficulty; 0 refers to the timestamp (the Unix epoch); () refers to the extradata and the sequences of both uncles and transactions, all empty. SHA3(42) refers to the SHA3 hash of a byte array of length one whose first and only byte is of value 42. SHA3RLP() values refer to the hashes of the transaction and uncle lists in RLP both empty. The proof-of-concept series include a development premine, making the state root hash some value stateRoot. The latest documentation should be consulted for the value of the state root. """ h256 = '\00' * 32 sr = genesis_fixture['genesis_state_root'].decode('hex') genesis_block_defaults = [ ["prevhash", "bin", h256], # h256() ["uncles_hash", "bin", utils.sha3(rlp.encode([]))], # sha3EmptyList ["coinbase", "addr", "0" * 40], # h160() ["state_root", "trie_root", sr], # stateRoot ["tx_list_root", "trie_root", trie.BLANK_ROOT], # h256() ["difficulty", "int", 2 ** 22], # c_genesisDifficulty ["number", "int", 0], # 0 ["min_gas_price", "int", 0], # 0 ["gas_limit", "int", 10 ** 6], # 10**6 for genesis ["gas_used", "int", 0], # 0 ["timestamp", "int", 0], # 0 ["extra_data", "bin", ""], # "" ["nonce", "bin", utils.sha3(chr(42))], # sha3(bytes(1, 42)); ] cpp_genesis_block = rlp.decode( genesis_fixture['genesis_rlp_hex'].decode('hex')) cpp_genesis_header = cpp_genesis_block[0] for i, (name, typ, genesis_default) in enumerate(genesis_block_defaults): assert utils.decoders[typ](cpp_genesis_header[i]) == genesis_default assert getattr(genesis, name) == genesis_default assert genesis.hex_hash() == genesis_fixture['genesis_hash'] assert genesis.hex_hash() == utils.sha3( genesis_fixture['genesis_rlp_hex'].decode('hex') ).encode('hex')
def test_genesis_hash(genesis_fixture): set_db() genesis = blocks.genesis() """ YP: https://raw.githubusercontent.com/ethereum/latexpaper/master/Paper.tex 0256 , SHA3RLP(), 0160 , stateRoot, 0256 , 2**22 , 0, 0, 1000000, 0, 0, (), SHA3(42), (), () Where 0256 refers to the parent and state and transaction root hashes, a 256-bit hash which is all zeroes; 0160 refers to the coinbase address, a 160-bit hash which is all zeroes; 2**22 refers to the difficulty; 0 refers to the timestamp (the Unix epoch); () refers to the extradata and the sequences of both uncles and transactions, all empty. SHA3(42) refers to the SHA3 hash of a byte array of length one whose first and only byte is of value 42. SHA3RLP() values refer to the hashes of the transaction and uncle lists in RLP both empty. The proof-of-concept series include a development premine, making the state root hash some value stateRoot. The latest documentation should be consulted for the value of the state root. """ h256 = '\00' * 32 sr = genesis_fixture['genesis_state_root'].decode('hex') genesis_block_defaults = [ ["prevhash", "bin", h256], # h256() ["uncles_hash", "bin", utils.sha3(rlp.encode([]))], # sha3EmptyList ["coinbase", "addr", "0" * 40], # h160() ["state_root", "trie_root", sr], # stateRoot ["tx_list_root", "trie_root", trie.BLANK_ROOT], # h256() ["difficulty", "int", 2**22], # c_genesisDifficulty ["number", "int", 0], # 0 ["min_gas_price", "int", 0], # 0 ["gas_limit", "int", 10**6], # 10**6 for genesis ["gas_used", "int", 0], # 0 ["timestamp", "int", 0], # 0 ["extra_data", "bin", ""], # "" ["nonce", "bin", utils.sha3(chr(42))], # sha3(bytes(1, 42)); ] cpp_genesis_block = rlp.decode( genesis_fixture['genesis_rlp_hex'].decode('hex')) cpp_genesis_header = cpp_genesis_block[0] for i, (name, typ, genesis_default) in enumerate(genesis_block_defaults): assert utils.decoders[typ](cpp_genesis_header[i]) == genesis_default assert getattr(genesis, name) == genesis_default assert genesis.hex_hash() == genesis_fixture['genesis_hash'] assert genesis.hex_hash() == utils.sha3( genesis_fixture['genesis_rlp_hex'].decode('hex')).encode('hex')
def test_transfer(): k, v, k2, v2 = accounts() blk = blocks.genesis({v: utils.denoms.ether * 1}) b_v = blk.get_balance(v) b_v2 = blk.get_balance(v2) value = 42 success = blk.transfer_value(v, v2, value) assert success assert blk.get_balance(v) == b_v - value assert blk.get_balance(v2) == b_v2 + value
def test_failing_transfer(): k, v, k2, v2 = accounts() blk = blocks.genesis({v: utils.denoms.ether * 1}) b_v = blk.get_balance(v) b_v2 = blk.get_balance(v2) value = utils.denoms.ether * 2 # should fail success = blk.transfer_value(v, v2, value) assert not success assert blk.get_balance(v) == b_v assert blk.get_balance(v2) == b_v2
def test_genesis(): k, v, k2, v2 = accounts() set_db() blk = blocks.genesis({v: utils.denoms.ether * 1}) sr = blk.state_root db = DB(utils.get_db_path()) assert blk.state.db.db == db.db db.put(blk.hash, blk.serialize()) blk.state.db.commit() assert sr in db db.commit() assert sr in db blk2 = blocks.genesis({v: utils.denoms.ether * 1}) blk3 = blocks.genesis() assert blk == blk2 assert blk != blk3 set_db() blk2 = blocks.genesis({v: utils.denoms.ether * 1}) blk3 = blocks.genesis() assert blk == blk2 assert blk != blk3
def test_gas_deduction(): k, v, k2, v2 = accounts() blk = blocks.genesis({v: utils.denoms.ether * 1}) v_old_balance = blk.get_balance(v) assert blk.get_balance(blk.coinbase) == 0 gasprice = 1 startgas = 10000 code1 = serpent.compile(namecoin_code) tx1 = transactions.contract(0, gasprice, startgas, 0, code1).sign(k) success, addr = processblock.apply_transaction(blk, tx1) assert success assert blk.coinbase != v assert v_old_balance > blk.get_balance(v) assert v_old_balance == blk.get_balance(v) + blk.get_balance(blk.coinbase) intrinsic_gas_used = processblock.GTXCOST + \ processblock.GTXDATA * len(tx1.data) assert v_old_balance - blk.get_balance(v) >= intrinsic_gas_used * gasprice
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 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 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 mkquickgenesis(initial_alloc={}): "set INITIAL_DIFFICULTY to a value that is quickly minable" return blocks.genesis(initial_alloc, difficulty=2 ** 16)
def mkgenesis(initial_alloc={}): return blocks.genesis(initial_alloc)
def mkquickgenesis(initial_alloc={}): "set INITIAL_DIFFICULTY to a value that is quickly minable" return blocks.genesis(initial_alloc, difficulty=2**16)
def step_impl(context): context.key = utils.sha3('cows') context.addr = utils.privtoaddr(context.key) context.gen = blocks.genesis({context.addr: 10**60})