示例#1
0
        private async Task <ClientStartDealParams> CreateTTGraphsyncClientStartDealParams(ClientStartDealRequest model)
        {
            var dataRef = new TransferDataRef()
            {
                TransferType = TransferType.graphsync.ToString(),
                Root         = new Cid()
                {
                    Value = model.DataCid
                },
            };

            var walletAddress = await _lotusClient.WalletDefaultAddress();

            if (!walletAddress.Success)
            {
                return(null);
            }
            var dealParams = new ClientStartDealParams()
            {
                Data               = dataRef,
                Miner              = model.Miner,
                MinBlocksDuration  = (ulong)model.Duration,
                EpochPrice         = model.Price,
                Wallet             = walletAddress.Result,
                VerifiedDeal       = false,
                ProviderCollateral = "0"
            };

            return(dealParams);
        }
示例#2
0
        public async Task <DealResult> OnlineExecute(string fileDealId, string fileCidId, ClientStartDealRequest dealRequest)
        {
            var path = _uploadSetting.GetStoragePath(fileCidId);

            if (!File.Exists(path))
            {
                _logger.LogError("data not found " + path);
                return(new DealResult {
                    Success = false
                });
            }
            var fileCid = await _cloudSpeedManager.GetFileCid(fileCidId);

            string cid = fileCid.Cid;

            if (string.IsNullOrEmpty(cid))
            {
                _logger.LogError("data cid is required");
                return(new DealResult {
                    Success = false
                });
            }
            var  fileSize    = new FileInfo(path).Length;
            var  lotusClient = GlobalServices.ServiceProvider.GetService <LotusClient>();
            long dealSize    = fileCid.DealSize;

            if (dealSize == 0)
            {
                _logger.LogInformation("client deal size ...");
                var dealSizeResult = await lotusClient.ClientDealSize(new Cid { Value = cid });

                if (dealSizeResult.Success)
                {
                    dealSize = dealSizeResult.Result.PieceSize;
                    _logger.LogInformation(string.Format("client deal size ...{0}", dealSize));
                    await _cloudSpeedManager.UpdateFileCidDealSize(fileCidId, dealSize, dealSizeResult.Result.PayloadSize);
                }
                else
                {
                    _logger.LogError("client deal size failed: " + dealSizeResult.Error?.Message);
                    return(new DealResult {
                        Success = false
                    });
                }
            }
            dealRequest.CalPriceByDealSize(dealSize);
            var dealParams = await CreateTTGraphsyncClientStartDealParams(lotusClient, dealRequest);

            if (dealParams == null)
            {
                _logger.LogError(string.Format("CreateTTGraphsyncClientStartDealParams empty."));
                return(new DealResult {
                    Success = false
                });
            }
            _logger.LogInformation("lotus client start dealing: {dataCid} (datacid) {miner} {price} {duration}", cid, dealRequest.Miner, dealRequest.Price, dealRequest.Duration);
            var dealCid = await lotusClient.ClientStartDeal(dealParams);

            if (dealCid.Success)
            {
                var did = dealCid.Result.Value;
                _logger.LogInformation("lotus client start deal result: {datacid}(datacid) - {dealcid} (dealcid) {price} {duration}", cid, did, dealRequest.Price, dealRequest.Duration);
                await _cloudSpeedManager.UpdateFileDeal(fileDealId, dealRequest.Miner, did, FileDealStatus.Processing);

                return(new DealResult {
                    Success = true, DealCid = did, DealSize = fileSize, DealDate = DateTime.Now
                });
            }
            else
            {
                await _cloudSpeedManager.UpdateFileDeal(fileDealId, FileDealStatus.Failed, dealCid.Error.Message);

                _logger.LogError("lotus client start deal failed: {datacid}(datacid) - (errorMessage)", cid, dealCid.Error?.Message);
                return(new DealResult {
                    Success = false
                });
            }
        }
示例#3
0
        public async Task <DealResult> OfflineExecute(string fileDealId, string fileCidId, ClientStartDealRequest dealRequest, CancellationToken stoppingToken)
        {
            var path = _uploadSetting.GetStoragePath(fileCidId);

            if (!File.Exists(path))
            {
                _logger.LogError("data not found " + path);
                return(new DealResult {
                    Success = false
                });
            }
            var fileCid = await _cloudSpeedManager.GetFileCid(fileCidId);

            string cid = fileCid.Cid;

            if (string.IsNullOrEmpty(cid))
            {
                _logger.LogError("data cid is required");
                return(new DealResult {
                    Success = false
                });
            }
            var  fileSize    = new FileInfo(path).Length;
            var  lotusClient = GlobalServices.ServiceProvider.GetService <LotusClient>();
            long dealSize    = fileCid.DealSize;

            if (dealSize == 0)
            {
                _logger.LogInformation("client deal size ...");
                var dealSizeResult = await lotusClient.ClientDealSize(new Cid { Value = cid });

                if (dealSizeResult.Success)
                {
                    dealSize = dealSizeResult.Result.PieceSize;
                    _logger.LogInformation(string.Format("client deal size ...{0}", dealSize));
                    await _cloudSpeedManager.UpdateFileCidDealSize(fileCidId, dealSize, dealSizeResult.Result.PayloadSize);
                }
                else
                {
                    _logger.LogError("client deal size failed: " + dealSizeResult.Error?.Message);
                    return(new DealResult {
                        Success = false
                    });
                }
            }
            dealRequest.CalPriceByDealSize(dealSize);
            var  pieceCid  = fileCid.PieceCid;
            long pieceSize = fileCid.PieceSize;
            var  carPath   = path + ".car";

            if (!File.Exists(carPath))
            {
                _logger.LogInformation("Lotus ClientGenCar: {path}", path);
                var genCar = await lotusClient.ClientGenCar(new FileRef
                {
                    Path  = path,
                    IsCAR = false
                }, carPath);

                if (!genCar.Success)
                {
                    _logger.LogError("client ClientGenCar failed: " + genCar.Error?.Message);
                    return(new DealResult {
                        Success = false
                    });
                }
            }
            if (string.IsNullOrEmpty(pieceCid))
            {
                _logger.LogInformation("Lotus ClientCalcCommP: {carPath}", carPath);
                var commP = await lotusClient.ClientCalcCommP(carPath);

                if (commP.Success)
                {
                    pieceCid  = commP.Result.Root.Value;
                    pieceSize = commP.Result.Size;
                    await _cloudSpeedManager.UpdateFileCidCommP(fileCid.Id, pieceCid, pieceSize);
                }
                else
                {
                    _logger.LogError("client ClientCalcCommP failed: " + commP.Error?.Message);
                    return(new DealResult {
                        Success = false
                    });
                }
            }

            var dealParams = await CreateTTManualClientStartDealParams(lotusClient, dealRequest, new CommPRet
            {
                Root = new Cid {
                    Value = pieceCid
                },
                Size = pieceSize
            });

            if (dealParams == null)
            {
                _logger.LogError(0, string.Format("CreateTTManualClientStartDealParams empty."));
                return(new DealResult {
                    Success = false
                });
            }
            _logger.LogInformation("lotus client start dealing: {dataCid} (datacid) {miner} {price} {duration}", cid, dealRequest.Miner, dealRequest.Price, dealRequest.Duration);
            var dealCid = await lotusClient.ClientStartDeal(dealParams);

            if (dealCid.Success)
            {
                var did = dealCid.Result.Value;
                _logger.LogInformation("lotus client start deal result: {datacid}(datacid) - {dealcid} (dealcid) {price} {duration}", cid, did, dealRequest.Price, dealRequest.Duration);
                await _cloudSpeedManager.UpdateFileDeal(fileDealId, dealRequest.Miner, did, FileDealStatus.Processing);

                _logger.LogInformation("MarketImportDealData : {miner} - {dealcid} (dealcid) {carPath}", dealRequest.Miner, did, carPath);
                var startImport = DateTime.Now;
                while (true)
                {
                    var timeout = DateTime.Now - startImport;
                    if (timeout.TotalMinutes > 30)
                    {
                        _logger.LogWarning("market import deal data fail: {miner} - {dealcid} (dealcid) {carPath} timeout", dealRequest.Miner, did, carPath);
                        return(new DealResult {
                            Success = false
                        });
                    }
                    await Task.Delay(15000, stoppingToken);

                    var importResult = await lotusClient.MinerClients[dealRequest.Miner].MarketImportDealData(new Cid {
                        Value = did
                    }, carPath);
                    if (importResult.Success)
                    {
                        _logger.LogInformation("market import deal data success: {miner} - {dealcid} (dealcid) {carPath}", dealRequest.Miner, did, carPath);
                        break;
                    }
                    _logger.LogWarning("market import deal data fail: {miner} - {dealcid} (dealcid) {carPath}", dealRequest.Miner, did, carPath);
                }
                return(new DealResult {
                    Success = true, DealCid = did, DealSize = fileSize, DealDate = DateTime.Now
                });
            }
            else
            {
                await _cloudSpeedManager.UpdateFileDeal(fileDealId, FileDealStatus.Failed, dealCid.Error.Message);

                _logger.LogError(0, "lotus client start deal failed: {datacid}(datacid) - {errorMessage}", cid, dealCid.Error.Message);
                return(new DealResult {
                    Success = false
                });
            }
        }
示例#4
0
        private async Task <ClientStartDealParams> CreateTTManualClientStartDealParams(LotusClient lotusClient, ClientStartDealRequest model, CommPRet commPRet)
        {
            var dataRef = new TransferDataRef()
            {
                TransferType = TransferType.manual.ToString(),
                Root         = new Cid()
                {
                    Value = model.DataCid
                },
                PieceCid  = commPRet.Root,
                PieceSize = commPRet.Size
            };

            var walletAddress = await lotusClient.WalletDefaultAddress();

            if (!walletAddress.Success)
            {
                _logger.LogError(0, "can't get wallet default address.");
                return(null);
            }
            var dealParams = new ClientStartDealParams()
            {
                Data               = dataRef,
                Miner              = model.Miner,
                MinBlocksDuration  = (ulong)model.Duration,
                EpochPrice         = model.Price,
                Wallet             = walletAddress.Result,
                VerifiedDeal       = false,
                ProviderCollateral = "0"
            };

            return(dealParams);
        }
示例#5
0
        private async Task ClientStartDeal(LotusClient lotusClient, string fileDealId, string cid, CancellationToken stoppingToken)
        {
            try
            {
                //limit transfering
                while (_transferingDealIdBytes.Count > 0)
                {
                    if (stoppingToken.IsCancellationRequested)
                    {
                        break;
                    }

                    var keys = _transferingDealIdBytes.Keys.ToArray();
                    var maxSizeDealTransfering = LotusMinerSetting.GetMaxTransferingSizeInBytes(_lotusClientSetting.MaxTransferingSize);
                    if (maxSizeDealTransfering == 0 && _transferingDealIdBytes.Count < _lotusClientSetting.MaxTransferingCount)
                    {
                        break;
                    }

                    var sizeTransferingDealIds = _transferingDealIdBytes.Sum(a => a.Value);
                    if (sizeTransferingDealIds < maxSizeDealTransfering && _transferingDealIdBytes.Count < _lotusClientSetting.MaxTransferingCount)
                    {
                        break;
                    }

                    if (sizeTransferingDealIds >= maxSizeDealTransfering)
                    {
                        _logger.LogWarning(0, string.Format("limit transfering by sizes {0} >= {1}(max).", sizeTransferingDealIds, maxSizeDealTransfering));
                    }
                    if (_transferingDealIdBytes.Count > _lotusClientSetting.MaxTransferingCount)
                    {
                        _logger.LogWarning(0, string.Format("limit transfering by count {0} >= {1}(max).", _transferingDealIdBytes.Count, _lotusClientSetting.MaxTransferingCount));
                    }

                    await Task.Delay(10000, stoppingToken);
                }

                if (stoppingToken.IsCancellationRequested)
                {
                    return;
                }

                var fileCid = await _cloudSpeedManager.GetFileCidByCid(cid);

                if (fileCid == null)
                {
                    _logger.LogError(0, string.Format("fileCid not found {0}.", cid));
                    await _cloudSpeedManager.UpdateFileDeal(fileDealId, FileDealStatus.Failed, "fileCid not found");

                    return;
                }
                var fileFullPath = _uploadSetting.GetStoragePath(fileCid.Id);
                if (!File.Exists(fileFullPath))
                {
                    _logger.LogError(0, string.Format("file not found {0}.", cid));
                    await _cloudSpeedManager.UpdateFileDeal(fileDealId, FileDealStatus.Failed, "file not found");

                    return;
                }
                _logger.LogInformation("query file length: {path}", fileFullPath);
                var fileSize = new FileInfo(fileFullPath).Length;

                var online = fileSize < SectorSizeConstants.Bytes512MiB;

                var miner = _lotusClientSetting.GetMinerByFileSize(fileSize, online);

                if (miner == null)
                {
                    online = !online;
                    miner  = _lotusClientSetting.GetMinerByFileSize(fileSize, online);
                }

                if (miner == null)
                {
                    _logger.LogError(0, string.Format("can't found any miner for :{0} {1}.", cid, fileSize));
                    return;
                }

                var minerInfo = await lotusClient.StateMinerInfo(new StateMinerInfoRequest { Miner = miner.Miner });

                if (!minerInfo.Success)
                {
                    _logger.LogError(0, string.Format("can't get state info from :{0}.", miner));
                    return;
                }
                var askingPrice = miner.AskingPrice;
                if (askingPrice == 0)
                {
                    var ask = await lotusClient.ClientQueryAsk(new ClientQueryAskRequest
                    {
                        PeerId = minerInfo.Result.PeerId,
                        Miner  = miner.Miner
                    });

                    if (!ask.Success)
                    {
                        _logger.LogError(0, string.Format("can't query ask from :{0} {1}.", minerInfo.Result.PeerId, miner));
                        return;
                    }
                    if (!decimal.TryParse(ask.Result.Price, out askingPrice))
                    {
                        _logger.LogError(0, string.Format("can't parse ask price :{0}.", ask.Result.Price));
                        return;
                    }
                }

                if (askingPrice == 0)
                {
                    _logger.LogError(0, "asking price should be more than zero.");
                    return;
                }

                _logger.LogInformation("will use askingPrice {askingPrice} for miner {miner}", askingPrice, miner.Miner);

                var minDealDuration = 180 * LotusConstants.EpochsInDay;
                var dealRequest     = new ClientStartDealRequest
                {
                    DataCid     = cid,
                    Miner       = miner.Miner,
                    Duration    = minDealDuration,
                    AskingPrice = askingPrice
                };

                if (online)
                {
                    await _dealerService.OnlineExecute(fileDealId, fileCid.Id, dealRequest);
                }
                else
                {
                    await _dealerService.OfflineExecute(fileDealId, fileCid.Id, dealRequest, stoppingToken);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(0, ex, "lotus client start deal fail:" + ex.ToString());
            }
        }