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

Addressed a few TODOs in auth #2

Merged
merged 8 commits into from
Jan 9, 2024
1 change: 1 addition & 0 deletions proto/cosmos/auth/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ message QueryAccountRequest {
option (gogoproto.goproto_getters) = false;

// address defines the address to query for.
// TODO HV2 change it to HeimdallAddress
string address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

Expand Down
1 change: 0 additions & 1 deletion types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type AccountI interface {
GetAddress() HeimdallAddress
SetAddress(HeimdallAddress) error // errors if already set.

// TODO HV2 is this key what we want to use?
GetPubKey() cryptotypes.PubKey // can return nil.
SetPubKey(cryptotypes.PubKey) error

Expand Down
7 changes: 6 additions & 1 deletion x/auth/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ type HandlerOptions struct {
// NewAnteHandler returns an AnteHandler that checks and increments sequence
// numbers, checks signatures & account numbers, and deducts fees from the first
// signer.
// TODO HV2 is this enough to reconcile with heimdall's auth/ante.go? (

// TODO HV2 is this enough to reconcile with heimdall's auth/ante.go?
// ^ we will nedd to add the following in `HandlerOptions` as NewAnteHandler() in heimdall is using them:
// - chainKeeper chainmanager.Keeper
// - contractCaller helper.IContractCaller
// - sigGasConsumer SignatureVerificationGasConsumer
func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.AccountKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "account keeper is required for ante builder")
Expand Down
6 changes: 4 additions & 2 deletions x/auth/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ func (ak AccountKeeper) InitGenesis(ctx sdk.Context, data types.GenesisState, pr
n := ak.NextAccountNumber(ctx)
lastAccNum = &n
}

baseAcc := types.NewBaseAccount(acc.GetAddress(), acc.GetPubKey(), accNum, acc.GetSequence())

// execute account processors
for _, p := range processors {
// TODO HV2 fill processors here (check heimdall's auth/genesis.go)
// acc = p(&gacc, d) //nolint
acc = p(&acc, baseAcc) //nolint
}
ak.SetAccount(ctx, acc)
}
Expand Down
29 changes: 29 additions & 0 deletions x/auth/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (s queryServer) Account(ctx context.Context, req *types.QueryAccountRequest
return nil, status.Error(codes.InvalidArgument, "Address cannot be empty")
}

// PSP - TODO HV2 - we might need wo change this to use heimdall address
addr, err := s.k.addressCodec.StringToBytes(req.Address)
if err != nil {
return nil, err
Expand All @@ -88,6 +89,25 @@ func (s queryServer) Account(ctx context.Context, req *types.QueryAccountRequest
return &types.QueryAccountResponse{Account: any}, nil
}

// func queryAccount(ctx sdk.Context, req abci.RequestQuery, keeper AccountKeeper) ([]byte, sdk.Error) {
// var params types.QueryAccountParams
// if err := keeper.cdc.UnmarshalJSON(req.Data, &params); err != nil {
// return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
// }

// account := keeper.GetAccount(ctx, params.Address)
// if account == nil {
// return nil, sdk.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", params.Address))
// }

// bz, err := codec.MarshalJSONIndent(keeper.cdc, account)
// if err != nil {
// return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
// }

// return bz, nil
// }

// Params returns parameters of auth module
func (s queryServer) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
Expand All @@ -99,6 +119,15 @@ func (s queryServer) Params(c context.Context, req *types.QueryParamsRequest) (*
return &types.QueryParamsResponse{Params: params}, nil
}

// func queryParams(ctx sdk.Context, _ abci.RequestQuery, keeper AccountKeeper) ([]byte, sdk.Error) {
// bz, err := jsoniter.ConfigFastest.Marshal(keeper.GetParams(ctx))
// if err != nil {
// return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
// }

// return bz, nil
// }

// ModuleAccounts returns all the existing Module Accounts
func (s queryServer) ModuleAccounts(c context.Context, req *types.QueryModuleAccountsRequest) (*types.QueryModuleAccountsResponse, error) {
if req == nil {
Expand Down
24 changes: 5 additions & 19 deletions x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type AccountKeeper struct {
storeService store.KVStoreService
cdc codec.BinaryCodec
permAddrs map[string]types.PermissionsForAddress
// TODO HV2 bech32Prefix?
// TODO HV2 bech32Prefix? Keeping this as it is for now. Check if it is interfering with heimdall address
bech32Prefix string

// The prototypical AccountI constructor.
Expand Down Expand Up @@ -281,37 +281,23 @@ func (ak AccountKeeper) GetParams(ctx context.Context) (params types.Params) {

// GetBlockProposer returns block proposer
func (ak AccountKeeper) GetBlockProposer(ctx sdk.Context) (sdk.HeimdallAddress, bool) {
// TODO HV2 are these implementations equivalent for GetBlockProposer
kvStore := ak.storeService.OpenKVStore(ctx)
isProposerPresent, _ := kvStore.Has(types.ProposerKey()) // TODO HV2 handle error?
isProposerPresent, _ := kvStore.Has(types.ProposerKey())
if !isProposerPresent {
return sdk.HeimdallAddress{}, false
}
blockProposerBytes, _ := kvStore.Get(types.ProposerKey()) // TODO HV2 handle error?
blockProposerBytes, _ := kvStore.Get(types.ProposerKey())
return sdk.BytesToHeimdallAddress(blockProposerBytes), true

//store := ctx.KVStore(ak.key)
//if !store.Has(types.ProposerKey()) {
// return hmTypes.HeimdallAddress{}, false
//}
//bz := store.Get(types.ProposerKey())
//return hmTypes.BytesToHeimdallAddress(bz), true
}

// SetBlockProposer sets block proposer
func (ak AccountKeeper) SetBlockProposer(ctx sdk.Context, addr sdk.HeimdallAddress) {
// TODO HV2 are these implementations equivalent for SetBlockProposer
kvStore := ak.storeService.OpenKVStore(ctx)
kvStore.Set(types.ProposerKey(), addr.Bytes()) // TODO HV2 handle error?
//store := ctx.KVStore(ak.key)
//store.Set(types.ProposerKey(), addr.Bytes())
kvStore.Set(types.ProposerKey(), addr.Bytes())
}

// RemoveBlockProposer removes block proposer from store
func (ak AccountKeeper) RemoveBlockProposer(ctx sdk.Context) {
// TODO HV2 are these implementations equivalent for RemoveBlockProposer
kvStore := ak.storeService.OpenKVStore(ctx)
kvStore.Delete(types.ProposerKey()) // TODO HV2 handle error?
//store := ctx.KVStore(ak.key)
//store.Delete(types.ProposerKey())
kvStore.Delete(types.ProposerKey())
}
18 changes: 2 additions & 16 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/maticnetwork/heimdall/helper"

modulev1 "cosmossdk.io/api/cosmos/auth/module/v1"
"cosmossdk.io/core/address"
Expand Down Expand Up @@ -93,7 +94,6 @@ type AppModule struct {
// legacySubspace is used solely for migration of x/params managed parameters
legacySubspace exported.Subspace

// TODO HV2 check contractCaller and processors in this whole file
contractCaller helper.IContractCaller
processors []types.AccountProcessor
}
Expand Down Expand Up @@ -203,19 +203,13 @@ type ModuleInputs struct {

// LegacySubspace is used solely for migration of x/params managed parameters
LegacySubspace exported.Subspace `optional:"true"`

// TODO HV2 is this depinject needed?
contractCaller helper.IContractCaller
processors []types.AccountProcessor
}

type ModuleOutputs struct {
depinject.Out

AccountKeeper keeper.AccountKeeper
Module appmodule.AppModule

// TODO HV2 check contractCaller and processors in this whole file
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
Expand All @@ -239,16 +233,8 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.AccountI = types.ProtoBaseAccount
}

if in.contractCaller == nil {
in.contractCaller = helper.IContractCaller
}

if in.processors == nil {
in.processors = []types.AccountProcessor{}
}

k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, authority.String())
m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn, in.LegacySubspace, in.contractCaller, in.processors)
m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn, in.LegacySubspace, nil, nil)

return ModuleOutputs{AccountKeeper: k, Module: m}
}
2 changes: 1 addition & 1 deletion x/auth/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

// // TODO HV2 inject heimdall app
func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {
t.Skip() // skipping as we are not using depinject
var accountKeeper keeper.AccountKeeper
app, err := simtestutil.SetupAtGenesis(
depinject.Configs(
Expand Down
30 changes: 2 additions & 28 deletions x/auth/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package types

import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/secp256k1"
"strings"

"github.com/cometbft/cometbft/crypto"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -116,29 +115,6 @@ func (acc *BaseAccount) SetSequence(seq uint64) error {
return nil
}

// TODO HV2 It's should not be needed, but verify (proto implements it in `auth.pb.go`)
// String implements fmt.Stringer
func (acc BaseAccount) String() string {
var pubkey string

if acc.PubKey != nil {
// pubkey = sdk.MustBech32ifyAccPub(acc.PubKey)

var pubObject secp256k1.PubKey

cdc.MustUnmarshalBinaryBare(acc.PubKey.Bytes(), &pubObject)

pubkey = "0x" + hex.EncodeToString(pubObject[:])
}

return fmt.Sprintf(`Account:
Address: %s
Pubkey: %s
AccountNumber: %d
Sequence: %d`,
acc.Address, pubkey, acc.AccountNumber, acc.Sequence)
}

// Validate checks for errors on the account fields
func (acc BaseAccount) Validate() error {
if acc.Address == "" || acc.PubKey == nil {
Expand Down Expand Up @@ -332,13 +308,11 @@ func (ga GenesisAccounts) Contains(addr sdk.HeimdallAddress) bool {
}

// GenesisAccount defines a genesis account that embeds an AccountI with validation capabilities.
// TODO HV2 genesis account is different
type GenesisAccount interface {
sdk.AccountI

Validate() error
}

// TODO HV2 imported from heimdall. Needed?
// AccountProcessor is an interface to process account as per module
type AccountProcessor func(*GenesisAccount, *BaseAccount) sdk.AccountI
Loading