Skip to content

Commit 6d1e4d4

Browse files
chore: Lazy initialize SNARK_KEYS (#18709)
Signed-off-by: Neeharika-Sompalli <[email protected]>
1 parent a608c2c commit 6d1e4d4

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

hedera-node/hedera-app/src/main/java/com/hedera/node/app/history/impl/HistoryLibraryImpl.java

+14-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import static java.util.Objects.requireNonNull;
55

6+
import com.google.common.base.Supplier;
7+
import com.google.common.base.Suppliers;
68
import com.hedera.cryptography.rpm.HistoryLibraryBridge;
79
import com.hedera.cryptography.rpm.ProvingAndVerifyingSnarkKeys;
810
import com.hedera.cryptography.rpm.SigningAndVerifyingSchnorrKeys;
@@ -23,11 +25,18 @@ public class HistoryLibraryImpl implements HistoryLibrary {
2325

2426
private static final SplittableRandom RANDOM = new SplittableRandom();
2527
private static final HistoryLibraryBridge BRIDGE = HistoryLibraryBridge.getInstance();
26-
private static ProvingAndVerifyingSnarkKeys SNARK_KEYS;
28+
private static final Supplier<ProvingAndVerifyingSnarkKeys> SNARK_KEYS = Suppliers.memoize(() -> {
29+
try {
30+
var elf = HistoryLibraryBridge.loadAddressBookRotationProgram();
31+
return BRIDGE.snarkVerificationKey(elf);
32+
} catch (IOException e) {
33+
throw new IllegalStateException("Could not load HistoryLibrary ELF", e);
34+
}
35+
});
2736

2837
@Override
2938
public Bytes snarkVerificationKey() {
30-
return Bytes.wrap(snarkKeys().verifyingKey());
39+
return Bytes.wrap(SNARK_KEYS.get().verifyingKey());
3140
}
3241

3342
@Override
@@ -94,8 +103,8 @@ public Bytes proveChainOfTrust(
94103
throw new IllegalArgumentException("The number of weights and verifying keys must be the same");
95104
}
96105
final var proof = BRIDGE.proveChainOfTrust(
97-
snarkKeys().provingKey(),
98-
snarkKeys().verifyingKey(),
106+
SNARK_KEYS.get().provingKey(),
107+
SNARK_KEYS.get().verifyingKey(),
99108
genesisAddressBookHash.toByteArray(),
100109
currentAddressBookVerifyingKeys,
101110
currentAddressBookWeights,
@@ -111,18 +120,6 @@ public Bytes proveChainOfTrust(
111120
@Override
112121
public boolean verifyChainOfTrust(@NonNull final Bytes proof) {
113122
requireNonNull(proof);
114-
return BRIDGE.verifyChainOfTrust(snarkKeys().verifyingKey(), proof.toByteArray());
115-
}
116-
117-
private ProvingAndVerifyingSnarkKeys snarkKeys() {
118-
try {
119-
if (SNARK_KEYS == null) {
120-
final var elf = HistoryLibraryBridge.loadAddressBookRotationProgram();
121-
SNARK_KEYS = BRIDGE.snarkVerificationKey(elf);
122-
}
123-
return SNARK_KEYS;
124-
} catch (IOException e) {
125-
throw new IllegalStateException("Could not load HistoryLibrary ELF", e);
126-
}
123+
return BRIDGE.verifyChainOfTrust(SNARK_KEYS.get().verifyingKey(), proof.toByteArray());
127124
}
128125
}

0 commit comments

Comments
 (0)