Skip to content

Commit a4f6732

Browse files
author
Tanmay
committed
multiple admin
1 parent d47f0ce commit a4f6732

File tree

20 files changed

+285
-143
lines changed

20 files changed

+285
-143
lines changed

app/app.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,11 @@ func NewSifApp(
500500
evidencetypes.ModuleName,
501501
ibctransfertypes.ModuleName,
502502
feegrant.ModuleName,
503+
tokenregistrytypes.ModuleName,
503504
clptypes.ModuleName,
504505
oracletypes.ModuleName,
505506
ethbridge.ModuleName,
506507
dispensation.ModuleName,
507-
tokenregistry.ModuleName,
508508
)
509509
app.mm.RegisterInvariants(&app.CrisisKeeper)
510510
app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino)

cmd/sifnoded/cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
7979
AddGenesisAccountCmd(app.DefaultNodeHome),
8080
AddGenesisCLPAdminCmd(app.DefaultNodeHome),
8181
SetGenesisDenomWhitelist(app.DefaultNodeHome),
82-
SetGenesisWhitelisterAdminCmd(app.DefaultNodeHome),
82+
//SetGenesisWhitelisterAdminCmd(app.DefaultNodeHome),
8383
AddGenesisValidatorCmd(app.DefaultNodeHome),
8484
SetGenesisOracleAdminCmd(app.DefaultNodeHome),
8585
tmcli.NewCompletionCmd(rootCmd, true),

cmd/sifnoded/cmd/setwhitelistadmin.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
func SetGenesisWhitelisterAdminCmd(defaultNodeHome string) *cobra.Command {
2121
cmd := &cobra.Command{
22-
Use: "set-genesis-whitelister-admin [address_or_key_name]",
22+
Use: "set-genesis-whitelist-admin [address_or_key_name]",
2323
Short: "Add a genesis account to genesis.json that has permission to edit token whitelist.",
2424
Long: `Add a genesis account to genesis.json. The provided account must specify
2525
the account address or key name. If a key name is given,
@@ -56,7 +56,17 @@ the address will be looked up in the local Keybase.
5656
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
5757
}
5858
state := tokenregistrytypes.UnmarshalGenesis(cdc, appState[tokenregistrytypes.ModuleName])
59-
state.AdminAccount = addr.String()
59+
fmt.Println(addr)
60+
a := []*tokenregistrytypes.AdminAccount{
61+
{
62+
AdminType: tokenregistrytypes.AdminType_TOKENREGISTRY,
63+
AdminAddress: addr.String(),
64+
},
65+
}
66+
if state.AdminAccounts.AdminAccounts != nil {
67+
a = append(a, state.AdminAccounts.AdminAccounts...)
68+
}
69+
state.AdminAccounts.AdminAccounts = a
6070
stateBz, err := json.Marshal(state)
6171
if err != nil {
6272
return fmt.Errorf("failed to marshal auth genesis state: %w", err)

proto/sifnode/tokenregistry/v1/types.proto

+10-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ message RegistryEntry {
5353
string ibc_counterparty_chain_id = 18;
5454
}
5555

56+
57+
enum AdminType {
58+
CLPDEX = 0;
59+
PMTPREWARDS = 1;
60+
TOKENREGISTRY = 2;
61+
ETHBRIDGE = 3;
62+
}
63+
5664
message AdminAccount {
57-
string moduleName = 1;
58-
string address = 2;
65+
AdminType admin_type = 1;
66+
string admin_address = 2;
5967
}
6068

6169
message AdminAccounts { repeated AdminAccount admin_accounts = 1; }

scripts/demos/clp/clp.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sifnoded query clp pool ceth
1616

1717
echo "Query Liquidity Provider / Pool creator is the first lp for the pool"
1818
sleep 8
19-
sifnoded query clp lp ceth $(sifnoded keys show sif -a)
19+
sifnoded query clp lp ceth $(sifnoded keys show sif -a )
2020

2121
echo "adding more liquidity"
2222
sleep 8

scripts/init.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ sifnoded add-genesis-clp-admin $(sifnoded keys show akasha -a --keyring-backend=
2424
sifnoded set-genesis-oracle-admin sif --keyring-backend=test
2525
sifnoded add-genesis-validators $(sifnoded keys show sif -a --bech val --keyring-backend=test) --keyring-backend=test
2626

27-
sifnoded set-genesis-whitelister-admin sif --keyring-backend=test
27+
#sifnoded set-genesis-whitelist-admin sif --keyring-backend=test
2828
sifnoded set-gen-denom-whitelist scripts/denoms.json
2929

3030
sifnoded gentx sif 1000000000000000000000000stake --chain-id=localnet --keyring-backend=test

tools/sifgen/common/types/genesis.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"encoding/json"
5+
"github.com/Sifchain/sifnode/x/tokenregistry/types"
56
"time"
67
)
78

@@ -63,8 +64,39 @@ type Crisis struct {
6364
ConstantFee ConstantFee `json:"constant_fee"`
6465
}
6566

67+
type AdminAccount struct {
68+
AdminType types.AdminType `json:"admin_type"`
69+
AdminAddress string `json:"admin_address"`
70+
}
71+
type AdminAccounts struct {
72+
AdminAccounts []*AdminAccount `json:"admin_accounts"`
73+
}
74+
75+
type Registry struct {
76+
Entries []*RegistryEntry
77+
}
78+
79+
type RegistryEntry struct {
80+
Decimals int64
81+
Denom string
82+
BaseDenom string
83+
Path string
84+
IbcChannelId string
85+
IbcCounterpartyChannelId string
86+
DisplayName string
87+
DisplaySymbol string
88+
Network string
89+
Address string
90+
ExternalSymbol string
91+
TransferLimit string
92+
Permissions []types.Permission
93+
UnitDenom string
94+
IbcCounterpartyDenom string
95+
IbcCounterpartyChainId string
96+
}
6697
type TokenRegistry struct {
67-
AdminAccount string `json:"admin_account"`
98+
AdminAccount AdminAccounts `json:"admin_account"`
99+
Registry Registry `json:"registry"`
68100
}
69101

70102
type ConstantFee struct {

x/clp/keeper/msg_server.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,36 @@ type msgServer struct {
2323
}
2424

2525
func (k msgServer) UpdateRewardsParams(goCtx context.Context, msg *types.MsgUpdateRewardsParamsRequest) (*types.MsgUpdateRewardsParamsResponse, error) {
26+
response := &types.MsgUpdateRewardsParamsResponse{}
2627
ctx := sdk.UnwrapSDKContext(goCtx)
2728
signer, err := sdk.AccAddressFromBech32(msg.Signer)
2829
if err != nil {
29-
return nil, err
30+
return response, err
3031
}
31-
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, types.ModuleName, signer) {
32-
return nil, errors.Wrap(types.ErrNotEnoughPermissions, fmt.Sprintf("Sending Account : %s", msg.Signer))
32+
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, tokenregistrytypes.AdminType_PMTPREWARDS, signer) {
33+
return response, errors.Wrap(types.ErrNotEnoughPermissions, fmt.Sprintf("Sending Account : %s", msg.Signer))
3334
}
3435
params := k.GetRewardsParams(ctx)
3536
params.LiquidityRemovalLockPeriod = msg.LiquidityRemovalLockPeriod
3637
params.LiquidityRemovalCancelPeriod = msg.LiquidityRemovalCancelPeriod
3738
k.SetRewardParams(ctx, params)
38-
return &types.MsgUpdateRewardsParamsResponse{}, err
39+
return response, err
3940
}
4041

4142
func (k msgServer) AddRewardPeriod(goCtx context.Context, msg *types.MsgAddRewardPeriodRequest) (*types.MsgAddRewardPeriodResponse, error) {
43+
response := &types.MsgAddRewardPeriodResponse{}
4244
ctx := sdk.UnwrapSDKContext(goCtx)
4345
signer, err := sdk.AccAddressFromBech32(msg.Signer)
4446
if err != nil {
45-
return nil, err
47+
return response, err
4648
}
47-
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, types.ModuleName, signer) {
48-
return nil, errors.Wrap(types.ErrNotEnoughPermissions, fmt.Sprintf("Sending Account : %s", msg.Signer))
49+
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, tokenregistrytypes.AdminType_PMTPREWARDS, signer) {
50+
return response, errors.Wrap(types.ErrNotEnoughPermissions, fmt.Sprintf("Sending Account : %s", msg.Signer))
4951
}
5052
params := k.GetRewardsParams(ctx)
5153
params.RewardPeriods = msg.RewardPeriods
5254
k.SetRewardParams(ctx, params)
53-
return &types.MsgAddRewardPeriodResponse{}, nil
55+
return response, nil
5456
}
5557

5658
// NewMsgServerImpl returns an implementation of the clp MsgServer interface
@@ -68,7 +70,7 @@ func (k msgServer) UpdatePmtpParams(goCtx context.Context, msg *types.MsgUpdateP
6870
if err != nil {
6971
return response, err
7072
}
71-
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, types.ModuleName, signer) {
73+
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, tokenregistrytypes.AdminType_PMTPREWARDS, signer) {
7274
return response, errors.Wrap(types.ErrNotEnoughPermissions, fmt.Sprintf("Sending Account : %s", msg.Signer))
7375
}
7476
params := k.GetPmtpParams(ctx)
@@ -115,7 +117,7 @@ func (k msgServer) ModifyPmtpRates(goCtx context.Context, msg *types.MsgModifyPm
115117
if err != nil {
116118
return response, err
117119
}
118-
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, types.ModuleName, signer) {
120+
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, tokenregistrytypes.AdminType_PMTPREWARDS, signer) {
119121
return response, errors.Wrap(types.ErrNotEnoughPermissions, fmt.Sprintf("Sending Account : %s", msg.Signer))
120122
}
121123
params := k.GetPmtpParams(ctx)
@@ -213,6 +215,7 @@ func (k msgServer) DecommissionPool(goCtx context.Context, msg *types.MsgDecommi
213215
if err != nil {
214216
return nil, err
215217
}
218+
// TODO : Deprecate this Admin in favor of TokenRegistry
216219
if !k.Keeper.ValidateAddress(ctx, addAddr) {
217220
return nil, sdkerrors.Wrap(types.ErrInvalid, "user does not have permission to decommission pool")
218221
}

x/clp/types/expected_keepers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ type TokenRegistryKeeper interface {
3434
GetEntry(registry tokenregistryTypes.Registry, denom string) (*tokenregistryTypes.RegistryEntry, error)
3535
CheckEntryPermissions(entry *tokenregistryTypes.RegistryEntry, permissions []tokenregistryTypes.Permission) bool
3636
GetRegistry(ctx sdk.Context) tokenregistryTypes.Registry
37-
IsAdminAccount(ctx sdk.Context, moduleName string, adminAccount sdk.AccAddress) bool
37+
IsAdminAccount(ctx sdk.Context, moduleName tokenregistryTypes.AdminType, adminAccount sdk.AccAddress) bool
3838
}

x/ethbridge/keeper/blacklist.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package keeper
33
import (
44
"github.com/Sifchain/sifnode/x/ethbridge/types"
55
oracletypes "github.com/Sifchain/sifnode/x/oracle/types"
6+
tokenregistrytypes "github.com/Sifchain/sifnode/x/tokenregistry/types"
67
sdk "github.com/cosmos/cosmos-sdk/types"
78
)
89

@@ -17,7 +18,7 @@ func (k Keeper) SetBlacklist(ctx sdk.Context, msg *types.MsgSetBlacklist) error
1718
return err
1819
}
1920

20-
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, types.ModuleName, from) {
21+
if !k.tokenRegistryKeeper.IsAdminAccount(ctx, tokenregistrytypes.AdminType_ETHBRIDGE, from) {
2122
return oracletypes.ErrNotAdminAccount
2223
}
2324

x/ethbridge/types/expected_keepers.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
oracletypes "github.com/Sifchain/sifnode/x/oracle/types"
5+
tokenregistryTypes "github.com/Sifchain/sifnode/x/tokenregistry/types"
56
sdk "github.com/cosmos/cosmos-sdk/types"
67
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
78
)
@@ -32,5 +33,5 @@ type OracleKeeper interface {
3233
}
3334

3435
type TokenRegistryKeeper interface {
35-
IsAdminAccount(ctx sdk.Context, moduleName string, adminAccount sdk.AccAddress) bool
36+
IsAdminAccount(ctx sdk.Context, moduleName tokenregistryTypes.AdminType, adminAccount sdk.AccAddress) bool
3637
}

x/tokenregistry/keeper/genesis.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package keeper
22

33
import (
4+
"fmt"
45
sdk "github.com/cosmos/cosmos-sdk/types"
56
abci "github.com/tendermint/tendermint/abci/types"
67

@@ -9,12 +10,10 @@ import (
910

1011
func (k keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) []abci.ValidatorUpdate {
1112
if state.AdminAccounts != nil {
13+
1214
for _, adminAccount := range state.AdminAccounts.AdminAccounts {
13-
addr, err := sdk.AccAddressFromBech32(adminAccount.Address)
14-
if err != nil {
15-
panic(err)
16-
}
17-
k.SetAdminAccount(ctx, adminAccount.ModuleName, addr)
15+
fmt.Println("Setting Admin account ", adminAccount)
16+
k.SetAdminAccount(ctx, adminAccount)
1817
}
1918
}
2019
if state.Registry != nil {

x/tokenregistry/keeper/keeper.go

+43-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package keeper
22

33
import (
4-
"bytes"
54
"fmt"
65
"strings"
76

@@ -24,46 +23,64 @@ func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey) types.Keeper {
2423
}
2524
}
2625

27-
func (k keeper) SetAdminAccount(ctx sdk.Context, moduleName string, adminAccount sdk.AccAddress) {
26+
func (k keeper) SetAdminAccount(ctx sdk.Context, account *types.AdminAccount) {
2827
store := ctx.KVStore(k.storeKey)
29-
key := types.GetAdminAccountKey(moduleName)
30-
31-
store.Set(key, k.cdc.MustMarshal(&types.AdminAccount{
32-
ModuleName: moduleName,
33-
Address: adminAccount.String(),
34-
}))
28+
key := types.GetAdminAccountKey(*account)
29+
store.Set(key, k.cdc.MustMarshal(account))
3530
}
3631

37-
func (k keeper) IsAdminAccount(ctx sdk.Context, moduleName string, adminAccount sdk.AccAddress) bool {
38-
account := k.GetAdminAccount(ctx, moduleName)
39-
if account == nil {
32+
func (k keeper) IsAdminAccount(ctx sdk.Context, adminType types.AdminType, adminAccount sdk.AccAddress) bool {
33+
accounts := k.GetAdminAccountsForType(ctx, adminType)
34+
if len(accounts.AdminAccounts) == 0 {
4035
return false
4136
}
42-
return bytes.Equal(account, adminAccount)
43-
}
44-
45-
func (k keeper) GetAdminAccount(ctx sdk.Context, moduleName string) (adminAccount sdk.AccAddress) {
46-
var aA types.AdminAccount
47-
store := ctx.KVStore(k.storeKey)
48-
key := types.GetAdminAccountKey(moduleName)
49-
bz := store.Get(key)
50-
if len(bz) == 0 {
51-
return sdk.AccAddress{}
37+
for _, account := range accounts.AdminAccounts {
38+
if strings.EqualFold(account.AdminAddress, adminAccount.String()) {
39+
return true
40+
}
5241
}
53-
k.cdc.MustUnmarshal(bz, &aA)
54-
adminAccount = sdk.AccAddress(aA.Address)
55-
return adminAccount
42+
return false
5643
}
5744

5845
func (k keeper) GetAdminAccountIterator(ctx sdk.Context) sdk.Iterator {
5946
store := ctx.KVStore(k.storeKey)
6047
return sdk.KVStorePrefixIterator(store, types.AdminAccountStorePrefix)
6148
}
6249

50+
func (k keeper) GetAdminAccountsForType(ctx sdk.Context, adminType types.AdminType) *types.AdminAccounts {
51+
var res types.AdminAccounts
52+
iterator := k.GetAdminAccountIterator(ctx)
53+
defer func(iterator sdk.Iterator) {
54+
err := iterator.Close()
55+
if err != nil {
56+
panic(err)
57+
}
58+
}(iterator)
59+
for ; iterator.Valid(); iterator.Next() {
60+
var al types.AdminAccount
61+
bytesValue := iterator.Value()
62+
err := k.cdc.Unmarshal(bytesValue, &al)
63+
if err != nil {
64+
// Log unmarshal distribution error instead of panic.
65+
ctx.Logger().Error(fmt.Sprintf("Unmarshal failed for admin account bytes : %s ", bytesValue))
66+
continue
67+
}
68+
if al.AdminType == adminType {
69+
res.AdminAccounts = append(res.AdminAccounts, &al)
70+
}
71+
}
72+
return &res
73+
}
74+
6375
func (k keeper) GetAdminAccounts(ctx sdk.Context) *types.AdminAccounts {
6476
var res types.AdminAccounts
6577
iterator := k.GetAdminAccountIterator(ctx)
66-
defer iterator.Close()
78+
defer func(iterator sdk.Iterator) {
79+
err := iterator.Close()
80+
if err != nil {
81+
panic(err)
82+
}
83+
}(iterator)
6784
for ; iterator.Valid(); iterator.Next() {
6885
var al types.AdminAccount
6986
bytesValue := iterator.Value()
@@ -74,6 +91,7 @@ func (k keeper) GetAdminAccounts(ctx sdk.Context) *types.AdminAccounts {
7491
continue
7592
}
7693
res.AdminAccounts = append(res.AdminAccounts, &al)
94+
7795
}
7896
return &res
7997
}

x/tokenregistry/keeper/migrations.go

+8
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ func (m Migrator) MigrateToVer2(ctx sdk.Context) error {
2424
m.keeper.SetRegistry(ctx, registry)
2525
return nil
2626
}
27+
28+
func (m Migrator) MigrateToVer3(ctx sdk.Context) error {
29+
accounts := tkrtypes.InitialAdminAccounts()
30+
for _, account := range accounts.AdminAccounts {
31+
m.keeper.SetAdminAccount(ctx, account)
32+
}
33+
return nil
34+
}

0 commit comments

Comments
 (0)