-
Notifications
You must be signed in to change notification settings - Fork 15
Introduce consistent typed error handling #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| use thiserror::Error; | ||
|
|
||
| #[derive(Error, Debug, PartialEq, Eq)] | ||
| pub enum ImtError { | ||
| #[error("Too many leaves: got {got}, max for arity {arity} and depth {depth} is max {max}")] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a blocker but there's a redundant "max" word here |
||
| TooManyLeaves { | ||
| got: usize, | ||
| arity: usize, | ||
| depth: usize, | ||
| max: usize, | ||
| }, | ||
| #[error("The tree is full")] | ||
| TreeFull, | ||
| #[error("The leaf does not exist in this tree")] | ||
| NotContained, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| use crate::errors::ImtError; | ||
|
|
||
| pub struct IMT { | ||
| nodes: Vec<Vec<IMTNode>>, | ||
| zeroes: Vec<IMTNode>, | ||
|
|
@@ -23,9 +25,16 @@ impl IMT { | |
| zero_value: IMTNode, | ||
| arity: usize, | ||
| leaves: Vec<IMTNode>, | ||
| ) -> Result<IMT, &'static str> { | ||
| if leaves.len() > arity.pow(depth as u32) { | ||
| return Err("The tree cannot contain more than arity^depth leaves"); | ||
| ) -> Result<IMT, ImtError> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change since How should we approach this? @vplasencia
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this what about we use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it doesn't apply here since you can't deprecate a variant shape change, and there's no new_with_error method? Let's confirm with Vivian how to approach these kind of breaking changes |
||
| let got = leaves.len(); | ||
| let max = arity.pow(depth as u32); | ||
| if got > max { | ||
| return Err(ImtError::TooManyLeaves { | ||
| got, | ||
| arity, | ||
| depth, | ||
| max, | ||
| }); | ||
| } | ||
|
|
||
| let mut imt = IMT { | ||
|
|
@@ -93,19 +102,19 @@ impl IMT { | |
| self.nodes.get(0)?.iter().position(|n| n == &leaf) | ||
| } | ||
|
|
||
| pub fn insert(&mut self, leaf: IMTNode) -> Result<(), &'static str> { | ||
| pub fn insert(&mut self, leaf: IMTNode) -> Result<(), ImtError> { | ||
| if self.nodes[0].len() >= self.arity.pow(self.depth as u32) { | ||
| return Err("The tree is full"); | ||
| return Err(ImtError::TreeFull); | ||
| } | ||
|
|
||
| let index = self.nodes[0].len(); | ||
| self.nodes[0].push(leaf); | ||
| self.update(index, self.nodes[0][index].clone()) | ||
| } | ||
|
|
||
| pub fn update(&mut self, mut index: usize, new_leaf: IMTNode) -> Result<(), &'static str> { | ||
| pub fn update(&mut self, mut index: usize, new_leaf: IMTNode) -> Result<(), ImtError> { | ||
| if index >= self.nodes[0].len() { | ||
| return Err("The leaf does not exist in this tree"); | ||
| return Err(ImtError::NotContained); | ||
| } | ||
|
|
||
| let mut node = new_leaf; | ||
|
|
@@ -138,13 +147,13 @@ impl IMT { | |
| Ok(()) | ||
| } | ||
|
|
||
| pub fn delete(&mut self, index: usize) -> Result<(), &'static str> { | ||
| pub fn delete(&mut self, index: usize) -> Result<(), ImtError> { | ||
| self.update(index, self.zeroes[0].clone()) | ||
| } | ||
|
|
||
| pub fn create_proof(&self, index: usize) -> Result<IMTMerkleProof, &'static str> { | ||
| pub fn create_proof(&self, index: usize) -> Result<IMTMerkleProof, ImtError> { | ||
| if index >= self.nodes[0].len() { | ||
| return Err("The leaf does not exist in this tree"); | ||
| return Err(ImtError::NotContained); | ||
| } | ||
|
|
||
| let mut siblings = Vec::with_capacity(self.depth); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| pub mod errors; | ||
| pub mod hash; | ||
|
protocolwhisper marked this conversation as resolved.
|
||
| pub mod imt; | ||
|
|
||
| pub use errors::ImtError; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ description = "Sparse Merkle Tree" | |
|
|
||
| [dependencies] | ||
| num-bigint = "0.4.6" | ||
| thiserror = "2" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| use thiserror::Error; | ||
|
protocolwhisper marked this conversation as resolved.
|
||
|
|
||
| #[derive(Error, Debug, PartialEq, Eq)] | ||
| pub enum SMTError { | ||
| #[error("Key {0} already exists")] | ||
| KeyAlreadyExist(String), | ||
| #[error("Key {0} does not exist")] | ||
| KeyDoesNotExist(String), | ||
| #[error("Parameter {name} must be a {expected_type}")] | ||
| InvalidParameterType { name: String, expected_type: String }, | ||
| #[error("Invalid sibling index")] | ||
| InvalidSiblingIndex, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| pub mod errors; | ||
| pub mod smt; | ||
|
protocolwhisper marked this conversation as resolved.
|
||
| mod utils; | ||
|
|
||
| pub use errors::SMTError; | ||
Uh oh!
There was an error while loading. Please reload this page.