예제 #1
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);
        }
예제 #2
0
        public override async Task Commit(JsonElement block)
        {
            var blockCommit = new BlockCommit(this);
            await blockCommit.Apply(block);

            await new SoftwareCommit(this).Apply(blockCommit.Block, block);

            var freezerCommit = new FreezerCommit(this);
            await freezerCommit.Apply(blockCommit.Block, block);

            await new RevelationPenaltyCommit(this).Apply(blockCommit.Block, block);
            await new DeactivationCommit(this).Apply(blockCommit.Block, block);

            var operations = block.RequiredArray("operations", 4);

            #region operations 0
            foreach (var operation in operations[0].EnumerateArray())
            {
                foreach (var content in operation.RequiredArray("contents", 1).EnumerateArray())
                {
                    switch (content.RequiredString("kind"))
                    {
                    case "endorsement":
                        await new EndorsementsCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

                    default:
                        throw new NotImplementedException($"'{content.RequiredString("kind")}' is not allowed in operations[0]");
                    }
                }
            }
            #endregion

            #region operations 1
            foreach (var operation in operations[1].EnumerateArray())
            {
                foreach (var content in operation.RequiredArray("contents", 1).EnumerateArray())
                {
                    switch (content.RequiredString("kind"))
                    {
                    case "proposals":
                        await new ProposalsCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

                    case "ballot":
                        await new BallotsCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

                    default:
                        throw new NotImplementedException($"'{content.RequiredString("kind")}' is not allowed in operations[1]");
                    }
                }
            }
            #endregion

            #region operations 2
            foreach (var operation in operations[2].EnumerateArray())
            {
                foreach (var content in operation.RequiredArray("contents", 1).EnumerateArray())
                {
                    switch (content.RequiredString("kind"))
                    {
                    case "activate_account":
                        await new ActivationsCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

                    case "double_baking_evidence":
                        await new DoubleBakingCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

                    case "double_endorsement_evidence":
                        await new DoubleEndorsingCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

                    case "seed_nonce_revelation":
                        await new NonceRevelationsCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

                    default:
                        throw new NotImplementedException($"'{content.RequiredString("kind")}' is not allowed in operations[2]");
                    }
                }
            }
            #endregion

            var bigMapCommit = new BigMapCommit(this);

            #region operations 3
            foreach (var operation in operations[3].EnumerateArray())
            {
                Cache.AppState.IncreaseManagerCounter(operation.RequiredArray("contents").Count());

                foreach (var content in operation.RequiredArray("contents").EnumerateArray())
                {
                    switch (content.RequiredString("kind"))
                    {
                    case "reveal":
                        await new RevealsCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

                    case "delegation":
                        await new DelegationsCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

                    case "origination":
                        var orig = new OriginationsCommit(this);
                        await orig.Apply(blockCommit.Block, operation, content);

                        if (orig.BigMapDiffs != null)
                        {
                            bigMapCommit.Append(orig.Origination, orig.Origination.Contract, orig.BigMapDiffs);
                        }
                        break;

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

                        if (parent.BigMapDiffs != null)
                        {
                            bigMapCommit.Append(parent.Transaction, parent.Transaction.Target as Contract, parent.BigMapDiffs);
                        }

                        if (content.Required("metadata").TryGetProperty("internal_operation_results", out var internalResult))
                        {
                            foreach (var internalContent in internalResult.EnumerateArray())
                            {
                                switch (internalContent.RequiredString("kind"))
                                {
                                case "delegation":
                                    await new DelegationsCommit(this).ApplyInternal(blockCommit.Block, parent.Transaction, internalContent);
                                    break;

                                case "origination":
                                    var internalOrig = new OriginationsCommit(this);
                                    await internalOrig.ApplyInternal(blockCommit.Block, parent.Transaction, internalContent);

                                    if (internalOrig.BigMapDiffs != null)
                                    {
                                        bigMapCommit.Append(internalOrig.Origination, internalOrig.Origination.Contract, internalOrig.BigMapDiffs);
                                    }
                                    break;

                                case "transaction":
                                    var internalTx = new TransactionsCommit(this);
                                    await internalTx.ApplyInternal(blockCommit.Block, parent.Transaction, internalContent);

                                    if (internalTx.BigMapDiffs != null)
                                    {
                                        bigMapCommit.Append(internalTx.Transaction, internalTx.Transaction.Target as Contract, internalTx.BigMapDiffs);
                                    }
                                    break;

                                default:
                                    throw new NotImplementedException($"internal '{content.RequiredString("kind")}' is not implemented");
                                }
                            }
                        }
                        break;

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

            await bigMapCommit.Apply();

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

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

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

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

            await new StatisticsCommit(this).Apply(blockCommit.Block, freezerCommit.FreezerUpdates);
            await new VotingCommit(this).Apply(blockCommit.Block, block);
            await new StateCommit(this).Apply(blockCommit.Block, block);
        }