diff --git a/app/upgrades/v4/upgrades.go b/app/upgrades/v4/upgrades.go index e4a7060f3..3a5ac325f 100644 --- a/app/upgrades/v4/upgrades.go +++ b/app/upgrades/v4/upgrades.go @@ -93,6 +93,26 @@ func UpdateFileTree(ctx sdk.Context, fk filetreemodulekeeper.Keeper, merkleMap m } } +func UpdatePaymentInfo(ctx sdk.Context, sk storagekeeper.Keeper) { + paymentInfo := sk.GetAllStoragePaymentInfo(ctx) + for _, info := range paymentInfo { + + planTime := info.End.Sub(info.Start) + millis := planTime.Milliseconds() + seconds := millis / 1000 + minutes := seconds / 60 + hours := minutes / 60 + + cost := sk.GetStorageCostKbs(ctx, info.SpaceAvailable, hours) + + price := sdk.NewCoin("ujkl", cost) + + info.Coins = sdk.NewCoins(price) + + sk.SetStoragePaymentInfo(ctx, info) + } +} + func UpdateFiles(ctx sdk.Context, u *Upgrade) map[string][]byte { fidMerkle := make(map[string][]byte) @@ -174,6 +194,8 @@ func (u *Upgrade) Handler() upgradetypes.UpgradeHandler { UpdateFileTree(ctx, u.fk, fidMerkleMap) + UpdatePaymentInfo(ctx, u.sk) // updating payment info with values at time of upgrade + newVM, err := u.mm.RunMigrations(ctx, u.configurator, fromVM) if err != nil { return newVM, err diff --git a/proto/canine_chain/storage/payment_info.proto b/proto/canine_chain/storage/payment_info.proto index c235a335c..7d754cee4 100644 --- a/proto/canine_chain/storage/payment_info.proto +++ b/proto/canine_chain/storage/payment_info.proto @@ -1,15 +1,23 @@ syntax = "proto3"; package canine_chain.storage; +import "cosmos/base/v1beta1/coin.proto"; import "gogoproto/gogo.proto"; import "google/protobuf/timestamp.proto"; +import "cosmos_proto/cosmos.proto"; option go_package = "github.com/jackalLabs/canine-chain/x/storage/types"; message StoragePaymentInfo { + option (gogoproto.equal) = false; + option (gogoproto.goproto_getters) = false; + google.protobuf.Timestamp start = 1 [ (gogoproto.stdtime) = true, (gogoproto.nullable) = false ]; google.protobuf.Timestamp end = 2 [ (gogoproto.stdtime) = true, (gogoproto.nullable) = false]; int64 spaceAvailable = 3; int64 spaceUsed = 4; string address = 5; + + repeated cosmos.base.v1beta1.Coin coins = 6 + [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"]; } diff --git a/x/storage/keeper/msg_server_buy_storage.go b/x/storage/keeper/msg_server_buy_storage.go index 341dd2d71..2be239c45 100644 --- a/x/storage/keeper/msg_server_buy_storage.go +++ b/x/storage/keeper/msg_server_buy_storage.go @@ -50,11 +50,13 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) ( hours := sdk.NewDec(duration.Milliseconds()).Quo(sdk.NewDec(60 * 60 * 1000)) priceTokens := sdk.NewCoin(denom, k.GetStorageCost(ctx, gbs, hours.TruncateInt().Int64())) + priceTokenList := sdk.NewCoins(priceTokens) + add, err := sdk.AccAddressFromBech32(msg.Creator) if err != nil { return nil, err } - err = k.bankkeeper.SendCoinsFromAccountToModule(ctx, add, types.ModuleName, sdk.NewCoins(priceTokens)) + err = k.bankkeeper.SendCoinsFromAccountToModule(ctx, add, types.ModuleName, priceTokenList) if err != nil { return nil, err } @@ -77,12 +79,15 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) ( return nil, fmt.Errorf("please use MsgUpgradeStorage if you want to upgrade/downgrade") } + c := payInfo.Coins.Add(priceTokens) + spi = types.StoragePaymentInfo{ Start: ctx.BlockTime(), End: ctx.BlockTime().Add(duration), SpaceAvailable: bytes, SpaceUsed: payInfo.SpaceUsed, Address: msg.ForAddress, + Coins: c, } } else { spi = types.StoragePaymentInfo{ @@ -91,6 +96,7 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) ( SpaceAvailable: bytes, SpaceUsed: 0, Address: msg.ForAddress, + Coins: priceTokenList, } } diff --git a/x/storage/types/payment_info.pb.go b/x/storage/types/payment_info.pb.go index 4399c37f0..357a5f6ce 100644 --- a/x/storage/types/payment_info.pb.go +++ b/x/storage/types/payment_info.pb.go @@ -5,6 +5,9 @@ package types import ( fmt "fmt" + _ "github.com/cosmos/cosmos-proto" + github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" + types1 "github.com/cosmos/cosmos-sdk/types" _ "github.com/cosmos/gogoproto/gogoproto" proto "github.com/gogo/protobuf/proto" _ "github.com/gogo/protobuf/types" @@ -28,11 +31,12 @@ var _ = time.Kitchen const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type StoragePaymentInfo struct { - Start time.Time `protobuf:"bytes,1,opt,name=start,proto3,stdtime" json:"start"` - End time.Time `protobuf:"bytes,2,opt,name=end,proto3,stdtime" json:"end"` - SpaceAvailable int64 `protobuf:"varint,3,opt,name=spaceAvailable,proto3" json:"spaceAvailable,omitempty"` - SpaceUsed int64 `protobuf:"varint,4,opt,name=spaceUsed,proto3" json:"spaceUsed,omitempty"` - Address string `protobuf:"bytes,5,opt,name=address,proto3" json:"address,omitempty"` + Start time.Time `protobuf:"bytes,1,opt,name=start,proto3,stdtime" json:"start"` + End time.Time `protobuf:"bytes,2,opt,name=end,proto3,stdtime" json:"end"` + SpaceAvailable int64 `protobuf:"varint,3,opt,name=spaceAvailable,proto3" json:"spaceAvailable,omitempty"` + SpaceUsed int64 `protobuf:"varint,4,opt,name=spaceUsed,proto3" json:"spaceUsed,omitempty"` + Address string `protobuf:"bytes,5,opt,name=address,proto3" json:"address,omitempty"` + Coins github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,6,rep,name=coins,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coins" json:"coins"` } func (m *StoragePaymentInfo) Reset() { *m = StoragePaymentInfo{} } @@ -68,41 +72,6 @@ func (m *StoragePaymentInfo) XXX_DiscardUnknown() { var xxx_messageInfo_StoragePaymentInfo proto.InternalMessageInfo -func (m *StoragePaymentInfo) GetStart() time.Time { - if m != nil { - return m.Start - } - return time.Time{} -} - -func (m *StoragePaymentInfo) GetEnd() time.Time { - if m != nil { - return m.End - } - return time.Time{} -} - -func (m *StoragePaymentInfo) GetSpaceAvailable() int64 { - if m != nil { - return m.SpaceAvailable - } - return 0 -} - -func (m *StoragePaymentInfo) GetSpaceUsed() int64 { - if m != nil { - return m.SpaceUsed - } - return 0 -} - -func (m *StoragePaymentInfo) GetAddress() string { - if m != nil { - return m.Address - } - return "" -} - func init() { proto.RegisterType((*StoragePaymentInfo)(nil), "canine_chain.storage.StoragePaymentInfo") } @@ -112,27 +81,33 @@ func init() { } var fileDescriptor_b1e2face38d88409 = []byte{ - // 308 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x91, 0x31, 0x4f, 0x02, 0x31, - 0x18, 0x86, 0xaf, 0x22, 0x2a, 0x35, 0x71, 0x68, 0x18, 0x2e, 0xc4, 0x14, 0xe2, 0xa0, 0x2c, 0xb6, - 0x09, 0x26, 0x0e, 0x6e, 0xb2, 0x99, 0x30, 0x18, 0xd4, 0xc5, 0x85, 0x7c, 0x77, 0x57, 0x4a, 0xf5, - 0xae, 0xbd, 0x5c, 0x8b, 0x91, 0x7f, 0xc1, 0xcf, 0x62, 0x64, 0x74, 0x52, 0x03, 0x8b, 0x3f, 0xc3, - 0x70, 0xf5, 0xd4, 0xb8, 0xb9, 0xf5, 0x7b, 0xf3, 0xbc, 0x4f, 0xd3, 0x7e, 0xf8, 0x24, 0x06, 0xad, - 0xb4, 0x18, 0xc5, 0x13, 0x50, 0x9a, 0x5b, 0x67, 0x0a, 0x90, 0x82, 0xe7, 0x30, 0xcb, 0x84, 0x76, - 0x23, 0xa5, 0xc7, 0x86, 0xe5, 0x85, 0x71, 0x86, 0x34, 0x7f, 0x83, 0xec, 0x0b, 0x6c, 0x35, 0xa5, - 0x91, 0xa6, 0x04, 0xf8, 0xe6, 0xe4, 0xd9, 0x56, 0x5b, 0x1a, 0x23, 0x53, 0xc1, 0xcb, 0x29, 0x9a, - 0x8e, 0xb9, 0x53, 0x99, 0xb0, 0x0e, 0xb2, 0xdc, 0x03, 0x47, 0x1f, 0x08, 0x93, 0x1b, 0xaf, 0xb8, - 0xf6, 0x57, 0x5d, 0xe9, 0xb1, 0x21, 0x17, 0xb8, 0x6e, 0x1d, 0x14, 0x2e, 0x44, 0x1d, 0xd4, 0xdd, - 0xef, 0xb5, 0x98, 0xf7, 0xb0, 0xca, 0xc3, 0x6e, 0x2b, 0x4f, 0x7f, 0x6f, 0xf1, 0xda, 0x0e, 0xe6, - 0x6f, 0x6d, 0x34, 0xf4, 0x15, 0x72, 0x8e, 0x6b, 0x42, 0x27, 0xe1, 0xd6, 0x3f, 0x9a, 0x9b, 0x02, - 0x39, 0xc6, 0x07, 0x36, 0x87, 0x58, 0x5c, 0x3e, 0x81, 0x4a, 0x21, 0x4a, 0x45, 0x58, 0xeb, 0xa0, - 0x6e, 0x6d, 0xf8, 0x27, 0x25, 0x87, 0xb8, 0x51, 0x26, 0x77, 0x56, 0x24, 0xe1, 0x76, 0x89, 0xfc, - 0x04, 0x24, 0xc4, 0xbb, 0x90, 0x24, 0x85, 0xb0, 0x36, 0xac, 0x77, 0x50, 0xb7, 0x31, 0xac, 0xc6, - 0xfe, 0x60, 0xb1, 0xa2, 0x68, 0xb9, 0xa2, 0xe8, 0x7d, 0x45, 0xd1, 0x7c, 0x4d, 0x83, 0xe5, 0x9a, - 0x06, 0x2f, 0x6b, 0x1a, 0xdc, 0xf7, 0xa4, 0x72, 0x93, 0x69, 0xc4, 0x62, 0x93, 0xf1, 0x07, 0x88, - 0x1f, 0x21, 0x1d, 0x40, 0x64, 0xb9, 0xff, 0xe7, 0x53, 0xbf, 0x90, 0xe7, 0xef, 0x95, 0xb8, 0x59, - 0x2e, 0x6c, 0xb4, 0x53, 0x3e, 0xe8, 0xec, 0x33, 0x00, 0x00, 0xff, 0xff, 0xbc, 0x98, 0xbb, 0xc9, - 0xb7, 0x01, 0x00, 0x00, + // 404 bytes of a gzipped FileDescriptorProto + 0x1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a1, 0x9f, + 0x20, 0x9d, 0x41, 0x31, 0x82, 0x44, 0x33, 0x7b, 0x0f, 0xb7, 0xf6, 0x70, 0x3e, 0xff, 0x39, 0x9d, + 0xd6, 0x24, 0x39, 0x6f, 0xd3, 0x7b, 0xf1, 0x3b, 0x00, 0x00, 0xff, 0xff, 0xe6, 0xba, 0x8a, 0x53, + 0x5f, 0x02, 0x00, 0x00, } func (m *StoragePaymentInfo) Marshal() (dAtA []byte, err error) { @@ -155,6 +130,20 @@ func (m *StoragePaymentInfo) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Coins) > 0 { + for iNdEx := len(m.Coins) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Coins[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintPaymentInfo(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x32 + } + } if len(m.Address) > 0 { i -= len(m.Address) copy(dAtA[i:], m.Address) @@ -222,6 +211,12 @@ func (m *StoragePaymentInfo) Size() (n int) { if l > 0 { n += 1 + l + sovPaymentInfo(uint64(l)) } + if len(m.Coins) > 0 { + for _, e := range m.Coins { + l = e.Size() + n += 1 + l + sovPaymentInfo(uint64(l)) + } + } return n } @@ -396,6 +391,40 @@ func (m *StoragePaymentInfo) Unmarshal(dAtA []byte) error { } m.Address = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 6: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Coins", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPaymentInfo + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthPaymentInfo + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthPaymentInfo + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Coins = append(m.Coins, types1.Coin{}) + if err := m.Coins[len(m.Coins)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipPaymentInfo(dAtA[iNdEx:])