예제 #1
0
    def add_block(self, block):
        status = False
        branch = self.get_branch_by_hash(block.block_header.previous_hash)
        max_height = self.get_height()
        if block.height > max_height:
            # we're working on the tallest branch
            if branch > 0:
                # if an alternate branch is the tallest branch, it becomes our primary branch
                self.restructure_primary_branch(branch)
                branch = 0
        else:
            # we're not on the tallest branch, so there could be a split here
            competing_branches = self.get_branches_by_prevhash(
                block.block_header.previous_hash)
            if competing_branches and branch in competing_branches:
                branch = self.get_new_branch_number(block.block_header.hash,
                                                    block.height)

        sql_strings = list()
        sql_strings.append(
            "INSERT INTO blocks (hash, prevhash, merkleRoot, height, nonce, timestamp, version, branch"
            + ") VALUES ('{}', '{}', '{}', {}, {}, {}, {}, {})".format(
                block.block_header.hash, block.block_header.previous_hash,
                block.block_header.merkle_root, block.height,
                block.block_header.nonce, block.block_header.timestamp,
                block.block_header.version, branch))
        for transaction in block.transactions:
            sql_strings.append(
                "INSERT INTO transactions (hash, src, dest, amount, fee, timestamp, signature, type,"
                + " blockHash, asset, data, branch, prevHash)" +
                " VALUES ('{}', '{}', '{}', {}, {}, {}, '{}', {},'{}', '{}', '{}', {}, '{}')"
                .format(transaction.tx_hash, transaction.source,
                        transaction.destination, transaction.amount,
                        transaction.fee, transaction.timestamp,
                        transaction.signature, transaction.tx_type,
                        block.block_header.hash, transaction.asset,
                        transaction.data, branch, transaction.prev_hash))
        sql_strings.append(
            "UPDATE branches SET currentHash = '{}', currentHeight = {} WHERE id = {}"
            .format(block.block_header.hash, block.height, branch))

        try:
            with sqlite3.connect(self.CHAIN_DB) as conn:
                cursor = conn.cursor()
                for sql in sql_strings:
                    cursor.execute(sql)
                status = True
        except sqlite3.OperationalError as err:
            logger.error("Database Error: ", err.message)
        return status
예제 #2
0
    def start_queue(cls):
        try:
            context = zmq.Context(1)
            # Socket facing producers
            frontend = context.socket(zmq.PULL)
            frontend.bind(cls.QUEUE_BIND_IN)
            # Socket facing consumers
            backend = context.socket(zmq.PUSH)
            backend.bind(cls.QUEUE_BIND_OUT)

            zmq.proxy(frontend, backend)

        except Exception as e:
            logger.error("could not start queue: %s", e)
            raise
예제 #3
0
    def start_publisher(cls):
        try:
            bind_to = "tcp://*:{}".format(cls.PUBSUB_PORT)
            ctx = zmq.Context()
            s = ctx.socket(zmq.PUB)
            s.bind(bind_to)

            #cls.sync_with_subscriber(bind_to)
        except Exception as e:
            logger.error("could not start publishing server: %s", e.message)
            raise

        print("Publishing...")
        while True:
            a = time.clock()
            s.send_string(str(a))
예제 #4
0
    def start_subscriber(cls):
        try:
            connect_to = "tcp://localhost:{}".format(cls.PUBSUB_PORT)
            array_count = 10
            ctx = zmq.Context()
            s = ctx.socket(zmq.SUB)
            s.connect(connect_to)
            s.setsockopt(zmq.SUBSCRIBE, '')

            #cls.sync_with_publisher(connect_to)
        except Exception as e:
            logger.error("could not start subscribe: %s", e.message)
            raise

        print("Listening...")
        while True:
            a = s.recv_string()
            print(a)