Skip to content

Commit e4e5d20

Browse files
committed
[PSL-1194] add file size limit for upload asset endpoint of cascade
1 parent 98a0ecf commit e4e5d20

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

walletnode/api/services/cascade.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import (
2525
"goa.design/goa/v3/security"
2626
)
2727

28+
const (
29+
maxFileSize = 350 * 1024 * 1024 // 350MB in bytes
30+
)
31+
2832
// CascadeAPIHandler - CascadeAPIHandler service
2933
type CascadeAPIHandler struct {
3034
*Common
@@ -63,6 +67,12 @@ func (service *CascadeAPIHandler) UploadAsset(ctx context.Context, p *cascade.Up
6367
return nil, cascade.MakeBadRequest(errors.New("file not specified"))
6468
}
6569

70+
fileSize := utils.GetFileSizeInMB(p.Bytes)
71+
if fileSize > maxFileSize {
72+
log.WithError(err).Error("file size exceeds than 350Mb, please use V2 endpoint for uploading the file")
73+
return nil, cascade.MakeInternalServerError(err)
74+
}
75+
6676
id, expiry, err := service.register.StoreFile(ctx, p.Filename)
6777
if err != nil {
6878
log.WithError(err).Error("error storing File")
@@ -77,11 +87,25 @@ func (service *CascadeAPIHandler) UploadAsset(ctx context.Context, p *cascade.Up
7787
}
7888
log.WithField("file-id", id).WithField("filename", *p.Filename).Infof("estimated fee has been calculated: %f", fee)
7989

90+
totalEstimatedFee := fee + 10.0
91+
reqPreBurnAmount := fee * 0.2
92+
93+
err = service.register.StoreFileMetadata(ctx, cascaderegister.FileMetadata{
94+
TaskID: id,
95+
TotalEstimatedFee: totalEstimatedFee,
96+
ReqPreBurnAmount: reqPreBurnAmount,
97+
UploadAssetReq: p,
98+
})
99+
if err != nil {
100+
log.WithError(err).Error(err)
101+
return nil, cascade.MakeInternalServerError(err)
102+
}
103+
80104
res = &cascade.Asset{
81105
FileID: id,
82106
ExpiresIn: expiry,
83-
TotalEstimatedFee: fee + 10.0, // 10 is activation ticket fee
84-
RequiredPreburnAmount: fee * 0.2,
107+
TotalEstimatedFee: totalEstimatedFee,
108+
RequiredPreburnAmount: reqPreBurnAmount,
85109
}
86110

87111
return res, nil

walletnode/cmd/app.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/pastelnetwork/gonode/common/storage/fs"
1717
"github.com/pastelnetwork/gonode/common/storage/memory"
1818
"github.com/pastelnetwork/gonode/common/storage/queries"
19+
"github.com/pastelnetwork/gonode/common/storage/ticketstore"
1920
"github.com/pastelnetwork/gonode/common/sys"
2021
"github.com/pastelnetwork/gonode/common/version"
2122
"github.com/pastelnetwork/gonode/pastel"
@@ -195,6 +196,13 @@ func runApp(ctx context.Context, config *configs.Config) error {
195196
}
196197
defer hDB.CloseHistoryDB(ctx)
197198

199+
//Initialize ticket DB
200+
tDB, err := ticketstore.OpenTicketingDb()
201+
if err != nil {
202+
log.WithContext(ctx).WithError(err).Error("error connecting ticket db..")
203+
}
204+
defer tDB.CloseTicketDB(ctx)
205+
198206
//Set minimum confirmation requirements for transactions to ensure completion
199207
if config.RegTxMinConfirmations > 0 {
200208
config.NftRegister.NFTRegTxMinConfirmations = config.RegTxMinConfirmations
@@ -225,7 +233,7 @@ func runApp(ctx context.Context, config *configs.Config) error {
225233
nftSearch := nftsearch.NewNftSearchService(&config.NftSearch, pastelClient, nodeClient, hDB)
226234
nftDownload := download.NewNftDownloadService(&config.NftDownload, pastelClient, nodeClient, hDB)
227235

228-
cascadeRegister := cascaderegister.NewService(&config.CascadeRegister, pastelClient, nodeClient, fileStorage, db, *nftDownload, hDB)
236+
cascadeRegister := cascaderegister.NewService(&config.CascadeRegister, pastelClient, nodeClient, fileStorage, db, *nftDownload, hDB, tDB)
229237
senseRegister := senseregister.NewService(&config.SenseRegister, pastelClient, nodeClient, fileStorage, nftDownload, db, hDB)
230238
nftRegister := nftregister.NewService(&config.NftRegister, pastelClient, nodeClient, fileStorage, db, nftDownload, hDB)
231239
collectionRegister := collectionregister.NewService(&config.CollectionRegister, pastelClient, nodeClient, hDB)

walletnode/services/cascaderegister/service.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package cascaderegister
22

33
import (
44
"context"
5-
"github.com/pastelnetwork/gonode/common/storage/queries"
5+
"github.com/google/uuid"
66
"time"
77

8+
"github.com/pastelnetwork/gonode/common/storage/queries"
9+
"github.com/pastelnetwork/gonode/common/storage/ticketstore"
10+
"github.com/pastelnetwork/gonode/common/types"
11+
812
rqgrpc "github.com/pastelnetwork/gonode/raptorq/node/grpc"
913

1014
// Package image/jpeg is not used explicitly in the code below,
@@ -45,6 +49,7 @@ type CascadeRegistrationService struct {
4549

4650
downloadHandler download.NftDownloadingService
4751
historyDB queries.LocalStoreInterface
52+
ticketDB ticketstore.TicketStorageInterface
4853
}
4954

5055
// Run starts worker.
@@ -124,11 +129,48 @@ func (service *CascadeRegistrationService) CalculateFee(ctx context.Context, fil
124129
return service.pastelHandler.GetEstimatedCascadeFee(ctx, utils.GetFileSizeInMB(fileData))
125130
}
126131

132+
type FileMetadata struct {
133+
TaskID string
134+
TotalEstimatedFee float64
135+
ReqPreBurnAmount float64
136+
UploadAssetReq *cascade.UploadAssetPayload
137+
}
138+
139+
// StoreFileMetadata stores file metadata into the ticket db
140+
func (service *CascadeRegistrationService) StoreFileMetadata(ctx context.Context, m FileMetadata) error {
141+
blockCount, err := service.pastelHandler.PastelClient.GetBlockCount(ctx)
142+
if err != nil {
143+
return errors.Errorf("cannot get block count: %w", err)
144+
}
145+
146+
basefileID := uuid.NewString()
147+
err = service.ticketDB.UpsertFile(types.File{
148+
FileID: basefileID,
149+
UploadTimestamp: time.Now().UTC(),
150+
Index: "00",
151+
BaseFileID: basefileID,
152+
TaskID: m.TaskID,
153+
ReqBurnTxnAmount: m.ReqPreBurnAmount,
154+
ReqAmount: m.TotalEstimatedFee,
155+
UUIDKey: basefileID,
156+
HashOfOriginalBigFile: utils.GetHashStringFromBytes(m.UploadAssetReq.Bytes),
157+
NameOfOriginalBigFileWithExt: *m.UploadAssetReq.Filename,
158+
SizeOfOriginalBigFile: utils.GetFileSizeInMB(m.UploadAssetReq.Bytes),
159+
StartBlock: blockCount,
160+
})
161+
if err != nil {
162+
return errors.Errorf("error upsert for file data: %w", err)
163+
}
164+
165+
return nil
166+
}
167+
127168
// NewService returns a new Service instance
128169
func NewService(config *Config, pastelClient pastel.Client, nodeClient node.ClientInterface,
129170
fileStorage storage.FileStorageInterface, db storage.KeyValue,
130171
downloadService download.NftDownloadingService,
131172
historyDB queries.LocalStoreInterface,
173+
ticketDB ticketstore.TicketStorageInterface,
132174
) *CascadeRegistrationService {
133175
return &CascadeRegistrationService{
134176
Worker: task.NewWorker(),
@@ -139,5 +181,6 @@ func NewService(config *Config, pastelClient pastel.Client, nodeClient node.Clie
139181
rqClient: rqgrpc.NewClient(),
140182
downloadHandler: downloadService,
141183
historyDB: historyDB,
184+
ticketDB: ticketDB,
142185
}
143186
}

walletnode/services/cascaderegister/task_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func TestTaskRun(t *testing.T) {
195195
rqClientMock.ListenOnConnect(testCase.args.connectErr)
196196

197197
downloadService := download.NewNftDownloadService(download.NewConfig(), pastelClientMock, nodeClient, nil)
198-
service := NewService(NewConfig(), pastelClientMock, nodeClient, nil, nil, *downloadService, nil)
198+
service := NewService(NewConfig(), pastelClientMock, nodeClient, nil, nil, *downloadService, nil, nil)
199199
service.rqClient = rqClientMock
200200
service.config.WaitTxnValidInterval = 1
201201

0 commit comments

Comments
 (0)