You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat!: updates the HashNode and HashLeaf methods to return error instead of panic and refactors the code (celestiaorg#136)
## Overview
Closescelestiaorg#109 and celestiaorg#99.
This pull request ensures that the `HashNode` and `HashLeaf` functions
return errors instead of panicking, and that caller functions properly
handle any emitted errors. When generating proofs, any hashing errors
are propagated with additional context. During verification, hashing
errors are treated as unsuccessful proof verification.
Note that in this PR, the term `Irrecoverable errors` used to describe
errors that any number of retries can correct the them.
## Checklist
- [x] New and updated code has appropriate documentation
- [x] New and updated code has new and/or updated testing
- [x] Required CI checks are passing
- [x] Visual proof for any user facing features like CLI or
documentation updates
- [x] Linked issues closed with keywords
// ValidateNodeFormat checks whether the supplied node conforms to the
187
+
// namespaced hash format and returns an error if it does not. Specifically, it returns ErrInvalidNodeLen if the length of the node is less than the 2*namespace length which indicates it does not match the namespaced hash format.
// HashNode calculates a namespaced hash of a node using the supplied left and
214
-
// right children. The input values, "left" and "right," are namespaced hash
215
-
// values with the format "minNID || maxNID || hash." The HashNode function may
216
-
// panic if the inputs provided are invalid, i.e., when left and right are not
217
-
// in the namespaced hash format or when left.maxNID is greater than
218
-
// right.minNID. To avoid causing panic, it is recommended to first call
219
-
// ValidateNodes(left, right) to check if the criteria are met before invoking
220
-
// the HashNode function. By default, the normal namespace hash calculation is
221
-
// followed, which is "res = min(left.minNID, right.minNID) || max(left.maxNID,
222
-
// right.maxNID) || H(NodePrefix, left, right)". "res" refers to the return
223
-
// value of the HashNode. However, if the "ignoreMaxNs" property of the Hasher
231
+
// right children. The input values, `left` and `right,` are namespaced hash
232
+
// values with the format `minNID || maxNID || hash.`
233
+
// The HashNode function returns an error if the provided inputs are invalid. Specifically, it returns the ErrInvalidNodeLen error if the left and right inputs are not in the namespaced hash format,
234
+
// and the ErrUnorderedSiblings error if left.maxNID is greater than right.minNID.
235
+
// By default, the normal namespace hash calculation is
236
+
// followed, which is `res = min(left.minNID, right.minNID) || max(left.maxNID,
237
+
// right.maxNID) || H(NodePrefix, left, right)`. `res` refers to the return
238
+
// value of the HashNode. However, if the `ignoreMaxNs` property of the Hasher
224
239
// is set to true, the calculation of the namespace ID range of the node
225
240
// slightly changes. In this case, when setting the upper range, the maximum
226
241
// possible namespace ID (i.e., 2^NamespaceIDSize-1) should be ignored if
227
242
// possible. This is achieved by taking the maximum value among only those namespace
228
243
// IDs available in the range of its left and right children that are not
229
244
// equal to the maximum possible namespace ID value. If all the namespace IDs are equal
230
245
// to the maximum possible value, then the maximum possible value is used.
231
-
func (n*Hasher) HashNode(left, right []byte) []byte {
246
+
func (n*Hasher) HashNode(left, right []byte) ([]byte, error) {
232
247
h:=n.baseHasher
233
248
h.Reset()
234
249
235
-
iferr:=n.validateNodeFormat(left); err!=nil {
236
-
panic(err)
250
+
iferr:=n.ValidateNodeFormat(left); err!=nil {
251
+
returnnil, err
237
252
}
238
-
iferr:=n.validateNodeFormat(right); err!=nil {
239
-
panic(err)
253
+
iferr:=n.ValidateNodeFormat(right); err!=nil {
254
+
returnnil, err
240
255
}
241
256
242
257
// check the namespace range of the left and right children
0 commit comments