Skip to content

Commit 60a3610

Browse files
authored
Merge pull request #167 from keep-network/implementation-details-joining-and-leaving
Implementation Details - Joining and Leaving This PR fleshes out the joining and leaving sections of the implementation details guide!
2 parents c922521 + b13bab7 commit 60a3610

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

docs/implementation-details.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,50 @@ right-most 32 bits, and then do a bitwise `&` with `2^32 - 1`, which is 224 0's
130130
and 32 1s. This has the effect of erasing everything but the 32 relevant bits,
131131
allowing us to read *only* the slot.
132132

133+
### Joining and Leaving The Pool
134+
135+
Operators join the pool according to two pieces of state: `rightmostLeaf` and
136+
`emptyLeaves`. `rightmostLeaf` is an always-increasing counter that starts at 0
137+
and increases each time an operator needs to join the pool. Eventually,
138+
`rightmostLeaf` will exceed `2,097,152`, which is the size of the leaf layer,
139+
and we'll ignore it forever, and instead rely on `emptyLeaves`.
140+
141+
`emptyLeaves` is an array that is appended to with the position of the operator
142+
who *leaves* the pool. Once `rightmostLeaf` is useless, we `pop` `emptyLeaves`,
143+
insert the new operator in that position, and repeat. We should never run out
144+
of positions with this strategy because the number of leaves far exceeds the
145+
total token supply divided by the minimum stake.
146+
147+
Here's a sample event log with state, using a max leaf length of `4` instead of
148+
`2097152` for brevity.
149+
150+
```
151+
state 1) slots: [-, -, -, -], rightmostLeaf: 0, emptyLeaves: []
152+
event: A joins
153+
state 2) slots: [A, -, -, -], rightmostLeaf: 1, emptyLeaves: []
154+
event: B joins
155+
state 3) slots: [A, B, -, -], rightmostLeaf: 2, emptyLeaves: []
156+
event: C joins
157+
state 4) slots: [A, B, C, -], rightmostLeaf: 3, emptyLeaves: []
158+
event: B leaves
159+
state 5) slots: [A, -, C, -], rightmostLeaf: 3, emptyLeaves: [1]
160+
event: D joins
161+
state 6) slots: [A, -, C, D], rightmostLeaf: 4, emptyLeaves: [1] // rightmostLeaf is forever useless now
162+
event: A leaves
163+
state 7) slots: [-, -, C, D], rightmostLeaf: 4, emptyLeaves: [1, 0]
164+
event: E joins
165+
state 8) slots: [E, -, C, D], rightmostLeaf: 4, emptyLeaves: [1]
166+
```
167+
168+
Each time an operator joins or leaves the pool, we need to update all of the
169+
branches on the path from the operator to the root, as well as the root. The
170+
branch with the operator as a child will have its slot updated with the child's
171+
weight directly. That branch's total weight will change, which will update it's
172+
slot in that branch's parent, and so on, all the way up to the root.
173+
174+
For an in-depth explanation of how this information is structured, refer to the
175+
[Branch and Root Deserialization and
176+
Serialization](#branch-and-root-deserialization-and-serialization) section.
177+
133178
TODO: Joining and Leaving The Pool
134179
TODO: Selecting A Random Group

0 commit comments

Comments
 (0)