Beispiel #1
0
        public override async Task Revert()
        {
            var currBlock = await Cache.Blocks.CurrentAsync();

            await BlockCommit.Revert(this, currBlock);

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

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

            await StateCommit.Apply(this, blockCommit.Block, rawBlock);
        }
Beispiel #3
0
        public override async Task Revert()
        {
            var currBlock = await Cache.Blocks.CurrentAsync();

            await BakerCycleCommit.Revert(this);

            await DelegatorCycleCommit.Revert(this);

            await CycleCommit.Revert(this);

            await BakingRightsCommit.Revert(this);

            await VotingCommit.Revert(this, currBlock);

            await BootstrapCommit.Revert(this, currBlock);

            await BlockCommit.Revert(this, currBlock);

            await StateCommit.Revert(this, currBlock);
        }
Beispiel #4
0
        public override async Task Commit(IBlock block)
        {
            var rawBlock = block as RawBlock;

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

            var bootstrapCommit = await BootstrapCommit.Apply(this, blockCommit.Block, rawBlock);

            await VotingCommit.Apply(this, rawBlock);

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

            await CycleCommit.Apply(this, blockCommit.Block, bootstrapCommit.BootstrapedAccounts);

            await DelegatorCycleCommit.Apply(this, blockCommit.Block, bootstrapCommit.BootstrapedAccounts);

            await BakerCycleCommit.Apply(this,
                                         blockCommit.Block,
                                         bootstrapCommit.BootstrapedAccounts,
                                         brCommit.FutureBakingRights,
                                         brCommit.FutureEndorsingRights);

            await StateCommit.Apply(this, blockCommit.Block, rawBlock);
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
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);
        }
Beispiel #8
0
        public override async Task Commit(JsonElement block)
        {
            var blockCommit = new BlockCommit(this);
            await blockCommit.Apply(block);

            var freezerCommit = new FreezerCommit(this);
            await freezerCommit.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
            // there were no voting operations
            #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 "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

            #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":
                        await new OriginationsCommit(this).Apply(blockCommit.Block, operation, content);
                        break;

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

                        if (content.Required("metadata").TryGetProperty("internal_operation_results", out var internalResult))
                        {
                            foreach (var internalContent in internalResult.EnumerateArray())
                            {
                                switch (internalContent.RequiredString("kind"))
                                {
                                case "transaction":
                                    await new TransactionsCommit(this).ApplyInternal(blockCommit.Block, parent.Transaction, internalContent);
                                    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

            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);
        }