Example #1
0
        public override Task ProcessAsync(Block block, Dictionary <TransactionResult, List <LogEvent> > logEventsMap)
        {
            foreach (var events in logEventsMap)
            {
                var transactionResult = events.Key;
                foreach (var logEvent in events.Value)
                {
                    // a new task for time-consuming code check job
                    Task.Run(async() =>
                    {
                        var eventData = new CodeCheckRequired();
                        eventData.MergeFrom(logEvent);
                        var codeCheckResult = await _codeCheckService.PerformCodeCheckAsync(
                            eventData.Code.ToByteArray(),
                            transactionResult.BlockHash, transactionResult.BlockNumber, eventData.Category, eventData.IsSystemContract);
                        if (!codeCheckResult)
                        {
                            return;
                        }

                        var proposalId = ProposalCreated.Parser
                                         .ParseFrom(transactionResult.Logs.First(l => l.Name == nameof(ProposalCreated)).NonIndexed)
                                         .ProposalId;
                        // Cache proposal id to generate system approval transaction later
                        _proposalService.AddNotApprovedProposal(proposalId, transactionResult.BlockNumber);
                    });
                }
            }

            return(Task.CompletedTask);
        }
        public async Task ProcessAsync(Block block, TransactionResult transactionResult, LogEvent logEvent)
        {
            if (CrossChainConfigOptions.CurrentValue.CrossChainDataValidationIgnored)
            {
                Logger.LogTrace("Cross chain data validation disabled.");
                return;
            }

            var crossChainIndexingDataProposedEvent = new CrossChainIndexingDataProposedEvent();

            crossChainIndexingDataProposedEvent.MergeFrom(logEvent);
            var crossChainBlockData = crossChainIndexingDataProposedEvent.ProposedCrossChainData;

            if (crossChainBlockData.IsNullOrEmpty())
            {
                return;
            }
            var validationResult =
                await _crossChainIndexingDataValidationService.ValidateCrossChainIndexingDataAsync(crossChainBlockData,
                                                                                                   block.GetHash(), block.Height);

            if (validationResult)
            {
                Logger.LogDebug(
                    $"Valid cross chain indexing proposal found, block height {block.Height}, block hash {block.GetHash()} ");
                var proposalId = ProposalCreated.Parser
                                 .ParseFrom(transactionResult.Logs.First(l => l.Name == nameof(ProposalCreated)).NonIndexed)
                                 .ProposalId;
                _proposalService.AddNotApprovedProposal(proposalId, block.Height);
            }
        }
Example #3
0
        public void AddNotApprovedProposalTest()
        {
            var proposalCacheProvider = GetRequiredService <IProposalProvider>();

            {
                var       proposalId = Hash.FromString("proposal");
                const int height     = 100;
                _proposalService.AddNotApprovedProposal(proposalId, height);
                var exist = proposalCacheProvider.TryGetProposalCreatedHeight(proposalId, out var h);
                exist.ShouldBeTrue();
                h.ShouldBe(height);
            }

            {
                var       proposalId = Hash.FromString("proposal");
                const int height1    = 101;
                const int height2    = 100;
                _proposalService.AddNotApprovedProposal(proposalId, height1);
                _proposalService.AddNotApprovedProposal(proposalId, height2);
                var exist = proposalCacheProvider.TryGetProposalCreatedHeight(proposalId, out var h);
                exist.ShouldBeTrue();
                h.ShouldBe(height1);
            }
        }
Example #4
0
        public Task HandleAsync(Block block, TransactionResult transactionResult, LogEvent logEvent)
        {
            // a new task for time-consuming code check job
            Task.Run(async() =>
            {
                var eventData = new CodeCheckRequired();
                eventData.MergeFrom(logEvent);
                var codeCheckResult = await _codeCheckService.PerformCodeCheckAsync(eventData.Code.ToByteArray(),
                                                                                    transactionResult.BlockHash, transactionResult.BlockNumber);
                if (!codeCheckResult)
                {
                    return;
                }

                var proposalId = ProposalCreated.Parser
                                 .ParseFrom(transactionResult.Logs.First(l => l.Name == nameof(ProposalCreated)).NonIndexed)
                                 .ProposalId;
                // Cache proposal id to generate system approval transaction later
                _proposalService.AddNotApprovedProposal(proposalId, transactionResult.BlockNumber);
            });

            return(Task.CompletedTask);
        }
Example #5
0
        public Task PerformCodeCheckAsync(TransactionResult transactionResult, LogEvent logEvent)
        {
            if (!_isEnabled)
            {
                return(Task.CompletedTask);
            }

            Logger.LogTrace("Start code check.");

            var eventData = new CodeCheckRequired();

            eventData.MergeFrom(logEvent);

            try
            {
                // Check contract code
                _contractAuditor.Audit(eventData.Code.ToByteArray(), true);

                // Approve proposal related to CodeCheckRequired event
                var proposalId = ProposalCreated.Parser
                                 .ParseFrom(transactionResult.Logs.First(l => l.Name == nameof(ProposalCreated)).NonIndexed)
                                 .ProposalId;

                // Cache proposal id to generate system approval transaction later
                _proposalService.AddNotApprovedProposal(proposalId, transactionResult.BlockNumber);
            }
            catch (InvalidCodeException e)
            {
                // May do something else to indicate that the contract has an issue
                Logger.LogWarning(e.Message);
            }

            Logger.LogTrace("Finish code check.");

            return(Task.CompletedTask);
        }