-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: yaci-blockfrost and original_blockfrost separation as well a…
…s ChainSyncService with HealthIndicator. (#85) * Feature: yaci-blockfrost and original_blockfrost separation. * Feature: implemented ChainSyncService as well as chain sync indicator. --------- Co-authored-by: Mateusz Czeladka <mateusz.czeladka@cardanofoundation.org>
- Loading branch information
1 parent
b88eece
commit 9821d06
Showing
6 changed files
with
130 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...-app/src/main/java/org/cardano/foundation/voting/service/chain_sync/ChainSyncService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.cardano.foundation.voting.service.chain_sync; | ||
|
||
import com.bloxbean.cardano.client.api.exception.ApiException; | ||
import com.bloxbean.cardano.client.backend.blockfrost.service.BFBackendService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@Slf4j | ||
public class ChainSyncService { | ||
|
||
@Autowired | ||
@Qualifier("original_blockfrost") | ||
private BFBackendService orgBackendService; | ||
|
||
@Autowired | ||
@Qualifier("yaci_blockfrost") | ||
private BFBackendService yaciBackendService; | ||
|
||
@Value("${chain.sync.buffer:5}") | ||
private final int chainSyncBuffer = 5; | ||
|
||
public SyncStatus getSyncStatus() { | ||
try { | ||
var orgLastBlockResult = orgBackendService.getBlockService().getLatestBlock(); | ||
var yaciLastBlockResult = yaciBackendService.getBlockService().getLatestBlock(); | ||
|
||
if (orgLastBlockResult.isSuccessful() && yaciLastBlockResult.isSuccessful()) { | ||
long diff = orgLastBlockResult.getValue().getSlot() - yaciLastBlockResult.getValue().getSlot(); | ||
|
||
log.info("Current diff: {} (slots) between org and yaci.", diff); | ||
|
||
boolean isSynced = diff >= 0 && diff <= chainSyncBuffer; | ||
|
||
if (isSynced) { | ||
return SyncStatus.ok(diff); | ||
} | ||
|
||
log.warn("Yaci is not synced with the original chain. Diff: {} (slots)", diff); | ||
|
||
return SyncStatus.notYet(diff); | ||
} | ||
|
||
return SyncStatus.unknownError(); | ||
} catch (ApiException e) { | ||
log.error("Backend service is not available: {}", e.getMessage()); | ||
|
||
return SyncStatus.error(e); | ||
} | ||
} | ||
|
||
public record SyncStatus(boolean isSynced, Optional<Long> diff, Optional<Exception> ex) { | ||
|
||
static SyncStatus ok(long diff) { | ||
return new SyncStatus(true, Optional.of(diff), Optional.empty()); | ||
} | ||
|
||
static SyncStatus notYet(long diff) { | ||
return new SyncStatus(false, Optional.of(diff), Optional.empty()); | ||
} | ||
|
||
static SyncStatus error(Exception ex) { | ||
return new SyncStatus(false, Optional.empty(), Optional.of(ex)); | ||
} | ||
|
||
static SyncStatus unknownError() { | ||
return new SyncStatus(false, Optional.empty(), Optional.empty()); | ||
} | ||
|
||
} | ||
|
||
} |
31 changes: 27 additions & 4 deletions
31
...c/main/java/org/cardano/foundation/voting/service/health/YaciStoreTipHealthIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters