示例#1
0
    def update(self, set_items, delete_items=None, virtual=True):
        """

        Args:
            set_items (dict): dict key, values where keys are addresses
            delete_items (list): list of addresses
            virtual (boolean): True if not committing to disk. I.e.,
                speculative root hash
        Returns:
            the state root after the operations
        """
        c_set_items = (ctypes.POINTER(_Entry) * len(set_items))()
        for (i, (key, value)) in enumerate(set_items.items()):
            c_set_items[i] = ctypes.pointer(_Entry.new(key, _encode(value)))

        if delete_items is None:
            delete_items = []

        c_delete_items = (ctypes.c_char_p * len(delete_items))()
        for (i, address) in enumerate(delete_items):
            c_delete_items[i] = ctypes.c_char_p(address.encode())

        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()
        _libexec('merkle_db_update', self.pointer, c_set_items,
                 ctypes.c_size_t(len(set_items)), c_delete_items,
                 ctypes.c_size_t(len(delete_items)), virtual,
                 ctypes.byref(string_ptr), ctypes.byref(string_len),
                 ctypes.byref(string_cap))

        return ffi.from_rust_string(string_ptr, string_len,
                                    string_cap).decode()
示例#2
0
    def get_merkle_root(self):
        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()
        _libexec('merkle_db_get_merkle_root', self.pointer,
                 ctypes.byref(string_ptr), ctypes.byref(string_len),
                 ctypes.byref(string_cap))

        return ffi.from_rust_string(string_ptr, string_len,
                                    string_cap).decode()
示例#3
0
    def get_merkle_root(self):
        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()
        _libexec(
            'merkle_db_get_merkle_root',
            self.pointer,
            ctypes.byref(string_ptr),
            ctypes.byref(string_len),
            ctypes.byref(string_cap))

        return ffi.from_rust_string(
            string_ptr, string_len, string_cap).decode()
示例#4
0
    def delete(self, address):
        c_address = ctypes.c_char_p(address.encode())

        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()

        _libexec('merkle_db_delete', self.pointer, c_address,
                 ctypes.byref(string_ptr), ctypes.byref(string_len),
                 ctypes.byref(string_cap))

        return ffi.from_rust_string(string_ptr, string_len,
                                    string_cap).decode()
示例#5
0
    def set(self, address, value):
        c_address = ctypes.c_char_p(address.encode())

        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()

        data = _encode(value)
        _libexec('merkle_db_set', self.pointer, c_address, data, len(data),
                 ctypes.byref(string_ptr), ctypes.byref(string_len),
                 ctypes.byref(string_cap))

        return ffi.from_rust_string(string_ptr, string_len,
                                    string_cap).decode()
示例#6
0
    def delete(self, address):
        c_address = ctypes.c_char_p(address.encode())

        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()

        _libexec(
            'merkle_db_delete',
            self.pointer,
            c_address,
            ctypes.byref(string_ptr),
            ctypes.byref(string_len),
            ctypes.byref(string_cap))

        return ffi.from_rust_string(
            string_ptr, string_len, string_cap).decode()
示例#7
0
    def __next__(self):
        if not self._c_iter_ptr:
            raise StopIteration()

        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()
        (vec_ptr, vec_len, vec_cap) = ffi.prepare_vec_result()

        _libexec('merkle_db_leaf_iterator_next', self._c_iter_ptr,
                 ctypes.byref(string_ptr), ctypes.byref(string_len),
                 ctypes.byref(string_cap), ctypes.byref(vec_ptr),
                 ctypes.byref(vec_len), ctypes.byref(vec_cap))

        address = ffi.from_rust_string(string_ptr, string_len,
                                       string_cap).decode()
        value = _decode(ffi.from_rust_vec(vec_ptr, vec_len, vec_cap))

        return (address, value)
示例#8
0
    def set(self, address, value):
        c_address = ctypes.c_char_p(address.encode())

        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()

        data = _encode(value)
        _libexec(
            'merkle_db_set',
            self.pointer,
            c_address,
            data,
            len(data),
            ctypes.byref(string_ptr),
            ctypes.byref(string_len),
            ctypes.byref(string_cap))

        return ffi.from_rust_string(
            string_ptr, string_len, string_cap).decode()
示例#9
0
    def __next__(self):
        if not self._c_iter_ptr:
            raise StopIteration()

        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()
        (vec_ptr, vec_len, vec_cap) = ffi.prepare_vec_result()

        _libexec(
            'merkle_db_leaf_iterator_next',
            self._c_iter_ptr,
            ctypes.byref(string_ptr),
            ctypes.byref(string_len),
            ctypes.byref(string_cap),
            ctypes.byref(vec_ptr),
            ctypes.byref(vec_len),
            ctypes.byref(vec_cap))

        address = ffi.from_rust_string(
            string_ptr, string_len, string_cap).decode()
        value = _decode(ffi.from_rust_vec(vec_ptr, vec_len, vec_cap))

        return (address, value)
示例#10
0
    def update(self, set_items, delete_items=None, virtual=True):
        """

        Args:
            set_items (dict): dict key, values where keys are addresses
            delete_items (list): list of addresses
            virtual (boolean): True if not committing to disk. I.e.,
                speculative root hash
        Returns:
            the state root after the operations
        """
        c_set_items = (ctypes.POINTER(_Entry) * len(set_items))()
        for (i, (key, value)) in enumerate(set_items.items()):
            c_set_items[i] = ctypes.pointer(_Entry.new(key, _encode(value)))

        if delete_items is None:
            delete_items = []

        c_delete_items = (ctypes.c_char_p * len(delete_items))()
        for (i, address) in enumerate(delete_items):
            c_delete_items[i] = ctypes.c_char_p(address.encode())

        (string_ptr, string_len, string_cap) = ffi.prepare_string_result()
        _libexec(
            'merkle_db_update',
            self.pointer,
            c_set_items,
            ctypes.c_size_t(len(set_items)),
            c_delete_items,
            ctypes.c_size_t(len(delete_items)),
            virtual,
            ctypes.byref(string_ptr),
            ctypes.byref(string_len),
            ctypes.byref(string_cap))

        return ffi.from_rust_string(
            string_ptr, string_len, string_cap).decode()