From f57932876ff9d05a91c1ed903c4f0e4f5d90e1a6 Mon Sep 17 00:00:00 2001 From: dangershony Date: Tue, 7 Dec 2021 12:38:46 +0000 Subject: [PATCH] Make some fixes to work better with bitcoins chain --- .../Sync/SyncTasks/BlockIndexer.cs | 34 ++++++++++++++++--- .../Sync/SyncTasks/BlockPuller.cs | 25 +++++++++++++- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/Blockcore.Indexer/Sync/SyncTasks/BlockIndexer.cs b/src/Blockcore.Indexer/Sync/SyncTasks/BlockIndexer.cs index 9b251d4a..b490451e 100644 --- a/src/Blockcore.Indexer/Sync/SyncTasks/BlockIndexer.cs +++ b/src/Blockcore.Indexer/Sync/SyncTasks/BlockIndexer.cs @@ -44,6 +44,7 @@ public class BlockIndexer : TaskRunner bool initialized; long? inputCopyLastBlockHeight; + bool? inputAddressIndexCompleted; public BlockIndexer( IOptions configuration, @@ -200,11 +201,11 @@ await mongoData.InputTable.Indexes }).ContinueWith(async task => { - log.LogDebug($"Creating indexes on {nameof(InputTable)}.{nameof(InputTable.Address)}"); + //log.LogDebug($"Creating indexes on {nameof(InputTable)}.{nameof(InputTable.Address)}"); - await mongoData.InputTable.Indexes - .CreateOneAsync(new CreateIndexModel(Builders - .IndexKeys.Ascending(trxBlk => trxBlk.Address))); + //await mongoData.InputTable.Indexes + // .CreateOneAsync(new CreateIndexModel(Builders + // .IndexKeys.Ascending(trxBlk => trxBlk.Address))); }).ContinueWith(async task => { @@ -255,7 +256,7 @@ await mongoData.Mempool.Indexes { if (indexingCompletTask != null && indexingCompletTask.IsCompleted) { - if (inputCopyLastBlockHeight == null) + if (inputAddressIndexCompleted == null && inputCopyLastBlockHeight == null) { var addressNulls = mongoData.InputTable.AsQueryable() .OrderBy(b => b.BlockIndex) @@ -309,6 +310,29 @@ await mongoData.Mempool.Indexes } else { + if (inputAddressIndexCompleted == null) + { + inputAddressIndexCompleted = false; + + indexingCompletTask = Task.Run(async () => + { + log.LogDebug($"Creating indexes on {nameof(InputTable)}.{nameof(InputTable.Address)}"); + + await mongoData.InputTable.Indexes + .CreateOneAsync(new CreateIndexModel(Builders + .IndexKeys.Ascending(trxBlk => trxBlk.Address))); + + inputAddressIndexCompleted = true; + }); + + return true; + } + + if (inputAddressIndexCompleted == false) + { + return true; + } + Runner.GlobalState.IndexMode = false; Runner.GlobalState.IndexModeCompleted = true; diff --git a/src/Blockcore.Indexer/Sync/SyncTasks/BlockPuller.cs b/src/Blockcore.Indexer/Sync/SyncTasks/BlockPuller.cs index 3496839c..5829e3f8 100644 --- a/src/Blockcore.Indexer/Sync/SyncTasks/BlockPuller.cs +++ b/src/Blockcore.Indexer/Sync/SyncTasks/BlockPuller.cs @@ -93,7 +93,9 @@ public override async Task OnExecute() } // fetch the next block form the fullnode - string nextHash = await client.GetblockHashAsync(Runner.GlobalState.PullingTip.Height + 1); + //string nextHash = await client.GetblockHashAsync(Runner.GlobalState.PullingTip.Height + 1); + + string nextHash = await NextHashAsync(client, Runner.GlobalState.PullingTip.Height + 1); if (string.IsNullOrEmpty(nextHash)) { @@ -149,5 +151,26 @@ public override async Task OnExecute() return await Task.FromResult(true); } + + private async Task NextHashAsync(BitcoinClient client, long height) + { + // ugly hack for now + + try + { + string nextHash = await client.GetblockHashAsync(height); + + return nextHash; + } + catch (Exception e) + { + if (e.Message.Contains("Block height out of range")) + { + return null; + } + + throw; + } + } } }