Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

06788 Better handling of failed async hashing #6796

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.swirlds.common.merkle.exceptions;

/**
* This type of exception is thrown if there is a problem with rehashing of a merkle node.
*/
public class FailedRehashException extends RuntimeException {
public FailedRehashException(final Throwable cause) {
super(cause);
}
}
Expand Up @@ -16,15 +16,22 @@

package com.swirlds.common.merkle.utility;

import static com.swirlds.logging.LogMarker.EXCEPTION;

import com.swirlds.common.crypto.Hash;
import com.swirlds.common.merkle.MerkleInternal;
import com.swirlds.common.merkle.MerkleNode;
import com.swirlds.common.merkle.crypto.MerkleCryptoFactory;
import com.swirlds.common.merkle.exceptions.FailedRehashException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class MerkleUtils {

private static final Logger logger = LogManager.getLogger(MerkleUtils.class);

private MerkleUtils() {}

/**
Expand Down Expand Up @@ -58,8 +65,12 @@ public static Hash rehashTree(final MerkleNode root) {
final Future<Hash> future = MerkleCryptoFactory.getInstance().digestTreeAsync(root);
try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
} catch (InterruptedException e) {
logger.error(EXCEPTION.getMarker(), "Interrupted while waiting for tree hashing to complete", e);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.error(EXCEPTION.getMarker(), "Async tree hashing failed", e);
throw new FailedRehashException(e);
}
}

Expand Down
Expand Up @@ -18,10 +18,14 @@

import static com.swirlds.common.merkle.utility.MerkleUtils.invalidateTree;
import static com.swirlds.common.merkle.utility.MerkleUtils.rehashTree;
import static com.swirlds.common.test.merkle.util.MerkleTestUtils.generateRandomTree;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import com.swirlds.common.crypto.CryptographyHolder;
import com.swirlds.common.crypto.Hash;
Expand All @@ -31,9 +35,11 @@
import com.swirlds.common.merkle.MerkleLeaf;
import com.swirlds.common.merkle.MerkleNode;
import com.swirlds.common.merkle.crypto.MerkleCryptoFactory;
import com.swirlds.common.merkle.exceptions.FailedRehashException;
import com.swirlds.common.merkle.impl.PartialMerkleLeaf;
import com.swirlds.common.merkle.impl.PartialNaryMerkleInternal;
import com.swirlds.common.merkle.route.MerkleRoute;
import com.swirlds.common.test.merkle.dummy.DummyMerkleNode;
import com.swirlds.common.test.merkle.util.MerkleTestUtils;
import com.swirlds.test.framework.TestComponentTags;
import com.swirlds.test.framework.TestTypeTags;
Expand All @@ -43,6 +49,8 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

@DisplayName("Merkle Rehash Tests")
class MerkleRehashTests {
Expand Down Expand Up @@ -149,6 +157,29 @@ void rehashBehavior() {
}
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
@Tag(TestTypeTags.FUNCTIONAL)
@Tag(TestComponentTags.MERKLE)
@DisplayName("Failed Rehash Behavior")
public void failedRehash() {

DummyMerkleNode root = spy(generateRandomTree(0, 2, 1, 1, 0, 3, 1, 0.25));
when(root.getHash()).then(new Answer<Hash>() {
private int count = 0;

@Override
public Hash answer(final InvocationOnMock invocation) throws Throwable {
if (count++ < 2) {
return (Hash) invocation.callRealMethod();
} else {
throw new RuntimeException("Test failure");
}
}
});
assertThrows(FailedRehashException.class, () -> rehashTree(root));
}

private static class DummySelfHashingLeaf extends PartialMerkleLeaf implements MerkleLeaf {

@Override
Expand Down