Exemplo n.º 1
0
    def forks(self, head):
        (vec_ptr, vec_len,
         vec_cap) = ffi.prepare_vec_result(pointer_type=_BlockPayload)

        head = ctypes.c_char_p(head.encode())

        _libexec('chain_controller_forks', self.pointer, head,
                 ctypes.byref(vec_ptr), ctypes.byref(vec_len),
                 ctypes.byref(vec_cap))

        # Check if NULL
        if not vec_ptr:
            return None

        blocks = []
        for i in range(vec_len.value):
            block_payload = vec_ptr[i]
            payload = ffi.from_rust_vec(
                block_payload.block_ptr,
                ctypes.c_size_t(block_payload.block_len),
                ctypes.c_size_t(block_payload.block_cap),
            )
            block = Block()
            block.ParseFromString(payload)
            blocks.append(BlockWrapper(block))

        LIBRARY.call("chain_controller_reclaim_block_payload_vec", vec_ptr,
                     vec_len, vec_cap)

        return blocks
Exemplo n.º 2
0
    def has_batch(self, batch_id):
        has = ctypes.c_bool(False)
        c_batch_id = ctypes.c_char_p(batch_id.encode())

        LIBRARY.call('incoming_batch_sender_has_batch', self.pointer,
                     c_batch_id, ctypes.byref(has))

        return has
Exemplo n.º 3
0
    def has_batch(self, batch_id):
        has = ctypes.c_bool(False)
        c_batch_id = ctypes.c_char_p(batch_id.encode())

        LIBRARY.call(
            'incoming_batch_sender_has_batch',
            self.pointer,
            c_batch_id,
            ctypes.byref(has))

        return has
Exemplo n.º 4
0
    def __init__(self, path, indexes=None, _size=1024**4):
        super(NativeLmdbDatabase, self).__init__('lmdb_database_drop')

        if indexes is None:
            indexes = []

        c_path = ctypes.c_char_p(path.encode())

        c_indexes = (ctypes.c_char_p * len(indexes))()
        for (i, index) in enumerate(indexes):
            c_indexes[i] = ctypes.c_char_p(index.encode())

        c_size = ctypes.c_size_t(_size)
        res = LIBRARY.call('lmdb_database_new', c_path, c_size, c_indexes,
                           ctypes.c_size_t(len(indexes)),
                           ctypes.byref(self.pointer))
        if res == ErrorCode.Success:
            return
        elif res == ErrorCode.NullPointerProvided:
            raise TypeError("Path cannot be null")
        elif res == ErrorCode.InvalidFilePath:
            raise TypeError("Invalid file path {}".format(path))
        elif res == ErrorCode.InvalidIndexString:
            raise TypeError("Invalid index string {}".format(indexes))
        elif res == ErrorCode.InitializeContextError:
            raise TypeError("Unable to initialize LMDB Context")
        elif res == ErrorCode.InitializeDatabaseError:
            raise TypeError("Unable to initialize LMDB Database")
        else:
            raise TypeError("Unknown error occurred: {}".format(res.error))
Exemplo n.º 5
0
    def release(self):
        res = LIBRARY.call("chain_head_guard_drop", self._guard.pointer)

        if res == ChainHeadLockErrorCode.Success:
            return
        if res == ChainHeadLockErrorCode.NullPointerProvided:
            raise TypeError("Provided null pointer(s)")

        raise ValueError("An unknown error occurred: {}".format(res))
Exemplo n.º 6
0
    def _notify(self, fn_name, *args):
        return_code = LIBRARY.call(fn_name, self.pointer, *args)

        if return_code == ErrorCode.Success:
            return
        if return_code == ErrorCode.NullPointerProvided:
            raise TypeError("Provided null pointer(s)")
        if return_code == ErrorCode.InvalidArgument:
            raise ValueError("Input was not valid ")
Exemplo n.º 7
0
    def release(self):
        res = LIBRARY.call(
            "chain_head_guard_drop",
            self._guard.pointer)

        if res == ChainHeadLockErrorCode.Success:
            return
        elif res == ChainHeadLockErrorCode.NullPointerProvided:
            raise TypeError("Provided null pointer(s)")
        else:
            raise ValueError("An unknown error occurred: {}".format(res))
Exemplo n.º 8
0
    def acquire(self):
        guard_ptr = ctypes.c_void_p()
        res = LIBRARY.call("chain_head_lock_acquire", self._ptr,
                           ctypes.byref(guard_ptr))

        if res == ChainHeadLockErrorCode.Success:
            self._guard = ChainHeadGuard(guard_ptr)
            return self._guard
        if res == ChainHeadLockErrorCode.NullPointerProvided:
            raise TypeError("Provided null pointer(s)")

        raise ValueError("An unknown error occurred: {}".format(res))
Exemplo n.º 9
0
    def _notify(self, fn_name, *args):
        return_code = LIBRARY.call(
            fn_name,
            self.pointer,
            *args)

        if return_code == ErrorCode.Success:
            return
        if return_code == ErrorCode.NullPointerProvided:
            raise TypeError("Provided null pointer(s)")
        if return_code == ErrorCode.InvalidArgument:
            raise ValueError("Input was not valid ")
Exemplo n.º 10
0
    def acquire(self):
        guard_ptr = ctypes.c_void_p()
        res = LIBRARY.call(
            "chain_head_lock_acquire",
            self._ptr,
            ctypes.byref(guard_ptr))

        if res == ChainHeadLockErrorCode.Success:
            self._guard = ChainHeadGuard(guard_ptr)
            return self._guard
        elif res == ChainHeadLockErrorCode.NullPointerProvided:
            raise TypeError("Provided null pointer(s)")
        else:
            raise ValueError("An unknown error occurred: {}".format(res))
Exemplo n.º 11
0
    def forks(self, head):
        (vec_ptr, vec_len, vec_cap) = ffi.prepare_vec_result(
            pointer_type=_BlockPayload)

        head = ctypes.c_char_p(head.encode())

        _libexec(
            'chain_controller_forks',
            self.pointer,
            head,
            ctypes.byref(vec_ptr),
            ctypes.byref(vec_len),
            ctypes.byref(vec_cap))

        # Check if NULL
        if not vec_ptr:
            return None

        blocks = []
        for i in range(vec_len.value):
            block_payload = vec_ptr[i]
            payload = ffi.from_rust_vec(
                block_payload.block_ptr,
                ctypes.c_size_t(block_payload.block_len),
                ctypes.c_size_t(block_payload.block_cap),
            )
            block = Block()
            block.ParseFromString(payload)
            blocks.append(block)

        LIBRARY.call(
            "chain_controller_reclaim_block_payload_vec",
            vec_ptr,
            vec_len,
            vec_cap)

        return blocks
Exemplo n.º 12
0
    def notify_on_chain_updated(self,
                                chain_head,
                                committed_batches=None,
                                uncommitted_batches=None):
        res = LIBRARY.call("chain_head_guard_on_chain_updated", self.pointer,
                           ctypes.py_object(chain_head),
                           ctypes.py_object(committed_batches),
                           ctypes.py_object(uncommitted_batches))

        if res == ChainHeadLockErrorCode.Success:
            return
        if res == ChainHeadLockErrorCode.NullPointerProvided:
            raise TypeError("Provided null pointer(s)")

        raise ValueError("An unknown error occurred: {}".format(res))
Exemplo n.º 13
0
    def send(self, item):
        res = LIBRARY.call("incoming_batch_sender_send", self._ptr,
                           ctypes.py_object(item))

        if res == IncomingBatchSenderErrorCode.Success:
            return

        if res == IncomingBatchSenderErrorCode.NullPointerProvided:
            raise TypeError("Provided null pointer(s)")
        if res == IncomingBatchSenderErrorCode.InvalidInput:
            raise ValueError("Input was not valid ")
        if res == IncomingBatchSenderErrorCode.Disconnected:
            raise Disconnected()

        raise ValueError("An unknown error occurred: {}".format(res))
Exemplo n.º 14
0
    def notify_on_chain_updated(self,
                                chain_head,
                                committed_batches=None,
                                uncommitted_batches=None):
        res = LIBRARY.call(
            "chain_head_guard_on_chain_updated",
            self.pointer,
            ctypes.py_object(chain_head),
            ctypes.py_object(committed_batches),
            ctypes.py_object(uncommitted_batches))

        if res == ChainHeadLockErrorCode.Success:
            return
        elif res == ChainHeadLockErrorCode.NullPointerProvided:
            raise TypeError("Provided null pointer(s)")
        else:
            raise ValueError("An unknown error occurred: {}".format(res))
Exemplo n.º 15
0
    def __init__(self, path, _size=1024**4):
        self._db_ptr = ctypes.c_void_p()

        c_path = ctypes.c_char_p(path.encode())
        c_size = ctypes.c_size_t(_size)
        res = LIBRARY.call('lmdb_database_new', c_path, c_size,
                           ctypes.byref(self._db_ptr))
        if res == ErrorCode.Success:
            return
        elif res == ErrorCode.NullPointerProvided:
            raise TypeError("Path cannot be null")
        elif res == ErrorCode.InvalidFilePath:
            raise TypeError("Invalid file path {}".format(path))
        elif res == ErrorCode.InitializeContextError:
            raise TypeError("Unable to initialize LMDB Context")
        elif res == ErrorCode.InitializeDatabaseError:
            raise TypeError("Unable to initialize LMDB Database")
        else:
            raise TypeError("Unknown error occurred: {}".format(res.error))
Exemplo n.º 16
0
    def __init__(self):
        super().__init__("block_status_store_drop")

        _to_exception(
            LIBRARY.call("block_status_store_new", ctypes.byref(self.pointer)))
Exemplo n.º 17
0
 def stop(self):
     _to_exception(LIBRARY.call("block_validator_stop", self.pointer))
Exemplo n.º 18
0
 def __del__(self):
     if self._db_ptr:
         LIBRARY.call('lmdb_database_drop', self._db_ptr)
         self._db_ptr = None
Exemplo n.º 19
0
    def __init__(self):
        super(BlockValidationResultStore, self).__init__(
            "block_status_store_drop")

        _to_exception(LIBRARY.call("block_status_store_new",
                                   ctypes.byref(self.pointer)))
Exemplo n.º 20
0
 def stop(self):
     _to_exception(LIBRARY.call("block_validator_stop", self.pointer))