Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sudo): CLI fns + tests. Also, switches testutil to use secp256k1 #1317

Merged
merged 79 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
9d9fcb2
golang structs for the contract binding types
Unique-Divine Apr 13, 2023
f4fc5b6
feat(wasm): implement custom queries: Reserves, AllMarkets
Unique-Divine Apr 14, 2023
38e151e
refactor: make request type names more obvious
Unique-Divine Apr 14, 2023
55d6d99
Wire in the WasmKeeper to the app with []wasm.Option set for the bind…
Unique-Divine Apr 21, 2023
cbfa332
add FundAccount fn to x/common/testutil
Unique-Divine Apr 22, 2023
842ee3f
Merge branch 'master' into realu/wasm-binding
Unique-Divine Apr 22, 2023
cf44be1
rm fund.go. These functions were defined in the recent x/inflation PR
Unique-Divine Apr 22, 2023
a47342b
refactor: test genesis setup to testutil/genesis so other modules can…
Unique-Divine Apr 22, 2023
73f14b7
fix: add json serialization info to cw_query.go
Unique-Divine Apr 22, 2023
4be7372
test(wasmbin): add perp bindings contract and add correctness tests
Unique-Divine Apr 22, 2023
4c40fa9
test(wasm/binding): successful integration test using the real contract
Unique-Divine Apr 22, 2023
28de273
update CHANGELOG and include marshal.go
Unique-Divine Apr 22, 2023
cccb70f
fix: linter
Unique-Divine Apr 22, 2023
78f149f
(wasm): impl and test base price query
Unique-Divine Apr 23, 2023
58c70dc
add utils
Unique-Divine Apr 23, 2023
8490906
impl and test BasePrice, Metrics, and PremiumFraction queries
Unique-Divine Apr 23, 2023
0127f12
add remaining integration tests for the PR
Unique-Divine Apr 23, 2023
35b73d2
test,fix(wasm): all integration tests working on a real contract
Unique-Divine Apr 24, 2023
dd0ccb4
test,fix(wasm): all integration tests working on a real contract
Unique-Divine Apr 24, 2023
48ab854
comment
Unique-Divine Apr 24, 2023
dce8b94
Merge branch 'master' into realu/wasm-binding
Unique-Divine Apr 24, 2023
ca33381
refactor(wasm): small rename to query_responses.json
Unique-Divine Apr 25, 2023
8978d75
Merge branch 'master' into realu/wasm-binding
Unique-Divine Apr 25, 2023
0bfb626
Merge branch 'master' into realu/wasm-binding
Unique-Divine Apr 25, 2023
9cf0a0b
refactor,docs: PR comments
Unique-Divine Apr 25, 2023
b884ec5
test(cw_struct): test compatibility with rust binding types for execu…
Unique-Divine Apr 25, 2023
be605de
feat(wasm): add execute msg bindings for open pos, add & remove margi…
Unique-Divine Apr 26, 2023
9e9acef
(wasm.go): wire the execute msg as a wasm.Option
Unique-Divine Apr 26, 2023
b899c62
test(wasm): unit + integration tests for perp transactions
Unique-Divine Apr 26, 2023
456cb07
changelog
Unique-Divine Apr 26, 2023
55724b0
Merge branch 'master' into realu/wasm-binding
Unique-Divine Apr 26, 2023
cd01af7
changelog
Unique-Divine Apr 26, 2023
582fdff
Merge branch 'master' into realu/wasm-binding
Unique-Divine Apr 27, 2023
ec7d55c
Merge branch 'master' into realu/wasm-binding
Unique-Divine Apr 27, 2023
5574f9c
Merge branch 'master' into realu/wasm-binding
Unique-Divine Apr 28, 2023
15853f7
Merge branch 'realu/wasm-binding' of https://github.com/NibiruChain/n…
Unique-Divine Apr 28, 2023
af9a1e1
test(wasm): check for position in state after it's open
Unique-Divine Apr 28, 2023
5a7e6e8
golangci-lint
Unique-Divine Apr 28, 2023
9c17041
(wasm): new bindings contract
Unique-Divine Apr 30, 2023
ab82fd0
feat(wasm): peg shift bindings + unit test + integration test
Unique-Divine Apr 30, 2023
ee969cc
changelog
Unique-Divine Apr 30, 2023
ac2c652
Merge branch 'master' into realu/wasm-binding
Unique-Divine Apr 30, 2023
eb54aae
Merge branch 'realu/wasm-binding' into realu/wasm2
Unique-Divine Apr 30, 2023
7ade8b9
Merge branch 'master' into realu/wasm2
Unique-Divine Apr 30, 2023
992c720
build: new contract with String types for sdk.Dec values that can be …
Unique-Divine May 1, 2023
0df1bd4
impl Position query with unit test
Unique-Divine May 1, 2023
baab679
test: integration test for Position query
Unique-Divine May 1, 2023
b6f2630
impl and test multiple position query
Unique-Divine May 1, 2023
89e73c2
Merge branch 'master' into realu/wasm2
Unique-Divine May 1, 2023
8300be9
test(wasm): verify correctness of fields in positions query
Unique-Divine May 1, 2023
f03dd97
checkpoint #wip sudo core business logic
Unique-Divine May 1, 2023
3504d95
checkpoint #wip: implement all business logic with full test coverage
Unique-Divine May 2, 2023
e4d61b9
test: add integration tests of the sudo contracts in x/wasm
Unique-Divine May 2, 2023
6ea876a
changelog
Unique-Divine May 2, 2023
c5f3343
changelog
Unique-Divine May 2, 2023
3090cea
Merge branch 'master' into realu/sudo
Unique-Divine May 2, 2023
9e1b463
Merge branch 'master' into realu/sudo
Unique-Divine May 2, 2023
cb43718
more docs
Unique-Divine May 2, 2023
6a30250
refactor(sudo): run msg.ValidateBasic before the main handler
Unique-Divine May 4, 2023
a5a000e
Merge branch 'master' into realu/sudo
Unique-Divine May 4, 2023
dfb88d8
refactor: small cleanup
Unique-Divine May 4, 2023
eb89617
doc comment
Unique-Divine May 4, 2023
4653018
Merge branch 'master' into realu/sudo2
Unique-Divine May 4, 2023
8444cef
feat(sudo): cli commands + tests for tx and query
Unique-Divine May 5, 2023
3e24f0b
feat(sample.go): secp256k1 usage for #1316
Unique-Divine May 5, 2023
99dd972
chore: changelog + linter
Unique-Divine May 5, 2023
5a4a026
test(sudo): slice order can change when written into state
Unique-Divine May 5, 2023
6f28a8b
Merge branch 'master' into realu/sudo2
Unique-Divine May 5, 2023
9163b00
test(sudo): fix boolean checks
Unique-Divine May 5, 2023
4905aec
Merge branch 'master' into realu/sudo2
Unique-Divine May 8, 2023
81019fe
chore: linter
Unique-Divine May 8, 2023
68ae55c
chore: linter
Unique-Divine May 8, 2023
a3281ae
changelog
Unique-Divine May 8, 2023
2e9d99c
Merge branch 'master' into realu/sudo2
Unique-Divine May 8, 2023
5eea4a7
remove ed25519Algo support in sample.go
Unique-Divine May 8, 2023
5a086e1
Merge branch 'master' into realu/sudo2
Unique-Divine May 8, 2023
5a5ef8a
Merge branch 'realu/sudo2' of https://github.com/NibiruChain/nibiru i…
Unique-Divine May 8, 2023
d762e79
Merge branch 'master' into realu/sudo2
Unique-Divine May 9, 2023
bcb7a2b
Merge branch 'master' into realu/sudo2
matthiasmatt May 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Breaking

* [#1317](https://github.com/NibiruChain/nibiru/pull/1317) - feat(sudo): Implement and test CLI commands for tx and queries.
* [#1307](https://github.com/NibiruChain/nibiru/pull/1307) - feat(sudo): Create the x/sudo module + integration tests
* [#1299](https://github.com/NibiruChain/nibiru/pull/1299) - feat(wasm): Add peg shift bindings
* [#1292](https://github.com/NibiruChain/nibiru/pull/1292) - feat(wasm): Add module bindings for execute calls in x/perp: OpenPosition, ClosePosition, AddMargin, RemoveMargin.
Expand Down Expand Up @@ -69,8 +70,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Improvements

* [#1321](https://github.com/NibiruChain/nibiru/pull/1321) - build(deps): bump github.com/prometheus/client_golang from 1.15.0 to 1.15.1
* [#1317](https://github.com/NibiruChain/nibiru/pull/1317) - feat(testutil): Use secp256k1 algo for private key generation in common/testutil.
* [#1322](https://gitub.com/NibiruChain/nibiru/pull/1322) - build(deps): Bumps github.com/armon/go-metrics from 0.4.0 to 0.4.1.
* [#1321](https://github.com/NibiruChain/nibiru/pull/1321) - build(deps): bump github.com/prometheus/client_golang from 1.15.0 to 1.15.1
* [#1295](https://github.com/NibiruChain/nibiru/pull/1295) - refactor(app): Organize keepers, store keys, and module manager initialization in app.go
* [#1248](https://github.com/NibiruChain/nibiru/pull/1248) - refactor(common): Combine x/testutil and x/common/testutil.
* [#1245](https://github.com/NibiruChain/nibiru/pull/1245) - fix(localnet.sh): force localnet.sh to work even if Coingecko is down
Expand Down
15 changes: 15 additions & 0 deletions x/common/testutil/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
perpammtypes "github.com/NibiruChain/nibiru/x/perp/amm/types"
perpcli "github.com/NibiruChain/nibiru/x/perp/client/cli"
perptypes "github.com/NibiruChain/nibiru/x/perp/types"
sudocli "github.com/NibiruChain/nibiru/x/sudo/cli"
sudotypes "github.com/NibiruChain/nibiru/x/sudo/pb"
)

// ExecQueryOption defines a type which customizes a CLI query operation.
Expand Down Expand Up @@ -114,3 +116,16 @@ func QueryCumulativePremiumFraction(clientCtx client.Context, pair asset.Pair) (
}
return &queryResp, nil
}

func QuerySudoers(clientCtx client.Context) (*sudotypes.QuerySudoersResponse, error) {
var queryResp sudotypes.QuerySudoersResponse
if err := ExecQuery(
clientCtx,
sudocli.CmdQuerySudoers(),
[]string{},
&queryResp,
); err != nil {
return nil, err
}
return &queryResp, nil
}
2 changes: 2 additions & 0 deletions x/common/testutil/genesis/perp_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/app"

"github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/common/asset"
"github.com/NibiruChain/nibiru/x/common/denoms"

oracletypes "github.com/NibiruChain/nibiru/x/oracle/types"
perpammtypes "github.com/NibiruChain/nibiru/x/perp/amm/types"
perptypes "github.com/NibiruChain/nibiru/x/perp/types"
Expand Down
41 changes: 41 additions & 0 deletions x/common/testutil/genesis/sudo_genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package genesis

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/app"

"github.com/NibiruChain/nibiru/x/sudo"
sudotypes "github.com/NibiruChain/nibiru/x/sudo/pb"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"

"github.com/NibiruChain/nibiru/x/common/testutil"
)

func AddSudoGenesis(gen app.GenesisState) (
genState app.GenesisState,
rootPrivKey cryptotypes.PrivKey,
rootAddr sdk.AccAddress,
) {
sudoGenesis, rootPrivKey, rootAddr := SudoGenesis()
gen[sudotypes.ModuleName] = TEST_ENCODING_CONFIG.Marshaler.
MustMarshalJSON(sudoGenesis)
return gen, rootPrivKey, rootAddr
}

func SudoGenesis() (
genState *sudotypes.GenesisState,
rootPrivKey cryptotypes.PrivKey,
rootAddr sdk.AccAddress,
) {
sudoGenesis := sudo.DefaultGenesis()

// Set the root user
privKeys, addrs := testutil.PrivKeyAddressPairs(1)
rootPrivKey = privKeys[0]
rootAddr = addrs[0]
sudoGenesis.Sudoers.Root = rootAddr.String()

return sudoGenesis, rootPrivKey, rootAddr
}
23 changes: 16 additions & 7 deletions x/common/testutil/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package testutil
import (
"math/rand"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -13,15 +14,23 @@ import (
tmdb "github.com/tendermint/tm-db"
)

// AccAddress Returns a sample account address (sdk.AccAddress)
// AccAddress returns a sample address (sdk.AccAddress) created using secp256k1.
// Note that AccAddress().String() can be used to get a string representation.
func AccAddress() sdk.AccAddress {
pk := ed25519.GenPrivKey().PubKey()
addr := pk.Address()
return sdk.AccAddress(addr)
_, accAddr := PrivKey()
return accAddr
}

// PrivKey returns a private key and corresponding on-chain address.
func PrivKey() (*secp256k1.PrivKey, sdk.AccAddress) {
privKey := secp256k1.GenPrivKey()
pubKey := privKey.PubKey()
addr := pubKey.Address()
return privKey, sdk.AccAddress(addr)
}

// PrivKeyAddressPairs generates (deterministically) a total of n private keys and addresses.
// PrivKeyAddressPairs generates (deterministically) a total of n private keys
// and addresses.
func PrivKeyAddressPairs(n int) (keys []cryptotypes.PrivKey, addrs []sdk.AccAddress) {
r := rand.New(rand.NewSource(12345)) // make the generation deterministic
keys = make([]cryptotypes.PrivKey, n)
Expand All @@ -32,7 +41,7 @@ func PrivKeyAddressPairs(n int) (keys []cryptotypes.PrivKey, addrs []sdk.AccAddr
if err != nil {
panic("Could not read randomness")
}
keys[i] = ed25519.GenPrivKeyFromSecret(secret)
keys[i] = secp256k1.GenPrivKeyFromSecret(secret)
addrs[i] = sdk.AccAddress(keys[i].PubKey().Address())
}
return
Expand Down
143 changes: 143 additions & 0 deletions x/sudo/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package cli

import (
"fmt"
"os"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"

"github.com/spf13/cobra"

"github.com/NibiruChain/nibiru/x/sudo/pb"
)

// GetTxCmd returns a cli command for this module's transactions
func GetTxCmd() *cobra.Command {
txCmd := &cobra.Command{
Use: pb.ModuleName,
Short: fmt.Sprintf("x/%s transaction subcommands", pb.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

// Add subcommands
txCmd.AddCommand(
CmdEditSudoers(),
)

return txCmd
}

// GetQueryCmd returns a cli command for this module's queries
func GetQueryCmd() *cobra.Command {
moduleQueryCmd := &cobra.Command{
Use: pb.ModuleName,
Short: fmt.Sprintf(
"Query commands for the x/%s module", pb.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

// Add subcommands
cmds := []*cobra.Command{
CmdQuerySudoers(),
}
for _, cmd := range cmds {
moduleQueryCmd.AddCommand(cmd)
}

return moduleQueryCmd
}

// CmdEditSudoers is a terminal command corresponding to the EditSudoers
// function of the sdk.Msg handler for x/sudo.
func CmdEditSudoers() *cobra.Command {
cmd := &cobra.Command{
Use: "edit [edit-json]",
Args: cobra.ExactArgs(1),
Short: "Edit the x/sudo state (sudoers) by adding or removing contracts",
Example: strings.TrimSpace(fmt.Sprintf(`
Example:
$ %s tx sudo edit <path/to/edit.json> --from=<key_or_address>
`, version.AppName)),
Long: strings.TrimSpace(
`Adds or removes contracts from the x/sudo state, giving the
contracts permissioned access to certain bindings in x/wasm.

The edit.json for 'EditSudoers' is of the form:
{
"action": "add_contracts",
"contracts": "..."
}

- Valid action types: "add_contracts", "remove_contracts"
`),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := new(pb.MsgEditSudoers)

// marshals contents into the proto.Message to which 'msg' points.
contents, err := os.ReadFile(args[0])
if err != nil {
return err
}
if err = clientCtx.Codec.UnmarshalJSON(contents, msg); err != nil {
return err
}

// Parse the message sender
from := clientCtx.GetFromAddress()
msg.Sender = from.String()

if err = msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func CmdQuerySudoers() *cobra.Command {
cmd := &cobra.Command{
Use: "state",
Short: "displays the internal state (sudoers) of the x/sudo module",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := pb.NewQueryClient(clientCtx)

req := new(pb.QuerySudoersRequest)
resp, err := queryClient.QuerySudoers(
cmd.Context(), req,
)
if err != nil {
return err
}

return clientCtx.PrintProto(resp)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Loading