public async Task InitializeAsync(long initialHeight, byte historyDepth) { List <Block> lastBlocks = null; await _dbContextFactory.ExecuteAsync(async (DatabaseContext context) => { lastBlocks = await context.Blocks .Where(x => x.Status == BlockStatus.Valid) .OrderByDescending(x => x.Height) .Take(historyDepth) .ToListAsync(); }); var tipBlock = lastBlocks.FirstOrDefault(); if (tipBlock == null) { // TODO: no blocks in database we need to initialize from blockchain } _tipHeight = tipBlock.Height; _tipHash = tipBlock.Id; _history = lastBlocks.Select(x => x.Id).Reverse().ToList(); }