-
Notifications
You must be signed in to change notification settings - Fork 30
Add flat list proof support [ECR-3608] #1156
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
Merged
dmitry-timofeev
merged 24 commits into
exonum:dynamic-services
from
dmitry-timofeev:flat-list-proofs-ECR-3608
Oct 16, 2019
Merged
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
583097f
WIP Add FlatListProof tests
dmitry-timofeev 4b00ad3
Add tests for 2-element valid proofs
dmitry-timofeev b2188b4
Add todos on incomplete trees
dmitry-timofeev b2821c5
Add tests for trees having odd numbers of nodes on some levels
dmitry-timofeev 6df880a
Fix the number of elements
dmitry-timofeev c37810b
Extend the integration test to cover various odd-numbered lists
dmitry-timofeev 388b0a6
Add tests for empty-range proof.
dmitry-timofeev 9a3f6b0
WIP Impl proof verification:
dmitry-timofeev b48cd7c
Fix duplicate elements detection
dmitry-timofeev a169dc6
Add a test for invalid hash entry at the top.
dmitry-timofeev ab577b9
Improve test for redundant hash nodes overriding the calculated one.
dmitry-timofeev 06abb62
Migrate off multimap
dmitry-timofeev 1731e8b
Extract newCheckedProof
dmitry-timofeev 0bca025
Fix the checkstyle
dmitry-timofeev f1dc209
Refactor the verification code, improve documentation
dmitry-timofeev 0f6e9b6
Link the article
dmitry-timofeev 7d8e096
Disable the new IT
dmitry-timofeev 978230e
Add a test for size checks
dmitry-timofeev 3a461e1
Fix final
dmitry-timofeev e9ef8ab
Fix the HashMap with expected size instantiation:
dmitry-timofeev 1228987
Fix unused import
dmitry-timofeev c023f4c
Resolve the todo
dmitry-timofeev 8ecbff9
WIP resolve some todos
dmitry-timofeev cb286cb
Push #getHeight to the hashed entry:
dmitry-timofeev 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 hidden or 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 hidden or 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 hidden or 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
42 changes: 42 additions & 0 deletions
42
...ava-binding/common/src/main/java/com/exonum/binding/common/proofs/list/FlatListProof.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright 2019 The Exonum Team | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.exonum.binding.common.proofs.list; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A flat list proof. It proves that certain elements are present in a proof list | ||
| * of a certain size. | ||
| */ | ||
| class FlatListProof { | ||
| private final List<ListProofElementEntry> elements; | ||
| private final List<ListProofHashedEntry> proof; | ||
| private final long size; | ||
|
|
||
| FlatListProof(List<ListProofElementEntry> elements, | ||
| List<ListProofHashedEntry> proof, long size) { | ||
| this.elements = checkNotNull(elements); | ||
| this.proof = checkNotNull(proof); | ||
| this.size = size; | ||
| } | ||
|
|
||
| CheckedListProof<byte[]> verify() { | ||
| return null; | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...ing/common/src/main/java/com/exonum/binding/common/proofs/list/InvalidProofException.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright 2019 The Exonum Team | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.exonum.binding.common.proofs.list; | ||
|
|
||
| /** | ||
| * todo: | ||
| */ | ||
| public class InvalidProofException extends RuntimeException { | ||
|
|
||
| /** | ||
| * Constructs a new runtime exception with {@code null} as its detail message. The cause is not | ||
| * initialized, and may subsequently be initialized by a call to {@link #initCause}. | ||
| */ | ||
| public InvalidProofException() { | ||
| super(); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new runtime exception with the specified detail message. The cause is not | ||
| * initialized, and may subsequently be initialized by a call to {@link #initCause}. | ||
| * | ||
| * @param message the detail message. The detail message is saved for later retrieval by the | ||
| * {@link #getMessage()} method. | ||
| */ | ||
| public InvalidProofException(String message) { | ||
| super(message); | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
...ing/common/src/main/java/com/exonum/binding/common/proofs/list/ListProofElementEntry.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright 2019 The Exonum Team | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.exonum.binding.common.proofs.list; | ||
|
|
||
| import com.google.auto.value.AutoValue; | ||
|
|
||
| /** | ||
| * A value stored in the Merkle tree at its bottom level (at height 0). | ||
| */ | ||
| @AutoValue | ||
| abstract class ListProofElementEntry implements ListProofEntry { | ||
|
|
||
| /** | ||
| * Returns a value of the element stored at this index in the list. | ||
| */ | ||
| abstract byte[] getElement(); | ||
|
|
||
| static ListProofElementEntry newInstance(long index, byte[] element) { | ||
| ListProofEntry.checkIndex(index); | ||
| return new AutoValue_ListProofElementEntry(index, 0, element); | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
...va-binding/common/src/main/java/com/exonum/binding/common/proofs/list/ListProofEntry.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2019 The Exonum Team | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.exonum.binding.common.proofs.list; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
|
|
||
| interface ListProofEntry { | ||
|
|
||
| /** | ||
| * The maximum height of a list proof tree. | ||
| */ | ||
| int MAX_HEIGHT = 56; | ||
|
|
||
| /** | ||
| * The maximum index of a list proof node: 2^56 - 1. | ||
| */ | ||
| long MAX_INDEX = 0xFF_FFFF_FFFF_FFFFL; | ||
|
|
||
| /** | ||
| * Returns the index of the proof tree node at the height of its level. Indexes start | ||
| * from 0 for the leftmost node and up to <em>2^d - 1</em> for the rightmost node, | ||
| * where <em>d = ceil(log2(N)) - h</em> is the depth of the node at height <em>h</em>; | ||
| * <em>N</em> is the number of elements in the tree. | ||
| */ | ||
| long getIndex(); | ||
|
|
||
| /** | ||
| * Returns the height of the proof tree node corresponding to this entry. | ||
| * The height of leaf nodes is equal to 0; the height of the root, or top node: | ||
| * <em>ceil(log2(N))</em>. | ||
| */ | ||
| int getHeight(); | ||
|
|
||
| static void checkIndex(long index) { | ||
| checkArgument(0 <= index && index <= MAX_INDEX, | ||
| "Entry index (%s) is out of range [0; 2^56]", index); | ||
| } | ||
|
|
||
| static void checkHeight(int height) { | ||
| checkArgument(0 <= height && height <= MAX_HEIGHT, | ||
| "Entry height (%s) is out of range [0; 56]", height); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
...ding/common/src/main/java/com/exonum/binding/common/proofs/list/ListProofHashedEntry.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright 2019 The Exonum Team | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.exonum.binding.common.proofs.list; | ||
|
|
||
| import com.exonum.binding.common.hash.HashCode; | ||
| import com.google.auto.value.AutoValue; | ||
|
|
||
| /** | ||
| * A hash of a sub-tree in a Merkle proof tree. | ||
| */ | ||
| @AutoValue | ||
| abstract class ListProofHashedEntry implements ListProofEntry { | ||
| // todo: do we need the interface (are we going to operate on the entries using the interface?) | ||
| // Do we need entries at all, or just hashes? | ||
|
|
||
| /** | ||
| * Returns the hash of the sub-tree this entry represents. | ||
| */ | ||
| abstract HashCode getHash(); | ||
|
|
||
| static ListProofHashedEntry newInstance(long index, int height, HashCode nodeHash) { | ||
| ListProofEntry.checkIndex(index); | ||
| ListProofEntry.checkHeight(height); | ||
| return new AutoValue_ListProofHashedEntry(index, height, nodeHash); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Aren't we throwing an exception?
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.
The flat proof currently does not implement UncheckedProof interface, that's why it may and does throw exceptions. I decided exceptions are easier to implement and test. The question is on the original reasoning to represent them this way.
There is a year-old https://jira.bf.local/browse/ECR-2410 to reconsider.