Пример #1
0
        public override async Task Revert()
        {
            var currBlock = await Cache.Blocks.CurrentAsync();

            #region load operations
            var query = Db.Blocks.AsQueryable();

            if (currBlock.Operations.HasFlag(Operations.Activations))
            {
                query = query.Include(x => x.Activations);
            }

            if (currBlock.Operations.HasFlag(Operations.Delegations))
            {
                query = query.Include(x => x.Delegations);
            }

            if (currBlock.Operations.HasFlag(Operations.Endorsements))
            {
                query = query.Include(x => x.Endorsements);
            }

            if (currBlock.Operations.HasFlag(Operations.Originations))
            {
                query = query.Include(x => x.Originations);
            }

            if (currBlock.Operations.HasFlag(Operations.Reveals))
            {
                query = query.Include(x => x.Reveals);
            }

            if (currBlock.Operations.HasFlag(Operations.Revelations))
            {
                query = query.Include(x => x.Revelations);
            }

            if (currBlock.Operations.HasFlag(Operations.Transactions))
            {
                query = query.Include(x => x.Transactions);
            }

            if (currBlock.Operations.HasFlag(Operations.DoubleBakings))
            {
                query = query.Include(x => x.DoubleBakings);
            }

            if (currBlock.Operations.HasFlag(Operations.DoubleEndorsings))
            {
                query = query.Include(x => x.DoubleEndorsings);
            }

            if (currBlock.Operations.HasFlag(Operations.Proposals))
            {
                query = query.Include(x => x.Proposals);
            }

            if (currBlock.Operations.HasFlag(Operations.Ballots))
            {
                query = query.Include(x => x.Ballots);
            }

            if (currBlock.Operations.HasFlag(Operations.RevelationPenalty))
            {
                query = query.Include(x => x.RevelationPenalties);
            }

            if (currBlock.Events.HasFlag(BlockEvents.NewAccounts))
            {
                query = query.Include(x => x.CreatedAccounts);
            }

            currBlock = await query.FirstOrDefaultAsync(x => x.Level == currBlock.Level);

            Cache.Blocks.Add(currBlock);

            var operations = new List <BaseOperation>(40);
            if (currBlock.Activations != null)
            {
                operations.AddRange(currBlock.Activations);
            }

            if (currBlock.Delegations != null)
            {
                operations.AddRange(currBlock.Delegations);
            }

            if (currBlock.Endorsements != null)
            {
                operations.AddRange(currBlock.Endorsements);
            }

            if (currBlock.Originations != null)
            {
                operations.AddRange(currBlock.Originations);
            }

            if (currBlock.Reveals != null)
            {
                operations.AddRange(currBlock.Reveals);
            }

            if (currBlock.Revelations != null)
            {
                operations.AddRange(currBlock.Revelations);
            }

            if (currBlock.Transactions != null)
            {
                operations.AddRange(currBlock.Transactions);
            }

            if (currBlock.DoubleBakings != null)
            {
                operations.AddRange(currBlock.DoubleBakings);
            }

            if (currBlock.DoubleEndorsings != null)
            {
                operations.AddRange(currBlock.DoubleEndorsings);
            }

            if (currBlock.Proposals != null)
            {
                operations.AddRange(currBlock.Proposals);
            }

            if (currBlock.Ballots != null)
            {
                operations.AddRange(currBlock.Ballots);
            }

            if (currBlock.CreatedAccounts != null)
            {
                foreach (var account in currBlock.CreatedAccounts)
                {
                    Cache.Accounts.Add(account);
                }
            }
            #endregion

            await BakerCycleCommit.Revert(this, currBlock);

            await DelegatorCycleCommit.Revert(this, currBlock);

            await CycleCommit.Revert(this, currBlock);

            await BakingRightsCommit.Revert(this, currBlock);

            foreach (var operation in operations.OrderByDescending(x => x.Id))
            {
                switch (operation)
                {
                case EndorsementOperation endorsement:
                    await EndorsementsCommit.Revert(this, currBlock, endorsement);

                    break;

                case ProposalOperation proposal:
                    await ProposalsCommit.Revert(this, currBlock, proposal);

                    break;

                case BallotOperation ballot:
                    await BallotsCommit.Revert(this, currBlock, ballot);

                    break;

                case ActivationOperation activation:
                    await ActivationsCommit.Revert(this, currBlock, activation);

                    break;

                case DoubleBakingOperation doubleBaking:
                    await DoubleBakingCommit.Revert(this, currBlock, doubleBaking);

                    break;

                case DoubleEndorsingOperation doubleEndorsing:
                    await DoubleEndorsingCommit.Revert(this, currBlock, doubleEndorsing);

                    break;

                case NonceRevelationOperation revelation:
                    await NonceRevelationsCommit.Revert(this, currBlock, revelation);

                    break;

                case RevealOperation reveal:
                    await RevealsCommit.Revert(this, currBlock, reveal);

                    break;

                case DelegationOperation delegation:
                    await DelegationsCommit.Revert(this, currBlock, delegation);

                    break;

                case OriginationOperation origination:
                    await OriginationsCommit.Revert(this, currBlock, origination);

                    break;

                case TransactionOperation transaction:
                    await TransactionsCommit.Revert(this, currBlock, transaction);

                    break;

                default:
                    throw new NotImplementedException($"'{operation.GetType()}' is not implemented");
                }
            }

            await DeactivationCommit.Revert(this, currBlock);

            await RevelationPenaltyCommit.Revert(this, currBlock);

            await FreezerCommit.Revert(this, currBlock);

            await VotingCommit.Revert(this, currBlock);

            await BlockCommit.Revert(this, currBlock);

            await StateCommit.Revert(this, currBlock);
        }
Пример #2
0
        public override async Task Commit(IBlock block)
        {
            var rawBlock = block as RawBlock;

            var blockCommit = await BlockCommit.Apply(this, rawBlock);

            await VotingCommit.Apply(this, blockCommit.Block, rawBlock);

            await FreezerCommit.Apply(this, blockCommit.Block, rawBlock);

            await RevelationPenaltyCommit.Apply(this, blockCommit.Block, rawBlock);

            await DeactivationCommit.Apply(this, blockCommit.Block, rawBlock);

            #region operations 0
            foreach (var operation in rawBlock.Operations[0])
            {
                foreach (var content in operation.Contents)
                {
                    switch (content)
                    {
                    case RawEndorsementContent endorsement:
                        await EndorsementsCommit.Apply(this, blockCommit.Block, operation, endorsement);

                        break;

                    default:
                        throw new Exception($"'{content.GetType()}' is not expected in operations[0]");
                    }
                }
            }
            #endregion

            #region operations 1
            foreach (var operation in rawBlock.Operations[1])
            {
                foreach (var content in operation.Contents)
                {
                    if (content is RawProposalContent proposal)
                    {
                        await ProposalsCommit.Apply(this, blockCommit.Block, operation, proposal);
                    }
                    else if (content is RawBallotContent ballot)
                    {
                        await BallotsCommit.Apply(this, blockCommit.Block, operation, ballot);
                    }
                    else
                    {
                        throw new Exception($"'{content.GetType()}' is not expected in operations[1]");
                    }
                }
            }
            #endregion

            #region operations 2
            foreach (var operation in rawBlock.Operations[2])
            {
                foreach (var content in operation.Contents)
                {
                    switch (content)
                    {
                    case RawActivationContent activation:
                        await ActivationsCommit.Apply(this, blockCommit.Block, operation, activation);

                        break;

                    case RawDoubleBakingEvidenceContent doubleBaking:
                        await DoubleBakingCommit.Apply(this, blockCommit.Block, operation, doubleBaking);

                        break;

                    case RawDoubleEndorsingEvidenceContent doubleEndorsing:
                        await DoubleEndorsingCommit.Apply(this, blockCommit.Block, operation, doubleEndorsing);

                        break;

                    case RawNonceRevelationContent revelation:
                        await NonceRevelationsCommit.Apply(this, blockCommit.Block, operation, revelation);

                        break;

                    default:
                        throw new Exception($"'{content.GetType()}' is not expected in operations[2]");
                    }
                }
            }
            #endregion

            #region operations 3
            foreach (var operation in rawBlock.Operations[3])
            {
                Cache.AppState.IncreaseManagerCounter(operation.Contents.Count);

                foreach (var content in operation.Contents)
                {
                    switch (content)
                    {
                    case RawRevealContent reveal:
                        await RevealsCommit.Apply(this, blockCommit.Block, operation, reveal);

                        break;

                    case RawDelegationContent delegation:
                        await DelegationsCommit.Apply(this, blockCommit.Block, operation, delegation);

                        break;

                    case RawOriginationContent origination:
                        await OriginationsCommit.Apply(this, blockCommit.Block, operation, origination);

                        break;

                    case RawTransactionContent transaction:
                        var parent = await TransactionsCommit.Apply(this, blockCommit.Block, operation, transaction);

                        if (transaction.Metadata.InternalResults != null)
                        {
                            foreach (var internalContent in transaction.Metadata.InternalResults)
                            {
                                switch (internalContent)
                                {
                                case RawInternalTransactionResult internalTransaction:
                                    await TransactionsCommit.Apply(this, blockCommit.Block, parent.Transaction, internalTransaction);

                                    break;

                                case RawInternalDelegationResult internalDelegation:
                                    await DelegationsCommit.Apply(this, blockCommit.Block, parent.Transaction, internalDelegation);

                                    break;

                                case RawInternalOriginationResult internalOrigination:
                                    await OriginationsCommit.Apply(this, blockCommit.Block, parent.Transaction, internalOrigination);

                                    break;

                                default:
                                    throw new Exception($"internal '{internalContent.GetType()}' is not expected");
                                }
                            }
                        }
                        break;

                    default:
                        throw new Exception($"'{content.GetType()}' is not expected in operations[3]");
                    }
                }
            }
            #endregion

            var brCommit = await BakingRightsCommit.Apply(this, blockCommit.Block);

            var cycleCommit = await CycleCommit.Apply(this, blockCommit.Block);

            await DelegatorCycleCommit.Apply(this, blockCommit.Block, cycleCommit.FutureCycle);

            await BakerCycleCommit.Apply(this,
                                         blockCommit.Block,
                                         cycleCommit.FutureCycle,
                                         brCommit.FutureBakingRights,
                                         brCommit.FutureEndorsingRights,
                                         cycleCommit.Snapshots,
                                         brCommit.CurrentRights);

            await StateCommit.Apply(this, blockCommit.Block, rawBlock);
        }