diff --git a/src/Blockcore.Indexer.Core/Handlers/StatsHandler.cs b/src/Blockcore.Indexer.Core/Handlers/StatsHandler.cs index 138eb9cf..9ac5f5e2 100644 --- a/src/Blockcore.Indexer.Core/Handlers/StatsHandler.cs +++ b/src/Blockcore.Indexer.Core/Handlers/StatsHandler.cs @@ -136,6 +136,11 @@ public async Task CoinInformation() public async Task Statistics() { + if (globalState.StoreTip?.BlockHash == null) + { + return new Statistics { Symbol = syncConnection.Symbol, Error = "node is not ready"}; + } + var cachedStats = cache.Get(Key); if (cachedStats != null && diff --git a/src/Blockcore.Indexer.Core/Sync/SyncTasks/BlockStartup.cs b/src/Blockcore.Indexer.Core/Sync/SyncTasks/BlockStartup.cs index c49ebdf0..44befc85 100644 --- a/src/Blockcore.Indexer.Core/Sync/SyncTasks/BlockStartup.cs +++ b/src/Blockcore.Indexer.Core/Sync/SyncTasks/BlockStartup.cs @@ -97,7 +97,7 @@ public override async Task OnExecute() Runner.GlobalState.StoreTip = storageOperations.PushStorageBatch(genesisBatch); } - BlockInfo fetchedBlock = await client.GetBlockAsync(Runner.GlobalState.StoreTip.BlockHash); + BlockInfo fetchedBlock = await GetBlockOrNullAsync(client, Runner.GlobalState.StoreTip.BlockHash); if (fetchedBlock == null) { // check if the fullnode is ahead of the indexer height @@ -117,5 +117,24 @@ public override async Task OnExecute() // update the chains tip Runner.GlobalState.ChainTipHeight = syncOperations.GetBlockCount(client); } + + private static async Task GetBlockOrNullAsync(IBlockchainClient client, string blockHash) + { + try + { + BlockInfo blockInfo = await client.GetBlockAsync(blockHash); + + return blockInfo; + } + catch (Exception e) + { + if (e.Message.Contains("Block not found")) + { + return null; + } + + throw; + } + } } }