示例#1
0
        public DataChain(int chainId, uint chainIndex, Node.Node node, CoreChain coreChain, ServiceChain serviceChain, ChainKeyStore keyStore, int attachementKey) : base(ChainType.Data, chainId, chainIndex, node)
        {
            _coreChain    = coreChain;
            _serviceChain = serviceChain;
            Attachements  = node.AttachementManager;

            ChainKeyIndex = keyStore.KeyIndex;
            ChainKey      = keyStore.DecryptedKey;
            KeyStore      = keyStore;

            AttachementKey = attachementKey;

            var endPoints = new HashSet <AvailableEndPoint>();

            var chainInfo = _coreChain.GetChainInfo(ChainId);

            if (chainInfo != null)
            {
                var cep = chainInfo.GetPublicEndpoints();
                foreach (var ep in cep)
                {
                    var e = new Uri(ep);
                    if (IsValidAvailableEndPoint(e))
                    {
                        endPoints.Add(new AvailableEndPoint(e));
                    }
                }
            }

            AvailableEndpoints = new List <AvailableEndPoint>(endPoints);
        }
示例#2
0
        public ServiceChain(int chainId, Node.Node node, CoreChain coreChain, ChainKeyStore keyStore) : base(ChainType.Service, chainId, 0, node)
        {
            CoreChain = coreChain;

            var endPoints = new HashSet <AvailableEndPoint>();

            _chainKeyIndex = keyStore.KeyIndex;
            _chainKey      = keyStore.DecryptedKey;
            KeyStore       = keyStore;

            var chainInfo = CoreChain.GetChainInfo(ChainId);

            if (chainInfo != null)
            {
                var cep = chainInfo.GetPublicEndpoints();
                foreach (var ep in cep)
                {
                    var e = new Uri(ep);
                    if (IsValidAvailableEndPoint(e))
                    {
                        endPoints.Add(new AvailableEndPoint(e));
                    }
                }
            }

            AvailableEndpoints = new List <AvailableEndPoint>(endPoints);
        }
        public CoreBlockGenerator(CoreChain coreChain, CoreBlock lastBlock)
        {
            _coreChain  = coreChain;
            _lastBlock  = lastBlock;
            _blockState = coreChain.GetChainInfo(Protocol.CoreChainId).LastState;

            NextAccountId = lastBlock.NextAccountId;
            NextChainId   = lastBlock.NextChainId;
        }
        public ServiceBlockGenerator(CoreChain coreChain, ServiceChain serviceChain, MaintainChain maintainChain, ServiceBlock lastBlock)
        {
            ChainId = serviceChain.ChainId;

            _blockInfo     = new ServiceBlockInfo(serviceChain);
            _coreChain     = coreChain;
            _serviceChain  = serviceChain;
            _maintainChain = maintainChain;
            _lastBlock     = lastBlock;
            _chainInfo     = coreChain.GetChainInfo(ChainId);
            _blockState    = _chainInfo.LastState;
        }
        ChainInfo GetChainInfo(int chainId)
        {
            if (_chains.TryGetValue(chainId, out var chainInfo))
            {
                return(chainInfo);
            }

            chainInfo = _coreChain.GetChainInfo(chainId, true, true);
            if (chainInfo == null)
            {
                return(null);
            }

            _chains[chainId] = chainInfo;
            return(chainInfo);
        }
示例#6
0
        public async Task <TransactionValidationResult> ValidateDataTransaction(DataTransaction transaction)
        {
            var userCode = 0L;
            var result   = TransactionResultTypes.Unknown;
            var message  = string.Empty;
            TransactionValidation nodeValidation = null;

            var type      = transaction.TransactionType;
            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 (transaction == null)
            {
                result = TransactionResultTypes.InvalidTransaction;
                goto end;
            }

            if (!transaction.IsDataTransactionValid)
            {
                result = TransactionResultTypes.InvalidContent;
                goto end;
            }

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

            var coreAccount = _coreChain.GetCoreAccount(accountId);

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

            var serviceAccount = _serviceChain.GetServiceAccount(accountId);

            if (transaction.SignKeyIndex == Protocol.CoreAccountSignKeyIndex)
            {
                if (!transaction.IsSignatureValid(coreAccount.AccountKey, null))
                {
                    result = TransactionResultTypes.InvalidSignature;
                    goto end;
                }
            }
            else
            {
                if (serviceAccount == null)
                {
                    result = TransactionResultTypes.InvalidServiceAccount;
                    goto end;
                }

                var accountKey = serviceAccount.GetValidAccountKey(transaction.SignKeyIndex, transaction.Timestamp);
                if (accountKey == null)
                {
                    result = TransactionResultTypes.InvalidServiceAccountKey;
                    goto end;
                }

                if (accountKey.IsExpired())
                {
                    result = TransactionResultTypes.Expired;
                    goto end;
                }

                if (!transaction.IsSignatureValid(coreAccount.AccountKey, accountKey))
                {
                    result = TransactionResultTypes.InvalidSignature;
                    goto end;
                }
            }

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

            if (type == DataTransactionTypes.Attachement)
            {
                if (!Attachements.AreAttachementsUploaded(transaction as AttachementDataTransaction))
                {
                    result = TransactionResultTypes.AttachementsNotUploaded;
                    goto end;
                }
            }

            var purchaseRequired = transaction.GetFeature <RequiredPurchase>(RequiredPurchase.FeatureId);

            if (purchaseRequired != null && purchaseRequired.RequiredPurchaseType != PurchaseTypes.None)
            {
                if (!serviceAccount.HasRequiredTransactionPurchase(transaction, purchaseRequired))
                {
                    result = TransactionResultTypes.PurchaseRequired;
                    goto end;
                }
            }

            var validation = await service.IsDataTransactionValid(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));
        }
示例#7
0
        void ConsumeTransaction(CommitItems commitItems, TransactionItem <ServiceTransaction> item)
        {
            var transaction = item.Transaction;

            if (transaction.TransactionId <= LastProcessedTransactionId)
            {
                return;
            }

            if (transaction.TargetChainId == ChainId)
            {
                var type = transaction.TransactionType;

                var accountId = transaction.AccountId;
                var account   = GetServiceAccount(accountId);

                if (type == ServiceTransactionTypes.Join)
                {
                    var joinTransaction = transaction as JoinServiceTransaction;
                    if (account == null)
                    {
                        account = new ServiceAccount(accountId, ChainId, joinTransaction.Timestamp);
                        if (joinTransaction.AccountKey != null)
                        {
                            account.AddAccountKey(joinTransaction.AccountKey, joinTransaction.Timestamp);
                        }

                        lock (_lock)
                            _serviceAccounts[accountId] = account;

                        _serviceAccountStorage.AddEntry(accountId, account.ToByteArray());

                        return; // skip dirty account stuff
                    }

                    if (joinTransaction.AccountKey != null)
                    {
                        account.AddAccountKey(joinTransaction.AccountKey, joinTransaction.Timestamp);
                        commitItems.DirtyAccounts.Add(accountId);
                    }
                }
                else if (type == ServiceTransactionTypes.Purchase)
                {
                    var purchaseTransaction = transaction as PurchaseServiceTransaction;
                    var purchase            = CoreChain.GetChainInfo(ChainId).GetPurchase(purchaseTransaction.PurchaseGroupId, purchaseTransaction.PurchaseItemId);

                    if (purchase != null)
                    {
                        var targetAccount = GetServiceAccount(purchaseTransaction.ReceiverAccountId);
                        if (targetAccount != null && purchase != null)
                        {
                            targetAccount.AddPurchase(purchaseTransaction, purchase);
                            commitItems.DirtyAccounts.Add(targetAccount.AccountId);
                        }
                    }
                }
                else if (type == ServiceTransactionTypes.RequestRevenue)
                {
                    var revenueTransaction = transaction as RequestRevenueServiceTransaction;
                    account.UpdateTotelRevenuePayout(revenueTransaction.CurrentTotalRevenuePayout + revenueTransaction.PayoutAmount);
                }

                ConsumeTransactionFeatures((ushort)ServiceTransactionTypes.FeatureRequest, transaction, account, commitItems);

                commitItems.DirtyAccounts.Add(accountId);
            }
        }