Exemplo n.º 1
0
 def set(self, i: int, v: View) -> None:
     elem_type: Type[View] = self.item_elem_cls(i)
     # if not the right type, try to coerce it
     if not isinstance(v, elem_type):
         v = elem_type.coerce_view(v)
     if self.is_packed():
         # basic types are more complicated: we operate on a subsection of a bottom chunk
         if isinstance(v, BasicView):
             elems_per_chunk = 32 // v.type_byte_length()
             chunk_i = i // elems_per_chunk
             target = to_gindex(chunk_i, self.tree_depth())
             chunk_setter_link: Link = self.get_backing().setter(target)
             chunk = self.get_backing().getter(target)
             new_chunk = v.backing_from_base(chunk, i % elems_per_chunk)
             self.set_backing(chunk_setter_link(new_chunk))
         else:
             raise Exception(
                 "cannot pack subtree elements that are not basic types")
     else:
         setter_link: Link = self.get_backing().setter(
             to_gindex(i, self.tree_depth()))
         self.set_backing(setter_link(v.get_backing()))
Exemplo n.º 2
0
    def append(self, v: View):
        ll = self.length()
        if ll >= self.__class__.limit():
            raise Exception("list is maximum capacity, cannot append")
        i = ll
        elem_type: Type[View] = self.__class__.element_cls()
        if not isinstance(v, elem_type):
            v = elem_type.coerce_view(v)
        if self.__class__.is_packed():
            next_backing = self.get_backing()
            if isinstance(elem_type, BasicTypeDef):
                if not isinstance(v, BasicView):
                    raise Exception("input element is not a basic view")
                basic_v: BasicView = v
                elems_per_chunk = 32 // elem_type.type_byte_length()
                chunk_i = i // elems_per_chunk
                target: Gindex = to_gindex(chunk_i,
                                           self.__class__.tree_depth())
                if i % elems_per_chunk == 0:
                    set_last = next_backing.setter(target, expand=True)
                    chunk = zero_node(0)
                else:
                    set_last = next_backing.setter(target)
                    chunk = next_backing.getter(target)
                chunk = basic_v.backing_from_base(chunk, i % elems_per_chunk)
                next_backing = set_last(chunk)
            else:
                raise Exception(
                    "cannot append a packed element that is not a basic type")
        else:
            target: Gindex = to_gindex(i, self.__class__.tree_depth())
            set_last = self.get_backing().setter(target, expand=True)
            next_backing = set_last(v.get_backing())

        set_length = next_backing.rebind_right
        new_length = uint256(ll + 1).get_backing()
        next_backing = set_length(new_length)
        self.set_backing(next_backing)
Exemplo n.º 3
0
def hash_tree_root(obj: View) -> Bytes32:
    return Bytes32(obj.get_backing().merkle_root())