Skip to content

Commit

Permalink
Merge branch 'main' into realu/devgas-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Dec 28, 2023
2 parents c4c24ca + 68bddeb commit a5ca911
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1712](https://github.com/NibiruChain/nibiru/pull/1712) - refactor(inflation): turn inflation off by default
* [#1713](https://github.com/NibiruChain/nibiru/pull/1713) - chore(build-release): use new rocksdb libraries and link required libs
* [#1715](https://github.com/NibiruChain/nibiru/pull/1715) - fix(build): revert! to working build + cherry-picks
* [#1731](https://github.com/NibiruChain/nibiru/pull/1731) - feat(cli): add cli command to decode stargate base64 messages

#### Dapp modules: perp, spot, etc

Expand Down Expand Up @@ -82,6 +83,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Dependencies
- Bump `google.golang.org/grpc` from 1.59.0 to 1.60.0 ([#1720](https://github.com/NibiruChain/nibiru/pull/1720))
- Bump `golang.org/x/crypto` from 0.15.0 to 0.17.0 ([#1724](https://github.com/NibiruChain/nibiru/pull/1724))
- Bump `github.com/holiman/uint256` from 1.2.3 to 1.2.4 ([#1730](https://github.com/NibiruChain/nibiru/pull/1730))
- Bump `github.com/dvsekhvalnov/jose2go` from 1.5.0 to 1.6.0 ([#1733](https://github.com/NibiruChain/nibiru/pull/1733))

* Bump `github.com/spf13/cast` from 1.5.1 to 1.6.0 ([#1689](https://github.com/NibiruChain/nibiru/pull/1689))
* Bump `cosmossdk.io/math` from 1.1.2 to 1.2.0 ([#1676](https://github.com/NibiruChain/nibiru/pull/1676))
Expand Down
186 changes: 183 additions & 3 deletions CHAOSNET.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
- [What is Chaosnet?](#what-is-chaosnet)
- [How to run "chaosnet"](#how-to-run-chaosnet)
- [How to force pull images from the registry](#how-to-force-pull-images-from-the-registry)
- [IBC Commands](#ibc-commands)
- [Endpoints](#endpoints)
- [FAQ](#faq)
- [`make chaosnet` says that "Additional property name is not allowed"](#make-chaosnet-says-that-additional-property-name-is-not-allowed)
- [Does data persist between runs?](#does-data-persist-between-runs)
- [My `make chaosnet` takes forever to run](#my-make-chaosnet-takes-forever-to-run)

## What is Chaosnet?

Expand Down Expand Up @@ -36,6 +38,8 @@ Enter your GitHub username for the `username` field, and your personal access to

4. Run `make chaosnet`

Note that this will take a while the first time you run it, as it will need to pull all the images from the registry, build the chaonset image locally, and set up the IBC channel (which has a lot of round trip packet commits).

## How to force pull images from the registry

By default, most images (heart-monitor, liquidator, etc.) are cached locally and won't re-fetch from upstream registries. To force a pull, you can run
Expand All @@ -44,13 +48,185 @@ By default, most images (heart-monitor, liquidator, etc.) are cached locally and
make chaosnet-build
```

## IBC Commands

To send an IBC transfer from nibiru-0 to nibiru-1, run:

1. SSH into nibiru-0

```sh
docker compose -f ./contrib/docker-compose/docker-compose-chaosnet.yml exec -it nibiru-0 /bin/ash
```

2. Transfer tokens from nibiru-0 to nibiru-1

```sh
nibid tx ibc-transfer transfer transfer \
channel-0 \
nibi18mxturdh0mjw032c3zslgkw63cukkl4q5skk8g \
1000000unibi \
--from validator \
--fees 5000unibi \
--yes | jq
```

3. In a new shell, SSH into nibiru-1

```sh
docker compose -f ./contrib/docker-compose/docker-compose-chaosnet.yml exec -it nibiru-1 /bin/ash
```

4. Query the balance of nibiru-1

```sh
# set the config since nibiru-1 has different ports
nibid config node "http://localhost:36657"
nibid q bank balances $(nibid keys show validator -a) | jq
```

Output:

```json
{
"balances": [
{
"denom": "ibc/9BEE732637B12723D26E365D19CCB624587CE6254799EEE7C5F77B587BD677B0",
"amount": "1000000"
},
{
"denom": "unibi",
"amount": "9999100000000"
}
],
"pagination": {
"next_key": null,
"total": "0"
}
}
```

5. Send tokens from nibiru-1 to nibiru-0

```sh
nibid tx ibc-transfer transfer transfer \
channel-0 \
nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl \
5555unibi \
--from validator \
--fees 5000unibi \
--yes | jq
```

6. Go back to the nibiru-0 and query the balance

```sh
nibid q bank balances $(nibid keys show validator -a) | jq
```

Output:

```json
{
"balances": [
{
"denom": "ibc/9BEE732637B12723D26E365D19CCB624587CE6254799EEE7C5F77B587BD677B0",
"amount": "5555"
},
{
"denom": "unibi",
"amount": "9999098995000"
}
],
"pagination": {
"next_key": null,
"total": "0"
}
}
```

7. Send IBC tokens back to nibiru-1

```sh
nibid tx ibc-transfer transfer transfer \
channel-0 \
nibi18mxturdh0mjw032c3zslgkw63cukkl4q5skk8g \
5555ibc/9BEE732637B12723D26E365D19CCB624587CE6254799EEE7C5F77B587BD677B0 \
--from validator \
--fees 5000unibi \
--yes | jq
```

8. Verify tokens are sent

```sh
nibid q bank balances $(nibid keys show validator -a) | jq
```

Output:

```json
{
"balances": [
{
"denom": "unibi",
"amount": "9999098990000"
}
],
"pagination": {
"next_key": null,
"total": "0"
}
}
```

9. Back in the nibiru-1 shell, send tokens back to nibiru-0

```sh
nibid tx ibc-transfer transfer transfer \
channel-0 \
nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl \
1000000ibc/9BEE732637B12723D26E365D19CCB624587CE6254799EEE7C5F77B587BD677B0 \
--from validator \
--fees 5000unibi \
--yes | jq
```

10. Verify tokens are sent

```sh
nibid q bank balances $(nibid keys show validator -a) | jq
```

Output:

```json
{
"balances": [
{
"denom": "unibi",
"amount": "9999099990000"
}
],
"pagination": {
"next_key": null,
"total": "0"
}
}
```

## Endpoints

- `http://localhost:5555` -> GraphQL server
- `http://localhost:26657` -> Tendermint RPC server
- `tcp://localhost:9090` -> Cosmos SDK gRPC server
- `http://localhost:1317` -> Cosmos SDK LCD (REST) server
- `http://localhost:8000` -> Faucet server (HTTP POST only)
-
- `http://localhost:26657` -> nibiru-0 Tendermint RPC server
- `tcp://localhost:9090` -> nibiru-0 Cosmos SDK gRPC server
- `http://localhost:1317` -> nibiru-0 Cosmos SDK LCD (REST) server
-
- `http://localhost:36657` -> nibiru-1 Tendermint RPC server
- `tcp://localhost:19090` -> nibiru-1 Cosmos SDK gRPC server
- `http://localhost:11317` -> nibiru-1 Cosmos SDK LCD (REST) server

## FAQ

Expand All @@ -61,3 +237,7 @@ Make sure to update your docker application to version >=23.0.1
### Does data persist between runs?

No, all volumes are deleted and recreated every time you run `make chaosnet`. This is to ensure that you always start with a clean network.

### My `make chaosnet` takes forever to run

It usually takes a few minutes to set everything up and create the IBC channels. If it takes more than 5 minutes, then check the logs of the chaosnet containers to see if any step failed. Reach out to <[email protected]> for help.
126 changes: 126 additions & 0 deletions cmd/nibid/cmd/decode_base64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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 parses the JSON and sends wasmvm.StargateMsg objects to a channel
func YieldStargateMsgs(jsonBz []byte) ([]wasmvm.StargateMsg, error) {
var data interface{}
if err := json.Unmarshal(jsonBz, &data); err != nil {
return nil, err
}

Check warning on line 23 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L22-L23

Added lines #L22 - L23 were not covered by tests

var msgs []wasmvm.StargateMsg
parseStargateMsgs(data, &msgs)
return msgs, nil
}

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)
}

Check warning on line 47 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L44-L47

Added lines #L44 - L47 were not covered by tests
}
}

type StargateMsgDecoded struct {
TypeURL string `json:"type_url"`
Value string `json:"value"`
}

func DecodeBase64StargateMsgs(
jsonBz []byte, context client.Context,
) (newSgMsgs []StargateMsgDecoded, err error) {
codec := context.Codec

var data interface{}
if err := json.Unmarshal(jsonBz, &data); err != nil {
return []StargateMsgDecoded{}, err
}

sgMsgs, err := YieldStargateMsgs(jsonBz)
if err != nil {
return
}

Check warning on line 69 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L68-L69

Added lines #L68 - L69 were not covered by tests
for _, sgMsg := range sgMsgs {
valueStr := string(sgMsg.Value)
value := strings.Replace(string(sgMsg.Value), `\"`, `"`, -1)
value = strings.Replace(value, `"{`, `{`, -1)
value = strings.Replace(value, `}"`, `}`, -1)

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

Check warning on line 80 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L79-L80

Added lines #L79 - L80 were not covered by tests

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

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

Check warning on line 88 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L87-L88

Added lines #L87 - L88 were not covered by tests

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

Check warning on line 93 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L92-L93

Added lines #L92 - L93 were not covered by tests

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)
}

Check warning on line 101 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L99-L101

Added lines #L99 - L101 were not covered by tests
}
return newSgMsgs, nil
}

// DecodeBase64Cmd creates a cobra command for base64 decoding.
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)
fmt.Println(outMessage)

return err
},
}

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

return cmd
}
Loading

0 comments on commit a5ca911

Please sign in to comment.