Skip to content

Commit 528cbfe

Browse files
Add FlatListProof#verify benchmark and make it public
[skip ci]
1 parent 72ea871 commit 528cbfe

File tree

6 files changed

+108
-8
lines changed

6 files changed

+108
-8
lines changed

exonum-java-binding/benchmarks/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ THE POSSIBILITY OF SUCH DAMAGE.
6666
<version>${jmh.version}</version>
6767
<scope>provided</scope>
6868
</dependency>
69+
70+
<dependency>
71+
<groupId>com.google.guava</groupId>
72+
<artifactId>guava</artifactId>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>com.exonum.binding</groupId>
77+
<artifactId>exonum-java-binding-common</artifactId>
78+
<version>${project.version}</version>
79+
</dependency>
6980
</dependencies>
7081

7182
<build>
@@ -91,6 +102,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
91102
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
92103
<mainClass>org.openjdk.jmh.Main</mainClass>
93104
</transformer>
105+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
94106
</transformers>
95107
<filters>
96108
<filter>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2019 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+
* http://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.bench;
18+
19+
import static com.exonum.binding.common.hash.Hashing.sha256;
20+
import static com.exonum.binding.common.proofs.list.ListProofEntry.checkHeight;
21+
import static java.util.Collections.singletonList;
22+
23+
import com.exonum.binding.common.hash.HashCode;
24+
import com.exonum.binding.common.proofs.list.CheckedListProof;
25+
import com.exonum.binding.common.proofs.list.FlatListProof;
26+
import com.exonum.binding.common.proofs.list.ListProofElementEntry;
27+
import com.exonum.binding.common.proofs.list.ListProofHashedEntry;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import java.util.concurrent.TimeUnit;
31+
import org.openjdk.jmh.annotations.Benchmark;
32+
import org.openjdk.jmh.annotations.BenchmarkMode;
33+
import org.openjdk.jmh.annotations.Level;
34+
import org.openjdk.jmh.annotations.Mode;
35+
import org.openjdk.jmh.annotations.OutputTimeUnit;
36+
import org.openjdk.jmh.annotations.Param;
37+
import org.openjdk.jmh.annotations.Scope;
38+
import org.openjdk.jmh.annotations.Setup;
39+
import org.openjdk.jmh.annotations.State;
40+
41+
@State(Scope.Benchmark)
42+
@BenchmarkMode(Mode.Throughput)
43+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
44+
public class FlatListProofBenchmark {
45+
46+
@Param({"10"})
47+
private int height;
48+
49+
private FlatListProof proof;
50+
51+
/**
52+
* Creates a proof for a single element from a tree of the given height.
53+
*/
54+
@Setup(Level.Trial)
55+
public void createProof() {
56+
/*
57+
o
58+
/ \
59+
h
60+
/
61+
o
62+
/ \
63+
o h
64+
/ \
65+
e h
66+
*/
67+
checkHeight(height);
68+
long size = 1L << height;
69+
// Create the element(s)
70+
// The value size can also be made configurable, though it is not expected to have
71+
// a large effect on anything but the first hashing step.
72+
byte[] value = {1};
73+
List<ListProofElementEntry> elements = singletonList(
74+
ListProofElementEntry.newInstance(0L, value));
75+
// Create the hash nodes
76+
List<ListProofHashedEntry> hashed = new ArrayList<>(height);
77+
for (int h = 0; h < height; h++) {
78+
HashCode hash = sha256().hashInt(h);
79+
hashed.add(ListProofHashedEntry.newInstance(1L, h, hash));
80+
}
81+
proof = new FlatListProof(elements, hashed, size);
82+
}
83+
84+
@Benchmark
85+
public CheckedListProof<?> verify() {
86+
return proof.verify();
87+
}
88+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* A flat list proof. It proves that certain elements are present in a proof list
4646
* of a certain size.
4747
*/
48-
class FlatListProof {
48+
public class FlatListProof {
4949
/*
5050
Proof lists are represented as BSTs, where all leaf elements are at the same height.
5151
Here is a tree for a three-element proof list:
@@ -95,14 +95,14 @@ class FlatListProof {
9595
private final List<ListProofHashedEntry> proof;
9696
private final long size;
9797

98-
FlatListProof(List<ListProofElementEntry> elements,
98+
public FlatListProof(List<ListProofElementEntry> elements,
9999
List<ListProofHashedEntry> proof, long size) {
100100
this.elements = checkNotNull(elements);
101101
this.proof = checkNotNull(proof);
102102
this.size = size;
103103
}
104104

105-
CheckedListProof<byte[]> verify() {
105+
public CheckedListProof<byte[]> verify() {
106106
// Check the size
107107
if (size < 0 || MAX_SIZE < size) {
108108
throw new InvalidProofException(String.format("Invalid size (%s), must be in range [0; 2^56]",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
* A value stored in the Merkle tree at its bottom level (at height 0).
2323
*/
2424
@AutoValue
25-
abstract class ListProofElementEntry implements ListProofEntry {
25+
public abstract class ListProofElementEntry implements ListProofEntry {
2626

2727
/**
2828
* Returns a value of the element stored at this index in the list.
2929
*/
3030
abstract byte[] getElement();
3131

32-
static ListProofElementEntry newInstance(long index, byte[] element) {
32+
public static ListProofElementEntry newInstance(long index, byte[] element) {
3333
ListProofEntry.checkIndex(index);
3434
return new AutoValue_ListProofElementEntry(index, element);
3535
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
2020

21-
interface ListProofEntry {
21+
public interface ListProofEntry {
2222

2323
/**
2424
* The maximum height of a list proof tree.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* A hash of a sub-tree in a Merkle proof tree.
2424
*/
2525
@AutoValue
26-
abstract class ListProofHashedEntry implements ListProofEntry {
26+
public abstract class ListProofHashedEntry implements ListProofEntry {
2727

2828
/**
2929
* Returns the height of the proof tree node corresponding to this entry.
@@ -37,7 +37,7 @@ abstract class ListProofHashedEntry implements ListProofEntry {
3737
*/
3838
abstract HashCode getHash();
3939

40-
static ListProofHashedEntry newInstance(long index, int height, HashCode nodeHash) {
40+
public static ListProofHashedEntry newInstance(long index, int height, HashCode nodeHash) {
4141
ListProofEntry.checkIndex(index);
4242
ListProofEntry.checkHeight(height);
4343
return new AutoValue_ListProofHashedEntry(index, height, nodeHash);

0 commit comments

Comments
 (0)