public async Task FlushAsync()
        {
            // before flushing the coinview persist the stake store
            // the stake store depends on the last block hash
            // to be stored after the stake store is persisted
            if (this.stakeChainStore != null)
            {
                await this.stakeChainStore.Flush(true);
            }

            if (innerBlockHash == null)
            {
                innerBlockHash = await inner.GetBlockHashAsync().ConfigureAwait(false);
            }

            using (this.lockobj.LockWrite())
            {
                WaitOngoingTasks();
                if (innerBlockHash == null)
                {
                    return;
                }
                var unspent =
                    unspents.Where(u => u.Value.IsDirty)
                    .ToArray();

                var originalOutputs = unspent.Select(u => u.Value.OriginalOutputs).ToList();
                foreach (var u in unspent)
                {
                    u.Value.IsDirty         = false;
                    u.Value.ExistInInner    = true;
                    u.Value.OriginalOutputs = u.Value.UnspentOutputs?._Outputs.ToArray();
                }
                this.flushing = Inner.SaveChangesAsync(unspent.Select(u => u.Value.UnspentOutputs).ToArray(), originalOutputs, innerBlockHash, blockHash);

                //Remove from cache prunable entries as they are being flushed down
                foreach (var c in unspent.Where(c => c.Value.UnspentOutputs != null && c.Value.UnspentOutputs.IsPrunable))
                {
                    unspents.Remove(c.Key);
                }
                innerBlockHash = blockHash;
            }
            //Can't await inside a lock
            await this.flushing.ConfigureAwait(false);
        }
        public async Task FlushAsync()
        {
            if (_InnerBlockHash == null)
            {
                _InnerBlockHash = await _Inner.GetBlockHashAsync().ConfigureAwait(false);
            }

            KeyValuePair <uint256, CacheItem>[] unspent = null;
            using (_Lock.LockWrite())
            {
                WaitOngoingTasks();
                if (_InnerBlockHash == null)
                {
                    return;
                }
                unspent =
                    _Unspents.Where(u => u.Value.IsDirty)
                    .ToArray();

                var originalOutputs = unspent.Select(u => u.Value.OriginalOutputs).ToList();
                foreach (var u in unspent)
                {
                    u.Value.IsDirty         = false;
                    u.Value.ExistInInner    = true;
                    u.Value.OriginalOutputs = u.Value.UnspentOutputs?._Outputs.ToArray();
                }
                _Flushing = Inner.SaveChangesAsync(unspent.Select(u => u.Value.UnspentOutputs).ToArray(), originalOutputs, _InnerBlockHash, _BlockHash);

                //Remove from cache prunable entries as they are being flushed down
                foreach (var c in unspent.Where(c => c.Value.UnspentOutputs != null && c.Value.UnspentOutputs.IsPrunable))
                {
                    _Unspents.Remove(c.Key);
                }
                _InnerBlockHash = _BlockHash;
            }
            //Can't await inside a lock
            await _Flushing.ConfigureAwait(false);
        }