Skip to content

Commit

Permalink
Merge branch 'releases/v1.0.3' into releases/v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Mar 19, 2024
2 parents a34b557 + 208012c commit cd24abb
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 30 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1799](https://github.com/NibiruChain/nibiru/pull/1799) refactor,docs(inflation): Document everything + delete unused code. Make perp and spot optional features in localnet.sh
* [#1808](https://github.com/NibiruChain/nibiru/pull/1808) - Bump `cosmos-sdk` to v0.47.9

## v1.0.2
## [v1.0.3](https://github.com/NibiruChain/nibiru/releases/tag/v1.0.3) - 2024-03-18

### Fix

* [#1816](https://github.com/NibiruChain/nibiru/pull/1816) - fix(ibc): fix ibc transaction from wasm contract

### CLI

* [#1731](https://github.com/NibiruChain/nibiru/pull/1731) - feat(cli): add cli command to decode stargate base64 messages
* [#1754](https://github.com/NibiruChain/nibiru/pull/1754) - refactor(decode-base64): clean code improvements and fn docs

## [v1.0.2](https://github.com/NibiruChain/nibiru/releases/tag/v1.0.2) - 2024-03-03

### Dependencies

* [#1808](https://github.com/NibiruChain/nibiru/pull/1808) - Bump `cosmos-sdk` to v0.47.9
* [65c06ba](https://github.com/NibiruChain/nibiru/commit/65c06ba774c260ece942131ad7a93de0e162266e) - Bump `cosmos-sdk` to v0.47.10

## [v1.0.1](https://github.com/NibiruChain/nibiru/releases/tag/v1.0.1) - 2024-02-09

Expand Down
29 changes: 14 additions & 15 deletions app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,18 @@ func (app *NibiruApp) InitKeepers(
app.BankKeeper,
)

app.ibcTransferKeeper = ibctransferkeeper.NewKeeper(
appCodec,
keys[ibctransfertypes.StoreKey],
/* paramSubspace */ app.GetSubspace(ibctransfertypes.ModuleName),
/* ibctransfertypes.ICS4Wrapper */ app.ibcFeeKeeper,
/* ibctransfertypes.ChannelKeeper */ app.ibcKeeper.ChannelKeeper,
/* ibctransfertypes.PortKeeper */ &app.ibcKeeper.PortKeeper,
app.AccountKeeper,
app.BankKeeper,
app.ScopedTransferKeeper,
)

app.ScopedWasmKeeper = app.capabilityKeeper.ScopeToModule(wasmtypes.ModuleName)

wasmDir := filepath.Join(homePath, "data")
Expand Down Expand Up @@ -453,21 +465,6 @@ func (app *NibiruApp) InitKeepers(
app.slashingKeeper,
)

/* Create IBC module and a static IBC router */
ibcRouter := porttypes.NewRouter()

app.ibcTransferKeeper = ibctransferkeeper.NewKeeper(
appCodec,
keys[ibctransfertypes.StoreKey],
/* paramSubspace */ app.GetSubspace(ibctransfertypes.ModuleName),
/* ibctransfertypes.ICS4Wrapper */ app.ibcFeeKeeper,
/* ibctransfertypes.ChannelKeeper */ app.ibcKeeper.ChannelKeeper,
/* ibctransfertypes.PortKeeper */ &app.ibcKeeper.PortKeeper,
app.AccountKeeper,
app.BankKeeper,
app.ScopedTransferKeeper,
)

// Mock Module setup for testing IBC and also acts as the interchain accounts authentication module
// NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do
// not replicate if you do not need to test core IBC or light clients.
Expand All @@ -484,6 +481,8 @@ func (app *NibiruApp) InitKeepers(
// - IBC Fee Middleware
// - Transfer

ibcRouter := porttypes.NewRouter()

// create IBC module from bottom to top of stack
var transferStack porttypes.IBCModule
transferStack = ibctransfer.NewIBCModule(app.ibcTransferKeeper)
Expand Down
5 changes: 5 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package app
import (
"fmt"

"github.com/NibiruChain/nibiru/app/upgrades/v1_0_3"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/NibiruChain/nibiru/app/upgrades"
"github.com/NibiruChain/nibiru/app/upgrades/v1_0_1"
"github.com/NibiruChain/nibiru/app/upgrades/v1_0_2"
"github.com/NibiruChain/nibiru/app/upgrades/v1_1_0"
)

var Upgrades = []upgrades.Upgrade{
v1_0_1.Upgrade,
v1_0_2.Upgrade,
v1_0_3.Upgrade,
v1_1_0.Upgrade,
}

Expand Down
23 changes: 23 additions & 0 deletions app/upgrades/v1_0_1/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v1_0_1

import (
"github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/NibiruChain/nibiru/app/upgrades"
)

const UpgradeName = "v1.0.1"

// pretty much a no-op store upgrade to test the upgrade process and include the newer version of rocksdb
var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: func(mm *module.Manager, cfg module.Configurator) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return mm.RunMigrations(ctx, cfg, fromVM)
}
},
StoreUpgrades: types.StoreUpgrades{},
}
2 changes: 1 addition & 1 deletion app/upgrades/v1_0_2/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const UpgradeName = "v1.0.2"

// pretty much a no-op store upgrade to test the upgrade process and include the newer version of rocksdb + cosmos-sdk
// a no-op store upgrade to test the upgrade process and include the newer version cosmos-sdk
var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: func(mm *module.Manager, cfg module.Configurator) upgradetypes.UpgradeHandler {
Expand Down
23 changes: 23 additions & 0 deletions app/upgrades/v1_0_3/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v1_0_3

import (
"github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/NibiruChain/nibiru/app/upgrades"
)

const UpgradeName = "v1.0.3"

// a no-op store upgrade to test the upgrade process and include the newer version cosmos-sdk
var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: func(mm *module.Manager, cfg module.Configurator) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return mm.RunMigrations(ctx, cfg, fromVM)
}
},
StoreUpgrades: types.StoreUpgrades{},
}
172 changes: 172 additions & 0 deletions cmd/nibid/cmd/decode_base64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package cmd

import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdkcodec "github.com/cosmos/cosmos-sdk/codec"

wasmvm "github.com/CosmWasm/wasmvm/types"
)

// YieldStargateMsgs takes a byte slice of JSON data and converts it into a slice
// of wasmvm.StargateMsg objects. This function is essential for processing
// JSON-formatted messages that contain base64-encoded protobuf messages.
//
// Args:
// - jsonBz []byte: A byte slice containing the JSON data to be parsed.
//
// Returns:
// - sgMsgs []wasmvm.StargateMsg: A slice of wasmvm.StargateMsg objects parsed
// from the provided JSON data.
// - err error: An error object, which is nil if the operation is successful.
func YieldStargateMsgs(jsonBz []byte) (sgMsgs []wasmvm.StargateMsg, err error) {
var data interface{}
if err := json.Unmarshal(jsonBz, &data); err != nil {
return sgMsgs, err
}

parseStargateMsgs(data, &sgMsgs)
return sgMsgs, nil
}

// parseStargateMsgs is a recursive function used by YieldStargateMsgs to
// traverse the JSON data, filter for any protobuf.Any messages in the
// "WasmVM.StargateMsg" format and decode them from base64 back to human-readable
// form as JSON objects.
//
// Args:
// - jsonData any: JSON data to parse. According to the JSON specification,
// possible value types are:
// Null, Bool, Number(f64), String, Array, or Object(Map<String, Value>)
// - msgs *[]wasmvm.StargateMsg: Mutable reference to a slice of protobuf
// messages. These are potentially altered in place if the value is an
// encoded base 64 string.
func parseStargateMsgs(jsonData any, msgs *[]wasmvm.StargateMsg) {
switch v := jsonData.(type) {
case map[string]interface{}:
if typeURL, ok := v["type_url"].(string); ok {
if value, ok := v["value"].(string); ok {
*msgs = append(*msgs, wasmvm.StargateMsg{
TypeURL: typeURL,
Value: []byte(value),
})
}
}
for _, value := range v {
parseStargateMsgs(value, msgs)
}
case []interface{}:
for _, value := range v {
parseStargateMsgs(value, msgs)
}
}
}

// StargateMsgDecoded is a struct designed to hold a decoded version of a
// "wasmvm.StargateMsg".
type StargateMsgDecoded struct {
TypeURL string `json:"type_url"`
Value string `json:"value"`
}

// DecodeBase64StargateMsgs decodes a series of base64-encoded
// wasmvm.StargateMsg objects from the provided JSON byte slice (jsonBz).
// This function is vital for extracting and interpreting the contents of these
// protobuf messages.
//
// Args:
// - jsonBz []byte: JSON data containing potential base64-encoded messages.
// - clientCtx client.Context: Context for the `nibid` CLI.
//
// Returns:
// - newSgMsgs []StargateMsgDecoded: The decoded stargate messages.
// - err error: An error object, which is nil if the operation is successful.
func DecodeBase64StargateMsgs(
jsonBz []byte, clientCtx client.Context,
) (newSgMsgs []StargateMsgDecoded, err error) {
codec := clientCtx.Codec

var data interface{}
if err := json.Unmarshal(jsonBz, &data); err != nil {
return newSgMsgs, fmt.Errorf(
"failed to decode stargate msgs due to invalid JSON: %w", err)
}

sgMsgs, err := YieldStargateMsgs(jsonBz)
if err != nil {
return newSgMsgs, err
}
for _, sgMsg := range sgMsgs {
valueStr := string(sgMsg.Value)
replacer := strings.NewReplacer(
`\"`, `"`, // old, new
`"{`, `{`,
`}"`, `}`,
)
value := replacer.Replace(string(sgMsg.Value))

if _, err := base64.StdEncoding.DecodeString(valueStr); err == nil {
protoMsg, err := clientCtx.InterfaceRegistry.Resolve(sgMsg.TypeURL)
if err != nil {
return newSgMsgs, err
}

decodedBz, _ := base64.StdEncoding.Strict().DecodeString(string(sgMsg.Value))
concrete := protoMsg.(sdkcodec.ProtoMarshaler)

err = codec.Unmarshal(decodedBz, concrete)
if err != nil {
return newSgMsgs, err
}

outBytes, err := codec.MarshalJSON(concrete)
if err != nil {
return newSgMsgs, err
}

newSgMsgs = append(newSgMsgs,
StargateMsgDecoded{sgMsg.TypeURL, string(outBytes)},
)
} else if _, err := json.Marshal(value); err == nil {
newSgMsgs = append(newSgMsgs,
StargateMsgDecoded{sgMsg.TypeURL, string(sgMsg.Value)},
)
} else {
return newSgMsgs, fmt.Errorf(
"parse error: encountered wasmvm.StargateMsg with unexpected format: %s", sgMsg)
}
}
return newSgMsgs, nil
}

// DecodeBase64Cmd creates a Cobra command used to decode base64-encoded protobuf
// messages from a JSON input. This function enables users to input arbitrary
// JSON strings and parse the contents of base-64 encoded protobuf.Any messages.
func DecodeBase64Cmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "base64-decode",
Short: "Decode a base64-encoded protobuf message",
Long: `Decode a base64-encoded protobuf message from JSON input.
The input should be a JSON object with 'type_url' and 'value' fields.`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

outMessage, err := DecodeBase64StargateMsgs([]byte(args[0]), clientCtx)
if err == nil {
fmt.Println(outMessage)
}
return err
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

return cmd
}
Loading

0 comments on commit cd24abb

Please sign in to comment.