Skip to content

Commit

Permalink
Genesis draft (#1924)
Browse files Browse the repository at this point in the history
* Genesis draft

* revert evm params

* revert evm params -2

* working simple test

* linting

* addressing linter errors

* remove creating address for now
  • Loading branch information
dssei authored Nov 7, 2024
1 parent 169c150 commit 94dce54
Show file tree
Hide file tree
Showing 16 changed files with 723 additions and 170 deletions.
6 changes: 6 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ import (
"github.com/sei-protocol/sei-chain/utils"
"github.com/sei-protocol/sei-chain/utils/metrics"
"github.com/sei-protocol/sei-chain/wasmbinding"
ctkeeper "github.com/sei-protocol/sei-chain/x/confidentialtransfers/keeper"
cttypes "github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
epochmodule "github.com/sei-protocol/sei-chain/x/epoch"
epochmodulekeeper "github.com/sei-protocol/sei-chain/x/epoch/keeper"
epochmoduletypes "github.com/sei-protocol/sei-chain/x/epoch/types"
Expand Down Expand Up @@ -339,6 +341,8 @@ type App struct {
EpochKeeper epochmodulekeeper.Keeper

TokenFactoryKeeper tokenfactorykeeper.Keeper
// ConfidentialTransfers module is not live yet, but we add the keeper for testing
ConfidentialTransfersKeeper ctkeeper.Keeper

// mm is the module manager
mm *module.Manager
Expand Down Expand Up @@ -412,6 +416,8 @@ func New(
evmtypes.StoreKey, wasm.StoreKey,
epochmoduletypes.StoreKey,
tokenfactorytypes.StoreKey,
// ConfidentialTransfers module is not live yet, but we add the key for testing
cttypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey, evmtypes.TransientStoreKey)
Expand Down
5 changes: 5 additions & 0 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func (s *KeeperTestHelper) SetupTokenFactory() {
s.App.TokenFactoryKeeper.CreateModuleAccount(s.Ctx)
}

// SetupConfidentialTransfers sets up a token module account for the ConfidentialTransfersKeeper.
func (s *KeeperTestHelper) SetupConfidentialTransfers() {
s.App.ConfidentialTransfersKeeper.CreateModuleAccount(s.Ctx)
}

// EndBlock ends the block.
func (s *KeeperTestHelper) EndBlock() {
reqEndBlock := abci.RequestEndBlock{Height: s.Ctx.BlockHeight()}
Expand Down
18 changes: 16 additions & 2 deletions proto/confidentialtransfers/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ package seiprotocol.seichain.confidentialtransfers;

import "gogoproto/gogo.proto";
import "confidentialtransfers/params.proto";
import "confidentialtransfers/confidential.proto";

option go_package = "github.com/sei-protocol/sei-chain/x/confidentialtransfers/types";

// GenesisState defines the confidential module's genesis state.
// TODO: Implement this. This should probably look like an array of empty Account objects.
message GenesisState {
// params defines the paramaters of the module.
// params defines the parameters of the module.
Params params = 1 [ (gogoproto.nullable) = false ];

// accounts is an array of confidential transfer accounts
repeated GenesisCtAccount accounts = 2 [ (gogoproto.nullable) = false ];

// TODO: consider adding total supply
}

message GenesisCtAccount {

// account key
bytes key = 1;

// confidential transfer account
CtAccount account = 2 [ (gogoproto.nullable) = false ];
}
6 changes: 3 additions & 3 deletions proto/confidentialtransfers/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ service Msg {
rpc Transfer(MsgTransfer) returns (MsgTransferResponse);

// InitializeAccount defines a method for creating a new confidential transfers account for some denom.
rpc InitializeAccount(MsgInitializeAccount) returns (MsgInitializeAccount);
rpc InitializeAccount(MsgInitializeAccount) returns (MsgInitializeAccountResponse);

// Deposit defines a method for depositing funds into a confidential transfers account.
rpc Deposit(MsgDeposit) returns (MsgDeposit);
rpc Deposit(MsgDeposit) returns (MsgDepositResponse);

// Withdraw defines a method for withdrawing funds from a confidential transfers account.
rpc Withdraw(MsgWithdraw) returns (MsgWithdraw);
rpc Withdraw(MsgWithdraw) returns (MsgWithdrawResponse);

// ApplyPendingBalance defines a method for applying pending balance to an account.
rpc ApplyPendingBalance(MsgApplyPendingBalance) returns (MsgApplyPendingBalanceResponse);
Expand Down
54 changes: 54 additions & 0 deletions x/confidentialtransfers/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package keeper

import (
"fmt"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
)

func (k BaseKeeper) InitGenesis(ctx sdk.Context, gs *types.GenesisState) {
k.SetParams(ctx, gs.Params)
for i := range gs.Accounts {
genesisCtAccount := gs.Accounts[i]
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&genesisCtAccount.Account) // Marshal the Account object into bytes
store.Set(genesisCtAccount.Key, bz)
}
}

func (k BaseKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
genesisCtAccounts, _, err := k.GetPaginatedAccounts(ctx, &query.PageRequest{Limit: query.MaxLimit})
if err != nil {
panic(fmt.Errorf("failed to fetch genesis ct accounts: %w", err))
}
return types.NewGenesisState(
k.GetParams(ctx),
genesisCtAccounts,
)
}

func (k BaseKeeper) GetPaginatedAccounts(ctx sdk.Context, pagination *query.PageRequest) ([]types.GenesisCtAccount, *query.PageResponse, error) {
store := ctx.KVStore(k.storeKey)
supplyStore := prefix.NewStore(store, types.AccountsKey)

genesisAccounts := make([]types.GenesisCtAccount, 0)
pageRes, err := query.Paginate(supplyStore, pagination, func(key, value []byte) error {
var ctAccount types.CtAccount
err := ctAccount.Unmarshal(value)
if err != nil {
return err
}

genesisAccounts = append(genesisAccounts, types.GenesisCtAccount{Key: key, Account: ctAccount})
return nil
})

if err != nil {
return nil, nil, err
}

return genesisAccounts, pageRes, nil
}
18 changes: 18 additions & 0 deletions x/confidentialtransfers/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package keeper_test

import (
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

func (suite *KeeperTestSuite) TestDefaultGenesisState() {
genesisState := types.DefaultGenesisState()

app := suite.App
suite.Ctx = app.BaseApp.NewContext(false, tmproto.Header{})

suite.App.ConfidentialTransfersKeeper.InitGenesis(suite.Ctx, genesisState)
exportedGenesis := suite.App.ConfidentialTransfersKeeper.ExportGenesis(suite.Ctx)
suite.Require().NotNil(exportedGenesis)
suite.Require().Equal(genesisState, exportedGenesis)
}
71 changes: 53 additions & 18 deletions x/confidentialtransfers/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/store/prefix"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"

"github.com/tendermint/tendermint/libs/log"
Expand All @@ -15,19 +17,29 @@ import (
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

// TODO: This is just scaffolding. To be implemented.
type (
Keeper struct {
storeKey sdk.StoreKey
type Keeper interface {
InitGenesis(sdk.Context, *types.GenesisState)
ExportGenesis(sdk.Context) *types.GenesisState

cdc codec.Codec
GetAccount(ctx sdk.Context, address sdk.AccAddress, denom string) (types.Account, bool)
SetAccount(ctx sdk.Context, address sdk.AccAddress, denom string, account types.Account)

// TODO: Add any required keepers here
// accountKeeper types.AccountKeeper
}
)
GetParams(ctx sdk.Context) types.Params
SetParams(ctx sdk.Context, params types.Params)

CreateModuleAccount(ctx sdk.Context)
}

type BaseKeeper struct {
storeKey sdk.StoreKey

cdc codec.Codec

paramSpace paramtypes.Subspace
accountKeeper types.AccountKeeper
}

func (k Keeper) TestQuery(ctx context.Context, request *types.TestQueryRequest) (*types.TestQueryResponse, error) {
func (k BaseKeeper) TestQuery(ctx context.Context, request *types.TestQueryRequest) (*types.TestQueryResponse, error) {
//TODO: This is not a real gRPC query. This was added to the query.proto file as a placeholder. We should remove this and add the real queries once we better define query.proto.
panic("implement me")
}
Expand All @@ -37,14 +49,22 @@ func NewKeeper(
codec codec.Codec,
storeKey sdk.StoreKey,
paramSpace paramtypes.Subspace,
accountKeeper types.AccountKeeper,
) Keeper {
return Keeper{
cdc: codec,
storeKey: storeKey,

if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return BaseKeeper{
cdc: codec,
storeKey: storeKey,
accountKeeper: accountKeeper,
paramSpace: paramSpace,
}
}

func (k Keeper) GetAccount(ctx sdk.Context, address sdk.AccAddress, denom string) (types.Account, bool) {
func (k BaseKeeper) GetAccount(ctx sdk.Context, address sdk.AccAddress, denom string) (types.Account, bool) {
store := ctx.KVStore(k.storeKey)
key := types.GetAccountKey(address, denom)
if !store.Has(key) {
Expand All @@ -61,22 +81,22 @@ func (k Keeper) GetAccount(ctx sdk.Context, address sdk.AccAddress, denom string
return *account, true
}

func (k Keeper) SetAccount(ctx sdk.Context, address sdk.AccAddress, denom string, account types.Account) {
func (k BaseKeeper) SetAccount(ctx sdk.Context, address sdk.AccAddress, denom string, account types.Account) {
store := ctx.KVStore(k.storeKey)
key := types.GetAccountKey(address, denom)
ctAccount := account.ToProto()
ctAccount := types.NewCtAccount(&account)
bz := k.cdc.MustMarshal(ctAccount) // Marshal the Account object into bytes
store.Set(key, bz) // Store the serialized account under the key
}

// Logger returns a logger for the x/confidentialtransfers module
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
func (k BaseKeeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

// GetAccountsForAddress iterates over all accounts associated with a given address
// and returns a mapping of denom:account
func (k Keeper) GetAccountsForAddress(ctx sdk.Context, address sdk.AccAddress) (map[string]*types.Account, error) {
func (k BaseKeeper) GetAccountsForAddress(ctx sdk.Context, address sdk.AccAddress) (map[string]*types.Account, error) {
// Create a prefix store scoped to the address
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.GetAddressPrefix(address))

Expand All @@ -103,3 +123,18 @@ func (k Keeper) GetAccountsForAddress(ctx sdk.Context, address sdk.AccAddress) (

return accounts, nil
}

func (k BaseKeeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSpace.GetParamSet(ctx, &params)
return params
}

// SetParams sets the total set of bank parameters.
func (k BaseKeeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}

func (k BaseKeeper) CreateModuleAccount(ctx sdk.Context) {
moduleAcc := authtypes.NewEmptyModuleAccount(types.ModuleName, authtypes.Minter, authtypes.Burner)
k.accountKeeper.SetModuleAccount(ctx, moduleAcc)
}
35 changes: 35 additions & 0 deletions x/confidentialtransfers/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package keeper_test

import (
"github.com/sei-protocol/sei-chain/app/apptesting"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/keeper"
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
"github.com/stretchr/testify/suite"
"testing"
)

type KeeperTestSuite struct {
apptesting.KeeperTestHelper

queryClient types.QueryClient
msgServer types.MsgServer
// defaultDenom is on the suite, as it depends on the creator test address.
defaultDenom string
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func (suite *KeeperTestSuite) SetupTest() {
suite.Setup()

suite.queryClient = types.NewQueryClient(suite.QueryHelper)
// TODO: remove this once the app initializes confidentialtransfers keeper
suite.App.ConfidentialTransfersKeeper = keeper.NewKeeper(
suite.App.AppCodec(),
suite.App.GetKey(types.StoreKey),
suite.App.GetSubspace(types.ModuleName),
suite.App.AccountKeeper)
suite.msgServer = keeper.NewMsgServerImpl(suite.App.ConfidentialTransfersKeeper)
}
38 changes: 36 additions & 2 deletions x/confidentialtransfers/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
package keeper

import "github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
import (
"context"

"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
)

type msgServer struct {
Keeper
}

func (m msgServer) Transfer(ctx context.Context, transfer *types.MsgTransfer) (*types.MsgTransferResponse, error) {
//TODO implement me
panic("implement me")
}

func (m msgServer) InitializeAccount(ctx context.Context, account *types.MsgInitializeAccount) (*types.MsgInitializeAccountResponse, error) {
//TODO implement me
panic("implement me")
}

func (m msgServer) Deposit(ctx context.Context, deposit *types.MsgDeposit) (*types.MsgDepositResponse, error) {
//TODO implement me
panic("implement me")
}

func (m msgServer) Withdraw(ctx context.Context, withdraw *types.MsgWithdraw) (*types.MsgWithdrawResponse, error) {
//TODO implement me
panic("implement me")
}

func (m msgServer) ApplyPendingBalance(ctx context.Context, balance *types.MsgApplyPendingBalance) (*types.MsgApplyPendingBalanceResponse, error) {
//TODO implement me
panic("implement me")
}

func (m msgServer) CloseAccount(ctx context.Context, account *types.MsgCloseAccount) (*types.MsgCloseAccountResponse, error) {
//TODO implement me
panic("implement me")
}

// NewMsgServerImpl returns an implementation of the MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
return msgServer{keeper}
}

var _ types.MsgServer = msgServer{}
Loading

0 comments on commit 94dce54

Please sign in to comment.