def test_transaction_hashable_fail(): test_time = int(time.time()) block_time = int(time.time()) transaction_1 = Transaction('block_hash_test', '', 'to_steven_test', 'from_matt_test', 1, test_time) transaction_2 = Transaction('block_hash_test2', '', 'to_matt_test', 'from_steven_test', 1, test_time) block = Block('_hash_test', '','', 'LAST_BLOCK', 0, [ transaction_1, transaction_2 ], [], [], [], [], [], block_time) actual = block.get_hashable() expected = { 'owner': '', 'prev_block': 'LAST_BLOCK', 'height': 0, 'signature': '', 'transactions': [ transaction_1.get_sendable(), transaction_2.get_sendable() ], 'pos_transactions': [], 'contract_transactions': [], 'contracts': [], 'signed_contracts': [], 'terminated_contracts': [], 'timestamp': block_time } assert actual == expected
def generate_from_priv(private_key): time_now = 1520135639.4713802 public = EcdsaHashing.recover_public_key_str(private_key) transactions = [ Transaction('', '', public, '', 1, 1, time_now), Transaction('', '', public, '', 10, 1, time_now), Transaction('', '', public, '', 100, 1, time_now), Transaction('', '', public, '', 1000, 1, time_now), ] for tranaction in transactions: tranaction.update_signature(private_key) tranaction.update_hash() pos_transactions = [PosTransaction('', '', public, 100, 1, time_now)] for pos_transaction in pos_transactions: pos_transaction.update_signature(private_key) pos_transaction.update_hash() block = Block('', '', public, '', 0, transactions, pos_transactions, [], [], [], [], time_now) block.update_signature(private_key) block.update_hash() return block
def test_store_same_hash(): b = Block('block_service_test_1', '', '', 'LAST_BLOCK', 0, [], [], [], [], [], []) bs = BlockService() bs.store_block(b) new_b = bs.find_by_hash('block_service_test_1') assert b.get_sendable() != new_b.get_sendable() r = redis.Redis() r.flushdb()
def test_hashable_fail(): test_time = int(time.time()) block = Block('block_hash_test', '', '', 'LAST_BLOCK', 0, [], [], [], [], [], [], test_time) actual = block.get_hashable() not_expected = { 'owner': '', 'prev_block': 'LAST_BLOCK', 'height': 1, 'signature': '', 'transactions': [], 'pos_transactions': [], 'contract_transactions': [], 'contracts': [], 'signed_contracts': [], 'terminated_contracts': [], 'timestamp': test_time } assert actual != not_expected
def test_sendable(): test_time = int(time.time()) block = Block('block_send_test', '', '', 'LAST_BLOCK', 0, [], [], [], [], [], [], test_time) actual = block.get_sendable() expected = { 'owner': '', 'prev_block': 'LAST_BLOCK', 'height': 0, 'signature': '', 'transactions': [], 'pos_transactions': [], 'contract_transactions': [], 'contracts': [], 'signed_contracts': [], 'terminated_contracts': [], 'timestamp': test_time, '_hash': 'block_send_test' } assert actual == expected
def test_get_store_block(): test_time = int(time.time()) t1 = Transaction('transaction_test_1', '', 'matt', 'will', 45, 0, test_time) t2 = Transaction('transaction_test_2', '', 'matt', 'denys', 7, 0, test_time) t3 = Transaction('transaction_test_3', '', 'denys', 'steven', 33, 0, test_time) t4 = Transaction('transaction_test_4', '', 'steven', 'naween', 5040, 0, test_time) t5 = Transaction('transaction_test_5', '', 'will', 'naween', 22, 0, test_time) t6 = Transaction('transaction_test_6', '', 'naween', 'matt', 588, 0, test_time) b = Block('block_service_test_1', '', '', 'LAST_BLOCK', 0, [t1, t2, t3, t4, t5, t6], [], [], [], [], [], test_time) bs = BlockService() bs.store_block(b) new_b = bs.find_by_hash('block_service_test_1') b_send = b.get_sendable() new_send = new_b.get_sendable() assert b_send == new_send
def mine_block(miner_priv_key, last_block): balances = BlockService.get_all_balances() transactions = BaseObjectService.get_all_mempool_objects(Transaction) transactions_to_add = [] for transaction in transactions: if len(transactions_to_add) == 100: break valid, balances = Miner.validate_transaction(balances, transaction) if valid: transactions_to_add.append(transaction) pos_transactions = BaseObjectService.get_all_mempool_objects( PosTransaction) pos_transactions_to_add = [] for pos_transaction in pos_transactions: if len(pos_transactions_to_add) == 10: break valid, balances = Miner.validate_pos_transaction( balances, pos_transaction) if valid: pos_transactions_to_add.append(pos_transaction) contract_transactions = BaseObjectService.get_all_mempool_objects( ContractTransaction) contract_transactions_to_add = [] for contract_transaction in contract_transactions: if len(contract_transactions_to_add) == 10: break valid, balances = Miner.validate_contract_transaction( balances, contract_transaction) if valid: contract_transactions_to_add.append(contract_transaction) contracts = BaseObjectService.get_all_mempool_objects(Contract) contracts_to_add = [] for contract in contracts: if len(contracts_to_add) == 20: break valid, balances = Miner.validate_contract(balances, contract) if valid: contracts_to_add.append(contract) signed_contracts = BaseObjectService.get_all_mempool_objects( SignedContract) signed_contracts_to_add = [] for signed_contract in signed_contracts: print('signed_contract:' + signed_contract._hash) if len(signed_contracts_to_add) == 20: break valid, balances = Miner.validate_signed_contract( balances, signed_contract) if valid: signed_contracts_to_add.append(signed_contract) terminated_contracts_to_add = [] signed_contracts_to_check = SignedContractService.get_all_open_signed_contracts( ) for sc_to_check in signed_contracts_to_check: print('signed_c to check: ' + sc_to_check._hash) end_time = sc_to_check.signed_timestamp + sc_to_check.duration now = int(time.time()) # print('signed_time=' + str(sc_to_check.signed_timestamp) + ' duration=' + str(sc_to_check.duration) + ' end time='+ str(end_time) + ' now=' + str(now) + ' end<=now=' + str(end_time<=now)) if end_time <= now: rs = RedisService() r = rs._connect() prices = r.zrangebyscore('price_stamps', end_time - 2000, end_time + 2000, withscores=True) if len(prices) != 0: price = min(prices, key=lambda x: abs(x[1] - end_time)) print('terminated contract: ' + sc_to_check._hash) tc = TerminatedContract(sc_to_check._hash, price[0], end_time) terminated_contracts_to_add.append(tc) else: print('no prices found') last_block_hash = last_block._hash miner_pub_key = EcdsaHashing.recover_public_key_str(miner_priv_key) height = last_block.height + 1 block = Block('', '', miner_pub_key, last_block_hash, height, transactions_to_add, pos_transactions_to_add, contract_transactions_to_add, contracts_to_add, signed_contracts_to_add, terminated_contracts_to_add, time.time()) block.update_signature(miner_priv_key) block.update_hash() return block
def find_by_hash(self, block_hash): """ Find a block using it's hash. Will return the block object, fully populated with all of the objects encased in it. Arguments: block_hash -- hash of the block to retrieve Returns: block object -- Block object containing all of the objects included in the block """ r = self._connect() #rs = RedisService() # get key to retrieve list of block's fields name = self.key_suffix + block_hash if r.exists(name): # get all of the fields in the list hashes = r.lrange(name, 0, -1) # timestamp will always be first prev_block = hashes[0] # remove for iteration hashes.remove(prev_block) # timestamp will always be first height = hashes[0] # remove for iteration hashes.remove(height) # timestamp will always be first owner = hashes[0] # remove for iteration hashes.remove(owner) # timestamp will always be first signature = hashes[0] # remove for iteration hashes.remove(signature) # timestamp will always be first timestamp = hashes[0] # remove for iteration hashes.remove(timestamp) prefix = '' # list to hold all of the objects transactions = [] pos_transactions = [] contract_transactions = [] contracts = [] signed_contracts = [] terminated_contracts = [] # temporary list to copy from temp_list = [] obj = None contract_section = False signed_contract_section = False terminated_contract_section = False for h in hashes: # if at a new type of object, change some variables if h == 'transactions': prefix = Transaction._to_index()[-1] obj = Transaction continue elif h == 'pos_transactions': prefix = PosTransaction._to_index()[-1] obj = PosTransaction transactions = temp_list.copy() temp_list.clear() continue elif h == 'contract_transactions': prefix = ContractTransaction._to_index()[-1] obj = ContractTransaction pos_transactions = temp_list.copy() temp_list.clear() continue elif h == 'contracts': contract_section = True elif h == 'signed_contracts': contract_section = False signed_contract_section = True elif h == 'terminated_contracts': signed_contract_section = False terminated_contract_section = True # get the object from redis and add to the temporary list if contract_section: contract = ContractService.get_contract_by_hash(h) if contract != None: contracts.append(contract) elif signed_contract_section: signed_contract = SignedContractService.get_signed_contract_by_hash(h) if signed_contract != None: signed_contracts.append(signed_contract) elif terminated_contract_section: terminated_contract = self.rs.get_object_by_full_key('terminated_contract:' + h, TerminatedContract, r) if terminated_contract != None: terminated_contracts.append(terminated_contract) else: t = self.rs.get_object_by_full_key(prefix + ":" + h, obj, r) temp_list.append(t) contract_transactions = temp_list.copy() temp_list.clear() # create block object and return it block = Block(block_hash, signature, owner, prev_block, height, transactions, pos_transactions, contract_transactions, contracts, signed_contracts, terminated_contracts, timestamp) return block else: return None