Skip to content

Commit e34be40

Browse files
MakarovSskletsun
authored andcommitted
Remove ListProof class as redundant [ECR-3429] (#1089)
1 parent 039fa53 commit e34be40

File tree

11 files changed

+124
-197
lines changed

11 files changed

+124
-197
lines changed

exonum-java-binding/common/src/main/java/com/exonum/binding/common/proofs/list/ListProof.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

exonum-java-binding/common/src/main/java/com/exonum/binding/common/proofs/list/ListProofHashCalculator.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,17 @@
2323
import com.exonum.binding.common.hash.HashFunction;
2424
import com.exonum.binding.common.hash.Hashing;
2525
import com.exonum.binding.common.hash.PrimitiveSink;
26-
import com.exonum.binding.common.serialization.CheckingSerializerDecorator;
27-
import com.exonum.binding.common.serialization.Serializer;
2826
import com.google.common.annotations.VisibleForTesting;
27+
import com.google.protobuf.ByteString;
2928

3029
import java.util.NavigableMap;
3130
import java.util.Optional;
3231
import java.util.TreeMap;
3332

3433
/**
3534
* List proof hash calculator.
36-
*
37-
* @param <E> the type of elements in the corresponding list
3835
*/
39-
final class ListProofHashCalculator<E> implements ListProofVisitor {
36+
final class ListProofHashCalculator implements ListProofVisitor {
4037

4138
@VisibleForTesting
4239
static final byte BLOB_PREFIX = 0x00;
@@ -45,11 +42,9 @@ final class ListProofHashCalculator<E> implements ListProofVisitor {
4542
@VisibleForTesting
4643
static final byte LIST_ROOT_PREFIX = 0x02;
4744

48-
private final CheckingSerializerDecorator<E> serializer;
49-
5045
private final HashFunction hashFunction;
5146

52-
private final NavigableMap<Long, E> elements;
47+
private final NavigableMap<Long, ByteString> elements;
5348

5449
private long index;
5550

@@ -58,26 +53,24 @@ final class ListProofHashCalculator<E> implements ListProofVisitor {
5853
/**
5954
* Creates a new ListProofHashCalculator.
6055
*
61-
* @param serializer a serializer of list elements
56+
* @param rootNode the root node of the proof tree
57+
* @param length the length of the corresponding index
6258
*/
63-
ListProofHashCalculator(ListProof listProof, Serializer<E> serializer) {
64-
this(listProof, serializer, Hashing.defaultHashFunction());
59+
ListProofHashCalculator(ListProofNode rootNode, long length) {
60+
this(rootNode, length, Hashing.defaultHashFunction());
6561
}
6662

67-
private ListProofHashCalculator(ListProof listProof, Serializer<E> serializer,
68-
HashFunction hashFunction) {
69-
this.serializer = CheckingSerializerDecorator.from(serializer);
63+
private ListProofHashCalculator(ListProofNode rootNode, long length, HashFunction hashFunction) {
7064
this.hashFunction = checkNotNull(hashFunction);
7165
elements = new TreeMap<>();
7266
index = 0;
7367
hash = null;
7468

75-
ListProofNode rootNode = listProof.getRootNode();
7669
rootNode.accept(this);
7770

7871
hash = hashFunction.newHasher()
7972
.putByte(LIST_ROOT_PREFIX)
80-
.putLong(listProof.getLength())
73+
.putLong(length)
8174
.putObject(hash, hashCodeFunnel())
8275
.hash();
8376
}
@@ -107,7 +100,7 @@ public void visit(ListProofElement value) {
107100
"Error: already an element by such index in the map: i=" + index
108101
+ ", e=" + elements.get(index);
109102

110-
E element = serializer.fromBytes(value.getElement().toByteArray());
103+
ByteString element = value.getElement();
111104
elements.put(index, element);
112105
hash = hashFunction.newHasher()
113106
.putByte(BLOB_PREFIX)
@@ -136,7 +129,7 @@ private Optional<HashCode> visitRight(ListProofBranch branch, long parentIndex)
136129
/**
137130
* Returns a collection of list entries: index-element pairs, ordered by indices.
138131
*/
139-
NavigableMap<Long, E> getElements() {
132+
NavigableMap<Long, ByteString> getElements() {
140133
return elements;
141134
}
142135

exonum-java-binding/common/src/main/java/com/exonum/binding/common/proofs/list/UncheckedListProof.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface UncheckedListProof {
2828
CheckedListProof check();
2929

3030
/**
31-
* Returns raw source proof of this UncheckedListProof.
31+
* Returns the root node of the corresponding proof tree of this UncheckedListProof.
3232
*/
33-
ListProof getListProof();
33+
ListProofNode getListProofRootNode();
3434
}

exonum-java-binding/common/src/main/java/com/exonum/binding/common/proofs/list/UncheckedListProofAdapter.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,34 @@
1616

1717
package com.exonum.binding.common.proofs.list;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
1921
import com.exonum.binding.common.hash.HashCode;
20-
import com.exonum.binding.common.serialization.Serializer;
21-
import com.google.common.base.Preconditions;
2222

2323
/**
2424
* An Adapter class used to simplify work with ListProof interfaces.
2525
*/
26-
public class UncheckedListProofAdapter<E> implements UncheckedListProof {
26+
public class UncheckedListProofAdapter implements UncheckedListProof {
2727

28-
private final ListProof listProof;
28+
private final ListProofNode rootNode;
2929

3030
private final ListProofStructureValidator listProofStructureValidator;
3131

32-
private final ListProofHashCalculator<E> listProofHashCalculator;
32+
private final ListProofHashCalculator listProofHashCalculator;
3333

3434
/**
3535
* Creates UncheckedListProofAdapter for convenient usage of ListProof interfaces.
3636
*
3737
* <p>UncheckedListProofAdapter {@link #check()} method will return CheckedListProof containing
3838
* results of list proof verification.
3939
*
40-
* @param listProof source list proof with index length
41-
* @param serializer proof elements serializer
40+
* @param rootNode the root node of the proof tree
41+
* @param length the length of the corresponding index (needed for index hash calculation)
4242
*/
43-
public UncheckedListProofAdapter(ListProof listProof, Serializer<E> serializer) {
44-
Preconditions.checkNotNull(listProof, "ListProof node must be not null");
45-
Preconditions.checkNotNull(serializer, "Serializer must be not null");
46-
47-
this.listProof = listProof;
48-
this.listProofStructureValidator = new ListProofStructureValidator(listProof.getRootNode());
49-
this.listProofHashCalculator = new ListProofHashCalculator<>(listProof, serializer);
43+
public UncheckedListProofAdapter(ListProofNode rootNode, long length) {
44+
this.rootNode = checkNotNull(rootNode);
45+
this.listProofStructureValidator = new ListProofStructureValidator(rootNode);
46+
this.listProofHashCalculator = new ListProofHashCalculator(rootNode, length);
5047
}
5148

5249
@Override
@@ -59,7 +56,7 @@ public CheckedListProof check() {
5956
}
6057

6158
@Override
62-
public ListProof getListProof() {
63-
return listProof;
59+
public ListProofNode getListProofRootNode() {
60+
return rootNode;
6461
}
6562
}

exonum-java-binding/common/src/test/java/com/exonum/binding/common/proofs/list/ListProofHashCalculatorTest.java

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,52 @@
1919
import static com.exonum.binding.common.proofs.list.ListProofUtils.getBranchHashCode;
2020
import static com.exonum.binding.common.proofs.list.ListProofUtils.getNodeHashCode;
2121
import static com.exonum.binding.common.proofs.list.ListProofUtils.getProofListHash;
22-
import static com.exonum.binding.common.proofs.list.ListProofUtils.leafOf;
2322
import static com.google.common.collect.ImmutableMap.of;
2423
import static org.hamcrest.CoreMatchers.equalTo;
2524
import static org.hamcrest.MatcherAssert.assertThat;
2625
import static org.junit.jupiter.api.Assertions.assertEquals;
2726

2827
import com.exonum.binding.common.hash.HashCode;
29-
import com.exonum.binding.common.serialization.StandardSerializers;
28+
import com.google.protobuf.ByteString;
3029
import org.junit.jupiter.api.Test;
3130

3231
class ListProofHashCalculatorTest {
3332

34-
private static final String V1 = "v1";
35-
private static final String V2 = "v2";
36-
private static final String V3 = "v3";
37-
private static final String V4 = "v4";
33+
private static final ByteString V1 = ByteString.copyFromUtf8("v1");
34+
private static final ByteString V2 = ByteString.copyFromUtf8("v2");
35+
private static final ByteString V3 = ByteString.copyFromUtf8("v3");
36+
private static final ByteString V4 = ByteString.copyFromUtf8("v4");
3837

3938
private static final HashCode H1 = HashCode.fromString("a1");
4039
private static final HashCode H2 = HashCode.fromString("a2");
4140

42-
private ListProofHashCalculator<String> calculator;
41+
private ListProofHashCalculator calculator;
4342

4443
@Test
4544
void visit_SingletonListProof() {
46-
ListProofNode root = leafOf(V1);
45+
ListProofNode root = new ListProofElement(V1);
4746

48-
ListProof listProof = new ListProof(root, 1);
49-
calculator = createListProofCalculator(listProof);
47+
long length = 1;
48+
calculator = new ListProofHashCalculator(root, length);
5049

51-
HashCode expectedProofListHash = getProofListHash(getNodeHashCode(V1), listProof.getLength());
50+
HashCode expectedProofListHash = getProofListHash(getNodeHashCode(V1), length);
5251

5352
assertThat(calculator.getElements(), equalTo(of(0L, V1)));
5453
assertEquals(expectedProofListHash, calculator.getHash());
5554
}
5655

5756
@Test
5857
void visit_FullProof2elements() {
59-
ListProofElement left = leafOf(V1);
60-
ListProofElement right = leafOf(V2);
58+
ListProofElement left = new ListProofElement(V1);
59+
ListProofElement right = new ListProofElement(V2);
6160
ListProofBranch root = new ListProofBranch(left, right);
6261

63-
ListProof listProof = new ListProof(root, 2);
64-
calculator = createListProofCalculator(listProof);
62+
long length = 2;
63+
calculator = new ListProofHashCalculator(root, length);
6564

6665
// Calculate expected proof list hash
6766
HashCode expectedRootHash = getBranchHashCode(getNodeHashCode(V1), getNodeHashCode(V2));
68-
HashCode expectedProofListHash = getProofListHash(expectedRootHash, listProof.getLength());
67+
HashCode expectedProofListHash = getProofListHash(expectedRootHash, length);
6968

7069
assertThat(calculator.getElements(), equalTo(of(0L, V1,
7170
1L, V2)));
@@ -76,23 +75,23 @@ void visit_FullProof2elements() {
7675
void visit_FullProof4elements() {
7776
ListProofBranch root = new ListProofBranch(
7877
new ListProofBranch(
79-
leafOf(V1),
80-
leafOf(V2)
78+
new ListProofElement(V1),
79+
new ListProofElement(V2)
8180
),
8281
new ListProofBranch(
83-
leafOf(V3),
84-
leafOf(V4)
82+
new ListProofElement(V3),
83+
new ListProofElement(V4)
8584
)
8685
);
8786

88-
ListProof listProof = new ListProof(root, 4);
89-
calculator = createListProofCalculator(listProof);
87+
long length = 4;
88+
calculator = new ListProofHashCalculator(root, length);
9089

9190
// Calculate expected proof list hash
9291
HashCode leftBranchHash = getBranchHashCode(getNodeHashCode(V1), getNodeHashCode(V2));
9392
HashCode rightBranchHash = getBranchHashCode(getNodeHashCode(V3), getNodeHashCode(V4));
9493
HashCode expectedRootHash = getBranchHashCode(leftBranchHash, rightBranchHash);
95-
HashCode expectedProofListHash = getProofListHash(expectedRootHash, listProof.getLength());
94+
HashCode expectedProofListHash = getProofListHash(expectedRootHash, length);
9695

9796

9897
assertThat(calculator.getElements(),
@@ -105,15 +104,15 @@ void visit_FullProof4elements() {
105104

106105
@Test
107106
void visit_ProofLeftValue() {
108-
ListProofNode left = leafOf(V1);
107+
ListProofNode left = new ListProofElement(V1);
109108
ListProofNode right = new ListProofHashNode(H2);
110109
ListProofBranch root = new ListProofBranch(left, right);
111110

112-
ListProof listProof = new ListProof(root, 2);
113-
calculator = createListProofCalculator(listProof);
111+
long length = 2;
112+
calculator = new ListProofHashCalculator(root, length);
114113

115114
HashCode expectedRootHash = getBranchHashCode(getNodeHashCode(V1), H2);
116-
HashCode expectedProofListHash = getProofListHash(expectedRootHash, listProof.getLength());
115+
HashCode expectedProofListHash = getProofListHash(expectedRootHash, length);
117116

118117
assertThat(calculator.getElements(), equalTo(of(0L, V1)));
119118
assertEquals(expectedProofListHash, calculator.getHash());
@@ -122,14 +121,14 @@ void visit_ProofLeftValue() {
122121
@Test
123122
void visit_ProofRightValue() {
124123
ListProofNode left = new ListProofHashNode(H1);
125-
ListProofNode right = leafOf(V2);
124+
ListProofNode right = new ListProofElement(V2);
126125
ListProofBranch root = new ListProofBranch(left, right);
127126

128-
ListProof listProof = new ListProof(root, 2);
129-
calculator = createListProofCalculator(listProof);
127+
long length = 2;
128+
calculator = new ListProofHashCalculator(root, length);
130129

131130
HashCode expectedRootHash = getBranchHashCode(H1, getNodeHashCode(V2));
132-
HashCode expectedProofListHash = getProofListHash(expectedRootHash, listProof.getLength());
131+
HashCode expectedProofListHash = getProofListHash(expectedRootHash, length);
133132

134133
assertThat(calculator.getElements(), equalTo(of(1L, V2)));
135134
assertEquals(expectedProofListHash, calculator.getHash());
@@ -139,22 +138,22 @@ void visit_ProofRightValue() {
139138
void visit_FullProof3elements() {
140139
ListProofBranch root = new ListProofBranch(
141140
new ListProofBranch(
142-
leafOf(V1),
143-
leafOf(V2)
141+
new ListProofElement(V1),
142+
new ListProofElement(V2)
144143
),
145144
new ListProofBranch(
146-
leafOf(V3),
145+
new ListProofElement(V3),
147146
null
148147
)
149148
);
150149

151-
ListProof listProof = new ListProof(root, 3);
152-
calculator = createListProofCalculator(listProof);
150+
long length = 3;
151+
calculator = new ListProofHashCalculator(root, length);
153152

154153
HashCode leftBranchHash = getBranchHashCode(getNodeHashCode(V1), getNodeHashCode(V2));
155154
HashCode rightBranchHash = getBranchHashCode(getNodeHashCode(V3), null);
156155
HashCode expectedRootHash = getBranchHashCode(leftBranchHash, rightBranchHash);
157-
HashCode expectedProofListHash = getProofListHash(expectedRootHash, listProof.getLength());
156+
HashCode expectedProofListHash = getProofListHash(expectedRootHash, length);
158157

159158
assertThat(calculator.getElements(),
160159
equalTo(of(
@@ -164,8 +163,4 @@ void visit_FullProof3elements() {
164163
);
165164
assertEquals(expectedProofListHash, calculator.getHash());
166165
}
167-
168-
private ListProofHashCalculator<String> createListProofCalculator(ListProof listProof) {
169-
return new ListProofHashCalculator<>(listProof, StandardSerializers.string());
170-
}
171166
}

0 commit comments

Comments
 (0)