public void Success_Begin()
        {
            List <ServiceItem> services = new List <ServiceItem>
            {
                new ServiceItem
                {
                    ServiceName     = "Test",
                    DisplayName     = "Test Service",
                    BinaryPathName  = @"G:\test.exe",
                    TargetFramework = TargetFramework.Net35
                }
            };

            ServiceTransaction.Begin(services);

            var serviceTransactionFileName = ServiceTransaction.FilePathName;

            string actual = File.ReadAllText(serviceTransactionFileName);

            File.Delete(serviceTransactionFileName);

            string expected = Resources.BeginInstalledServicesXmlFile;

            Assert.AreEqual(
                expected,
                actual);
        }
        public void Getting_Uncommited_Services()
        {
            File.WriteAllText(
                ServiceTransaction.FilePathName,
                Resources.BeginInstalledServicesXmlFile);

            var service = ServiceTransaction.GetUncommited();

            File.Delete(ServiceTransaction.FilePathName);

            Assert.AreEqual(
                service.Count,
                1);

            Assert.AreEqual(
                "Test",
                service[0].ServiceName);

            Assert.AreEqual(
                "Test Service",
                service[0].DisplayName);

            Assert.AreEqual(
                @"G:\test.exe",
                service[0].BinaryPathName);

            Assert.AreEqual(
                TargetFramework.Net35,
                service[0].TargetFramework);
        }
예제 #3
0
        /// <summary>
        /// Deletes a ServiceTransaction
        /// </summary>
        /// <param name="serviceTransaction">ServiceTransaction</param>
        public void DeleteServiceTransaction(ServiceTransaction serviceTransaction)
        {
            if (serviceTransaction == null)
            {
                throw new ArgumentNullException(nameof(ServiceTransaction));
            }

            _serviceTransactionRepository.Delete(serviceTransaction);
        }
예제 #4
0
        /// <summary>
        /// Creates a ServiceTransaction
        /// </summary>
        /// <param name="serviceTransaction">ServiceTransaction</param>
        public async Task InsertServiceTransactionAsync(ServiceTransaction serviceTransaction)
        {
            if (serviceTransaction == null)
            {
                throw new ArgumentNullException(nameof(ServiceTransaction));
            }

            await _serviceTransactionRepository.InsertAsync(serviceTransaction);
        }
예제 #5
0
        public async Task CompleteAsync_SaveChangesAsyncCalledAsync()
        {
            this.dbMockHelper.MockDbContextScopeFactory();

            using (var transaction =
                       new ServiceTransaction(this.dbMockHelper.DbContextScopeFactoryMock.Object))
            {
                await transaction.CompleteAsync().ConfigureAwait(false);
            }

            this.dbMockHelper.DbContextScopeMock.Verify(m => m.SaveChangesAsync(), Times.Once);
        }
예제 #6
0
        public void Disposal_ObjectsCorrectlyDisposed()
        {
            this.dbMockHelper.MockDbContextScopeFactory();

            using (var transaction =
                       new ServiceTransaction(this.dbMockHelper.DbContextScopeFactoryMock.Object))
            {
                Assert.That(transaction, Is.Not.Null);
            }

            this.dbMockHelper.DbContextScopeFactoryMock
            .Verify(m => m.Create(DbContextScopeOption.JoinExisting), Times.Once);
            this.dbMockHelper.DbContextScopeMock.Verify(m => m.Dispose(), Times.Once);
        }
        public void Success_Commit()
        {
            File.WriteAllText(
                ServiceTransaction.FilePathName,
                Resources.BeginInstalledServicesXmlFile);

            ServiceTransaction.Commit();

            bool actual = File.Exists(ServiceTransaction.FilePathName);

            Assert.AreEqual(
                false,
                actual);
        }
예제 #8
0
        async Task <HeleusClientResponse> SendServiceTransactionWithCoreAccount(ServiceTransaction transaction, bool awaitResponse)
        {
            if (CurrentCoreAccount == null)
            {
                Log.Trace($"Sending service transaction {transaction.GetType().Name} with core account failed, no core account set.", this);
                return(new HeleusClientResponse(HeleusClientResultTypes.InternalError));
            }

            if (!await SetTargetChain(transaction.TargetChainId))
            {
                return(new HeleusClientResponse(HeleusClientResultTypes.EndpointConnectionError));
            }

            transaction.SignKey = CurrentCoreAccount.DecryptedKey;
            return(await SendTransaction(transaction, awaitResponse));
        }
        public async Task <TransactResponseViewModel> Transact(TransactViewModel transactViewModel)
        {
            var supporter  = _customerService.GetCustomerById(transactViewModel.SupporterId);
            var ambassador = _customerService.GetCustomerById(supporter.AmbassadorId);
            var service    = _serviceService.GetServiceById(transactViewModel.ServiceId);
            var commission = ComputeAmbassadorCommission(transactViewModel.Amount, service.AmbassadorCommissionRate);

            if (supporter != null && service != null)
            {
                var newServiceTransaction = new ServiceTransaction
                {
                    ServiceId        = transactViewModel.ServiceId,
                    AmbassadorId     = supporter.AmbassadorId,
                    SupporterId      = transactViewModel.SupporterId,
                    Amount           = _dataProtector.Protect(transactViewModel.Amount.ToString()),
                    CommissionAmount = _dataProtector.Protect(commission.ToString()),
                    CreatedOn        = DateTime.UtcNow
                };

                await _serviceTransactionService.InsertServiceTransactionAsync(newServiceTransaction);

                if (ambassador.CommissionBalance == null)
                {
                    ambassador.CommissionBalance = newServiceTransaction.CommissionAmount;
                }
                else
                {
                    var ambassadorCommissionBalance = Convert.ToDecimal(_dataProtector.Unprotect(ambassador.CommissionBalance)) + commission;
                    ambassador.CommissionBalance = _dataProtector.Protect(ambassadorCommissionBalance.ToString());
                }

                _customerService.UpdateCustomer(ambassador);
            }

            return(new TransactResponseViewModel
            {
                ServicePaid = service.Name,
                Amount = transactViewModel.Amount,
                PaidBy = supporter.Name,
                CommissionPaidTo = ambassador.Name
            });
        }
        public void Failed_Begin()
        {
            File.WriteAllText(
                ServiceTransaction.FilePathName,
                Resources.BeginInstalledServicesXmlFile);

            List <ServiceItem> services = new List <ServiceItem>
            {
                new ServiceItem
                {
                    ServiceName     = "Test",
                    DisplayName     = "Test Service",
                    BinaryPathName  = @"G:\test.exe",
                    TargetFramework = TargetFramework.Net35
                }
            };

            TestHelper.Throws(
                () => ServiceTransaction.Begin(services),
                typeof(InvalidOperationException));

            File.Delete(ServiceTransaction.FilePathName);
        }
예제 #11
0
        private void button1_Click(object sender, EventArgs e)
        {
            ServiceTransaction hackers = new ServiceTransaction();

            hackers.ShowDialog();
        }
        public TransactionResultTypes ConsumeTransaction(ServiceTransaction transaction, bool addExtraTime = false)
        {
            if (transaction.IsExpired(addExtraTime))
            {
                return(TransactionResultTypes.Expired);
            }

            var transactionResult = _serviceChain.BlockStorage.HistoryContainsTransactionOrRegistration(transaction);

            if (transactionResult != TransactionResultTypes.Ok)
            {
                return(transactionResult);
            }

            var accountId      = transaction.AccountId;
            var coreAccount    = _coreChain.GetCoreAccount(accountId);
            var serviceAccount = _serviceChain.GetServiceAccount(accountId);

            var type = transaction.TransactionType;

            switch (type)
            {
            case ServiceTransactionTypes.Join:

                var joinTransaction = (transaction as JoinServiceTransaction);
                var joinResult      = IsJoinValid(coreAccount, serviceAccount, joinTransaction);
                if (joinResult == TransactionResultTypes.Ok)
                {
                    if (_blockInfo.AddJoin(joinTransaction, serviceAccount, _serviceChain))
                    {
                        return(TransactionResultTypes.Ok);
                    }

                    return(TransactionResultTypes.BlockLimitExceeded);
                }

                return(joinResult);

            case ServiceTransactionTypes.Purchase:

                if (serviceAccount == null)
                {
                    return(TransactionResultTypes.InvalidServiceAccount);
                }

                var purchaseTransaction = (transaction as PurchaseServiceTransaction);
                var receiver            = _serviceChain.GetServiceAccount(purchaseTransaction.ReceiverAccountId);
                var purchaseResult      = IsPurchaseValid(coreAccount, receiver, _chainInfo, purchaseTransaction);
                if (purchaseResult == TransactionResultTypes.Ok)
                {
                    if (_blockInfo.AddPurchase(purchaseTransaction, serviceAccount, _serviceChain))
                    {
                        return(TransactionResultTypes.Ok);
                    }

                    return(TransactionResultTypes.BlockLimitExceeded);
                }

                return(purchaseResult);

            case ServiceTransactionTypes.RequestRevenue:

                if (serviceAccount == null)
                {
                    return(TransactionResultTypes.InvalidServiceAccount);
                }

                var maintainAccount = _maintainChain.GetMaintainAccount(accountId);
                if (maintainAccount == null)
                {
                    return(TransactionResultTypes.InvalidServiceAccount);
                }

                var revenueTransaction = transaction as RequestRevenueServiceTransaction;

                if (serviceAccount.TotalRevenuePayout != revenueTransaction.CurrentTotalRevenuePayout)
                {
                    return(TransactionResultTypes.RevenueAmoutInvalid);
                }

                if (revenueTransaction.PayoutAmount <= 0)
                {
                    return(TransactionResultTypes.RevenueAmoutInvalid);
                }

                var totalPayout = serviceAccount.TotalRevenuePayout + revenueTransaction.PayoutAmount;
                if (totalPayout > maintainAccount.TotalRevenue)
                {
                    return(TransactionResultTypes.RevenueAmoutInvalid);
                }

                if (_blockInfo.AddRevenue(revenueTransaction, serviceAccount, _serviceChain))
                {
                    return(TransactionResultTypes.Ok);
                }

                return(TransactionResultTypes.BlockLimitExceeded);

            case ServiceTransactionTypes.FeatureRequest:
            case ServiceTransactionTypes.Service:
                return(TransactionResultTypes.Ok);
            }

            return(TransactionResultTypes.InvalidTransaction);
        }
예제 #13
0
        public async Task <TransactionValidationResult> ValidateServiceTransaction(ServiceTransaction transaction)
        {
            var userCode = 0L;
            var result   = TransactionResultTypes.Unknown;
            var message  = string.Empty;
            TransactionValidation nodeValidation = null;

            if (transaction == null)
            {
                result = TransactionResultTypes.InvalidTransaction;
                goto end;
            }

            var chainInfo = CoreChain.GetChainInfo(ChainId);
            var accountId = transaction.AccountId;
            var service   = _node.ChainManager.GetService(ChainId);

            if (chainInfo == null)
            {
                result = TransactionResultTypes.ChainNotFound;
                goto end;
            }

            if (service == null)
            {
                result = TransactionResultTypes.ChainServiceUnavailable;
                goto end;
            }

            if (!_chainKeyValid)
            {
                var chainKey = chainInfo.GetValidChainKey(ChainIndex, _chainKeyIndex, Time.Timestamp);
                if (chainKey == null || chainKey.PublicKey != _chainKey.PublicKey)
                {
                    result = TransactionResultTypes.ChainNodeInvalid;
                    goto end;
                }
                _chainKeyValid = true;
            }

            if (BlockStorage.History.ContainsTransactionIdentifier(transaction))
            {
                result = TransactionResultTypes.AlreadyProcessed;
                goto end;
            }

            var coreAccount = CoreChain.GetCoreAccount(accountId);

            if (coreAccount == null)
            {
                result = TransactionResultTypes.InvalidCoreAccount;
                goto end;
            }

            {
                var(featResult, featCode) = ValidateTransactionFeatures((ushort)ServiceTransactionTypes.FeatureRequest, transaction);
                if (featResult != TransactionResultTypes.Ok)
                {
                    result   = featResult;
                    userCode = featCode;

                    goto end;
                }
            }

            var validation = await service.IsServiceTransactionValid(transaction);

            userCode = validation.UserCode;
            message  = validation.Message;

            if (!validation.IsOK)
            {
                if (validation.Result == ServiceResultTypes.PurchaseRequired)
                {
                    result = TransactionResultTypes.PurchaseRequired;
                }
                else
                {
                    result = TransactionResultTypes.ChainServiceErrorResponse;
                }
                goto end;
            }

            result         = TransactionResultTypes.Ok;
            nodeValidation = new TransactionValidation(transaction, _chainKeyIndex, _chainKey);
end:

            return(new TransactionValidationResult(result, userCode, message, nodeValidation));
        }
예제 #14
0
 public ServiceItem(long payload, SenderTypes sender, ServiceTransaction transaction, Connection connection) : base(payload, sender, connection)
 {
     Transaction = transaction;
 }