Exemplo n.º 1
0
    def parse_msg(self, msg_type, msg, sender):

        if msg_type != 'PON':
            logger.debug('parse_msg called with msg_type = %s', msg_type)
        if msg_type == 'RQB':
            obj = RequestBlockMessage.unserialize(msg)
            self.receive_request_blocks_message(obj, sender)
        elif msg_type == 'TXN':
            obj = Transaction.unserialize(msg)
            self.receive_transaction(obj)
        elif msg_type == 'BLK':
            obj = Block.unserialize(msg)
            self.receive_block(obj)
        elif msg_type == 'RSB':
            obj = RespondBlockMessage.unserialize(msg)
            self.receive_respond_blocks_message(obj)
        elif msg_type == 'PAM':
            obj = PaxosMessage.unserialize(msg)
            self.receive_paxos_message(obj, sender)
        elif msg_type == 'PON':
            obj = PongMessage.unserialize(msg)
            self.receive_pong_message(obj, sender.peer_node_id)
        elif msg_type == 'ACM':
            obj = AckCommitMessage.unserialize(msg)
            self.receive_ack_commit_message(obj)
Exemplo n.º 2
0
    def __init__(self, node_index):
        self.genesis = GENESIS
        self.head_block = GENESIS
        self.committed_block = GENESIS
        self.committed_blocks = [GENESIS.block_id]
        self.nodes = {}
        self.nodes.update({GENESIS.block_id: GENESIS})
        self.counter = 0
        self.ack_commits = {}

        # create a db instance (s.t blocks can be recovered after a crash)
        base_path = os.path.expanduser('~/.pichain')
        path = base_path + '/node_' + str(node_index)
        if not os.path.exists(path):
            os.makedirs(path)
        self.db = plyvel.DB(path, create_if_missing=True)

        # first load all the blocks
        for key, value in self.db:
            # block_id -> block
            if key.decode().isdigit():
                block_id = int(key.decode())
                block = Block.unserialize(value)
                self.nodes.update({block_id: block})

        # load all block ids and counter
        for key, value in self.db:
            if key == b'committed_block':
                block = self.nodes.get(int(value.decode()))
                self.committed_block = block
            elif key == b'head_block':
                block = self.nodes.get(int(value.decode()))
                self.head_block = block
            elif key == b'counter':
                self.counter = int(value.decode())
            elif key == b'genesis':
                block = self.nodes.get(int(value.decode()))
                self.genesis = block
            elif key == b'committed_blocks':
                block_ids = json.loads(value.decode())
                self.committed_blocks = block_ids