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

Fix sign contract #172

Merged
merged 5 commits into from
Dec 10, 2022
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
59 changes: 58 additions & 1 deletion x/storage/keeper/msg_server_contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -271,10 +272,66 @@ func (suite *KeeperTestSuite) TestSignContract() {
expErr: true,
expErrMsg: "you do not have permission to approve this contract",
},

{
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: true,
expErrMsg: "not enough storage space",
postRun: func() {
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 {
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(),
Expand Down
6 changes: 6 additions & 0 deletions x/storage/keeper/msg_server_sign_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down