Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PSL-1194] add file size limit for upload asset endpoint of cascade #878

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions walletnode/api/services/cascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion walletnode/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 44 additions & 1 deletion walletnode/services/cascaderegister/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -45,6 +49,7 @@ type CascadeRegistrationService struct {

downloadHandler download.NftDownloadingService
historyDB queries.LocalStoreInterface
ticketDB ticketstore.TicketStorageInterface
}

// Run starts worker.
Expand Down Expand Up @@ -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(),
Expand All @@ -139,5 +181,6 @@ func NewService(config *Config, pastelClient pastel.Client, nodeClient node.Clie
rqClient: rqgrpc.NewClient(),
downloadHandler: downloadService,
historyDB: historyDB,
ticketDB: ticketDB,
}
}
2 changes: 1 addition & 1 deletion walletnode/services/cascaderegister/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading