Example #1
0
    def load_data(self):
        try:
            with open('blockchain-{}.txt'.format(self.node_id),mode='r') as f:
                file_content = f.readlines()
                blockchain = json.loads(file_content[0][:-1])
                updated_blockchain = []
                for block in blockchain:
                    converted_tx = [Transaction(
                        tx['sender'],tx['recipient'],tx['signature'],tx['amount']) for tx in block['transactions']]
                    updated_block = Block(block['index'], block['previous_hash'],converted_tx,block['proof'],block['timestamp'])
                    updated_blockchain.append(updated_block)
                self.chain = updated_blockchain
                open_transactions = json.loads(file_content[1][:-1])
                updated_transactions = []
                for tx in open_transactions:
                    updated_transactions = Transaction(tx['sender'],tx['receipient'],tx['signature'],tx['amount'])
                    updated_transactions.append(updated_transaction)
                self.__open_transactions = updated_transactions
                peer_nodes = json.loads(file_content[2])
                self.__peer_nodes = set(peer_nodes)

        except (IOError,IndexError):
            print('Handled Exception.......')
        except ValueError:
            print("Value error")
        except:
            print('Thrown Wildcard while loading data')
        finally:
            print('Cleanup')
Example #2
0
def load_data():
    global blockchain
    global open_transactions
    try:
        with open('blockchain.txt', mode='r') as f:
            file_content = f.readlines()

            blockchain = json.loads(file_content[0][:-1])
            updated_blockchain = []
            for block in blockchain:
                converted_tx = [
                    Transaction(tx['sender'], tx['recipient', tx['amount']])
                    for tx in block['transactions']
                ]

                updated_block = Block(block['index'], block['previous_hash'],
                                      converted_tx, block['proof'],
                                      block['timestamp'])

                updated_blockchain.append(updated_block)
            blockchain = updated_blockchain
            open_transactions = json.loads(file_content[1])
            updated_transactions = []
            for tx in open_transactions:
                updated_transactions = Transaction(tx['sender'],
                                                   tx['recipient'],
                                                   tx['amount'])
                updated_transactions.append(updated_transactions)
                open_transactions = updated_transactions

    except (IOError, IndexError):
        # Our starting block for the blockchain
        genesis_block = Block(0, '', [], 100, 0)

        # Initializing our (empty) blockchain list
        blockchain = [genesis_block]
        # Unhandled transactions
        open_transactions = []