From e4e5d20ef9ebc25cc02db651323dcdba840ba32d Mon Sep 17 00:00:00 2001 From: j-rafique Date: Tue, 11 Jun 2024 22:34:46 +0500 Subject: [PATCH] [PSL-1194] add file size limit for upload asset endpoint of cascade --- walletnode/api/services/cascade.go | 28 +++++++++++- walletnode/cmd/app.go | 10 ++++- .../services/cascaderegister/service.go | 45 ++++++++++++++++++- .../services/cascaderegister/task_test.go | 2 +- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/walletnode/api/services/cascade.go b/walletnode/api/services/cascade.go index 967c9217f..ee77e6203 100644 --- a/walletnode/api/services/cascade.go +++ b/walletnode/api/services/cascade.go @@ -25,6 +25,10 @@ import ( "goa.design/goa/v3/security" ) +const ( + maxFileSize = 350 * 1024 * 1024 // 350MB in bytes +) + // CascadeAPIHandler - CascadeAPIHandler service type CascadeAPIHandler struct { *Common @@ -63,6 +67,12 @@ func (service *CascadeAPIHandler) UploadAsset(ctx context.Context, p *cascade.Up return nil, cascade.MakeBadRequest(errors.New("file not specified")) } + fileSize := utils.GetFileSizeInMB(p.Bytes) + if fileSize > maxFileSize { + log.WithError(err).Error("file size exceeds than 350Mb, please use V2 endpoint for uploading the file") + return nil, cascade.MakeInternalServerError(err) + } + id, expiry, err := service.register.StoreFile(ctx, p.Filename) if err != nil { log.WithError(err).Error("error storing File") @@ -77,11 +87,25 @@ func (service *CascadeAPIHandler) UploadAsset(ctx context.Context, p *cascade.Up } log.WithField("file-id", id).WithField("filename", *p.Filename).Infof("estimated fee has been calculated: %f", fee) + totalEstimatedFee := fee + 10.0 + reqPreBurnAmount := fee * 0.2 + + err = service.register.StoreFileMetadata(ctx, cascaderegister.FileMetadata{ + TaskID: id, + TotalEstimatedFee: totalEstimatedFee, + ReqPreBurnAmount: reqPreBurnAmount, + UploadAssetReq: p, + }) + if err != nil { + log.WithError(err).Error(err) + return nil, cascade.MakeInternalServerError(err) + } + res = &cascade.Asset{ FileID: id, ExpiresIn: expiry, - TotalEstimatedFee: fee + 10.0, // 10 is activation ticket fee - RequiredPreburnAmount: fee * 0.2, + TotalEstimatedFee: totalEstimatedFee, + RequiredPreburnAmount: reqPreBurnAmount, } return res, nil diff --git a/walletnode/cmd/app.go b/walletnode/cmd/app.go index cd353056e..f2058b3e0 100644 --- a/walletnode/cmd/app.go +++ b/walletnode/cmd/app.go @@ -16,6 +16,7 @@ import ( "github.com/pastelnetwork/gonode/common/storage/fs" "github.com/pastelnetwork/gonode/common/storage/memory" "github.com/pastelnetwork/gonode/common/storage/queries" + "github.com/pastelnetwork/gonode/common/storage/ticketstore" "github.com/pastelnetwork/gonode/common/sys" "github.com/pastelnetwork/gonode/common/version" "github.com/pastelnetwork/gonode/pastel" @@ -195,6 +196,13 @@ func runApp(ctx context.Context, config *configs.Config) error { } defer hDB.CloseHistoryDB(ctx) + //Initialize ticket DB + tDB, err := ticketstore.OpenTicketingDb() + if err != nil { + log.WithContext(ctx).WithError(err).Error("error connecting ticket db..") + } + defer tDB.CloseTicketDB(ctx) + //Set minimum confirmation requirements for transactions to ensure completion if config.RegTxMinConfirmations > 0 { config.NftRegister.NFTRegTxMinConfirmations = config.RegTxMinConfirmations @@ -225,7 +233,7 @@ func runApp(ctx context.Context, config *configs.Config) error { nftSearch := nftsearch.NewNftSearchService(&config.NftSearch, pastelClient, nodeClient, hDB) nftDownload := download.NewNftDownloadService(&config.NftDownload, pastelClient, nodeClient, hDB) - cascadeRegister := cascaderegister.NewService(&config.CascadeRegister, pastelClient, nodeClient, fileStorage, db, *nftDownload, hDB) + cascadeRegister := cascaderegister.NewService(&config.CascadeRegister, pastelClient, nodeClient, fileStorage, db, *nftDownload, hDB, tDB) senseRegister := senseregister.NewService(&config.SenseRegister, pastelClient, nodeClient, fileStorage, nftDownload, db, hDB) nftRegister := nftregister.NewService(&config.NftRegister, pastelClient, nodeClient, fileStorage, db, nftDownload, hDB) collectionRegister := collectionregister.NewService(&config.CollectionRegister, pastelClient, nodeClient, hDB) diff --git a/walletnode/services/cascaderegister/service.go b/walletnode/services/cascaderegister/service.go index 425690dd7..0cfd3d262 100644 --- a/walletnode/services/cascaderegister/service.go +++ b/walletnode/services/cascaderegister/service.go @@ -2,9 +2,13 @@ package cascaderegister import ( "context" - "github.com/pastelnetwork/gonode/common/storage/queries" + "github.com/google/uuid" "time" + "github.com/pastelnetwork/gonode/common/storage/queries" + "github.com/pastelnetwork/gonode/common/storage/ticketstore" + "github.com/pastelnetwork/gonode/common/types" + rqgrpc "github.com/pastelnetwork/gonode/raptorq/node/grpc" // Package image/jpeg is not used explicitly in the code below, @@ -45,6 +49,7 @@ type CascadeRegistrationService struct { downloadHandler download.NftDownloadingService historyDB queries.LocalStoreInterface + ticketDB ticketstore.TicketStorageInterface } // Run starts worker. @@ -124,11 +129,48 @@ func (service *CascadeRegistrationService) CalculateFee(ctx context.Context, fil return service.pastelHandler.GetEstimatedCascadeFee(ctx, utils.GetFileSizeInMB(fileData)) } +type FileMetadata struct { + TaskID string + TotalEstimatedFee float64 + ReqPreBurnAmount float64 + UploadAssetReq *cascade.UploadAssetPayload +} + +// StoreFileMetadata stores file metadata into the ticket db +func (service *CascadeRegistrationService) StoreFileMetadata(ctx context.Context, m FileMetadata) error { + blockCount, err := service.pastelHandler.PastelClient.GetBlockCount(ctx) + if err != nil { + return errors.Errorf("cannot get block count: %w", err) + } + + basefileID := uuid.NewString() + err = service.ticketDB.UpsertFile(types.File{ + FileID: basefileID, + UploadTimestamp: time.Now().UTC(), + Index: "00", + BaseFileID: basefileID, + TaskID: m.TaskID, + ReqBurnTxnAmount: m.ReqPreBurnAmount, + ReqAmount: m.TotalEstimatedFee, + UUIDKey: basefileID, + HashOfOriginalBigFile: utils.GetHashStringFromBytes(m.UploadAssetReq.Bytes), + NameOfOriginalBigFileWithExt: *m.UploadAssetReq.Filename, + SizeOfOriginalBigFile: utils.GetFileSizeInMB(m.UploadAssetReq.Bytes), + StartBlock: blockCount, + }) + if err != nil { + return errors.Errorf("error upsert for file data: %w", err) + } + + return nil +} + // NewService returns a new Service instance func NewService(config *Config, pastelClient pastel.Client, nodeClient node.ClientInterface, fileStorage storage.FileStorageInterface, db storage.KeyValue, downloadService download.NftDownloadingService, historyDB queries.LocalStoreInterface, + ticketDB ticketstore.TicketStorageInterface, ) *CascadeRegistrationService { return &CascadeRegistrationService{ Worker: task.NewWorker(), @@ -139,5 +181,6 @@ func NewService(config *Config, pastelClient pastel.Client, nodeClient node.Clie rqClient: rqgrpc.NewClient(), downloadHandler: downloadService, historyDB: historyDB, + ticketDB: ticketDB, } } diff --git a/walletnode/services/cascaderegister/task_test.go b/walletnode/services/cascaderegister/task_test.go index c9094ac85..8e115509b 100644 --- a/walletnode/services/cascaderegister/task_test.go +++ b/walletnode/services/cascaderegister/task_test.go @@ -195,7 +195,7 @@ func TestTaskRun(t *testing.T) { rqClientMock.ListenOnConnect(testCase.args.connectErr) downloadService := download.NewNftDownloadService(download.NewConfig(), pastelClientMock, nodeClient, nil) - service := NewService(NewConfig(), pastelClientMock, nodeClient, nil, nil, *downloadService, nil) + service := NewService(NewConfig(), pastelClientMock, nodeClient, nil, nil, *downloadService, nil, nil) service.rqClient = rqClientMock service.config.WaitTxnValidInterval = 1