Example #1
0
        private void WaitForBlock(long blockHeight)
        {
            long estimatedBlock;

            do
            {
                Log.Info($"Waiting for {blockHeight}");
                var block = TzScanConnector.GetBlock();
                estimatedBlock = block.Level;
                Log.Info($"Last Block {block.Level}");
                var dateTime = block.Timestamp.ToLocalTime().DateTime;
                var timeSpan = DateTime.Now - dateTime;
                Log.Info($"{block.Level} {dateTime} {timeSpan.ToPrettyFormat()}");
                if (timeSpan.Minutes > 0)
                {
                    estimatedBlock += timeSpan.Minutes;
                    Log.Info($"That block is old. We must be at block {estimatedBlock}");
                }

                if (estimatedBlock >= blockHeight)
                {
                    Log.Info($"We are at {estimatedBlock} finished loop.");
                    var newspanz = TimeSpan.FromSeconds(20);
                    Log.Info($"Waiting for {newspanz} for good measure!");
                    Thread.Sleep(newspanz);
                    return;
                }

                var untilNextBlock = 60 - timeSpan.TotalSeconds % 60;
                var span           = TimeSpan.FromSeconds(untilNextBlock);
                Log.Info($"Waiting for {span}");
                Thread.Sleep(span);
                estimatedBlock += 1;
                Log.Info($"We must be at block {estimatedBlock}");
                var blocksToWait = blockHeight - estimatedBlock;
                if (blocksToWait > 0)
                {
                    var spanToWait = TimeSpan.FromMinutes(blocksToWait);
                    Log.Info($"Waiting for {spanToWait}");
                    Thread.Sleep(spanToWait);
                }
            } while (estimatedBlock < blockHeight);

            var newspan = TimeSpan.FromSeconds(20);

            Log.Info($"Waiting for {newspan} for good measure!");
            Thread.Sleep(newspan);
        }