MCPcopy Create free account
hub / github.com/SeismicSystems/summit / grow

Method grow

types/src/ssz_tree.rs:226–247  ·  view source on GitHub ↗

Grow the tree to accommodate at least `min_leaves` leaves. Copies existing leaves to the new tree and does a full bottom-up rehash. No-op if the current capacity is sufficient.

(&mut self, min_leaves: usize)

Source from the content-addressed store, hash-verified

224 /// Copies existing leaves to the new tree and does a full bottom-up rehash.
225 /// No-op if the current capacity is sufficient.
226 pub fn grow(&mut self, min_leaves: usize) {
227 if min_leaves <= self.capacity {
228 return;
229 }
230 let new_capacity = min_leaves.next_power_of_two();
231 let new_depth = new_capacity.ilog2() as usize;
232 let mut new_nodes = vec![[0u8; 32]; 2 * new_capacity];
233
234 // Copy existing leaves
235 let old_leaf_start = self.capacity;
236 let old_leaf_end = 2 * self.capacity;
237 let new_leaf_start = new_capacity;
238 new_nodes[new_leaf_start..new_leaf_start + self.capacity]
239 .copy_from_slice(&self.nodes[old_leaf_start..old_leaf_end]);
240
241 self.nodes = new_nodes;
242 self.capacity = new_capacity;
243 self.depth = new_depth;
244
245 // Full bottom-up rehash from leaf parents
246 self.rehash_from(self.capacity / 2);
247 }
248
249 /// Shrink the tree so that its capacity matches `min_leaves.next_power_of_two()`.
250 ///

Callers 3

push_depositMethod · 0.80
push_withdrawalMethod · 0.80

Calls 1

rehash_fromMethod · 0.80

Tested by

no test coverage detected