def checkChain ( chain ): # Work through the chain from the genesis block (which gets special treatment), # checking that all transactions are internally valid, # that the transactions do not cause an overdraft, # and that the blocks are linked by their hashes. # This returns the state as a dictionary of accounts and balances, # or returns False if an error was detected ## Data input processing: Make sure that our chain is a list of dicts if type ( chain ) == str : try : chain = json . loads ( chain ) assert ( type ( chain ) == list ) except : # This is a catch-all, admittedly crude return False elif type ( chain ) != list : return False state = {} ## Prime the pump by checking the genesis block # We want to check the following conditions: # - Each of the transactions are valid updates to the system state # - Block hash is valid for the block contents for txn in chain [ 0 ][ 'contents' ][ 'txns' ]: state = updateState ( txn , state ) checkBlockHash ( chain [ 0 ]) parent = chain [ 0 ] ## Checking subsequent blocks: These additionally need to check # - the reference to the parent block's hash # - the validity of the block number for block in chain [ 1 :]: state = checkBlockValidity ( block , parent , state ) parent = block return state