Skip to content

Commit

Permalink
storage module version bump (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell committed Jun 29, 2024
2 parents 2c7c3b8 + d443c08 commit 529598d
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 78 deletions.
30 changes: 24 additions & 6 deletions app/upgrades/v4/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"strconv"

jklminttypes "github.com/jackalLabs/canine-chain/v4/x/jklmint/types"

notificationsmoduletypes "github.com/jackalLabs/canine-chain/v4/x/notifications/types"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -54,10 +56,12 @@ type FidContents struct {
}

type MerkleContents struct {
Merkles [][]byte `json:"merkles"`
Merkles [][]byte `json:"legacyMerkles"`
}

func UpdateFileTree(ctx sdk.Context, fk *filetreemodulekeeper.Keeper, merkleMap map[string][]byte) {
ctx.Logger().Info("Migrating filetree formatting...")

allFiles := fk.GetAllFiles(ctx)

for _, file := range allFiles {
Expand All @@ -83,6 +87,16 @@ func UpdateFileTree(ctx sdk.Context, fk *filetreemodulekeeper.Keeper, merkleMap

}

/**
{
"fids": ["fid1", "fid2", "fid3"]
}
{
"legacyMerkles": ["merkle1", "merkle2", "merkle3"]
}
*/

merkleContents := MerkleContents{Merkles: merkles}

merkleContentBytes, err := json.Marshal(merkleContents)
Expand All @@ -97,6 +111,8 @@ func UpdateFileTree(ctx sdk.Context, fk *filetreemodulekeeper.Keeper, merkleMap
}

func UpdatePaymentInfo(ctx sdk.Context, sk *storagekeeper.Keeper) {
ctx.Logger().Info("Updating all user payment information...")

paymentInfo := sk.GetAllStoragePaymentInfo(ctx)
for _, info := range paymentInfo {

Expand All @@ -117,6 +133,7 @@ func UpdatePaymentInfo(ctx sdk.Context, sk *storagekeeper.Keeper) {
}

func UpdateFiles(ctx sdk.Context, sk *storagekeeper.Keeper) map[string][]byte {
ctx.Logger().Info("Updating all files to Universal Files...")
fidMerkle := make(map[string][]byte)

allDeals := sk.GetAllLegacyActiveDeals(ctx)
Expand Down Expand Up @@ -193,18 +210,19 @@ func (u *Upgrade) Handler() upgradetypes.UpgradeHandler {
ctx.Logger().Info("\nNow updating the Jackal Protocol to:\n\n █████╗ ██████╗ █████╗ ██████╗██╗ █████╗ \n██╔══██╗██╔════╝██╔══██╗██╔════╝██║██╔══██╗\n███████║██║ ███████║██║ ██║███████║\n██╔══██║██║ ██╔══██║██║ ██║██╔══██║\n██║ ██║╚██████╗██║ ██║╚██████╗██║██║ ██║\n╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═════╝╚═╝╚═╝ ╚═╝\n \n")

fromVM[storagemoduletypes.ModuleName] = 5
fromVM[jklminttypes.ModuleName] = 4

newVM, err := u.mm.RunMigrations(ctx, u.configurator, fromVM)
if err != nil {
return newVM, err
}

fidMerkleMap := UpdateFiles(ctx, u.sk)

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
}

return newVM, err
}
}
Expand Down
14 changes: 4 additions & 10 deletions x/jklmint/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/v4/x/jklmint/exported"
v210 "github.com/jackalLabs/canine-chain/v4/x/jklmint/legacy/v210"
v3 "github.com/jackalLabs/canine-chain/v4/x/jklmint/legacy/v3"
"github.com/jackalLabs/canine-chain/v4/x/jklmint/legacy/v4"
)

// Migrator is a struct for handling in-place store migrations.
Expand All @@ -23,12 +22,7 @@ func NewMigrator(keeper Keeper, legacy exported.Subspace) Migrator {
}
}

// Migrate1to2 migrates from version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v210.MigrateStore(ctx, &m.k.paramSpace)
}

// Migrate1to2 migrates from version 2 to 3.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v3.MigrateStore(ctx, m.legacySubspace, &m.k.paramSpace)
// Migrate4to5 migrates from version 4 to 5.
func (m Migrator) Migrate4to5(ctx sdk.Context) error {
return v4.MigrateStore(ctx, m.legacySubspace, &m.k.paramSpace)
}
2 changes: 1 addition & 1 deletion x/jklmint/keeper/mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (suite *MintTestSuite) TestNoProviderBlockMint() {
supplyAfter, err := app.BankKeeper.TotalSupply(sdk.WrapSDKContext(ctx), &types.QueryTotalSupplyRequest{})
suite.Require().NoError(err)
suite.Require().Equal(1, len(supplyAfter.Supply))
suite.Require().Equal(sdk.NewInt(419999), supplyAfter.Supply.AmountOf(denom))
suite.Require().Equal(sdk.NewInt(4199999), supplyAfter.Supply.AmountOf(denom))
// After BlockMint we now have exactly 3.6JKL in the fee collector account
}

Expand Down
23 changes: 0 additions & 23 deletions x/jklmint/legacy/v210/store.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v3
package v4

import (
"fmt"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions x/jklmint/legacy/v3/store.go → x/jklmint/legacy/v4/store.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v3
package v4

import (
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -12,7 +12,7 @@ import (
// 1. setting up the next reason id and report id keys for existing subspaces
// 2. setting up the module params
func MigrateStore(ctx sdk.Context, legacy exported.Subspace, paramsSubspace *paramstypes.Subspace) error {
ctx.Logger().Error("MIGRATING MINT STORE!")
ctx.Logger().Info("MIGRATING MINT STORE!")
// Set the module params
var currParams Params
legacy.GetParamSet(ctx, &currParams)
Expand All @@ -22,9 +22,9 @@ func MigrateStore(ctx sdk.Context, legacy exported.Subspace, paramsSubspace *par
DevGrantsRatio: 8,
StorageProviderRatio: 12,
StakerRatio: 80,
TokensPerBlock: currParams.TokensPerBlock, // TODO: Double check this
MintDecrease: 6, // TODO: Double check this
StorageStipendAddress: types.DefaultStorageStipend,
TokensPerBlock: 3442500,
MintDecrease: 6,
StorageStipendAddress: "jkl1scpjepy0vgdpku7dwev20yxmvcezakv24k9arn",
}

paramsSubspace.SetParamSet(ctx, &t)
Expand Down
9 changes: 2 additions & 7 deletions x/jklmint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := keeper.NewMigrator(am.keeper, am.legacySubspace)

err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3)
if err != nil {
panic(err)
}

err = cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4)
err := cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4to5)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -186,7 +181,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 4 }
func (AppModule) ConsensusVersion() uint64 { return 5 }

// BeginBlock executes all ABCI BeginBlock logic respective to the capability module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down
4 changes: 3 additions & 1 deletion x/storage/keeper/grpc_query_active_deals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ func (suite *KeeperTestSuite) TestAllFiles() {
MaxContractAgeInBlocks: 100,
PricePerTbPerMonth: 8,
CollateralPrice: 2,
CheckWindow: 10,
CheckWindow: 11,
ReferralCommission: 25,
PolRatio: 40,
})

merkle := []byte("merkle")
Expand Down
8 changes: 6 additions & 2 deletions x/storage/keeper/grpc_query_find_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ func (suite *KeeperTestSuite) TestFindFile() {
DepositAccount: depoAccount,
ProofWindow: 50,
ChunkSize: 1024,
PriceFeed: "jklprice",
MissesToBurn: 3,
PriceFeed: "jklprice",
MaxContractAgeInBlocks: 100,
PricePerTbPerMonth: 8,
AttestFormSize: 0,
AttestMinToPass: 0,
CollateralPrice: 2,
CheckWindow: 10,
CheckWindow: 11,
PolRatio: 40,
ReferralCommission: 25,
})

merkle := []byte("merkle")
Expand Down
22 changes: 22 additions & 0 deletions x/storage/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
"time"

"github.com/jackalLabs/canine-chain/v4/testutil"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -63,6 +65,26 @@ func setupMsgServer(suite *KeeperTestSuite) (types.MsgServer, keeper.Keeper, goc
k := suite.storageKeeper
storage.InitGenesis(suite.ctx, *k, *types.DefaultGenesis())
ctx := sdk.WrapSDKContext(suite.ctx)

testAddresses, err := testutil.CreateTestAddresses("cosmos", 3)
suite.Require().NoError(err)

depoAccount := testAddresses[0]

k.SetParams(suite.ctx, types.Params{
DepositAccount: depoAccount,
ProofWindow: 50,
ChunkSize: 1024,
PriceFeed: "jklprice",
MissesToBurn: 3,
MaxContractAgeInBlocks: 100,
PricePerTbPerMonth: 15,
CollateralPrice: 2,
CheckWindow: 11,
ReferralCommission: 25,
PolRatio: 40,
})

return keeper.NewMsgServerImpl(*k), *k, ctx
}

Expand Down
12 changes: 9 additions & 3 deletions x/storage/keeper/msg_server_buy_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) (
}

pol := sdk.NewDec(params.PolRatio).QuoInt64(100)
discount := sdk.NewDec(0)
fmt.Printf("POL: %d / %f\n", params.PolRatio, pol.MustFloat64())
if referred {

Expand All @@ -107,9 +108,11 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) (
var hour int64 = 1000 * 60 * 60
if duration.Milliseconds() > 365*24*hour {
p = p.Mul(sdk.MustNewDecFromStr("0.95"))
discount = sdk.NewDec(5).QuoInt64(100) // 5% discount
pol = pol.Sub(sdk.MustNewDecFromStr("0.05"))
} else {
p = p.Mul(sdk.MustNewDecFromStr("0.90"))
discount = sdk.NewDec(10).QuoInt64(100) // 10% discount
pol = pol.Sub(sdk.MustNewDecFromStr("0.1"))
}

Expand Down Expand Up @@ -141,10 +144,13 @@ func (k msgServer) BuyStorage(goCtx context.Context, msg *types.MsgBuyStorage) (
}

refDec := sdk.NewDec(params.ReferralCommission).QuoInt64(100)
fmt.Printf("refcom: %d", params.ReferralCommission)
spr := sdk.NewDec(1).Sub(refDec).Sub(pol) // whatever is left from pol and referrals
fmt.Printf("RATIOS!\nref: %d\npol: %d\ndiscount: %d\n", refDec.MulInt64(100).TruncateInt64(), pol.MulInt64(100).TruncateInt64(), discount.MulInt64(100).TruncateInt64())
spr := sdk.NewDec(1).Sub(refDec).Sub(pol).Sub(discount) // whatever is left from pol and referrals

fmt.Printf("storageprovider ratio: %d\n", spr.TruncateInt().Int64())
// 1 - 0.25 = 0.75
//

fmt.Printf("storageprovider ratio: %d\n", spr.MulInt64(100).TruncateInt().Int64())

storageProviderCut := toPay.Amount.ToDec().Mul(spr)
spcToken := sdk.NewCoin(toPay.Denom, storageProviderCut.TruncateInt())
Expand Down
23 changes: 13 additions & 10 deletions x/storage/keeper/msg_server_buy_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
MaxContractAgeInBlocks: 100,
PricePerTbPerMonth: 15,
CollateralPrice: 2,
CheckWindow: 10,
CheckWindow: 11,
ReferralCommission: 25,
PolRatio: 40,
})
Expand Down Expand Up @@ -134,7 +134,7 @@ func (suite *KeeperTestSuite) TestBuyStorage() {
Referral: depoAccount,
},
expErr: false,
tokens: 25312499,
tokens: 19687499,
expErrMsg: "",
},
{
Expand Down Expand Up @@ -254,7 +254,7 @@ func (suite *KeeperTestSuite) TestBuyStorageValues() {
MaxContractAgeInBlocks: 100,
PricePerTbPerMonth: 15,
CollateralPrice: 2,
CheckWindow: 10,
CheckWindow: 11,
ReferralCommission: 25,
PolRatio: 40,
})
Expand Down Expand Up @@ -315,7 +315,13 @@ func (suite *KeeperTestSuite) TestBuyStorageReferralValues() {
err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, testAcc, coins)
suite.Require().NoError(err)

suite.storageKeeper.SetParams(suite.ctx, types.Params{
providerAccount, err := types.GetTokenHolderAccount()
suite.Require().NoError(err)

bal := suite.bankKeeper.GetBalance(suite.ctx, providerAccount, "ujkl")
suite.Require().Equal(int64(0), bal.Amount.Int64())

k.SetParams(suite.ctx, types.Params{
DepositAccount: depoAccount,
ProofWindow: 50,
ChunkSize: 1024,
Expand All @@ -324,7 +330,7 @@ func (suite *KeeperTestSuite) TestBuyStorageReferralValues() {
MaxContractAgeInBlocks: 100,
PricePerTbPerMonth: 15,
CollateralPrice: 2,
CheckWindow: 10,
CheckWindow: 11,
ReferralCommission: 25,
PolRatio: 40,
})
Expand All @@ -342,12 +348,9 @@ func (suite *KeeperTestSuite) TestBuyStorageReferralValues() {
})
suite.Require().NoError(err)

cost := float64(suite.storageKeeper.GetStorageCost(suite.ctx, bytes/1_000_000_000, days*24).Int64()) // * 0.90
cost := float64(suite.storageKeeper.GetStorageCost(suite.ctx, bytes/1_000_000_000, days*24).Int64()) * 0.9

providerAccount, err := types.GetTokenHolderAccount()
suite.Require().NoError(err)

bal := suite.bankKeeper.GetBalance(suite.ctx, providerAccount, "ujkl")
bal = suite.bankKeeper.GetBalance(suite.ctx, providerAccount, "ujkl")
suite.Require().Equal(int64(cost*0.35), bal.Amount.Int64())

polAccount, err := types.GetPOLAccount()
Expand Down
4 changes: 3 additions & 1 deletion x/storage/keeper/msg_server_post_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func (suite *KeeperTestSuite) TestPostFile() {
MaxContractAgeInBlocks: 100,
PricePerTbPerMonth: 8,
CollateralPrice: 2,
CheckWindow: 10,
CheckWindow: 11,
ReferralCommission: 25,
PolRatio: 40,
})

suite.storageKeeper.SetProviders(suite.ctx, types.Providers{
Expand Down
4 changes: 3 additions & 1 deletion x/storage/keeper/msg_server_proofs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ func (suite *KeeperTestSuite) TestPostProof() {
MaxContractAgeInBlocks: 100,
PricePerTbPerMonth: 8,
CollateralPrice: 2,
CheckWindow: 10,
CheckWindow: 11,
PolRatio: 40,
ReferralCommission: 25,
})

// Init Provider
Expand Down
Loading

0 comments on commit 529598d

Please sign in to comment.