Skip to content

Commit 613a259

Browse files
intermezzo
Signed-off-by: Michael Tinker <[email protected]>
1 parent 03df2f0 commit 613a259

File tree

11 files changed

+165
-58
lines changed

11 files changed

+165
-58
lines changed

hedera-node/configuration/dev/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ contracts.chainId=298
55
contracts.maxGasPerSec=15000000000
66
contracts.maxGasPerTransaction=15000000000
77
# Needed for end-end tests running on mod-service code
8-
staking.periodMins=1
8+
#staking.periodMins=1
9+
staking.periodMins=1440
910
staking.fees.nodeRewardPercentage=10
1011
staking.fees.stakingRewardPercentage=10
1112
# Needed for Restart and Reconnect HapiTests that run many transactions of each type

hedera-node/hedera-app/src/main/java/com/hedera/node/app/history/HistoryLibrary.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public interface HistoryLibrary {
2525
*/
2626
Bytes EMPTY_PUBLIC_KEY = Bytes.wrap(new byte[32]);
2727

28+
/**
29+
* A placeholder metadata for the genesis WRAPS proof.
30+
*/
31+
byte[] GENESIS_WRAPS_METADATA = new byte[1280];
32+
2833
/**
2934
* An address book for use in the history library.
3035
* @param weights the weights of the nodes in the address book

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

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
import com.hedera.cryptography.wraps.WRAPSLibraryBridge;
1313
import com.hedera.node.app.history.HistoryLibrary;
1414
import edu.umd.cs.findbugs.annotations.NonNull;
15+
import java.util.Arrays;
1516
import java.util.Set;
1617
import java.util.SplittableRandom;
18+
import org.apache.logging.log4j.LogManager;
19+
import org.apache.logging.log4j.Logger;
20+
import org.hiero.base.utility.CommonUtils;
1721

1822
/**
1923
* Default implementation of the {@link HistoryLibrary}.
2024
*/
2125
public class HistoryLibraryImpl implements HistoryLibrary {
22-
private static final byte[] DUMMY_HINTS_KEY = new byte[1280];
26+
private static final Logger logger = LogManager.getLogger(HistoryLibraryImpl.class);
27+
2328
public static final SplittableRandom RANDOM = new SplittableRandom();
2429
public static final WRAPSLibraryBridge WRAPS = WRAPSLibraryBridge.getInstance();
2530

@@ -124,14 +129,53 @@ public Proof constructGenesisWrapsProof(
124129
requireNonNull(aggregatedSignature);
125130
requireNonNull(signers);
126131
requireNonNull(addressBook);
132+
133+
final var prevSchnorrPublicKeys = addressBook.publicKeys();
134+
final var prevWeights = addressBook.weights();
135+
final var nextSchnorrPublicKeys = addressBook.publicKeys();
136+
final var nextWeights = addressBook.weights();
137+
final var mask = addressBook.signersMask(signers);
138+
final var tssVerificationKey = GENESIS_WRAPS_METADATA;
139+
logger.info(" genesisAddressBookHash == null ? {}", genesisAddressBookHash == null);
140+
logger.info(" genesisAddressBookHash.length == 0 ? {}", genesisAddressBookHash.length == 0);
141+
logger.info(" prevSchnorrPublicKeys == null ? {}", prevSchnorrPublicKeys == null);
142+
logger.info(" prevWeights == null ? {}", prevWeights == null);
143+
logger.info(" prevSchnorrPublicKeys.length == 0 ? {}", prevSchnorrPublicKeys.length == 0);
144+
logger.info(
145+
" prevSchnorrPublicKeys.length != prevWeights.length ? {}",
146+
prevSchnorrPublicKeys.length != prevWeights.length);
147+
logger.info(" !WRAPSLibraryBridge.validateWeightsSum(prevWeights) ? {}", !validateWeightsSum(prevWeights));
148+
logger.info(" nextSchnorrPublicKeys == null ? {}", nextSchnorrPublicKeys == null);
149+
logger.info(" nextWeights == null ? {}", nextWeights == null);
150+
logger.info(" nextSchnorrPublicKeys.length == 0 ? {}", nextSchnorrPublicKeys.length == 0);
151+
logger.info(
152+
" nextSchnorrPublicKeys.length != nextWeights.length ? {}",
153+
nextSchnorrPublicKeys.length != nextWeights.length);
154+
logger.info(" !WRAPSLibraryBridge.validateWeightsSum(nextWeights) ? {}", !validateWeightsSum(nextWeights));
155+
logger.info(" tssVerificationKey == null ? {}", tssVerificationKey == null);
156+
logger.info(" tssVerificationKey.length == 0 ? {}", tssVerificationKey.length == 0);
157+
logger.info(" aggregateSignature == null ? {}", aggregatedSignature == null);
158+
logger.info(" aggregateSignature.length == 0 ? {}", aggregatedSignature.length == 0);
159+
logger.info(" signers == null ? {}", mask == null);
160+
logger.info(
161+
" signers.length != prevSchnorrPublicKeys.length ? {}", mask.length != prevSchnorrPublicKeys.length);
162+
logger.info(
163+
" !WRAPSLibraryBridge.validateSchnorrPublicKeys(prevSchnorrPublicKeys) ? {}",
164+
!validateSchnorrPublicKeys(prevSchnorrPublicKeys));
165+
logger.info(
166+
") !WRAPSLibraryBridge.validateSchnorrPublicKeys(nextSchnorrPublicKeys) ? {}",
167+
!validateSchnorrPublicKeys(nextSchnorrPublicKeys));
168+
logger.info("Is proof supported? {}", WRAPSLibraryBridge.isProofSupported());
169+
logger.info("Genesis hash: " + CommonUtils.hex(genesisAddressBookHash));
170+
logger.info("Signers: " + Arrays.toString(mask));
127171
return WRAPS.constructWrapsProof(
128172
genesisAddressBookHash,
129173
addressBook.publicKeys(),
130174
addressBook.weights(),
131175
addressBook.publicKeys(),
132176
addressBook.weights(),
133177
null,
134-
DUMMY_HINTS_KEY,
178+
GENESIS_WRAPS_METADATA,
135179
aggregatedSignature,
136180
addressBook.signersMask(signers));
137181
}
@@ -174,4 +218,30 @@ public boolean isValidWraps(@NonNull final byte[] compressedProof) {
174218
public boolean wrapsProverReady() {
175219
return WRAPSLibraryBridge.isProofSupported();
176220
}
221+
222+
// TEMP
223+
private static boolean validateWeightsSum(final long weights[]) {
224+
try {
225+
long sum = 0;
226+
for (int i = 0; i < weights.length; i++) {
227+
if (weights[i] < 0) {
228+
return false;
229+
}
230+
// Math.addExact() throws ArithmeticException if the sum overflows
231+
sum = Math.addExact(sum, weights[i]);
232+
}
233+
return sum <= Long.MAX_VALUE;
234+
} catch (final ArithmeticException e) {
235+
return false;
236+
}
237+
}
238+
239+
private static boolean validateSchnorrPublicKeys(final byte[][] schnorrPublicKeys) {
240+
for (int i = 0; i < schnorrPublicKeys.length; i++) {
241+
if (schnorrPublicKeys[i] == null || schnorrPublicKeys[i].length == 0) {
242+
return false;
243+
}
244+
}
245+
return true;
246+
}
177247
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static com.hedera.hapi.util.HapiUtils.asInstant;
55
import static com.hedera.node.app.history.ReadableHistoryStore.WrapsMessagePublication.allFromHistory;
66
import static com.hedera.node.app.history.schemas.V059HistorySchema.ACTIVE_PROOF_CONSTRUCTION_STATE_ID;
7-
import static com.hedera.node.app.history.schemas.V059HistorySchema.HISTORY_SIGNATURES_STATE_ID;
87
import static com.hedera.node.app.history.schemas.V059HistorySchema.LEDGER_ID_STATE_ID;
98
import static com.hedera.node.app.history.schemas.V059HistorySchema.NEXT_PROOF_CONSTRUCTION_STATE_ID;
109
import static com.hedera.node.app.history.schemas.V059HistorySchema.PROOF_KEY_SETS_STATE_ID;
@@ -16,7 +15,6 @@
1615
import com.hedera.hapi.node.state.history.HistoryProofConstruction;
1716
import com.hedera.hapi.node.state.history.HistoryProofVote;
1817
import com.hedera.hapi.node.state.history.ProofKeySet;
19-
import com.hedera.hapi.node.state.history.RecordedHistorySignature;
2018
import com.hedera.hapi.node.state.history.WrapsMessageHistory;
2119
import com.hedera.hapi.node.state.primitives.ProtoBytes;
2220
import com.hedera.hapi.platform.state.NodeId;
@@ -43,7 +41,6 @@ public class ReadableHistoryStoreImpl implements ReadableHistoryStore {
4341
private final ReadableSingletonState<HistoryProofConstruction> nextConstruction;
4442
private final ReadableSingletonState<HistoryProofConstruction> activeConstruction;
4543
private final ReadableKVState<NodeId, ProofKeySet> proofKeySets;
46-
private final ReadableKVState<ConstructionNodeId, RecordedHistorySignature> signatures;
4744
private final ReadableKVState<ConstructionNodeId, HistoryProofVote> votes;
4845
private final ReadableKVState<ConstructionNodeId, WrapsMessageHistory> wrapsMessageHistories;
4946

@@ -53,7 +50,6 @@ public ReadableHistoryStoreImpl(@NonNull final ReadableStates states) {
5350
this.nextConstruction = states.getSingleton(NEXT_PROOF_CONSTRUCTION_STATE_ID);
5451
this.activeConstruction = states.getSingleton(ACTIVE_PROOF_CONSTRUCTION_STATE_ID);
5552
this.proofKeySets = states.get(PROOF_KEY_SETS_STATE_ID);
56-
this.signatures = states.get(HISTORY_SIGNATURES_STATE_ID);
5753
this.votes = states.get(PROOF_VOTES_STATE_ID);
5854
this.wrapsMessageHistories = states.get(WRAPS_MESSAGE_HISTORIES_STATE_ID);
5955
}

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

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static com.hedera.hapi.util.HapiUtils.asInstant;
99
import static com.hedera.node.app.hapi.utils.CommonUtils.noThrowSha384HashOf;
1010
import static com.hedera.node.app.history.HistoryLibrary.EMPTY_PUBLIC_KEY;
11+
import static com.hedera.node.app.history.HistoryLibrary.GENESIS_WRAPS_METADATA;
1112
import static com.hedera.node.app.history.impl.ProofControllers.isWrapsExtensible;
1213
import static com.hedera.node.app.service.roster.impl.RosterTransitionWeights.moreThanHalfOfTotal;
1314
import static java.util.Collections.emptySortedMap;
@@ -40,8 +41,10 @@
4041
import java.util.HashMap;
4142
import java.util.List;
4243
import java.util.Map;
44+
import java.util.Set;
4345
import java.util.SortedMap;
4446
import java.util.TreeMap;
47+
import java.util.TreeSet;
4548
import java.util.concurrent.CompletableFuture;
4649
import java.util.concurrent.Executor;
4750
import java.util.function.Consumer;
@@ -219,7 +222,11 @@ public Outcome advance(
219222
targetAddressBook = AddressBook.from(weights.targetNodeWeights(), nodeId -> targetProofKeys
220223
.getOrDefault(nodeId, EMPTY_PUBLIC_KEY)
221224
.toByteArray());
222-
wrapsMessage = historyLibrary.computeWrapsMessage(targetAddressBook, targetMetadata.toByteArray());
225+
// In general the metadata in the WRAPS message is the target metadata (which is the hinTS verification
226+
// key in the current TSS scheme); but for the special case of a source proof with a non-recursive
227+
// proof, the additional signing work we need to do is actually over certain placeholder metadata
228+
final var metadata = proofIsWrapsGenesis() ? GENESIS_WRAPS_METADATA : targetMetadata.toByteArray();
229+
wrapsMessage = historyLibrary.computeWrapsMessage(targetAddressBook, metadata);
223230
targetAddressBookHash = historyLibrary.hashAddressBook(targetAddressBook);
224231
}
225232
publishIfNeeded(
@@ -559,40 +566,64 @@ yield new AggregatePhaseOutput(
559566
signature,
560567
phaseMessages.get(R1).keySet().stream().toList());
561568
} else {
562-
if (true) {
563-
yield new ProofPhaseOutput(new byte[704], new byte[30331352]);
564-
} else {
565-
if (!historyLibrary.wrapsProverReady()) {
566-
yield new NoopOutput("WRAPS library is not ready");
567-
}
568-
Proof proof;
569-
if (!isWrapsExtensible(sourceProof)) {
570-
proof = historyLibrary.constructGenesisWrapsProof(
571-
requireNonNull(ledgerId).toByteArray(),
572-
signature,
573-
phaseMessages.get(R1).keySet(),
574-
targetBook);
575-
} else {
576-
proof = new Proof(
577-
sourceProof.uncompressedWrapsProof().toByteArray(),
578-
sourceProof
579-
.chainOfTrustProofOrThrow()
580-
.wrapsProofOrThrow()
581-
.toByteArray());
582-
}
583-
final var sourceBook = AddressBook.from(weights.sourceNodeWeights(), nodeId -> proofKeys
584-
.getOrDefault(nodeId, EMPTY_PUBLIC_KEY)
585-
.toByteArray());
586-
proof = historyLibrary.constructIncrementalWrapsProof(
569+
if (!historyLibrary.wrapsProverReady()) {
570+
yield new NoopOutput("WRAPS library is not ready");
571+
}
572+
final var isValid =
573+
historyLibrary.verifyAggregateSignature(message, publicKeysForR1(), signature);
574+
log.info("Aggregate signature is {}", isValid ? "valid" : "invalid");
575+
Proof proof;
576+
if (!isWrapsExtensible(sourceProof)) {
577+
final long now = System.nanoTime();
578+
log.info("Constructing genesis WRAPS proof...");
579+
proof = historyLibrary.constructGenesisWrapsProof(
587580
requireNonNull(ledgerId).toByteArray(),
588-
proof.uncompressed(),
589-
sourceBook,
590-
targetBook,
591-
targetMetadata.toByteArray(),
592581
signature,
593-
phaseMessages.get(R1).keySet());
594-
yield new ProofPhaseOutput(proof.compressed(), proof.uncompressed());
582+
phaseMessages.get(R1).keySet(),
583+
targetBook);
584+
log.info(
585+
"Constructed genesis WRAPS proof in {}ms",
586+
(System.nanoTime() - now) / 1_000_000);
587+
} else {
588+
proof = new Proof(
589+
sourceProof.uncompressedWrapsProof().toByteArray(),
590+
sourceProof
591+
.chainOfTrustProofOrThrow()
592+
.wrapsProofOrThrow()
593+
.toByteArray());
595594
}
595+
final var sourceBook = AddressBook.from(weights.sourceNodeWeights(), nodeId -> proofKeys
596+
.getOrDefault(nodeId, EMPTY_PUBLIC_KEY)
597+
.toByteArray());
598+
final long now = System.nanoTime();
599+
log.info(
600+
"Constructing incremental WRAPS proof (WRAPS genesis? {})...",
601+
proofIsWrapsGenesis());
602+
final var effectiveSignature = proofIsWrapsGenesis()
603+
? sourceProof
604+
.chainOfTrustProofOrThrow()
605+
.aggregatedNodeSignaturesOrThrow()
606+
.aggregatedSignature()
607+
.toByteArray()
608+
: signature;
609+
final Set<Long> effectiveSigners = proofIsWrapsGenesis()
610+
? new TreeSet<>(sourceProof
611+
.chainOfTrustProofOrThrow()
612+
.aggregatedNodeSignaturesOrThrow()
613+
.signingNodeIds())
614+
: phaseMessages.get(R1).keySet();
615+
proof = historyLibrary.constructIncrementalWrapsProof(
616+
requireNonNull(ledgerId).toByteArray(),
617+
proof.uncompressed(),
618+
sourceBook,
619+
targetBook,
620+
targetMetadata.toByteArray(),
621+
effectiveSignature,
622+
effectiveSigners);
623+
log.info(
624+
"Constructed incremental WRAPS proof in {}ms",
625+
(System.nanoTime() - now) / 1_000_000);
626+
yield new ProofPhaseOutput(proof.compressed(), proof.uncompressed());
596627
}
597628
}
598629
},
@@ -634,6 +665,10 @@ private Bytes selfProofHashOrThrow() {
634665
return explicitHistoryProofHashes.computeIfAbsent(selfId, k -> hashOf(requireNonNull(historyProof)));
635666
}
636667

668+
private boolean proofIsWrapsGenesis() {
669+
return sourceProof != null && !isWrapsExtensible(sourceProof);
670+
}
671+
637672
private static Bytes hashOf(@NonNull final HistoryProof proof) {
638673
return noThrowSha384HashOf(HistoryProof.PROTOBUF.toBytes(proof));
639674
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static com.hedera.hapi.util.HapiUtils.asTimestamp;
55
import static com.hedera.node.app.history.impl.ProofControllers.isWrapsExtensible;
66
import static com.hedera.node.app.history.schemas.V059HistorySchema.ACTIVE_PROOF_CONSTRUCTION_STATE_ID;
7-
import static com.hedera.node.app.history.schemas.V059HistorySchema.HISTORY_SIGNATURES_STATE_ID;
87
import static com.hedera.node.app.history.schemas.V059HistorySchema.LEDGER_ID_STATE_ID;
98
import static com.hedera.node.app.history.schemas.V059HistorySchema.NEXT_PROOF_CONSTRUCTION_STATE_ID;
109
import static com.hedera.node.app.history.schemas.V059HistorySchema.PROOF_KEY_SETS_STATE_ID;
@@ -20,7 +19,6 @@
2019
import com.hedera.hapi.node.state.history.HistoryProofConstruction;
2120
import com.hedera.hapi.node.state.history.HistoryProofVote;
2221
import com.hedera.hapi.node.state.history.ProofKeySet;
23-
import com.hedera.hapi.node.state.history.RecordedHistorySignature;
2422
import com.hedera.hapi.node.state.history.WrapsMessageHistory;
2523
import com.hedera.hapi.node.state.history.WrapsSigningState;
2624
import com.hedera.hapi.node.state.primitives.ProtoBytes;
@@ -58,7 +56,6 @@ public class WritableHistoryStoreImpl extends ReadableHistoryStoreImpl implement
5856
private final WritableSingletonState<HistoryProofConstruction> nextConstruction;
5957
private final WritableSingletonState<HistoryProofConstruction> activeConstruction;
6058
private final WritableKVState<NodeId, ProofKeySet> proofKeySets;
61-
private final WritableKVState<ConstructionNodeId, RecordedHistorySignature> signatures;
6259
private final WritableKVState<ConstructionNodeId, HistoryProofVote> votes;
6360
private final WritableKVState<ConstructionNodeId, WrapsMessageHistory> wrapsMessageHistories;
6461

@@ -68,7 +65,6 @@ public WritableHistoryStoreImpl(@NonNull final WritableStates states) {
6865
this.nextConstruction = states.getSingleton(NEXT_PROOF_CONSTRUCTION_STATE_ID);
6966
this.activeConstruction = states.getSingleton(ACTIVE_PROOF_CONSTRUCTION_STATE_ID);
7067
this.proofKeySets = states.get(PROOF_KEY_SETS_STATE_ID);
71-
this.signatures = states.get(HISTORY_SIGNATURES_STATE_ID);
7268
this.votes = states.get(PROOF_VOTES_STATE_ID);
7369
this.wrapsMessageHistories = states.get(WRAPS_MESSAGE_HISTORIES_STATE_ID);
7470
}
@@ -315,15 +311,14 @@ private void logNewConstruction(
315311
}
316312

317313
/**
318-
* Purges the votes for the given construction relative to the given roster.
314+
* Purges the publications for the given construction relative to the given roster.
319315
*
320316
* @param sourceRoster the construction
321317
*/
322318
private void purgePublications(final long constructionId, @NonNull final Roster sourceRoster) {
323319
sourceRoster.rosterEntries().forEach(entry -> {
324320
final var key = new ConstructionNodeId(constructionId, entry.nodeId());
325321
votes.remove(key);
326-
signatures.remove(key);
327322
wrapsMessageHistories.remove(key);
328323
});
329324
}

0 commit comments

Comments
 (0)