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

wip: contract execution #1

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 11 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,15 @@ var (

// module account permissions
maccPerms = map[string][]string{
authtypes.FeeCollectorName: nil,
distrtypes.ModuleName: nil,
icatypes.ModuleName: nil,
minttypes.ModuleName: {authtypes.Minter},
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
authtypes.FeeCollectorName: nil,
distrtypes.ModuleName: nil,
icatypes.ModuleName: nil,
minttypes.ModuleName: {authtypes.Minter},
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
cosmwasmlifecyclemoduletypes.ModuleName: {authtypes.Burner},
// this line is used by starport scaffolding # stargate/app/maccPerms
}
)
Expand Down Expand Up @@ -557,8 +558,8 @@ func New(
app.CosmwasmlifecycleKeeper = *cosmwasmlifecyclemodulekeeper.NewKeeper(
appCodec,
keys[cosmwasmlifecyclemoduletypes.StoreKey],
keys[cosmwasmlifecyclemoduletypes.MemStoreKey],
app.GetSubspace(cosmwasmlifecyclemoduletypes.ModuleName),
app.WasmKeeper,
app.BankKeeper,
)
cosmwasmlifecycleModule := cosmwasmlifecyclemodule.NewAppModule(appCodec, app.CosmwasmlifecycleKeeper, app.WasmKeeper)

Expand Down
Binary file added docs/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ go 1.19

require (
cosmossdk.io/api v0.3.1
cosmossdk.io/errors v1.0.0
github.com/CosmWasm/wasmd v0.42.0
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.5
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.3.0
Expand All @@ -32,7 +34,6 @@ require (
cloud.google.com/go/storage v1.30.1 // indirect
cosmossdk.io/core v0.5.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/errors v1.0.0 // indirect
cosmossdk.io/log v1.2.1 // indirect
cosmossdk.io/math v1.1.2 // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
Expand All @@ -58,7 +59,6 @@ require (
github.com/coinbase/rosetta-sdk-go/types v1.0.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/iavl v0.20.1 // indirect
Expand Down
24 changes: 24 additions & 0 deletions proto/cosmwasmlifecycle/contract.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
syntax = "proto3";
package cosmwasmlifecycle;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmwasmlifecycle/execution_type.proto";

option go_package = "github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/types";

// Contract defines the parameters for the module.
message Contract {
// Amount of strikes that the contract has at the moment.
int64 strikes = 1;

// Contract's execution type
ExecutionType execution_type = 2;

// Collateral deposited to the contract.
cosmos.base.v1beta1.Coin deposit = 3 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin"
];
}
12 changes: 0 additions & 12 deletions proto/cosmwasmlifecycle/cosmwasmlifecycle/genesis.proto

This file was deleted.

12 changes: 0 additions & 12 deletions proto/cosmwasmlifecycle/cosmwasmlifecycle/params.proto

This file was deleted.

7 changes: 0 additions & 7 deletions proto/cosmwasmlifecycle/cosmwasmlifecycle/tx.proto

This file was deleted.

17 changes: 17 additions & 0 deletions proto/cosmwasmlifecycle/events.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";
package cosmwasmlifecycle;

import "cosmos_proto/cosmos.proto";
option go_package = "github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/types";

message ContractDeleteEvent {
string module_name = 1;
string contract_address = 2 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message ContractStrikeEvent {
string module_name = 1;
string contract_address = 2 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
int64 current_strike = 3;
string strike_reason = 4;
}
10 changes: 10 additions & 0 deletions proto/cosmwasmlifecycle/execution_type.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";
package cosmwasmlifecycle;

option go_package = "github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/types";

enum ExecutionType {
BEGIN_AND_END_BLOCK = 0;
BEGIN_BLOCK = 1;
END_BLOCK = 2;
}
18 changes: 18 additions & 0 deletions proto/cosmwasmlifecycle/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";
package cosmwasmlifecycle;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "cosmwasmlifecycle/params.proto";
import "cosmwasmlifecycle/contract.proto";

option go_package = "github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/types";

// GenesisState defines the cosmwasmlifecycle module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];

// Contracts with their current information
// about the execution state.
repeated Contract contracts = 2 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}
27 changes: 27 additions & 0 deletions proto/cosmwasmlifecycle/params.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
syntax = "proto3";
package cosmwasmlifecycle;

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/types";

// Params defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;
// Enable or disable executions
// at the begin and end of the block
bool is_enabled = 1;

// Minimum deposit to enable contract execution at begin block and/or end block
// This deposit will be burned if the contract execution reaches the max strikes.
cosmos.base.v1beta1.Coin min_deposit = 2 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin"
];

// Amount of strikes that the contract can hold before being
// disabled from end and beging block executions.
// When this happens the min_desposit will also be burned.
int64 strikes_to_disable_execution = 3;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
syntax = "proto3";
package cosmwasmlifecycle.cosmwasmlifecycle;
package cosmwasmlifecycle;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "cosmwasmlifecycle/cosmwasmlifecycle/params.proto";
import "cosmwasmlifecycle/params.proto";

option go_package = "github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/types";

// Query defines the gRPC querier service.
service Query {
// Parameters queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/emidev98/cosmwasm-lifecycle/cosmwasmlifecycle/params";
option (google.api.http).get = "/cosmwasmlifecycle/params";
}
}

Expand Down
31 changes: 31 additions & 0 deletions proto/cosmwasmlifecycle/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
syntax = "proto3";
package cosmwasmlifecycle;
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "cosmwasmlifecycle/execution_type.proto";

option go_package = "github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/types";

// Msg defines the Msg service.
service Msg {
rpc EnableContractBlockExecution(MsgEnableContractBlockExecution) returns (MsgEnableContractBlockExecutionResponse);
rpc DisableContractBlockExecution(MsgDisableContractBlockExecution) returns (MsgDisableContractBlockExecutionResponse);
}

message MsgEnableContractBlockExecution {
option (gogoproto.equal) = true;
option (gogoproto.goproto_getters) = true;

string contract_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
ExecutionType execution = 2;
}
message MsgEnableContractBlockExecutionResponse {}

message MsgDisableContractBlockExecution {
option (gogoproto.equal) = true;
option (gogoproto.goproto_getters) = true;

string contract_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
ExecutionType execution = 2;
}
message MsgDisableContractBlockExecutionResponse {}
14 changes: 13 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# CosmWasm Lifecycle
<center>
<h1>CosmWasm Lifecycle Module</h1>
</center>

![IMG](./docs/logo.jpg)

CosmWasm Lifecycle blockchain module leverages [CosmoSDK's lifecycle](https://docs.cosmos.network/main/build/building-modules/beginblock-endblock) to facilitate the execution of smart contracts at the initiation and conclusion of each block. Given the necessity for swift and resource-light execution in both stages, this module mandates [Gov](https://docs.cosmos.network/main/build/modules/gov) voting and demands a collateral deposit for each smart contract on an individual basis. This collateral deposit will be burned if the smart contract fails to execute multiple times.






11 changes: 2 additions & 9 deletions testutil/keeper/cosmwasmlifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/keeper"
"github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/types"
"github.com/stretchr/testify/require"
Expand All @@ -30,17 +29,11 @@ func CosmwasmlifecycleKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

paramsSubspace := typesparams.NewSubspace(cdc,
types.Amino,
storeKey,
memStoreKey,
"CosmwasmlifecycleParams",
)
k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
paramsSubspace,
wasmKeeper,
bankKeeper,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
23 changes: 0 additions & 23 deletions x/cosmwasmlifecycle/genesis.go

This file was deleted.

29 changes: 0 additions & 29 deletions x/cosmwasmlifecycle/genesis_test.go

This file was deleted.

45 changes: 45 additions & 0 deletions x/cosmwasmlifecycle/keeper/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/emidev98/cosmwasm-lifecycle/x/cosmwasmlifecycle/types"
)

func (k Keeper) GetContract(ctx sdk.Context, contractAddr sdk.AccAddress) (d types.Contract, found bool) {
key := types.GetContractKeyByAddress(contractAddr)
b := ctx.KVStore(k.storeKey).Get(key)
if b == nil {
return d, false
}
k.cdc.MustUnmarshal(b, &d)
return d, true
}

func (k Keeper) SetContract(ctx sdk.Context, contractAddr sdk.AccAddress, contract types.Contract) {
key := types.GetContractKeyByAddress(contractAddr)
b := k.cdc.MustMarshal(&contract)
ctx.KVStore(k.storeKey).Set(key, b)
}

func (k Keeper) DeleteContract(ctx sdk.Context, contractAddr sdk.AccAddress) {
key := types.GetContractKeyByAddress(contractAddr)
store := ctx.KVStore(k.storeKey)
store.Delete(key)
}

func (k Keeper) IterateContracts(ctx sdk.Context, cb func(contractAddr sdk.AccAddress, contract types.Contract) error) (err error) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.ContractKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var contract types.Contract
b := iter.Value()
k.cdc.MustUnmarshal(b, &contract)
err = cb(iter.Key(), contract)
if err != nil {
return err
}
}

return nil
}
Loading
Loading