-
Notifications
You must be signed in to change notification settings - Fork 43
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
feat: verify the range of the namespace IDs of the children in the HashNode #102
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
622cc16
examines the namespace of children in the HashNode
staheri14 ed08c84
adds unit test for the correctness of namespace range check in the Ha…
staheri14 27d3763
removes spaces from the test titles
staheri14 20a2f53
adds two intermediate variables to hold the left and right children
staheri14 72a99b4
picks more clear variable names
staheri14 19d349f
fixes var name capitalization
staheri14 7caa9c8
removes an invalid testcase
staheri14 0cb2c49
renames panic to wantPanic for consistency with other tests
staheri14 64ee062
Merge branch 'master' into verify-nodes-order-in-hashnpde
staheri14 e7415b7
explains when HashNode can panic
staheri14 5866f52
Merge remote-tracking branch 'origin/master' into verify-nodes-order-…
staheri14 887c86f
fixes formatting issue causing CI failure
staheri14 e81c6ca
fixes gofumpt complain
staheri14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,14 +89,6 @@ func Test_namespacedTreeHasher_HashNode(t *testing.T) { | |
sum(crypto.SHA256, []byte{NodePrefix}, []byte{0, 0, 0, 0}, []byte{0, 0, 1, 1})..., | ||
), | ||
}, | ||
{ | ||
"leftmin==rightmin && leftmax>rightmax", 2, | ||
children{[]byte{0, 0, 1, 1}, []byte{0, 0, 0, 1}}, | ||
append( | ||
[]byte{0, 0, 1, 1}, | ||
sum(crypto.SHA256, []byte{NodePrefix}, []byte{0, 0, 1, 1}, []byte{0, 0, 0, 1})..., | ||
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 test case is deleted as this represents an invalid state given the newly added logic i.e., the max namespace of the right child cannot be greater than the min namespace of the right child. |
||
), | ||
}, | ||
// XXX: can this happen in practice? or is this an invalid state? | ||
{ | ||
"leftmin>rightmin && leftmax<rightmax", 2, | ||
|
@@ -197,3 +189,46 @@ func TestNamespaceHasherSum(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestHashNode_ChildrenNamespaceRange(t *testing.T) { | ||
type children struct { | ||
l []byte // namespace hash of the left child with the format of MinNs||MaxNs||h | ||
r []byte // namespace hash of the right child with the format of MinNs||MaxNs||h | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
nidLen namespace.IDSize | ||
children children | ||
wantPanic bool // whether the test should panic or nor | ||
}{ | ||
{ | ||
"left.maxNs>right.minNs", 2, | ||
children{[]byte{0, 0, 1, 1}, []byte{0, 0, 1, 1}}, | ||
true, // this test case should panic since in an ordered NMT, left.maxNs cannot be greater than right.minNs | ||
}, | ||
{ | ||
"left.maxNs=right.minNs", 2, | ||
children{[]byte{0, 0, 1, 1}, []byte{1, 1, 2, 2}}, | ||
false, | ||
}, | ||
{ | ||
"left.maxNs<right.minNs", 2, | ||
children{[]byte{0, 0, 1, 1}, []byte{2, 2, 3, 3}}, | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
defer func() { | ||
gotPanic := false | ||
if r := recover(); r != nil { // here we check whether panic happened | ||
gotPanic = true | ||
} | ||
assert.Equal(t, tt.wantPanic, gotPanic) | ||
}() | ||
n := NewNmtHasher(sha256.New(), tt.nidLen, false) | ||
n.HashNode(tt.children.l, tt.children.r) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would panicking here mean that someone could send a light node an invalid proof or sample and their node would crash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you mentioned, the
HashNode
method may only fail in the event of an attack on the system. However, it is important to note that the method was previously susceptible to panic, even prior to this pull request. For example, panic can occur while extracting the namespace identifier of the left or right children if they are not namespaced (i.e., if their length is less than NamespaceLen). It is not possible to return an error from this method without violating the interfaces it is intended to fulfill, namelyNodeHasher
andhash
.At this time, it may be better to allow the light client to crash rather than enter an invalid state.
I will investigate the implementation of proper error handling in the
HashNode
method, including handling other potential errors beyond just the namespace ID range, in a follow-up pull request. As this has a broad scope, it requires careful attention and consideration during implementation.Tracking issue #109
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think when this was implemented, we might have panic'ed bc it was supposed to be impossible to construct an invalid tree. But it is good thinking that someone might do so to crash light nodes and I do think we should error instead.
edit: I see. The Hash interface was another reason. Either we make it impossible client side that this panics (e.g. by doing some verification before calling this; then this should be very explicitly documented) or we have to break with the iface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the sake of getting this PR merged: can we add this property/panic to the godoc? So the caller has a hint that verification has to happen before to not cause unexpected panics?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! please check the following commit e7415b7