public async Task <Package> Handle(BuildPackageCommand request, CancellationToken cancellationToken)
        {
            var sourcePackage = request.SourcePackage;

            // Ensure to load the claims
            if (sourcePackage.Claims.Count == 0 || sourcePackage.ClaimPackages.Count == 0)
            {
                _db.LoadPackageClaims(sourcePackage);
            }

            if (sourcePackage.Claims.Count == 0)
            {
                _notifications.Add(new PackageNoClaimsNotification(sourcePackage));
                return(null);
            }

            // Build a new Package
            var builder = new PackageBuilder();

            builder.Package.Scopes = sourcePackage.Scopes;
            _db.Packages.Add(builder.Package); // Add the package to the DB context, so it will be handle in the claims bindings.

            foreach (var claim in sourcePackage.Claims)
            {
                if (claim.State.Match(ClaimStateType.Replaced))
                {
                    _db.Claims.Remove(claim); // Should BuildPackageCommandHandler do the cleanup?
                }
                else
                {
                    claim.ClaimPackages = claim.ClaimPackages.Where(p => p.PackageID != sourcePackage.DatabaseID).ToList(); // Remove relation to static build package
                    claim.ClaimPackages.Add(new ClaimPackageRelationship {
                        Claim = claim, Package = builder.Package
                    });                                                                                                 // Add relation to new package

                    builder.AddClaim(claim);
                }
            }

            if (builder.Package.Claims.Count == 0)
            {
                _notifications.Add(new PackageNoClaimsNotification(sourcePackage));
                return(null);
            }

            builder.OrderClaims(); // Order claims now ID before package ID calculation.
            SignPackage(builder);

            // Add timestamp to package, DB will auto connect from object reference
            builder.Package.AddTimestamp(await _mediator.Send(new CreateTimestampCommand(builder.Package.Id)));

            // Save the new package before calling notifications
            await _db.SaveChangesAsync();

            await _notifications.Publish(new PackageBuildNotification(builder.Package));

            return(builder.Package);
        }
        public async Task <BlockchainProof> Handle(UpdateBlockchainProofCommand request, CancellationToken cancellationToken)
        {
            _db.Proofs.Update(request.Proof);
            await _db.SaveChangesAsync();

            await _mediator.Publish(new BlockchainProofUpdatedNotification(request.Proof));

            return(request.Proof);
        }
        public override void Execute()
        {
            // Getting the current aggregator for timestamps
            var proof = new BlockchainProof();

            // TODO: Support more than one blockchain type!!
            proof.Timestamps = _db.Timestamps.Where(p => p.ProofDatabaseID == null).ToList();

            if (proof.Timestamps.Count == 0)
            {
                CombineLog(_logger, $"No timestamps found");
                Wait(_configuration.TimestampInterval()); // Default 10 min
                return;                                   // Exit workflow succesfully
            }

            // Ensure a new Proof object!
            try
            {
                Merkle(proof);

                // If funding key is available then use, local timestamping.

                if (IsFundingAvailable())
                {
                    LocalTimestamp(proof);
                }
                else
                {
                    RemoteTimestamp(proof);
                }

                proof.Status = ProofStatusType.Waiting.ToString();

                _db.Proofs.Add(proof);
                // Otherwise use the remote timestamp from trust.dance.
            }
            catch (Exception ex)
            {
                proof.Status = ProofStatusType.Failed.ToString();
                CombineLog(_logger, $"Error in proof ID:{proof.DatabaseID} " + ex.Message + ":" + ex.StackTrace);
            }
            finally
            {
                var result = _db.SaveChangesAsync().GetAwaiter().GetResult();
                _logger.LogTrace($"CreateProofWorkflow save with result code: {result}");
            }
            Wait(_configuration.ConfirmationWait(_configuration.Blockchain()));
        }
Exemplo n.º 4
0
        public async Task <NotificationSegment> Handle(StorePackageCommand request, CancellationToken cancellationToken)
        {
            // Save file to Package service
            var file = await _packageService.StorePackageAsync(request.Package);

            request.Package.File = file;
            if (_db.Packages.Local.Contains(request.Package))
            {
                // Only save the File property change!
                _db.Packages.Attach(request.Package).Property("File").IsModified = true;
                await _db.SaveChangesAsync();
            }

            _logger.LogInformation($"Package {request.Package.Id} stored, file name: {file}");
            _notifications.Add(new PackageStoredNotification(file, request.Package));

            return(_notifications);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(WorkflowContainer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
            }

            return(RedirectToPage("./Index"));
        }
Exemplo n.º 6
0
        public async Task <NotificationSegment> Handle(AddPackageCommand request, CancellationToken cancellationToken)
        {
            var package = request.Package;
            var claims  = package.Claims;

            _db.EnsurePackageState(package);
            Func <string, Package> getPackage = GetPackage;

            if (package.State.Match(PackageStateType.Signed))
            {
                // Check for existing packages
                if (await _db.DoPackageExistAsync(package.Id))
                {
                    _notifications.Add(new PackageExistNotification {
                        Package = package
                    });
                    _logger.LogInformation($"Package {package.Id.ToHex()} already exist in database");
                    return(_notifications);
                }

                _db.Packages.Add(package);       // Add package to DBContext
                getPackage = (scope) => package; // Replace default function and just return the inline package
            }

            foreach (var claim in claims)
            {
                claim.Id = PackageBuilder.GetClaimID(claim); // Make sure that the claim has an ID for the database.

                var claimNotifications = await _mediator.Send(new AddClaimCommand { Claim = claim, Package = getPackage(claim.Scope) });

                _notifications.AddRange(claimNotifications);
            }

            await _db.SaveChangesAsync();

            _logger.LogInformation($"Package Added Database ID: {package.DatabaseID}");

            await _notifications.Publish(new PackageAddedNotification { Package = package });

            return(_notifications);
        }