Skip to content

Commit

Permalink
refactor: Add iter_from() method in CyclicBoundedVec (#686)
Browse files Browse the repository at this point in the history
* refactor: Add `iter_from()` method in `CyclicBoundedVec`

`ConcurrentMerkleTree` uses `CyclicBoundedVec` for storing changelog
entries. And it often needs to iterate over changelogs which are full
and started overwriting elements from the start.

Even though `CyclicBoundedVec` was doing good job in preserving the
bounds, before this change, it didn't provide any safe way of iterating
over elements with a custom index as a start.

That unfortunately resulted in attempts to solve that problem in the
code of `ConcurrentMerkleTree`, which eventually resulted in bugs.
The bugs were harder to spot, because mixing the logic of Merkle proof
patching with leaked internals of `CyclicBoundedVec` made it confusing
to read, debug and test.

To avoid such issues, introduce the `iter_from()` method which allows
iteration from the given index and simplify the `ConcurrentMerkleTree` to
just use that iterator.

Other changes:

* Stop returning `Result` in `push()`. `push()` can never panic, unless
  we have a bug in the implementation. Our tests should make sure that
  the structure is bulletproof.

* fix: Point `getAccountState.sh` to new test crate location

* chore: Regenerate accounts

* test: Add more cases for `CyclicBoundedVec`

* chore: Update IDL
  • Loading branch information
vadorovsky authored May 9, 2024
1 parent 175fc0c commit e6dbd11
Show file tree
Hide file tree
Showing 10 changed files with 474 additions and 153 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions js/stateless.js/src/idls/account_compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ export type AccountCompression = {
{
name: 'merkleTreeStruct';
type: {
array: ['u8', 296];
array: ['u8', 320];
};
},
{
Expand Down Expand Up @@ -893,7 +893,7 @@ export type AccountCompression = {
name: 'stateMerkleTreeStruct';
docs: ['Merkle tree for the transaction state.'];
type: {
array: ['u8', 256];
array: ['u8', 272];
};
},
{
Expand Down Expand Up @@ -1975,7 +1975,7 @@ export const IDL: AccountCompression = {
{
name: 'merkleTreeStruct',
type: {
array: ['u8', 296],
array: ['u8', 320],
},
},
{
Expand Down Expand Up @@ -2083,7 +2083,7 @@ export const IDL: AccountCompression = {
name: 'stateMerkleTreeStruct',
docs: ['Merkle tree for the transaction state.'],
type: {
array: ['u8', 256],
array: ['u8', 272],
},
},
{
Expand Down
Loading

0 comments on commit e6dbd11

Please sign in to comment.