Skip to content

Commit

Permalink
refactoring buy storage
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell committed Nov 14, 2023
1 parent 4d37506 commit 7b01315
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 218 deletions.
4 changes: 2 additions & 2 deletions proto/canine_chain/storage/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ message MsgShutdownProviderResponse {}
message MsgBuyStorage {
string creator = 1;
string for_address = 2;
string duration = 3;
string bytes = 4;
int64 duration_days = 3;
int64 bytes = 4;
string payment_denom = 5;
}

Expand Down
14 changes: 12 additions & 2 deletions x/storage/client/cli/tx_buy_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func CmdBuyStorage() *cobra.Command {
argBytes := args[2]
argPaymentDenom := args[3]

bytes, err := strconv.ParseInt(argBytes, 10, 64)
if err != nil {
return err
}

duration, err := strconv.ParseInt(argDuration, 10, 64)
if err != nil {
return err
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
Expand All @@ -31,8 +41,8 @@ func CmdBuyStorage() *cobra.Command {
msg := types.NewMsgBuyStorage(
clientCtx.GetFromAddress().String(),
argForAddress,
argDuration,
argBytes,
duration,
bytes,
argPaymentDenom,
)
if err := msg.ValidateBasic(); err != nil {
Expand Down
13 changes: 3 additions & 10 deletions x/storage/keeper/msg_server_buy_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"
"fmt"
"strconv"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -20,20 +19,14 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) (
return nil, err
}

duration, err := time.ParseDuration(msg.Duration)
if err != nil {
return nil, fmt.Errorf("duration can't be parsed: %s", err.Error())
}
duration := time.Duration(msg.DurationDays) * time.Hour * 24

timeMonth := time.Hour * 24 * 30
if duration.Truncate(timeMonth) <= 0 {
if duration < timeMonth {
return nil, fmt.Errorf("duration can't be less than 1 month")
}

bytes, err := strconv.ParseInt(msg.Bytes, 10, 64)
if err != nil {
return nil, fmt.Errorf("bytes can't be parsed: %s", err.Error())
}
bytes := msg.Bytes

denom := msg.PaymentDenom
if denom != "ujkl" {
Expand Down
36 changes: 18 additions & 18 deletions x/storage/keeper/msg_server_buy_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "720h",
Bytes: "6000000000",
DurationDays: 30,
Bytes: 6000000000,
PaymentDenom: "ujkl",
},
expErr: true,
Expand All @@ -75,8 +75,8 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "720h",
Bytes: "3000000000",
DurationDays: 30,
Bytes: 3000000000,
PaymentDenom: "ujkl",
},
expErr: true,
Expand All @@ -97,8 +97,8 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "720h",
Bytes: "6000000000",
DurationDays: 30,
Bytes: 6000000000,
PaymentDenom: "ujkl",
},
expErr: false,
Expand All @@ -110,8 +110,8 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "2160h",
Bytes: "1000000000000",
DurationDays: 90,
Bytes: 1000000000000,
PaymentDenom: "ujkl",
},
expErr: false,
Expand All @@ -123,8 +123,8 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "720h",
Bytes: "-1",
DurationDays: 30,
Bytes: -1,
PaymentDenom: "ujkl",
},
expErr: true,
Expand All @@ -135,8 +135,8 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "2m",
Bytes: "1000000000",
DurationDays: 1,
Bytes: 1000000000,
PaymentDenom: "ujkl",
},
expErr: true,
Expand All @@ -148,8 +148,8 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "720h",
Bytes: "1000000000",
DurationDays: 30,
Bytes: 1000000000,
PaymentDenom: "uatom",
},
expErr: true,
Expand All @@ -160,8 +160,8 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
msg: types.MsgBuyStorage{
Creator: "invalid_address",
ForAddress: testAccount,
Duration: "720h",
Bytes: "1000000000",
DurationDays: 30,
Bytes: 1000000000,
PaymentDenom: "ujkl",
},
expErr: true,
Expand All @@ -172,8 +172,8 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: "invalid_address",
Duration: "720h",
Bytes: "1000000000",
DurationDays: 30,
Bytes: 1000000000,
PaymentDenom: "ujkl",
},
expErr: true,
Expand Down
4 changes: 2 additions & 2 deletions x/storage/keeper/msg_server_proofs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func (suite *KeeperTestSuite) TestPostProof() {
_, err = msgSrvr.BuyStorage(context, &types.MsgBuyStorage{
Creator: user.String(),
ForAddress: user.String(),
Bytes: "4000000000",
Duration: "720h",
Bytes: 4000000000,
DurationDays: 30,
PaymentDenom: "ujkl",
})
suite.Require().NoError(err)
Expand Down
20 changes: 10 additions & 10 deletions x/storage/keeper/msg_server_upgrade_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (suite *KeeperTestSuite) TestUpgradeStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "1440h",
Bytes: "6000000000",
DurationDays: 30,
Bytes: 6000000000,
PaymentDenom: "ujkl",
},
expErr: false,
Expand All @@ -78,8 +78,8 @@ func (suite *KeeperTestSuite) TestUpgradeStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "720h",
Bytes: "4000000000",
DurationDays: 30,
Bytes: 4000000000,
PaymentDenom: "ujkl",
},
expErr: true,
Expand All @@ -101,8 +101,8 @@ func (suite *KeeperTestSuite) TestUpgradeStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "2160h",
Bytes: "8000000000",
DurationDays: 30 * 4,
Bytes: 8000000000,
PaymentDenom: "ujkl",
},
expErr: false,
Expand All @@ -124,8 +124,8 @@ func (suite *KeeperTestSuite) TestUpgradeStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "2160h",
Bytes: "8000000000",
DurationDays: 30,
Bytes: 8000000000,
PaymentDenom: "ujkl",
},
expErr: false,
Expand All @@ -145,8 +145,8 @@ func (suite *KeeperTestSuite) TestUpgradeStorage() {
msg: types.MsgBuyStorage{
Creator: testAccount,
ForAddress: testAccount,
Duration: "2160h",
Bytes: "3000000000",
DurationDays: 30,
Bytes: 3000000000,
PaymentDenom: "ujkl",
},
expErr: true,
Expand Down
11 changes: 5 additions & 6 deletions x/storage/simulation/buy_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package simulation

import (
"math/rand"
"strconv"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
Expand Down Expand Up @@ -35,12 +33,13 @@ func SimulateMsgBuyStorage(

size := simtypes.RandIntBetween(r, 1_000_000_000, 10_000_000_000)

t := time.Hour * 720
hours := sdk.NewDec(t.Milliseconds()).Quo(sdk.NewDec(60 * 60 * 1000))
var t int64 = 30

hours := sdk.NewDec(t * 24)
cost := k.GetStorageCost(ctx, int64(size), hours.TruncateInt64())

msg.Bytes = strconv.Itoa(size)
msg.Duration = t.String()
msg.Bytes = int64(size)
msg.DurationDays = t

jBalance := bk.GetBalance(ctx, simAccount.Address, "ujkl")
// It is impossible to specify default bond denom through param.json
Expand Down
19 changes: 4 additions & 15 deletions x/storage/types/message_buy_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package types

import (
fmt "fmt"
"strconv"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
Expand All @@ -14,11 +12,11 @@ const TypeMsgBuyStorage = "buy_storage"

var _ sdk.Msg = &MsgBuyStorage{}

func NewMsgBuyStorage(creator string, forAddress string, duration string, bytes string, paymentDenom string) *MsgBuyStorage {
func NewMsgBuyStorage(creator string, forAddress string, duration int64, bytes int64, paymentDenom string) *MsgBuyStorage {
return &MsgBuyStorage{
Creator: creator,
ForAddress: forAddress,
Duration: duration,
DurationDays: duration,
Bytes: bytes,
PaymentDenom: paymentDenom,
}
Expand Down Expand Up @@ -62,17 +60,8 @@ func (msg *MsgBuyStorage) ValidateBasic() error {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator prefix (%s)", fmt.Errorf("%s is not a valid prefix here. Expected `jkl`", prefix))
}

if _, err := strconv.ParseInt(msg.Bytes, 10, 64); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot parse bytes (%s)", err)
}

duration, err := time.ParseDuration(msg.Duration)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot parse bytes (%s)", err)
}

if duration < 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "duration cannot be negative (%s)", msg.Duration)
if msg.DurationDays <= 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "duration cannot be less than 1 (%d)", msg.DurationDays)
}

return nil
Expand Down
32 changes: 11 additions & 21 deletions x/storage/types/message_buy_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func TestMsgBuyStorage_ValidateBasic(t *testing.T) {
msg: MsgBuyStorage{
Creator: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
ForAddress: "invalid_address",
Duration: "10000h",
Bytes: "4096",
DurationDays: 30,
Bytes: 4096,
PaymentDenom: "ujkl",
},
err: sdkerrors.ErrInvalidAddress,
Expand All @@ -28,8 +28,8 @@ func TestMsgBuyStorage_ValidateBasic(t *testing.T) {
msg: MsgBuyStorage{
Creator: "invalid_address",
ForAddress: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
Duration: "10000h",
Bytes: "4096",
DurationDays: 30,
Bytes: 4096,
PaymentDenom: "ujkl",
},
err: sdkerrors.ErrInvalidAddress,
Expand All @@ -38,38 +38,28 @@ func TestMsgBuyStorage_ValidateBasic(t *testing.T) {
msg: MsgBuyStorage{
Creator: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
ForAddress: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
Duration: "x",
Bytes: "4096",
DurationDays: 0,
Bytes: 4096,
PaymentDenom: "ujkl",
},
err: sdkerrors.ErrInvalidType,
err: sdkerrors.ErrInvalidRequest,
}, {
name: "invalid duration(negative)",
msg: MsgBuyStorage{
Creator: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
ForAddress: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
Duration: "-1h",
Bytes: "4096",
DurationDays: -10,
Bytes: 4096,
PaymentDenom: "ujkl",
},
err: sdkerrors.ErrInvalidRequest,
}, {
name: "invalid btyes",
msg: MsgBuyStorage{
Creator: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
ForAddress: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
Duration: "10000h",
Bytes: "c",
PaymentDenom: "ujkl",
},
err: sdkerrors.ErrInvalidType,
}, {
name: "valid address",
msg: MsgBuyStorage{
Creator: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
ForAddress: "jkl1j3p63s42w7ywaczlju626st55mzu5z399f5n6n",
Duration: "10000h",
Bytes: "4096",
DurationDays: 30,
Bytes: 4096,
PaymentDenom: "ujkl",
},
},
Expand Down
Loading

0 comments on commit 7b01315

Please sign in to comment.