def checkout(self, itemid, data): exists = 0 j = len(self.blocks) - 1 while j > 0: if self.blocks[j].evidenceID == int(itemid): exists = 1 if self.blocks[j].state == STATE[ 'in']: # if the block contains the evidence ID and it has a state of 'checkout' self.blocks.append( Block.Block( str(hashing(self.blocks[len(self.blocks) - 1])), #prev hash datetime.now(), #timestamp self.blocks[j].caseID, #caseID itemid, #evidence id STATE['out'], #state 0, #datalength b'', #data )) print('Case: ', self.blocks[j + 1].caseID) print('Checked out item: ', self.blocks[j + 1].evidenceID) print('Status: ', self.blocks[j + 1].state) print('Time of action: ', self.blocks[j + 1].timestamp) return elif self.blocks[j].state == STATE['out'] or self.blocks[ j].state == STATE['dis'] or self.blocks[ j].state == STATE['des'] or self.blocks[ j].state == STATE['rel'] or self.blocks[ j].state == STATE['init']: sys.exit( 'Evidence must be checked in first before checkout') j = j - 1 if exists == 0: sys.exit("This item does not exist in the blockchain")
def verify(self, isWrong=True): flag = True for i in range(1, len(self.blocks)): if i == 1 or i == 2: # print('initial Block') elif hashing(self.blocks[i - 1]) != self.blocks[ i].prevHash: # check that previous and current hash match flag = False # print(binascii.hexlify(hashing(self.blocks[i - 1])) , binascii.hexlify(self.blocks[i].prevHash)) if isWrong: print("Transactions in blockchain: ", len(self.blocks)) print("State of blockchain: ERROR") print("Bad block: ", i) sys.exit("Block contents do not block checksum") elif self.blocks[i - 1].timestamp >= self.blocks[ i].timestamp: # Check if block time were registered accordinaly flag = False if isWrong: print("Transactions in blockchain: ", len(self.blocks)) print("State of blockchain: ERROR") print("Bad block: ", i) sys.exit('Backdating at block') if flag and len(self.blocks) != 0: print("Transactions in blockchain: ", len(self.blocks) - 1) print("State of blockchain: CLEAN") else: sys.exit('BAD intial Block') return flag
class Blockchain(): # Blockchain Constructor def __init__(self): self.blocks = [] # create empty list to hold items # Add a new block to the chain and make sure there is no item duplicate def add(self, case, item, data): exist = 0 for j in range(0, len(self.blocks)): if str(self.blocks[j].evidenceID) == item: exist = 1 if exist == 1: sys.exit("This item already exists in the blockchain") else: if len(self.blocks) == 1: self.blocks.append( Block.Block( str(0), datetime.now(), UUID(case), item, STATE['in'], 0, b'', #Me )) else: self.blocks.append( Block.Block( str(hashing(self.blocks[len(self.blocks) - 1])), #prev hash datetime.now(), #timestamp UUID(case), #caseID item, #evidence id STATE['in'], #state 0, #datalength data, #data )) print('Case: ', self.blocks[len(self.blocks) - 1].caseID) print('item ID: ', self.blocks[len(self.blocks) - 1].evidenceID) print('Status: ', self.blocks[len(self.blocks) - 1].state.decode('utf-8')) print('Time of action: ', self.blocks[len(self.blocks) - 1].timestamp.isoformat())
def remove(self, itemid, status, data): exists = 0 #retrieve the first 3 leters of the status count = 0 stat = '' while (count < 3): stat = stat + status[count] count = count + 1 reason = stat.lower() if reason == 'dis' or reason == 'des' or reason == 'rel': j = len(self.blocks) - 1 while j > 0: if self.blocks[j].evidenceID == int(itemid): exists = 1 if self.blocks[j].state == STATE[ 'in']: # if the block contains the evidence ID and it has a state of 'checkin' if data == b'': self.blocks.append( Block.Block( str( hashing(self.blocks[len(self.blocks) - 1])), #prev hash datetime.now(), #timestamp self.blocks[j].caseID, #caseID itemid, #evidence id STATE[reason], #state 0, #datalength b'', #data )) else: data = data + '\x00' self.blocks.append( Block.Block( str( hashing(self.blocks[len(self.blocks) - 1])), #prev hash datetime.now(), #timestamp self.blocks[j].caseID, #caseID itemid, #evidence id STATE[reason], #state len(data), #datalength data.encode(), #data )) print('Case: ', self.blocks[j + 1].caseID) print('Removed item: ', self.blocks[j + 1].evidenceID) print('Status: ', self.blocks[j + 1].state) #print the owner infos if the reason is RELEASED if status == STATE['rel']: print('Owner info: ', self.blocks[j + 1].data) #Me print('Time of action: ', self.blocks[j + 1].timestamp) return elif self.blocks[j].state == STATE['out'] or self.blocks[ j].state == STATE['dis'] or self.blocks[ j].state == STATE['des'] or self.blocks[ j].state == STATE['rel'] or self.blocks[ j].state == STATE['init']: sys.exit( 'Evidence must be checked in first before remove') j = j - 1 else: sys.exit('Invalid remove reason (why) input') if exists == 0: sys.exit("This item does not exist in the blockchain")