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_genesis_db(): k, v, k2, v2 = accounts() db = new_db() blk = blocks.genesis(db, {v: utils.denoms.ether * 1}) store_block(blk) blk2 = blocks.genesis(db, {v: utils.denoms.ether * 1}) blk3 = blocks.genesis(db) assert blk == blk2 assert blk != blk3 db = new_db() blk2 = blocks.genesis(db, {v: utils.denoms.ether * 1}) blk3 = blocks.genesis(db) assert blk == blk2 assert blk != blk3
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) # print("AST", serpent.rewrite(serpent.parse(scode3))) # print("Assembly", serpent.compile_to_assembly(scode3)) blk = b.genesis({v: 10**18, v2: 10**18}) tx10 = t.contract(0, gasprice, startgas, 0, code3).sign(k) s, addr = pb.apply_tx(blk, tx10) tx11 = t.Transaction(1, gasprice, startgas, addr, 0, '').sign(k) s, o = pb.apply_tx(blk, tx11) tx12 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist([500])).sign(k) s, o = pb.apply_tx(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_tx(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_tx(blk, tx14) assert serpent.decode_datalist(o) == [726] return blk, addr
def test_genesis(): k, v, k2, v2 = accounts() set_db() blk = blocks.genesis({v: utils.denoms.ether * 1}) db_store(blk) assert blk in set([blk]) assert blk == blocks.Block.deserialize(blk.serialize())
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 mkgenesis(*args, **kargs): "set INITIAL_DIFFICULTY to a value that is quickly minable" tmp = blocks.INITIAL_DIFFICULTY blocks.INITIAL_DIFFICULTY = 2**16 g = blocks.genesis(*args, **kargs) blocks.INITIAL_DIFFICULTY = tmp return g
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 test_deserialize_commit(): k, v, k2, v2 = accounts() db = new_db() blk = blocks.genesis(db) db.put(blk.hash, blk.serialize()) db.commit() assert blk == blocks.get_block(db, blk.hash)
def mkgenesis(*args, **kargs): "set INITIAL_DIFFICULTY to a value that is quickly minable" tmp = blocks.INITIAL_DIFFICULTY blocks.INITIAL_DIFFICULTY = 2 ** 16 g = blocks.genesis(*args, **kargs) blocks.INITIAL_DIFFICULTY = tmp return g
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_state_root(): # 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') == CPP_PoC5_GENESIS_STATE_ROOT_HEX_HASH
def test_genesis_hash(): set_db() genesis = blocks.genesis() """ cpp: https://github.com/ethereum/cpp-ethereum/libethereum/BlockInfo.cpp#L64 h256() << sha3EmptyList << h160() << stateRoot << h256() << c_genesisDifficulty << 0 << 0 << 1000000 << 0 << (uint)0 << string() << sha3(bytes(1, 42)); PoC5 etherpad: https://ethereum.etherpad.mozilla.org/11 Genesis block is: ( B32(0, 0, ...), B32(sha3(B())), B20(0, 0, ...), B32(stateRoot), B32(0, 0, ...), P(2^22), P(0), P(0), P(1000000), P(0), P(0) << B() << B32(sha3(B(42))) ) Genesis hash: 69a7356a245f9dc5b865475ada5ee4e89b18f93c06503a9db3b3630e88e9fb4e 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 = "\x00" * 32 sr = CPP_PoC5_GENESIS_STATE_ROOT_HEX_HASH.decode('hex') genisi_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", h256], # h256() ["difficulty", "int", 2**22], # c_genesisDifficulty ["number", "int", 0], # 0 ["min_gas_price", "int", 0], # 0 ["gas_limit", "int", 1000000], # 1000000 ["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(CPP_PoC5_GENESIS_HEX.decode('hex')) cpp_genesis_header = cpp_genesis_block[0] for i, (name, typ, genesis_default) in enumerate(genisi_block_defaults): # print name, repr(getattr(genesis, name)), repr(genesis_default) assert utils.decoders[typ](cpp_genesis_header[i]) == genesis_default assert getattr(genesis, name) == genesis_default assert genesis.hex_hash() == CPP_PoC5_GENESIS_HEX_HASH
def _recv_Status(self, data): #old_status(self, data) h = blocks.genesis().hash print('Status RECEIVED') head_hash = data[3] print "head_hash", head_hash.encode('hex') print "head difficulty", idec(data[2]) assert not len(collected_blocks) self.send_GetBlockHashes(head_hash, NUM_BLOCKS_PER_REQUEST)
def test_genesis_hash(): set_db() genesis = blocks.genesis() """ cpp: https://github.com/ethereum/cpp-ethereum/libethereum/BlockInfo.cpp#L64 h256() << sha3EmptyList << h160() << stateRoot << h256() << c_genesisDifficulty << 0 << 0 << 1000000 << 0 << (uint)0 << string() << sha3(bytes(1, 42)); PoC5 etherpad: https://ethereum.etherpad.mozilla.org/11 Genesis block is: ( B32(0, 0, ...), B32(sha3(B())), B20(0, 0, ...), B32(stateRoot), B32(0, 0, ...), P(2^22), P(0), P(0), P(1000000), P(0), P(0) << B() << B32(sha3(B(42))) ) Genesis hash: 69a7356a245f9dc5b865475ada5ee4e89b18f93c06503a9db3b3630e88e9fb4e 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 = "\x00" * 32 sr = CPP_PoC5_GENESIS_STATE_ROOT_HEX_HASH.decode('hex') genisi_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", h256], # h256() ["difficulty", "int", 2 ** 22], # c_genesisDifficulty ["number", "int", 0], # 0 ["min_gas_price", "int", 0], # 0 ["gas_limit", "int", 1000000], # 1000000 ["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(CPP_PoC5_GENESIS_HEX.decode('hex')) cpp_genesis_header = cpp_genesis_block[0] for i, (name, typ, genesis_default) in enumerate(genisi_block_defaults): # print name, repr(getattr(genesis, name)), repr(genesis_default) assert utils.decoders[typ](cpp_genesis_header[i]) == genesis_default assert getattr(genesis, name) == genesis_default assert genesis.hex_hash() == CPP_PoC5_GENESIS_HEX_HASH
def test_genesis_hash(genesis_fixture): """ py current: 7e2c3861f556686d7bc3ce4e93fa0011020868dc769838aca66bcc82010a2c60 fixtures 15.10.:f68067286ddb7245c2203b18135456de1fc4ed6a24a2d9014195faa7900025bf py poc6: 08436a4d33c77e6acf013e586a3333ad152f25d31df8b68749d85046810e1f4b fixtures 19.9,: 08436a4d33c77e6acf013e586a3333ad152f25d31df8b68749d85046810e1f4b """ genesis = blocks.genesis(new_db()) assert genesis.hex_hash() == genesis_fixture['genesis_hash']
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_block_serialization_other_db(): k, v, k2, v2 = accounts() # mine two blocks set_db() a_blk = blocks.genesis() db_store(a_blk) a_blk2 = mine_next_block(a_blk) db_store(a_blk2) # receive in other db set_db() b_blk = blocks.genesis() assert b_blk == a_blk db_store(b_blk) b_blk2 = b_blk.deserialize(a_blk2.serialize()) assert a_blk2.hex_hash() == b_blk2.hex_hash() db_store(b_blk2) assert a_blk2.hex_hash() == b_blk2.hex_hash()
def test_transaction(): k, v, k2, v2 = accounts() set_db() blk = blocks.genesis({v: utils.denoms.ether * 1}) tx = get_transaction() assert not tx in blk.get_transactions() success, res = processblock.apply_tx(blk, tx) assert tx in blk.get_transactions() assert blk.get_balance(v) == utils.denoms.finney * 990 assert blk.get_balance(v2) == utils.denoms.finney * 10
def test_mine_block(): k, v, k2, v2 = accounts() set_db() blk = blocks.genesis({v: utils.denoms.ether * 1}) db_store(blk) blk2 = mine_next_block(blk, coinbase=v) db_store(blk2) assert blk2.get_balance(v) == blocks.BLOCK_REWARD + blk.get_balance(v) assert blk.state.db.db == blk2.state.db.db assert blk2.get_parent() == blk
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_genesis(): k, v, k2, v2 = accounts() db = new_db() blk = blocks.genesis(db, {v: utils.denoms.ether * 1}) sr = blk.state_root 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(db, {v: utils.denoms.ether * 1}) blk3 = blocks.genesis(db) assert blk == blk2 assert blk != blk3 db = new_db() blk2 = blocks.genesis(db, {v: utils.denoms.ether * 1}) blk3 = blocks.genesis(db) assert blk == blk2 assert blk != blk3
def _recv_Hello(self, data): old_hello(self, data) h = blocks.genesis().hash print('HELLO RECEIVED') head_hash = data[7] print "head_hash", head_hash.encode('hex') from peer import idec print "head difficulty", idec(data[6]) #self.send_GetBlocks([head_hash]) assert not len(collected_blocks) self.send_GetBlockHashes(head_hash, NUM_BLOCKS_PER_REQUEST)
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_block_serialization_with_transaction_other_db(): #k, v, k2, v2 = accounts() # mine two blocks set_db() a_blk = blocks.genesis() db_store(a_blk) tx = get_transaction() a_blk2 = mine_next_block(a_blk, transactions=[tx]) assert tx in a_blk2.get_transactions() db_store(a_blk2) # receive in other db set_db() b_blk = blocks.genesis() assert b_blk == a_blk db_store(b_blk) b_blk2 = b_blk.deserialize(a_blk2.serialize()) assert a_blk2.hex_hash() == b_blk2.hex_hash() assert tx in b_blk2.get_transactions() db_store(b_blk2) assert a_blk2.hex_hash() == b_blk2.hex_hash() assert tx in b_blk2.get_transactions()
def test_block_serialization_same_db(): k, v, k2, v2 = accounts() set_db() blk = blocks.genesis({v: utils.denoms.ether * 1}) assert blk.hex_hash() == \ blocks.Block.deserialize(blk.serialize()).hex_hash() db_store(blk) blk2 = mine_next_block(blk) assert blk.hex_hash() == \ blocks.Block.deserialize(blk.serialize()).hex_hash() assert blk2.hex_hash() == \ blocks.Block.deserialize(blk2.serialize()).hex_hash()
def test_mine_block_with_transaction(): k, v, k2, v2 = accounts() set_db() blk = blocks.genesis({v: utils.denoms.ether * 1}) db_store(blk) tx = get_transaction() blk2 = mine_next_block(blk, coinbase=v, transactions=[tx]) db_store(blk2) assert blocks.get_block(blk2.hash) == blk2 assert tx.gasprice == 0 assert blk2.get_balance( v) == blocks.BLOCK_REWARD + blk.get_balance(v) - tx.value assert blk.state.db.db == blk2.state.db.db assert blk2.get_parent() == blk assert tx in blk2.get_transactions() assert not tx in blk.get_transactions()
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_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_namecoin(): k, v, k2, v2 = accounts() blk = b.genesis({v: u.denoms.ether * 1}) scode1 = ''' if !contract.storage[msg.data[0]]: contract.storage[msg.data[0]] = msg.data[1] return(1) else: return(0) ''' code1 = serpent.compile(scode1) tx1 = t.contract(0, gasprice, startgas, 0, code1).sign(k) s, addr = pb.apply_tx(blk, tx1) snapshot = blk.snapshot() tx2 = t.Transaction(1, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 45])) tx2.sign(k) s, o = pb.apply_tx(blk, tx2) assert serpent.decode_datalist(o) == [1] tx3 = t.Transaction(2, gasprice, startgas, addr, 0, serpent.encode_datalist(['george', 20])).sign(k) s, o = pb.apply_tx(blk, tx3) assert serpent.decode_datalist(o) == [0] tx4 = t.Transaction(3, gasprice, startgas, addr, 0, serpent.encode_datalist(['harry', 60])).sign(k) s, o = pb.apply_tx(blk, tx4) assert serpent.decode_datalist(o) == [1]
h = n.encode('hex') else: h = "%02x" % n l = len(h) return "0" * (32 - l) + h nargs = pad32(1) d0 = pad32('hi') print nargs, d0 msg_hash = utils.sha3(nargs + d0) v, r, s = bitcoin.ecdsa_raw_sign(msg_hash, key) pubkey = bitcoin.privkey_to_pubkey(key) verified = bitcoin.ecdsa_raw_verify(msg_hash, (v, r, s), pubkey) gen = blocks.genesis({addr: 10**18}) print serpent.compile_to_assembly(open("DAOist frame.se").read()) DAOcode = serpent.compile(open("DAOist frame.se").read()) DAOcontract = transactions.contract(0, 1, 10**12, 100, DAOcode) DAOcontract.sign(key) success, contract_address = processblock.apply_tx(gen, DAOcontract) DCP = transactions.Transaction( 1, 10**12, 10000, contract_address, 0, serpent.encode_datalist([1, 1, v, r, s, 1, 'hi'])) DCP.sign(key)
def reset(self): self.genesis = blocks.genesis(self.founders) self.nonce = Counter()
def _recv_Hello(self, data): old_hello(self, data) h = blocks.genesis().hash print('HELLO RECEIVED')
def test_genesis_initial_alloc(genesis_fixture): genesis = blocks.genesis(new_db()) for k, v in blocks.GENESIS_INITIAL_ALLOC.items(): assert genesis.get_balance(k) == v
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 test_genesis_state_root(genesis_fixture): genesis = blocks.genesis(new_db()) assert genesis.state_root.encode( 'hex') == genesis_fixture['genesis_state_root']
def mkquickgenesis(initial_alloc={}): "set INITIAL_DIFFICULTY to a value that is quickly minable" return blocks.genesis(initial_alloc, difficulty=2 ** 16)
def __init__(self, name, genesis=None): self.private_key = utils.sha3(name) self.addr = utils.privtoaddr(self.private_key) self.genesis = genesis or blocks.genesis({self.addr: self.START_BALANCE})
root_key = utils.sha3('cow') root_addr = utils.privtoaddr(root_key) keys = {} for x in range(NUM_ACCOUNTS): key = utils.sha3(str(x+4)) addr = utils.privtoaddr(key) keys[addr] = key endowment = {root_addr: 10**18} for x in keys: endowment[x] = 10**18 genesis = blocks.genesis(endowment) tx1 = transactions.contract(0, 10**12, 100000, 0, root_code).sign(root_key) result, contract = processblock.apply_transaction(genesis, tx1) nonce=1 for address in keys: longitude = random.randint(1000, 110000) latitude = random.randint(1000, 110000) datalist = serpent.encode_datalist([0, longitude, latitude]) tx = transactions.Transaction(0, 10**12,
assert genesis.hex_hash() == genesis_fixture['genesis_hash'] if __name__ == '__main__': cpp_genesis_rlp_hex = 'f9012ff9012aa00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0c67c70f5d7d3049337d1dcc0503a249881120019a8e7322774dbfe57b463718ca056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b84000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830200008080830f4240808080a004994f67dc55b09e814ab7ffc8df3686b4afb2bb53e60eae97ef043fe03fb829c0c0' cpp_genesis_rlp = cpp_genesis_rlp_hex.decode('hex') poc7_genesis_hash_hex = '955f36d073ccb026b78ab3424c15cf966a7563aa270413859f78702b9e8e22cb' cpp_genesis = rlp.decode(cpp_genesis_rlp) cpp_genesis_hash_hex = utils.sha3(rlp.encode(cpp_genesis[0])).encode('hex') cpp_header = cpp_genesis[0] cpp_header_hex = [x.encode('hex') for x in cpp_header] py_genesis = rlp.decode(blocks.genesis().serialize()) py_genesis_hex_hash = blocks.genesis().hex_hash() py_header = py_genesis[0] py_header_hex = [x.encode('hex') for x in py_header] print 'py genesis hash hex', py_genesis_hex_hash print 'py state_root', py_header[blocks.block_structure_rev['state_root'] [0]].encode('hex') print 'py genesis rlp', blocks.genesis().hex_serialize() assert len(py_header_hex) == len(cpp_header_hex) assert cpp_genesis_hash_hex == poc7_genesis_hash_hex for i, e in enumerate(py_header_hex): print blocks.block_structure[i][0], repr(e) print blocks.block_structure[i][0], repr(cpp_header_hex[i]) print
def test_transient_block(): blk = blocks.genesis() tb_blk = blocks.TransientBlock(blk.serialize()) assert blk.hash == tb_blk.hash assert blk.number == tb_blk.number
def reset(self): self.genesis = blocks.genesis(self.founders) self.genesis.timestamp = 1388534400 # 2014-01-01 self.nonce = Counter()
#require command line args if len(sys.argv) < 3: print("Usage: %s [price] [recipient: transcation success: 2, transcation deny: 3] [sender: transcation success: 2, transcation deny: 3]" %sys.argv[0]) sys.exit(1) code = serpent.compile(escrow) sender_key = utils.sha3('sender') sender_addr = utils.privtoaddr(sender_key) recipient_key = utils.sha3('recipient') recipient_addr = utils.privtoaddr(recipient_key) host_key = utils.sha3('host') host_addr = utils.privtoaddr(host_key) #initialize the block genesis = blocks.genesis({sender_addr: 10**18, recipient_addr: 10**18, host_addr: 10**18}) #initialize the contract tx1 = transactions.contract(0, 10**12, 10000, 0, code).sign(host_key) result, contract = processblock.apply_transaction(genesis, tx1) #execute escrow transaction #nonce, gasprice, startgas, to, value, data price = int(sys.argv[1]) #user supplied price tx2 = transactions.Transaction(0, 10**12, 10000, contract, price, serpent.encode_datalist([1,recipient_addr])).sign(sender_key) result, ans = processblock.apply_transaction(genesis, tx2) tx3 = transactions.Transaction(0, 10**12, 10000, contract, 0, serpent.encode_datalist([int(sys.argv[2])])).sign(recipient_key) result, ans = processblock.apply_transaction(genesis, tx3) tx4 = transactions.Transaction(1, 10**12, 10000, contract, 0, serpent.encode_datalist([int(sys.argv[3])])).sign(sender_key) result, ans = processblock.apply_transaction(genesis, tx4)
def test_transient_block(): db = new_db() blk = blocks.genesis(db) tb_blk = blocks.TransientBlock(blk.serialize()) assert blk.hash == tb_blk.hash assert blk.number == tb_blk.number