From b146de97063939cde1f39bcdbbe29a91f1a58e9f Mon Sep 17 00:00:00 2001 From: Nugget Date: Fri, 9 Dec 2022 14:06:47 -0600 Subject: [PATCH 1/4] add tc for not enough storage space --- x/storage/keeper/msg_server_contracts_test.go | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/x/storage/keeper/msg_server_contracts_test.go b/x/storage/keeper/msg_server_contracts_test.go index 68805dbc5..0c7911a21 100644 --- a/x/storage/keeper/msg_server_contracts_test.go +++ b/x/storage/keeper/msg_server_contracts_test.go @@ -249,6 +249,7 @@ func (suite *KeeperTestSuite) TestSignContract() { { name: "invalid_permission_to_sign_contract", preRun: func() *types.MsgSignContract { + // creating a test contract to sign c := types.Contracts{ Cid: "123", Creator: provider.String(), @@ -271,17 +272,36 @@ func (suite *KeeperTestSuite) TestSignContract() { expErr: true, expErrMsg: "you do not have permission to approve this contract", }, - { - name: "successful_contract_signed", + name: "not enough storage", preRun: func() *types.MsgSignContract { + // create a test StoragePaymentInfo + spi := types.StoragePaymentInfo{ + SpaceAvailable: 200_000_000, + SpaceUsed: 200_000_000, + Address: user.String(), + } + sKeeper.SetStoragePaymentInfo(suite.ctx, spi) + _, found := sKeeper.GetStoragePaymentInfo(suite.ctx, user.String()) + suite.Require().True(found) return &types.MsgSignContract{ Cid: "123", Creator: user.String(), } }, - expErr: false, + expErr: true, + expErrMsg: "your mom", }, + // { + // name: "successful_contract_signed", + // preRun: func() *types.MsgSignContract { + // return &types.MsgSignContract{ + // Cid: "123", + // Creator: user.String(), + // } + // }, + // expErr: false, + // }, } for _, tc := range cases { From 21416003f131684980902a15c11761ec84c0b972 Mon Sep 17 00:00:00 2001 From: Nugget Date: Fri, 9 Dec 2022 14:20:09 -0600 Subject: [PATCH 2/4] add tc for not enough storage space again :) --- x/storage/keeper/msg_server_contracts_test.go | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/x/storage/keeper/msg_server_contracts_test.go b/x/storage/keeper/msg_server_contracts_test.go index 0c7911a21..ca29d04f5 100644 --- a/x/storage/keeper/msg_server_contracts_test.go +++ b/x/storage/keeper/msg_server_contracts_test.go @@ -290,18 +290,21 @@ func (suite *KeeperTestSuite) TestSignContract() { } }, expErr: true, - expErrMsg: "your mom", + expErrMsg: "not enough storage space", + postRun: func() { + sKeeper.RemoveStoragePaymentInfo(suite.ctx, user.String()) + }, + }, + { + name: "successful_contract_signed", + preRun: func() *types.MsgSignContract { + return &types.MsgSignContract{ + Cid: "123", + Creator: user.String(), + } + }, + expErr: false, }, - // { - // name: "successful_contract_signed", - // preRun: func() *types.MsgSignContract { - // return &types.MsgSignContract{ - // Cid: "123", - // Creator: user.String(), - // } - // }, - // expErr: false, - // }, } for _, tc := range cases { From 72ca7d8cdb05c8195474ae0796e59d301f61613b Mon Sep 17 00:00:00 2001 From: Nugget Date: Fri, 9 Dec 2022 15:29:08 -0600 Subject: [PATCH 3/4] add check for SPI end date and test --- x/storage/keeper/msg_server_contracts_test.go | 25 +++++++++++++++++++ x/storage/keeper/msg_server_sign_contract.go | 6 +++++ 2 files changed, 31 insertions(+) diff --git a/x/storage/keeper/msg_server_contracts_test.go b/x/storage/keeper/msg_server_contracts_test.go index ca29d04f5..79f9d36bc 100644 --- a/x/storage/keeper/msg_server_contracts_test.go +++ b/x/storage/keeper/msg_server_contracts_test.go @@ -295,6 +295,31 @@ func (suite *KeeperTestSuite) TestSignContract() { sKeeper.RemoveStoragePaymentInfo(suite.ctx, user.String()) }, }, + { + name: "expired storage subscription", + preRun: func() *types.MsgSignContract { + // create a test StoragePaymentInfo + spi := types.StoragePaymentInfo{ + SpaceAvailable: 200_000_000, + SpaceUsed: 0, + // set expiration date to yesterday + End: time.Now().AddDate(0, -1, 0), + Address: user.String(), + } + sKeeper.SetStoragePaymentInfo(suite.ctx, spi) + _, found := sKeeper.GetStoragePaymentInfo(suite.ctx, user.String()) + suite.Require().True(found) + return &types.MsgSignContract{ + Cid: "123", + Creator: user.String(), + } + }, + expErr: true, + expErrMsg: "storage subscription has expired", + postRun: func() { + sKeeper.RemoveStoragePaymentInfo(suite.ctx, user.String()) + }, + }, { name: "successful_contract_signed", preRun: func() *types.MsgSignContract { diff --git a/x/storage/keeper/msg_server_sign_contract.go b/x/storage/keeper/msg_server_sign_contract.go index 846086160..8d4001f54 100644 --- a/x/storage/keeper/msg_server_sign_contract.go +++ b/x/storage/keeper/msg_server_sign_contract.go @@ -57,10 +57,16 @@ func (k msgServer) SignContract(goCtx context.Context, msg *types.MsgSignContrac } payInfo, found := k.GetStoragePaymentInfo(ctx, msg.Creator) if found { + // check if user has any free space if payInfo.SpaceUsed+fsize.Int64() > payInfo.SpaceAvailable { return nil, fmt.Errorf("not enough storage space") } + // check if storage subscription still active + if payInfo.End.Before(ctx.BlockTime()) { + return nil, fmt.Errorf("storage subscription has expired") + } } + // we going to need an else statement to check for the free trial storage since they wont have payInfo payInfo.SpaceUsed += fsize.Int64() From e0363d7ff561f0a7bf8a5598bd115446f53d7b7c Mon Sep 17 00:00:00 2001 From: Nugget Date: Fri, 9 Dec 2022 15:35:09 -0600 Subject: [PATCH 4/4] edited successful_contract_signed tc --- x/storage/keeper/msg_server_contracts_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/x/storage/keeper/msg_server_contracts_test.go b/x/storage/keeper/msg_server_contracts_test.go index 79f9d36bc..fd03d3a15 100644 --- a/x/storage/keeper/msg_server_contracts_test.go +++ b/x/storage/keeper/msg_server_contracts_test.go @@ -323,6 +323,15 @@ func (suite *KeeperTestSuite) TestSignContract() { { name: "successful_contract_signed", preRun: func() *types.MsgSignContract { + spi := types.StoragePaymentInfo{ + SpaceAvailable: 200_000_000, + SpaceUsed: 0, + End: time.Now().AddDate(0, 10, 0), + Address: user.String(), + } + sKeeper.SetStoragePaymentInfo(suite.ctx, spi) + _, found := sKeeper.GetStoragePaymentInfo(suite.ctx, user.String()) + suite.Require().True(found) return &types.MsgSignContract{ Cid: "123", Creator: user.String(),