From b16adbbb0fba6f851385f21e59cebba49085d7c7 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Thu, 26 Oct 2023 08:00:12 +0200 Subject: [PATCH 1/4] feat: wasm v0.43 --- app/app.go | 33 --------------------------------- go.mod | 6 +++--- go.sum | 12 ++++++------ 3 files changed, 9 insertions(+), 42 deletions(-) diff --git a/app/app.go b/app/app.go index 0800cb2c..6d2d635e 100644 --- a/app/app.go +++ b/app/app.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "reflect" // #nosec G702 - "strings" authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation" @@ -174,33 +173,6 @@ import ( _ "github.com/terra-money/core/v2/client/docs/statik" ) -var ( - // If EnabledSpecificProposals is "", and this is "true", then enable all x/wasm proposals. - // If EnabledSpecificProposals is "", and this is not "true", then disable all x/wasm proposals. - ProposalsEnabled = "true" - // If set to non-empty string it must be comma-separated list of values that are all a subset - // of "EnableAllProposals" (takes precedence over ProposalsEnabled) - // https://github.com/CosmWasm/wasmd/blob/02a54d33ff2c064f3539ae12d75d027d9c665f05/x/wasm/internal/types/proposal.go#L28-L34 - EnableSpecificProposals = "" -) - -// GetEnabledProposals parses the ProposalsEnabled / EnableSpecificProposals values to -// produce a list of enabled proposals to pass into wasmd app. -func GetEnabledProposals() []wasmtypes.ProposalType { - if EnableSpecificProposals == "" { - if ProposalsEnabled == "true" { - return wasmtypes.EnableAllProposals - } - return wasmtypes.DisableAllProposals - } - chunks := strings.Split(EnableSpecificProposals, ",") - proposals, err := wasmtypes.ConvertToProposals(chunks) - if err != nil { - panic(err) - } - return proposals -} - // GetWasmOpts build wasm options func GetWasmOpts(app *TerraApp, appOpts servertypes.AppOptions) []wasmkeeper.Option { var wasmOpts []wasmkeeper.Option @@ -692,11 +664,6 @@ func NewTerraApp( app.TokenFactoryKeeper.Hooks(), ), ) - // register wasm gov proposal types - enabledProposals := GetEnabledProposals() - if len(enabledProposals) != 0 { - govRouter.AddRoute(wasmtypes.RouterKey, wasmkeeper.NewWasmProposalHandler(app.WasmKeeper, enabledProposals)) - } // Create fee enabled wasm ibc Stack var wasmStack porttypes.IBCModule diff --git a/go.mod b/go.mod index 151172e9..e0e19606 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( cosmossdk.io/errors v1.0.0 cosmossdk.io/math v1.1.2 cosmossdk.io/tools/rosetta v0.2.1 - github.com/CosmWasm/wasmd v0.41.0 - github.com/CosmWasm/wasmvm v1.3.0 + github.com/CosmWasm/wasmd v0.43.0 + github.com/CosmWasm/wasmvm v1.4.1 github.com/cometbft/cometbft v0.37.2 github.com/cometbft/cometbft-db v0.8.0 github.com/cosmos/cosmos-proto v1.0.0-beta.3 @@ -66,7 +66,7 @@ require ( github.com/confio/ics23/go v0.9.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect github.com/cosmos/gogogateway v1.2.0 // indirect - github.com/cosmos/iavl v0.20.0 // indirect + github.com/cosmos/iavl v0.20.1 // indirect github.com/cosmos/ics23/go v0.10.0 // indirect github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect github.com/cosmos/ledger-go v0.9.3 // indirect diff --git a/go.sum b/go.sum index e7da33c5..96fae55c 100644 --- a/go.sum +++ b/go.sum @@ -223,10 +223,10 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d h1:nalkkPQcITbvhmL4+C4cKA87NW0tfm3Kl9VXRoPywFg= github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4= -github.com/CosmWasm/wasmd v0.41.0 h1:fmwxSbwb50zZDcBaayYFRLIaSFca+EFld1WOaQi49jg= -github.com/CosmWasm/wasmd v0.41.0/go.mod h1:0Sds1q2IsPaTN1gHa3BNOYcUFgtGvxH7CXEXPgoihns= -github.com/CosmWasm/wasmvm v1.3.0 h1:x12X4bKlUPS7TT9QQP45+fJo2sp30GEbiSSgb9jsec8= -github.com/CosmWasm/wasmvm v1.3.0/go.mod h1:vW/E3h8j9xBQs9bCoijDuawKo9kCtxOaS8N8J7KFtkc= +github.com/CosmWasm/wasmd v0.43.0 h1:4xQAiNf6Ej+TuYe4PXwrtVJ9C+xyFrbsdqHH/kU9Vxk= +github.com/CosmWasm/wasmd v0.43.0/go.mod h1:gpri8YvkRErBz+qDme5jOThGZmSlHfyN532bWibXOl4= +github.com/CosmWasm/wasmvm v1.4.1 h1:YgodVlBrXa2HJZzOXjWDH0EIRwQzK3zuA73dDPRRLS4= +github.com/CosmWasm/wasmvm v1.4.1/go.mod h1:fXB+m2gyh4v9839zlIXdMZGeLAxqUdYdFQqYsTha2hc= github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/DataDog/zstd v1.5.0/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= @@ -398,8 +398,8 @@ github.com/cosmos/gogogateway v1.2.0/go.mod h1:iQpLkGWxYcnCdz5iAdLcRBSw3h7NXeOkZ github.com/cosmos/gogoproto v1.4.2/go.mod h1:cLxOsn1ljAHSV527CHOtaIP91kK6cCrZETRBrkzItWU= github.com/cosmos/gogoproto v1.4.10 h1:QH/yT8X+c0F4ZDacDv3z+xE3WU1P1Z3wQoLMBRJoKuI= github.com/cosmos/gogoproto v1.4.10/go.mod h1:3aAZzeRWpAwr+SS/LLkICX2/kDFyaYVzckBDzygIxek= -github.com/cosmos/iavl v0.20.0 h1:fTVznVlepH0KK8NyKq8w+U7c2L6jofa27aFX6YGlm38= -github.com/cosmos/iavl v0.20.0/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A= +github.com/cosmos/iavl v0.20.1 h1:rM1kqeG3/HBT85vsZdoSNsehciqUQPWrR4BYmqE2+zg= +github.com/cosmos/iavl v0.20.1/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A= github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.0.1-0.20231012160012-d0f49580a238 h1:vc9zQUjiYctU3q4uF5usbl2JUqaa3F6bEyboNKOKyBk= github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7 v7.0.1-0.20231012160012-d0f49580a238/go.mod h1:WO/xWf+I8m+9PZsBkwZkEREZIW24j90Sz2yOsXU3B9U= github.com/cosmos/ibc-apps/modules/ibc-hooks/v7 v7.0.0-20230803181732-7c8f814d3b79 h1:pCxyhIxgWTabAQC5UerkITraHG3SwajdLKKMCFDWCv4= From 3a99dd3cfc5ab5c65d84043f7b6f2a517d29e837 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Fri, 27 Oct 2023 17:21:37 +0200 Subject: [PATCH 2/4] feat: custom bank hooks --- custom/bank/keeper/keeper.go | 94 ++++++++++++++++++++++++-- custom/bank/types/errors.go | 12 ++++ custom/bank/types/output_coin_slice.go | 19 ++++++ 3 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 custom/bank/types/errors.go create mode 100644 custom/bank/types/output_coin_slice.go diff --git a/custom/bank/keeper/keeper.go b/custom/bank/keeper/keeper.go index bd28a1c1..69743fd7 100644 --- a/custom/bank/keeper/keeper.go +++ b/custom/bank/keeper/keeper.go @@ -1,13 +1,16 @@ package keeper import ( + "sort" + "github.com/cosmos/cosmos-sdk/codec" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + errorsmod "cosmossdk.io/errors" custombankkeeper "github.com/terra-money/alliance/custom/bank/keeper" customterratypes "github.com/terra-money/core/v2/custom/bank/types" ) @@ -50,7 +53,6 @@ func (k *Keeper) SetHooks(bh customterratypes.BankHooks) *Keeper { // SendCoins transfers amt coins from a sending account to a receiving account. // An error is returned upon failure. func (k Keeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error { - // BlockBeforeSend hook should always be called before the TrackBeforeSend hook. err := k.BlockBeforeSend(ctx, fromAddr, toAddr, amt) if err != nil { return err @@ -66,13 +68,95 @@ func (k Keeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.A func (k Keeper) SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error { senderAddr := k.ak.GetModuleAddress(senderModule) if senderAddr == nil { - panic(sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", senderModule)) + panic(errorsmod.Wrapf(customterratypes.ErrUnknownAddress, "senderModule address %s is nil", senderModule)) } - recipientAcc := k.ak.GetModuleAccount(ctx, recipientModule) if recipientAcc == nil { - panic(sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", recipientModule)) + panic(errorsmod.Wrapf(customterratypes.ErrUnknownAddress, "recipientModule address %s is nil", recipientModule)) } return k.Keeper.SendCoins(ctx, senderAddr, recipientAcc.GetAddress(), amt) } + +// UndelegateCoins performs undelegation by crediting amt coins to an account with +// address addr. For vesting accounts, undelegation amounts are tracked for both +// vesting and vested coins. The coins are then transferred from a ModuleAccount +// address to the delegator address. If any of the undelegation amounts are +// negative, an error is returned. +func (k Keeper) UndelegateCoins(ctx sdk.Context, moduleAccAddr, delegatorAddr sdk.AccAddress, amt sdk.Coins) error { + err := k.BlockBeforeSend(ctx, moduleAccAddr, delegatorAddr, amt) + if err != nil { + return err + } + k.TrackBeforeSend(ctx, moduleAccAddr, delegatorAddr, amt) + + return k.Keeper.UndelegateCoins(ctx, moduleAccAddr, delegatorAddr, amt) +} + +// DelegateCoins performs delegation by deducting amt coins from an account with +// address addr. For vesting accounts, delegations amounts are tracked for both +// vesting and vested coins. The coins are then transferred from the delegator +// address to a ModuleAccount address. If any of the delegation amounts are negative, +// an error is returned. +func (k Keeper) DelegateCoins(ctx sdk.Context, delegatorAddr, moduleAccAddr sdk.AccAddress, amt sdk.Coins) error { + err := k.BlockBeforeSend(ctx, delegatorAddr, moduleAccAddr, amt) + if err != nil { + return err + } + k.TrackBeforeSend(ctx, delegatorAddr, moduleAccAddr, amt) + + return k.Keeper.DelegateCoins(ctx, delegatorAddr, moduleAccAddr, amt) +} + +// InputOutputCoins performs multi-send functionality. It accepts a series of +// inputs that correspond to a series of outputs. It returns an error if the +// inputs and outputs don't line up or if any single transfer of tokens fails. +func (k Keeper) InputOutputCoins(ctx sdk.Context, inputs []banktypes.Input, outputs []banktypes.Output) error { + // ValidateInputsOutputs is executed in the k.Keeper.InputOutputCoins + // but we execute it here too just to avoid unnecessary computation + // if the inputs and outputs are invalid, that way we can assume things like + // the addreeses being valid and the inputs and outputs match balances + if err := banktypes.ValidateInputsOutputs(inputs, outputs); err != nil { + return err + } + + // Create a slice to hold OutputCoin structs + // to be sorted so the output is deterministic + var outputCoinSlice customterratypes.OutputCoinSlice + + // Populate the slice with outputs and corresponding + // coins to decrease the algorithm complexity. + for _, output := range outputs { + for _, coin := range output.Coins { + outputCoinSlice = append(outputCoinSlice, customterratypes.OutputCoin{Output: output, Coin: coin}) + } + } + + // Sort the slice based on denomination + sort.Sort(outputCoinSlice) + + // Iterate through all inputs + for _, input := range inputs { + // Iterate through all coins in the current input + for _, inputCoin := range input.Coins { + // Use binary search to find matching outputs + index := sort.Search(len(outputCoinSlice), func(i int) bool { return outputCoinSlice[i].Coin.Denom >= inputCoin.Denom }) + + for index < len(outputCoinSlice) && outputCoinSlice[index].Coin.Denom == inputCoin.Denom { + + fromAddr := sdk.MustAccAddressFromBech32(input.Address) + toAddr := sdk.MustAccAddressFromBech32(outputCoinSlice[index].Output.Address) + coins := sdk.NewCoins(inputCoin) + + err := k.BlockBeforeSend(ctx, fromAddr, toAddr, coins) + if err != nil { + return err + } + k.TrackBeforeSend(ctx, fromAddr, toAddr, coins) + index++ + } + } + } + + return k.Keeper.InputOutputCoins(ctx, inputs, outputs) +} diff --git a/custom/bank/types/errors.go b/custom/bank/types/errors.go new file mode 100644 index 00000000..30fcc409 --- /dev/null +++ b/custom/bank/types/errors.go @@ -0,0 +1,12 @@ +package types + +// DONTCOVER + +import ( + sdkerrors "cosmossdk.io/errors" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" +) + +var ( + ErrUnknownAddress = sdkerrors.Register(banktypes.ModuleName, 383838, "module account does not exist") +) diff --git a/custom/bank/types/output_coin_slice.go b/custom/bank/types/output_coin_slice.go new file mode 100644 index 00000000..eae0577c --- /dev/null +++ b/custom/bank/types/output_coin_slice.go @@ -0,0 +1,19 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" +) + +// Define a custom type to hold Output and Coin together for sorting +type OutputCoin struct { + Output banktypes.Output + Coin sdk.Coin +} + +// Define a slice of OutputCoin and implement sort.Interface +type OutputCoinSlice []OutputCoin + +func (o OutputCoinSlice) Len() int { return len(o) } +func (o OutputCoinSlice) Less(i, j int) bool { return o[i].Coin.Denom < o[j].Coin.Denom } +func (o OutputCoinSlice) Swap(i, j int) { o[i], o[j] = o[j], o[i] } From fb899b07f7f555f309ec68e481f605b32e9a4399 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Wed, 1 Nov 2023 08:53:27 +0200 Subject: [PATCH 3/4] fix: input output coins --- custom/bank/keeper/keeper.go | 57 ++++++++------------------ custom/bank/types/output_coin_slice.go | 19 --------- 2 files changed, 16 insertions(+), 60 deletions(-) delete mode 100644 custom/bank/types/output_coin_slice.go diff --git a/custom/bank/keeper/keeper.go b/custom/bank/keeper/keeper.go index 69743fd7..b55953a6 100644 --- a/custom/bank/keeper/keeper.go +++ b/custom/bank/keeper/keeper.go @@ -1,8 +1,6 @@ package keeper import ( - "sort" - "github.com/cosmos/cosmos-sdk/codec" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -112,50 +110,27 @@ func (k Keeper) DelegateCoins(ctx sdk.Context, delegatorAddr, moduleAccAddr sdk. // inputs that correspond to a series of outputs. It returns an error if the // inputs and outputs don't line up or if any single transfer of tokens fails. func (k Keeper) InputOutputCoins(ctx sdk.Context, inputs []banktypes.Input, outputs []banktypes.Output) error { - // ValidateInputsOutputs is executed in the k.Keeper.InputOutputCoins - // but we execute it here too just to avoid unnecessary computation - // if the inputs and outputs are invalid, that way we can assume things like - // the addreeses being valid and the inputs and outputs match balances - if err := banktypes.ValidateInputsOutputs(inputs, outputs); err != nil { - return err + // Only 1 input is allowed for all outputs check the following url: + // https://github.com/terra-money/cosmos-sdk/blob/release/v0.47.x/x/bank/types/msgs.go#L87-L89 + // + // This if statement is added here too so we know + // when multiple inputs are allowed in the future + // because ErrMultipleSenders will fail to import + // because will be removed from the code. + if len(inputs) != 1 { + return banktypes.ErrMultipleSenders } + input := inputs[0] + inputaddress := sdk.MustAccAddressFromBech32(input.Address) - // Create a slice to hold OutputCoin structs - // to be sorted so the output is deterministic - var outputCoinSlice customterratypes.OutputCoinSlice - - // Populate the slice with outputs and corresponding - // coins to decrease the algorithm complexity. for _, output := range outputs { - for _, coin := range output.Coins { - outputCoinSlice = append(outputCoinSlice, customterratypes.OutputCoin{Output: output, Coin: coin}) - } - } + outputaddress := sdk.MustAccAddressFromBech32(output.Address) - // Sort the slice based on denomination - sort.Sort(outputCoinSlice) - - // Iterate through all inputs - for _, input := range inputs { - // Iterate through all coins in the current input - for _, inputCoin := range input.Coins { - // Use binary search to find matching outputs - index := sort.Search(len(outputCoinSlice), func(i int) bool { return outputCoinSlice[i].Coin.Denom >= inputCoin.Denom }) - - for index < len(outputCoinSlice) && outputCoinSlice[index].Coin.Denom == inputCoin.Denom { - - fromAddr := sdk.MustAccAddressFromBech32(input.Address) - toAddr := sdk.MustAccAddressFromBech32(outputCoinSlice[index].Output.Address) - coins := sdk.NewCoins(inputCoin) - - err := k.BlockBeforeSend(ctx, fromAddr, toAddr, coins) - if err != nil { - return err - } - k.TrackBeforeSend(ctx, fromAddr, toAddr, coins) - index++ - } + err := k.BlockBeforeSend(ctx, inputaddress, outputaddress, output.Coins) + if err != nil { + return err } + k.TrackBeforeSend(ctx, inputaddress, outputaddress, output.Coins) } return k.Keeper.InputOutputCoins(ctx, inputs, outputs) diff --git a/custom/bank/types/output_coin_slice.go b/custom/bank/types/output_coin_slice.go deleted file mode 100644 index eae0577c..00000000 --- a/custom/bank/types/output_coin_slice.go +++ /dev/null @@ -1,19 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" -) - -// Define a custom type to hold Output and Coin together for sorting -type OutputCoin struct { - Output banktypes.Output - Coin sdk.Coin -} - -// Define a slice of OutputCoin and implement sort.Interface -type OutputCoinSlice []OutputCoin - -func (o OutputCoinSlice) Len() int { return len(o) } -func (o OutputCoinSlice) Less(i, j int) bool { return o[i].Coin.Denom < o[j].Coin.Denom } -func (o OutputCoinSlice) Swap(i, j int) { o[i], o[j] = o[j], o[i] } From 8fb97831aaf0ce53354e855fc48f7fa0f50e3bfc Mon Sep 17 00:00:00 2001 From: emidev98 Date: Wed, 1 Nov 2023 09:15:21 +0200 Subject: [PATCH 4/4] fix: lint --- custom/bank/keeper/keeper.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/custom/bank/keeper/keeper.go b/custom/bank/keeper/keeper.go index b1aaf712..b55953a6 100644 --- a/custom/bank/keeper/keeper.go +++ b/custom/bank/keeper/keeper.go @@ -11,8 +11,6 @@ import ( errorsmod "cosmossdk.io/errors" custombankkeeper "github.com/terra-money/alliance/custom/bank/keeper" customterratypes "github.com/terra-money/core/v2/custom/bank/types" - - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) type Keeper struct {