コード例 #1
0
        TransactionResultTypes PreCheck(Transaction transaction)
        {
            if (transaction == null)
            {
                return(TransactionResultTypes.InvalidTransaction);
            }

            if (transaction.IsExpired(false))
            {
                return(TransactionResultTypes.Expired);
            }

            var chain = _chainManager.GetChain(transaction.TargetChainType, transaction.TargetChainId, transaction.ChainIndex);

            if (chain == null)
            {
                return(TransactionResultTypes.ChainNodeInvalid);
            }

            if (transaction.TargetChainType != ChainType.Core)
            {
                var service = _chainManager.GetService(transaction.TargetChainId);
                if (service == null)
                {
                    return(TransactionResultTypes.ChainServiceUnavailable);
                }
            }

            if (_transactionsLookup.Contains(transaction.UniqueIdentifier))
            {
                return(TransactionResultTypes.AlreadySubmitted);
            }
            _transactionsLookup[transaction.UniqueIdentifier] = null; //transaction; // store the transaction? Not needed imho

            var r = chain.BlockStorage.HistoryContainsTransactionOrRegistration(transaction);

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

            return(TransactionResultTypes.Ok);
        }
コード例 #2
0
        public async Task <bool> Start()
        {
            Log.Write("Starting sync.", this);

            _chainSyncItems = await GetChainSyncItems();

            foreach (var chainSync in _chainSyncItems.Values)
            {
                var chainType  = chainSync.ChainType;
                var chainId    = chainSync.ChainId;
                var chainIndex = chainSync.ChainIndex;

                if (chainSync.Remotes.Count == 0)
                {
                    var chain = _chainManager.GetChain(chainType, chainId, chainIndex);
                    if (chain.AvailableEndpoints.Count > 0)
                    {
                        Log.Warn($"No valid sync node for chain {chainId} found.", this);
                    }

                    //chainSync.UpdateLastBlockInfo(_coreChain.GetChainInfo(chainId)?.LastState);
                }

                var failCount  = 0;
                var downloaded = true;

retrySlices:
                if (failCount > 10)
                {
                    Log.Error($"Sync failed, slice download for chain {chainId} failed.", this);
                    return(false);
                }

                foreach (var remote in chainSync.Remotes)
                {
                    downloaded = await DownloadTransactionSlices(remote, chainSync);

                    if (downloaded)
                    {
                        break;
                    }
                }

                if (!downloaded)
                {
                    failCount++;
                    goto retrySlices;
                }

                failCount  = 0;
                downloaded = true;

                foreach (var remote in chainSync.Remotes)
                {
                    downloaded = await DownloadBlockSlices(remote, chainSync);

                    if (downloaded)
                    {
                        break;
                    }
                }

                if (!downloaded)
                {
                    failCount++;
                    goto retrySlices;
                }
            }

            await _chainManager.Start(true);

            foreach (var chainItemSync in _chainSyncItems.Values)
            {
                var chain = chainItemSync.Chain;
                while (chain.LastProcessedBlockId < chain.BlockStorage.LastStoredBlockId)
                {
                    var blockData = await chain.BlockStorage.GetBlockData(chain.LastProcessedBlockId + 1);

                    _chainManager.ConsumeBlockData(blockData);
                }
            }

            foreach (var chainItemSync in _chainSyncItems.Values)
            {
                foreach (var remote in chainItemSync.Remotes)
                {
                    await DownloadBlocks(remote, chainItemSync);
                }
            }

            return(true);
        }