Conversation
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It is safe as the roots are fetched from justifications map, if a missing root is encountered then it is ideal to panic
| 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); |
There was a problem hiding this comment.
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.
|
Not sure, if there is a good way to add tests for this as specs doesn't contain any functional tests |
g11tech
left a comment
There was a problem hiding this comment.
looks good, this will be covered in spec tests otherwise stateroots won't match
* 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>
…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>
* 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>
Closes #201