def test_get_existing_tx(): """ test getting a block for an existing transaction """ txid = rcrypt.make_uuid() blkindex.put_index(txid, 555) assert blkindex.get_blockno(txid) == 555
def build_blk_index(): ''' build the blk_index database from the blockchain ''' blockno = 0 tx_count = 0 try: while True: # test whether the block file exists block_file = "block" + "_" + str(blockno) + ".dat" if os.path.isfile(block_file) == False: break # load the block file into a dictionary object f = open(block_file, 'rb') block = pickle.load(f) # process the transactions in the block for trx in block["tx"]: ret = blk_index.put_index(trx["transactionid"], blockno) tx_count += 1 if ret == False: raise(ValueError("failed to rebuild blk_index. block no: " \ + str(blockno))) blockno += 1 except Exception as err: print(str(err)) return False print("transactions processed: " + str(tx_count)) print("blocks processed: " + str(blockno)) return True
def add_block(block: "dictionary") -> "bool": """ add_block: adds a block to the blockchain. Receives a block. The block attributes are checked for validity and each transaction in the block is tested for validity. If there are no errors, the block is written to a file as a sequence of raw bytes. Then the block is added to the blockchain. The chainstate database and the blk_index databases are updated. returns True if the block is added to the blockchain and False otherwise """ try: # validate the received block parameters if validate_block(block) == False: raise(ValueError("block validation error")) # validate the transactions in the block # update the chainstate database for trx in block['tx']: # first transaction in the block is a coinbase transaction if block["height"] == 0 or block['tx'][0] == trx: zero_inputs = True else: zero_inputs = False if tx.validate_transaction(trx, zero_inputs) == False: raise(ValueError("transaction validation error")) if hchaindb.transaction_update(trx) == False: raise(ValueError("chainstate update transaction error")) # serialize the block to a file if (serialize_block(block) == False): raise(ValueError("serialize block error")) # add the block to the blockchain in memory blockchain.append(block) # update the blk_index for transaction in block['tx']: blockindex.put_index(transaction["transactionid"], block["height"]) except Exception as err: print(str(err)) logging.debug('add_block: exception: ' + str(err)) return False return True
def test_delete_tx(): txid = rcrypt.make_uuid() blkindex.put_index(txid, 555) assert blkindex.delete_index(txid) == True assert blkindex.get_blockno(txid) == False
def test_put_index(txid, value, ret): """ tests putting valid and invalid transaction_index and blockno pairs into the blk_index store """ assert blkindex.put_index(txid, value) == ret