Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigorov-Georgi committed Jan 3, 2025
1 parent 373a724 commit 9a8e387
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.limechain.exception.grandpa;

public class GhostExecutionException extends GrandpaGenericException {
public GhostExecutionException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.limechain.exception.grandpa;

public class GrandpaGenericException extends RuntimeException {
public GrandpaGenericException(String message) {
super(message);
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/limechain/grandpa/GrandpaService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.limechain.grandpa;

import com.limechain.exception.global.ExecutionFailedException;
import com.limechain.exception.grandpa.GhostExecutionException;
import com.limechain.exception.storage.BlockStorageGenericException;
import com.limechain.grandpa.state.GrandpaState;
import com.limechain.network.protocol.grandpa.messages.commit.Vote;
Expand All @@ -21,6 +21,7 @@
@Log
@Component
public class GrandpaService {

private final GrandpaState grandpaState;
private final BlockState blockState;

Expand All @@ -36,13 +37,13 @@ public GrandpaService(GrandpaState grandpaState, BlockState blockState) {
*
* @return GRANDPA GHOST block as a vote
*/
public Vote getGrandpaGHOST() {
public Vote getGrandpaGhost() {
var threshold = grandpaState.getThreshold();

Map<Hash256, BigInteger> blocks = getPossibleSelectedBlocks(threshold, Subround.PREVOTE);

if (blocks.isEmpty() || threshold.equals(BigInteger.ZERO)) {
throw new ExecutionFailedException("GHOST not found");
throw new GhostExecutionException("GHOST not found");
}

return selectBlockWithMostVotes(blocks);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/limechain/grandpa/GrandpaServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void setUp() {
void testGetGrandpaGHOSTWhereNoBlocksPassThreshold() {
when(grandpaState.getThreshold()).thenReturn(BigInteger.valueOf(10));
when(grandpaState.getPrevotes()).thenReturn(Map.of());
assertThrows(ExecutionFailedException.class, () -> grandpaService.getGrandpaGHOST());
assertThrows(ExecutionFailedException.class, () -> grandpaService.getGrandpaGhost());
}

@Test
Expand All @@ -77,7 +77,7 @@ void testGetGrandpaGHOSTWithBlockPassingThreshold() {
when(blockState.isDescendantOf(firstVote.getBlockHash(), firstVote.getBlockHash())).thenReturn(true);
when(blockState.isDescendantOf(secondVote.getBlockHash(), secondVote.getBlockHash())).thenReturn(true);

Vote result = grandpaService.getGrandpaGHOST();
Vote result = grandpaService.getGrandpaGhost();
assertNotNull(result);
assertEquals(firstVote.getBlockHash(), result.getBlockHash());
}
Expand Down

0 comments on commit 9a8e387

Please sign in to comment.