Skip to content

Commit

Permalink
feat(x/gov): adds constitution to x/gov (#15)
Browse files Browse the repository at this point in the history
* fin

* lint

* code review

---------

Co-authored-by: Adam Hanna <[email protected]>
  • Loading branch information
adam-hanna and Adam Hanna authored Sep 17, 2024
1 parent 184eee4 commit a1f5216
Show file tree
Hide file tree
Showing 14 changed files with 687 additions and 107 deletions.
27 changes: 26 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ docker-build-debug:
### Linting ###
###############################################################################
golangci_lint_cmd=golangci-lint
golangci_version=v1.53.3
golangci_version=v1.56.0 # note: needed to bump from v1.53.3 bc go.tmz.dev/musttag (A dep of golangci-lint) was no longer resolving

lint:
@echo "--> Running linter"
Expand All @@ -242,6 +242,31 @@ format:
$(golangci_lint_cmd) run --fix
.PHONY: format

# Get a list of all directories containing Go files, excluding vendor and other paths
GO_DIRS=$(shell find . -name '*.go' -not -path "./vendor*" -not -path "*.git*" \
-not -path "./client/docs/statik/statik.go" \
-not -path "./tests/mocks/*" \
-not -path "./crypto/keys/secp256k1/*" \
-not -name "*.pb.go" \
-not -name "*.pb.gw.go" \
-not -name "*.pulsar.go" | xargs -n1 dirname | sort -u)

format-batch:
@go install mvdan.cc/gofumpt@latest
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)

# Run gofumpt in a loop over each directory
@for dir in $(GO_DIRS); do \
echo "Running gofumpt on $$dir"; \
find $$dir -name '*.go' -type f -print0 | xargs -0 gofumpt -w -l; \
done

# Run golangci-lint separately for each directory
@for dir in $(GO_DIRS); do \
$(golangci_lint_cmd) run --fix $$dir || exit 1; \
done
.PHONY: format-batch

###############################################################################
### Localnet ###
###############################################################################
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
cosmossdk.io/math v1.3.0
cosmossdk.io/simapp v0.0.0-20230602123434-616841b9704d
cosmossdk.io/tools/rosetta v0.2.1
github.com/chzyer/readline v1.5.1
github.com/cometbft/cometbft v0.37.4
github.com/cometbft/cometbft-db v0.10.0
github.com/cosmos/cosmos-proto v1.0.0-beta.4
Expand All @@ -19,6 +20,7 @@ require (
github.com/golang/mock v1.6.0
github.com/google/gofuzz v1.2.0
github.com/gorilla/mux v1.8.1
github.com/manifoldco/promptui v0.9.0
github.com/ory/dockertest/v3 v3.10.0
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.6.0
Expand Down Expand Up @@ -66,7 +68,6 @@ require (
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/cockroachdb/errors v1.10.0 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
Expand Down Expand Up @@ -145,7 +146,6 @@ require (
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.8.11 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
Expand Down Expand Up @@ -221,4 +221,7 @@ replace (

// following versions might cause unexpected behavior
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7

// replace broken cosmos-sdk dep
nhooyr.io/websocket => github.com/coder/websocket v1.8.6
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ github.com/cockroachdb/pebble v1.0.0/go.mod h1:bynZ3gvVyhlvjLI7PT6dmZ7g76xzJ7Hpx
github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwPJ30=
github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/coder/websocket v1.8.6 h1:OmNKdwUvLj7VvHnl5o8skaVghSPLjWdHGCnFbkWqs9w=
github.com/coder/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
github.com/coinbase/kryptology v1.8.0/go.mod h1:RYXOAPdzOGUe3qlSFkMGn58i3xUA8hmxYHksuq+8ciI=
github.com/coinbase/rosetta-sdk-go v0.7.9 h1:lqllBjMnazTjIqYrOGv8h8jxjg9+hJazIGZr9ZvoCcA=
github.com/coinbase/rosetta-sdk-go v0.7.9/go.mod h1:0/knutI7XGVqXmmH4OQD8OckFrbQ8yMsUZTG7FXCR2M=
Expand Down Expand Up @@ -1953,8 +1955,6 @@ honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.1.3/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las=
nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k=
nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
Expand Down
4 changes: 4 additions & 0 deletions proto/atomone/gov/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ message GenesisState {
//
// Since: cosmos-sdk 0.47
Params params = 8;
// The constitution allows builders to lay a foundation and define purpose.
//
// Since: cosmos-sdk 0.48
string constitution = 9;
}
13 changes: 13 additions & 0 deletions proto/atomone/gov/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ option go_package = "github.com/atomone-hub/atomone/x/gov/types/v1";

// Query defines the gRPC querier service for gov module
service Query {
// Constitution queries the chain's constitution.
rpc Constitution(QueryConstitutionRequest) returns (QueryConstitutionResponse) {
option (google.api.http).get = "/cosmos/gov/v1/constitution";
}

// Proposal queries proposal details based on ProposalID.
rpc Proposal(QueryProposalRequest) returns (QueryProposalResponse) {
option (google.api.http).get = "/atomone/gov/v1/proposals/{proposal_id}";
Expand Down Expand Up @@ -53,6 +58,14 @@ service Query {
}
}

// QueryConstitutionRequest is the request type for the Query/Constitution RPC method
message QueryConstitutionRequest {}

// QueryConstitutionResponse is the response type for the Query/Constitution RPC method
message QueryConstitutionResponse {
string constitution = 1;
}

// QueryProposalRequest is the request type for the Query/Proposal RPC method.
message QueryProposalRequest {
// proposal_id defines the unique id of the proposal.
Expand Down
23 changes: 23 additions & 0 deletions x/gov/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func GetQueryCmd() *cobra.Command {
GetCmdQueryDeposit(),
GetCmdQueryDeposits(),
GetCmdQueryTally(),
GetCmdConstitution(),
)

return govQueryCmd
Expand Down Expand Up @@ -653,3 +654,25 @@ $ %s query gov proposer 1

return cmd
}

func GetCmdConstitution() *cobra.Command {
return &cobra.Command{
Use: "constitution",
Short: "Get the constitution",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
queryClient := v1.NewQueryClient(clientCtx)

resp, err := queryClient.Constitution(cmd.Context(), &v1.QueryConstitutionRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(resp)
},
}
}
24 changes: 24 additions & 0 deletions x/gov/client/cli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"

"github.com/atomone-hub/atomone/x/gov/client/cli"
)
Expand Down Expand Up @@ -382,3 +383,26 @@ func (s *CLITestSuite) TestCmdQueryVote() {
})
}
}

func (s *CLITestSuite) TestCmdGetConstitution() {
testCases := []struct {
name string
expOutput string
}{
{
name: "get constitution",
expOutput: "constitution",
},
}

for _, tc := range testCases {
tc := tc

s.Run(tc.name, func() {
cmd := cli.GetCmdConstitution()
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, []string{})
s.Require().NoError(err)
s.Require().Contains(out.String(), tc.expOutput)
})
}
}
3 changes: 3 additions & 0 deletions x/gov/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func InitGenesis(ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, k
if err := k.SetParams(ctx, *data.Params); err != nil {
panic(fmt.Sprintf("%s module params has not been set", types.ModuleName))
}
k.SetConstitution(ctx, data.Constitution)

// check if the deposits pool account exists
moduleAcc := k.GetGovernanceAccount(ctx)
Expand Down Expand Up @@ -60,6 +61,7 @@ func ExportGenesis(ctx sdk.Context, k *keeper.Keeper) *v1.GenesisState {
startingProposalID, _ := k.GetProposalID(ctx)
proposals := k.GetProposals(ctx)
params := k.GetParams(ctx)
constitution := k.GetConstitution(ctx)

var proposalsDeposits v1.Deposits
var proposalsVotes v1.Votes
Expand All @@ -77,5 +79,6 @@ func ExportGenesis(ctx sdk.Context, k *keeper.Keeper) *v1.GenesisState {
Votes: proposalsVotes,
Proposals: proposals,
Params: &params,
Constitution: constitution,
}
}
19 changes: 19 additions & 0 deletions x/gov/keeper/constitution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package keeper

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

"github.com/atomone-hub/atomone/x/gov/types"
)

func (keeper Keeper) GetConstitution(ctx sdk.Context) (constitution string) {
store := ctx.KVStore(keeper.storeKey)
bz := store.Get(types.KeyConstitution)

return string(bz)
}

func (keeper Keeper) SetConstitution(ctx sdk.Context, constitution string) {
store := ctx.KVStore(keeper.storeKey)
store.Set(types.KeyConstitution, []byte(constitution))
}
7 changes: 7 additions & 0 deletions x/gov/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import (

var _ v1.QueryServer = Keeper{}

func (q Keeper) Constitution(c context.Context, req *v1.QueryConstitutionRequest) (*v1.QueryConstitutionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
constitution := q.GetConstitution(ctx)

return &v1.QueryConstitutionResponse{Constitution: constitution}, nil
}

// Proposal returns proposal details based on ProposalID
func (q Keeper) Proposal(c context.Context, req *v1.QueryProposalRequest) (*v1.QueryProposalResponse, error) {
if req == nil {
Expand Down
3 changes: 3 additions & 0 deletions x/gov/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ var (

// ParamsKey is the key to query all gov params
ParamsKey = []byte{0x30}

// KeyConstitution is the key string used to store the chain's constitution
KeyConstitution = []byte{0x40}
)

var lenTime = len(sdk.FormatTimeBytes(time.Now()))
Expand Down
Loading

0 comments on commit a1f5216

Please sign in to comment.