Skip to content

Commit

Permalink
simply, add Gov register
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Oct 1, 2023
1 parent 1b46f63 commit cccc4e5
Show file tree
Hide file tree
Showing 22 changed files with 1,266 additions and 314 deletions.
12 changes: 0 additions & 12 deletions proto/juno/cwhooks/v1/cwhooks.proto

This file was deleted.

15 changes: 5 additions & 10 deletions proto/juno/cwhooks/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,9 @@ message GenesisState {

// Params defines the set of module parameters.
message Params {
// contract_addresses stores the list of executable contracts to be ticked on every block.
// repeated string contract_addresses = 1 [
// (gogoproto.jsontag) = "contract_addresses,omitempty",
// (gogoproto.moretags) = "yaml:\"contract_addresses\""
// ];

// uint64 contract_gas_limit = 2 [
// (gogoproto.jsontag) = "contract_gas_limit,omitempty",
// (gogoproto.moretags) = "yaml:\"contract_gas_limit\""
// ];
// contract_gas_limit is the contract call gas limit
uint64 contract_gas_limit = 1 [
(gogoproto.jsontag) = "contract_gas_limit,omitempty",
(gogoproto.moretags) = "yaml:\"contract_gas_limit\""
];
}
16 changes: 15 additions & 1 deletion proto/juno/cwhooks/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ service Query {
rpc StakingContracts(QueryStakingContractsRequest) returns (QueryStakingContractsResponse) {
option (google.api.http).get = "/juno/cwhooks/v1/staking_contracts";
}

// GovernanceContracts
rpc GovernanceContracts(QueryGovernanceContractsRequest) returns (QueryGovernanceContractsResponse) {
option (google.api.http).get = "/juno/cwhooks/v1/governance_contracts";
}
}


Expand All @@ -30,11 +35,20 @@ message QueryParamsResponse {
Params params = 1 [(gogoproto.jsontag) = "params", (gogoproto.moretags) = "yaml:\"params\""];
}

// TODO: This should be paginated
// TODO: These should be paginated

// QueryStakingContractsRequest
message QueryStakingContractsRequest {}

// QueryStakingContractsResponse
message QueryStakingContractsResponse {
repeated string contracts = 1 [(gogoproto.jsontag) = "contracts", (gogoproto.moretags) = "yaml:\"contracts\""];
}

// QueryStakingContractsRequest
message QueryGovernanceContractsRequest {}

// QueryGovernanceContractsResponse
message QueryGovernanceContractsResponse {
repeated string contracts = 1 [(gogoproto.jsontag) = "contracts", (gogoproto.moretags) = "yaml:\"contracts\""];
}
20 changes: 18 additions & 2 deletions proto/juno/cwhooks/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package juno.cwhooks.v1;

import "cosmos/msg/v1/msg.proto";
import "juno/cwhooks/v1/genesis.proto";
import "juno/cwhooks/v1/cwhooks.proto";
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "amino/amino.proto";
Expand All @@ -20,6 +19,9 @@ service Msg {

// RegisterStaking.
rpc RegisterStaking(MsgRegisterStaking) returns (MsgRegisterStakingResponse);

// RegisterGovernance.
rpc RegisterGovernance(MsgRegisterGovernance) returns (MsgRegisterGovernanceResponse);
}

// MsgUpdateParams is the Msg/UpdateParams request type.
Expand Down Expand Up @@ -48,8 +50,22 @@ message MsgUpdateParamsResponse {}
message MsgRegisterStaking {
option (gogoproto.equal) = false;

Contract contract = 1 [(gogoproto.nullable) = false];
string contract_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

string register_address = 2;
}

// MsgRegisterStakingResponse
message MsgRegisterStakingResponse {}

// MsgRegisterGovernance
message MsgRegisterGovernance {
option (gogoproto.equal) = false;

string contract_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

string register_address = 2;
}

// MsgRegisterGovernanceResponse
message MsgRegisterGovernanceResponse {}
39 changes: 35 additions & 4 deletions x/cw-hooks/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewTxCmd() *cobra.Command {

txCmd.AddCommand(
NewRegisterStaking(),
NewRegisterGovernance(),
)
return txCmd
}
Expand All @@ -41,10 +42,40 @@ func NewRegisterStaking() *cobra.Command {
contract := args[0]

msg := &types.MsgRegisterStaking{
Contract: types.Contract{
ContractAddress: contract,
RegisterAddress: deployer.String(),
},
ContractAddress: contract,
RegisterAddress: deployer.String(),
}

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

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

flags.AddTxFlagsToCmd(cmd)
return cmd
}

func NewRegisterGovernance() *cobra.Command {
cmd := &cobra.Command{
Use: "register-governance [contract_bech32]",
Short: "Register a contract for governance sudo message updates",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

deployer := cliCtx.GetFromAddress()

contract := args[0]

msg := &types.MsgRegisterGovernance{
ContractAddress: contract,
RegisterAddress: deployer.String(),
}

if err := msg.ValidateBasic(); err != nil {
Expand Down
29 changes: 13 additions & 16 deletions x/cw-hooks/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,27 @@ func InitGenesis(
}

for _, v := range data.StakingContractAddresses {
if _, err := sdk.AccAddressFromBech32(v); err != nil {
if accAddr, err := sdk.AccAddressFromBech32(v); err != nil {
panic(err)
} else {
k.SetContract(ctx, types.KeyPrefixStaking, accAddr)
}

k.SetStakingContract(ctx, types.Contract{
ContractAddress: v,
})
}

// TODO: gov
for _, v := range data.GovContractAddresses {
if accAddr, err := sdk.AccAddressFromBech32(v); err != nil {
panic(err)
} else {
k.SetContract(ctx, types.KeyPrefixGov, accAddr)
}
}
}

// ExportGenesis export module state
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
stakingContracts := k.GetAllStakingContract(ctx)

return &types.GenesisState{
Params: k.GetParams(ctx),
StakingContractAddresses: func() []string {
addresses := make([]string, len(stakingContracts))
for i, v := range stakingContracts {
addresses[i] = v.GetContractAddress()
}
return addresses
}(),
Params: k.GetParams(ctx),
StakingContractAddresses: k.GetAllContractsBech32(ctx, types.KeyPrefixStaking),
GovContractAddresses: k.GetAllContractsBech32(ctx, types.KeyPrefixGov),
}
}
70 changes: 70 additions & 0 deletions x/cw-hooks/keeper/contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) SetContract(ctx sdk.Context, keyPrefix []byte, contractAddr sdk.AccAddress) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), keyPrefix)
store.Set(contractAddr, []byte{})
}

func (k Keeper) IsContractRegistered(ctx sdk.Context, keyPrefix []byte, contractAddr sdk.AccAddress) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), keyPrefix)
return store.Has(contractAddr)
}

func (k Keeper) IterateContracts(
ctx sdk.Context,
keyPrefix []byte,
handlerFn func(contractAddr []byte) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, keyPrefix)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
keyAddr := iterator.Key()
addr := sdk.AccAddress(keyAddr)

if handlerFn(addr) {
break
}
}
}

func (k Keeper) GetAllContracts(ctx sdk.Context, keyPrefix []byte) (list []sdk.Address) {
k.IterateContracts(ctx, keyPrefix, func(addr []byte) bool {
list = append(list, sdk.AccAddress(addr))
return false
})
return
}
func (k Keeper) GetAllContractsBech32(ctx sdk.Context, keyPrefix []byte) []string {
contracts := k.GetAllContracts(ctx, keyPrefix)

list := make([]string, 0, len(contracts))
for _, c := range contracts {
list = append(list, c.String())
}
return list
}

func (k Keeper) DeleteContract(ctx sdk.Context, keyPrefix []byte, contractAddr sdk.AccAddress) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), keyPrefix)
store.Delete(contractAddr)
}

func (k Keeper) ExecuteMessageOnContracts(ctx sdk.Context, keyPrefix []byte, msgBz []byte) error {
p := k.GetParams(ctx)

for _, cAddr := range k.GetAllContracts(ctx, keyPrefix) {
gasLimitCtx := ctx.WithGasMeter(sdk.NewGasMeter(p.ContractGasLimit))
if _, err := k.contractKeeper.Sudo(gasLimitCtx, cAddr.Bytes(), msgBz); err != nil {
return err
}
}

return nil
}
25 changes: 5 additions & 20 deletions x/cw-hooks/keeper/gov_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"strconv"

"github.com/CosmosContracts/juno/v17/x/cw-hooks/types"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
Expand Down Expand Up @@ -71,22 +72,6 @@ type SudoAfterProposalVotingPeriodEnded struct {
AfterProposalVotingPeriodEnded string `json:"after_proposal_voting_period_ended"`
}

// TODO: move this to the keeper, and either caLL it with "staking" or "gov" using module type names?
func (h GovHooks) sendMsgToAll(ctx sdk.Context, msgBz []byte) {
// on errors return nil, if in a loop continue.

// TODO: add this in the keeper, anyone can register it.
// iter all contracts here
contract, _ := sdk.AccAddressFromBech32("juno14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9skjuwg8")

// 100k/250k gas limit?
gasLimitCtx := ctx.WithGasMeter(sdk.NewGasMeter(100_000))
_, _ = h.k.contractKeeper.Sudo(gasLimitCtx, contract, msgBz)

// ctx.GasMeter().ConsumeGas(100_000, "cw-hooks: AfterValidatorCreated")
// return nil
}

func (h GovHooks) AfterProposalSubmission(ctx sdk.Context, proposalID uint64) {
prop, found := h.k.govKeeper.GetProposal(ctx, proposalID)
if !found {
Expand All @@ -100,7 +85,7 @@ func (h GovHooks) AfterProposalSubmission(ctx sdk.Context, proposalID uint64) {
return
}

h.sendMsgToAll(ctx, msgBz)
h.k.ExecuteMessageOnContracts(ctx, types.KeyPrefixGov, msgBz)
}

func (h GovHooks) AfterProposalDeposit(ctx sdk.Context, proposalID uint64, _ sdk.AccAddress) {
Expand All @@ -116,7 +101,7 @@ func (h GovHooks) AfterProposalDeposit(ctx sdk.Context, proposalID uint64, _ sdk
return
}

h.sendMsgToAll(ctx, msgBz)
h.k.ExecuteMessageOnContracts(ctx, types.KeyPrefixGov, msgBz)
}

func (h GovHooks) AfterProposalVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) {
Expand All @@ -132,7 +117,7 @@ func (h GovHooks) AfterProposalVote(ctx sdk.Context, proposalID uint64, voterAdd
return
}

h.sendMsgToAll(ctx, msgBz)
h.k.ExecuteMessageOnContracts(ctx, types.KeyPrefixGov, msgBz)
}

func (h GovHooks) AfterProposalFailedMinDeposit(_ sdk.Context, _ uint64) {
Expand All @@ -146,5 +131,5 @@ func (h GovHooks) AfterProposalVotingPeriodEnded(ctx sdk.Context, proposalID uin
return
}

h.sendMsgToAll(ctx, msgBz)
h.k.ExecuteMessageOnContracts(ctx, types.KeyPrefixGov, msgBz)
}
Loading

0 comments on commit cccc4e5

Please sign in to comment.