Example #1
0
        public virtual async Task <AppState> RevertLastBlock(string predecessor)
        {
            Logger.LogDebug("Begin DB transaction");
            using var tx = await Db.Database.BeginTransactionAsync();

            try
            {
                Logger.LogDebug("Revert quotes");
                await Quotes.Revert();

                Logger.LogDebug("Revert post-changes");
                await BeforeRevert();

                var state = Cache.AppState.Get();
                Db.TryAttach(state);

                if (state.Protocol != state.NextProtocol)
                {
                    Logger.LogDebug("Deactivate latest protocol");
                    var nextProtocol = Services.GetProtocolHandler(state.Level + 1, state.NextProtocol);
                    await nextProtocol.Deactivate(state);
                }

                Logger.LogDebug("Revert block");
                await Revert();

                Logger.LogDebug("Touch accounts");
                ClearAccounts(state.Level + 1);

                if (Config.Diagnostics && state.Hash == predecessor)
                {
                    Logger.LogDebug("Diagnostics");
                    await Diagnostics.Run(state.Level);
                }

                Logger.LogDebug("Save changes");
                await Db.SaveChangesAsync();

                Logger.LogDebug("Commit DB transaction");
                await tx.CommitAsync();
            }
            catch (Exception)
            {
                await tx.RollbackAsync();

                throw;
            }

            ClearCachedRelations();
            return(Cache.AppState.Get());
        }