Skip to content

Commit 7608a6d

Browse files
Extract #getIndexHash in HashableIndex interface [ECR-4064]: (#1366)
HashableIndex is similar to ObjectHash trait in MerkleDB.
1 parent ec6bed2 commit 7608a6d

File tree

6 files changed

+57
-23
lines changed

6 files changed

+57
-23
lines changed

exonum-java-binding/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
6666
- Transaction index in block type changed from `long` to `int`. (#1348)
6767
- Extracted artifact version to the separate field from the artifact name.
6868
Artifact name format is `groupId/artifactId` now.
69-
PluginId format is `runtimeId:artifactName:artifactVersion` now. (#1349)
69+
PluginId format is `runtimeId:artifactName:artifactVersion` now. (#1349)
70+
- Extracted `#getIndexHash` into `HashableIndex` interface. (#1366)
7071

7172
### Removed
7273
- Classes supporting no longer used tree-like list proof representation.

exonum-java-binding/core/src/main/java/com/exonum/binding/core/service/Schema.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package com.exonum.binding.core.service;
1818

1919
import com.exonum.binding.core.blockchain.Block;
20-
import com.exonum.binding.core.storage.indices.ProofEntryIndexProxy;
21-
import com.exonum.binding.core.storage.indices.ProofListIndexProxy;
22-
import com.exonum.binding.core.storage.indices.ProofMapIndexProxy;
2320

2421
/**
2522
* A schema of the collections (a.k.a. indices) of a service.
@@ -38,9 +35,7 @@
3835
* the framework will not be able to verify that its transactions cause the same
3936
* results on different nodes.
4037
*
41-
* @see ProofListIndexProxy#getIndexHash()
42-
* @see ProofMapIndexProxy#getIndexHash()
43-
* @see ProofEntryIndexProxy#getIndexHash()
38+
* @see com.exonum.binding.core.storage.indices.HashableIndex
4439
* @see com.exonum.binding.core.blockchain.Blockchain
4540
*/
4641
public interface Schema {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2020 The Exonum Team
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.exonum.binding.core.storage.indices;
18+
19+
import com.exonum.binding.common.hash.HashCode;
20+
21+
/**
22+
* A hashable index provides a cryptographic hash which represents the complete state of this index.
23+
*
24+
* <p>Hashable indexes enable efficient verification of their contents. Two indexes contain
25+
* the same values if their index hashes are equal. This property is used in the consensus
26+
* algorithm to compare the database state on different nodes.
27+
*
28+
* <p>Index hashes are also used in verifiable index views. Such views contain a subset
29+
* of index elements and a proof that jointly allow to restore the index hash. The computed
30+
* index hash allows to verify that the elements come from an index with a certain state.
31+
*
32+
* <p>Hashable indexes may participate in
33+
* <a href="https://docs.rs/exonum-merkledb/0.13.0-rc.2/exonum_merkledb/#state-aggregation">state hash aggregation</a>.
34+
*
35+
* @see <a href="https://docs.rs/exonum-merkledb/0.13.0-rc.2/exonum_merkledb/trait.ObjectHash.html">ObjectHash trait</a>
36+
*/
37+
public interface HashableIndex extends StorageIndex {
38+
39+
/**
40+
* Returns the index hash which represents the complete state of this index.
41+
* Any modifications to the stored entries affect the index hash.
42+
*
43+
* <p>How index hash is computed depends on the index data structure implementation.
44+
*/
45+
HashCode getIndexHash();
46+
}

exonum-java-binding/core/src/main/java/com/exonum/binding/core/storage/indices/ProofEntryIndexProxy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
*
5151
* @see View
5252
*/
53-
public final class ProofEntryIndexProxy<T> extends AbstractIndexProxy {
53+
public final class ProofEntryIndexProxy<T> extends AbstractIndexProxy implements HashableIndex {
5454

5555
static {
5656
LibraryLoader.load();
@@ -178,8 +178,9 @@ public T get() {
178178
* <p>The entry index hash is computed as SHA-256 of the entry binary representation, or
179179
* a hash of zeroes if the entry is not set.
180180
*
181-
* @throws IllegalStateException if this list is not valid
181+
* @throws IllegalStateException if the proxy is invalid
182182
*/
183+
@Override
183184
public HashCode getIndexHash() {
184185
return HashCode.fromBytes(nativeGetIndexHash(getNativeHandle()));
185186
}

exonum-java-binding/core/src/main/java/com/exonum/binding/core/storage/indices/ProofListIndexProxy.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* @see View
5555
*/
5656
public final class ProofListIndexProxy<E> extends AbstractListIndexProxy<E>
57-
implements ListIndex<E> {
57+
implements ListIndex<E>, HashableIndex {
5858

5959
static {
6060
LibraryLoader.load();
@@ -212,12 +212,7 @@ private static ListProof parseProof(byte[] proofMessage) {
212212
}
213213
}
214214

215-
/**
216-
* Returns the index hash which represents the complete state of this list.
217-
* Any modifications to the stored entries affect the index hash.
218-
*
219-
* @throws IllegalStateException if this list is not valid
220-
*/
215+
@Override
221216
public HashCode getIndexHash() {
222217
return HashCode.fromBytes(nativeGetIndexHash(getNativeHandle()));
223218
}

exonum-java-binding/core/src/main/java/com/exonum/binding/core/storage/indices/ProofMapIndexProxy.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@
8585
* @param <V> the type of values in this map
8686
* @see View
8787
*/
88-
public final class ProofMapIndexProxy<K, V> extends AbstractIndexProxy implements MapIndex<K, V> {
88+
public final class ProofMapIndexProxy<K, V> extends AbstractIndexProxy implements MapIndex<K, V>,
89+
HashableIndex {
8990

9091
private final Serializer<K> keySerializer;
9192
private final CheckingSerializerDecorator<V> valueSerializer;
@@ -381,12 +382,7 @@ private static MapProof decodeProofMessage(byte[] proofMessage) {
381382
}
382383
}
383384

384-
/**
385-
* Returns the index hash which represents the complete state of this map.
386-
* Any modifications to the stored entries affect the index hash.
387-
*
388-
* @throws IllegalStateException if this map is not valid
389-
*/
385+
@Override
390386
public HashCode getIndexHash() {
391387
return HashCode.fromBytes(nativeGetIndexHash(getNativeHandle()));
392388
}

0 commit comments

Comments
 (0)