Skip to content

fix: flattening order#208

Merged
g11tech merged 1 commit intomainfrom
fix_justification_map
Sep 18, 2025
Merged

fix: flattening order#208
g11tech merged 1 commit intomainfrom
fix_justification_map

Conversation

@anshalshukla
Copy link
Copy Markdown
Collaborator

Closes #201

Copilot AI review requested due to automatic review settings September 18, 2025 10:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the flattening order of justifications data by ensuring deterministic ordering based on root values. The change addresses issue #201 by implementing a two-pass approach: first collecting and sorting roots, then flattening validators in the sorted order.

  • Separates key collection from value flattening to enable sorting
  • Implements lexicographic sorting of roots using byte comparison
  • Maintains deterministic output order for justifications processing

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


// Now iterate over sorted roots and flatten validators in order
for (justifications_roots.items) |root| {
try justifications_validators.appendSlice(justifications.get(root) orelse unreachable);
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using unreachable here is unsafe since the root comes from the sorted list that was built from the same hashmap keys. However, if the hashmap is modified between the key collection and this lookup, it could cause a panic. Consider using a more defensive approach or add a comment explaining why this is guaranteed to be safe.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is safe as the roots are fetched from justifications map, if a missing root is encountered then it is ideal to panic

Comment on lines +280 to +284
std.mem.sortUnstable(types.Root, justifications_roots.items, {}, struct {
fn lessThanFn(_: void, a: types.Root, b: types.Root) bool {
return std.mem.order(u8, &a, &b) == .lt;
}
}.lessThanFn);
Copy link

Copilot AI Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sortUnstable may not provide consistent ordering for equal elements across different runs. Since this fix is specifically about deterministic flattening order, consider using std.mem.sort instead to ensure stable sorting behavior.

Copilot uses AI. Check for mistakes.
@anshalshukla
Copy link
Copy Markdown
Collaborator Author

Not sure, if there is a good way to add tests for this as specs doesn't contain any functional tests

Copy link
Copy Markdown
Member

@g11tech g11tech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, this will be covered in spec tests otherwise stateroots won't match

@g11tech g11tech merged commit 8ada4a7 into main Sep 18, 2025
6 checks passed
@g11tech g11tech deleted the fix_justification_map branch September 18, 2025 12:15
GrapeBaBa pushed a commit to GrapeBaBa/zeam that referenced this pull request Sep 18, 2025
g11tech added a commit that referenced this pull request Sep 18, 2025
* fix: make start node test friendly and fix several memory leak

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* Update pkgs/types/src/lib.zig

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix: fix more memory leak when error

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* fix: fix one more error meory leak

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* Update pkgs/cli/src/node.zig

* Update pkgs/cli/src/main.zig

* fix: flattening order (#208)

* update sig size to 4000 bytes as per spec (#210)

* fix: abstract away network args into a function

Signed-off-by: Chen Kai <281165273grape@gmail.com>

---------

Signed-off-by: Chen Kai <281165273grape@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: g11tech <develop@g11tech.io>
Co-authored-by: Anshal Shukla <53994948+anshalshukla@users.noreply.github.com>
Co-authored-by: g11tech <gajinder@zeam.in>
GrapeBaBa added a commit to GrapeBaBa/zeam that referenced this pull request Sep 19, 2025
…blaz#198)

* fix: make start node test friendly and fix several memory leak

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* Update pkgs/types/src/lib.zig

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix: fix more memory leak when error

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* fix: fix one more error meory leak

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* Update pkgs/cli/src/node.zig

* Update pkgs/cli/src/main.zig

* fix: flattening order (blockblaz#208)

* update sig size to 4000 bytes as per spec (blockblaz#210)

* fix: abstract away network args into a function

Signed-off-by: Chen Kai <281165273grape@gmail.com>

---------

Signed-off-by: Chen Kai <281165273grape@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: g11tech <develop@g11tech.io>
Co-authored-by: Anshal Shukla <53994948+anshalshukla@users.noreply.github.com>
Co-authored-by: g11tech <gajinder@zeam.in>
g11tech added a commit that referenced this pull request Sep 21, 2025
* fix: make start node test friendly and fix several memory leak (#198)

* fix: make start node test friendly and fix several memory leak

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* Update pkgs/types/src/lib.zig

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix: fix more memory leak when error

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* fix: fix one more error meory leak

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* Update pkgs/cli/src/node.zig

* Update pkgs/cli/src/main.zig

* fix: flattening order (#208)

* update sig size to 4000 bytes as per spec (#210)

* fix: abstract away network args into a function

Signed-off-by: Chen Kai <281165273grape@gmail.com>

---------

Signed-off-by: Chen Kai <281165273grape@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: g11tech <develop@g11tech.io>
Co-authored-by: Anshal Shukla <53994948+anshalshukla@users.noreply.github.com>
Co-authored-by: g11tech <gajinder@zeam.in>

* refactor: abstract help fn for justifications

Fix #182

* fix: fix review comments

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* fix: resolve merge issue

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* fix: fix merge error

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* fix: make justifications deinit more explict

Signed-off-by: Chen Kai <281165273grape@gmail.com>

* free justifications values

* spacing

* fix log spacing

---------

Signed-off-by: Chen Kai <281165273grape@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: g11tech <develop@g11tech.io>
Co-authored-by: Anshal Shukla <53994948+anshalshukla@users.noreply.github.com>
Co-authored-by: g11tech <gajinder@zeam.in>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sort the justification roots in asc order before re-flattening the justifications

3 participants