Skip to content

Commit

Permalink
update store-block-list message
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Sep 26, 2024
1 parent bedbfc7 commit 2a32e8b
Show file tree
Hide file tree
Showing 11 changed files with 1,030 additions and 98 deletions.
13 changes: 12 additions & 1 deletion proto/cronos/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ service Query {
option (google.api.http).get = "/cronos/v1/permissions";
}

// BlockList
rpc BlockList(QueryBlockListRequest) returns (QueryBlockListResponse) {
option (google.api.http).get = "/cronos/v1/blocklist";
}

// this line is used by starport scaffolding # 2
}

Expand Down Expand Up @@ -105,4 +110,10 @@ message QueryPermissionsResponse {
bool can_turn_bridge = 2;
}

// this line is used by starport scaffolding # 3
// QueryBlockListRequest
message QueryBlockListRequest { }

// QueryBlockListResponse
message QueryBlockListResponse {
bytes blob = 1;
}
14 changes: 13 additions & 1 deletion proto/cronos/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ service Msg {

// UpdatePermissions defines a method to update cronos admins permissions
rpc UpdatePermissions(MsgUpdatePermissions) returns (MsgUpdatePermissionsResponse);

// StoreBlockList
rpc StoreBlockList(MsgStoreBlockList) returns (MsgStoreBlockListResponse);
}

// MsgConvertVouchers represents a message to convert ibc voucher coins to
Expand Down Expand Up @@ -110,4 +113,13 @@ message MsgUpdatePermissions {
// MsgUpdatePermissionsResponse defines the response type.
message MsgUpdatePermissionsResponse {}

// this line is used by starport scaffolding # proto/tx/message
// MsgStoreBlockList
message MsgStoreBlockList {
option (cosmos.msg.v1.signer) = "from";
string from = 1;
bytes blob = 2;
}

// MsgStoreBlockListResponse
message MsgStoreBlockListResponse {
}
40 changes: 40 additions & 0 deletions x/cronos/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cli
import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -44,6 +46,7 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(CmdUpdateTokenMapping())
cmd.AddCommand(CmdTurnBridge())
cmd.AddCommand(CmdUpdatePermissions())
cmd.AddCommand(CmdStoreBlockList())
cmd.AddCommand(MigrateGenesisCmd())
return cmd
}
Expand Down Expand Up @@ -317,6 +320,43 @@ func CmdUpdatePermissions() *cobra.Command {
return cmd
}

// CmdStoreBlockList returns a CLI command handler for updating cronos permissions
func CmdStoreBlockList() *cobra.Command {
cmd := &cobra.Command{
Use: "store-block-list [encrypted-block-list-file]",
Short: "Store encrypted block list",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

fp, err := os.Open(args[0])
if err != nil {
return err
}
defer fp.Close()

// Read the file
blob, err := io.ReadAll(fp)
if err != nil {
return err
}

msg := types.NewMsgStoreBlockList(clientCtx.GetFromAddress().String(), blob)
if err := msg.ValidateBasic(); err != nil {
return err
}

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

flags.AddTxFlagsToCmd(cmd)
return cmd
}

type ExportEvmGenesisState struct {
evmtypes.GenesisState
Params ExportEvmParams `json:"params"`
Expand Down
8 changes: 8 additions & 0 deletions x/cronos/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@ func (k Keeper) Permissions(goCtx context.Context, req *types.QueryPermissionsRe
CanTurnBridge: CanTurnBridge == (permissions & CanTurnBridge),
}, nil
}

func (k Keeper) BlockList(goCtx context.Context, req *types.QueryBlockListRequest) (*types.QueryBlockListResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
blob := ctx.KVStore(k.storeKey).Get(types.KeyPrefixBlockList)
return &types.QueryBlockListResponse{
Blob: blob,
}, nil
}
4 changes: 4 additions & 0 deletions x/cronos/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,7 @@ func (k Keeper) IBCSendPacketCallback(
) error {
return nil
}

func (k Keeper) GetBlockList(ctx sdk.Context) []byte {
return ctx.KVStore(k.storeKey).Get(types.KeyPrefixBlockList)
}
10 changes: 10 additions & 0 deletions x/cronos/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,13 @@ func (k msgServer) UpdatePermissions(goCtx context.Context, msg *types.MsgUpdate

return &types.MsgUpdatePermissionsResponse{}, nil
}

func (k msgServer) StoreBlockList(goCtx context.Context, msg *types.MsgStoreBlockList) (*types.MsgStoreBlockListResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
admin := k.Keeper.GetParams(ctx).CronosAdmin
if admin != msg.From {
return nil, errors.Wrap(sdkerrors.ErrUnauthorized, "msg sender is not authorized")
}
ctx.KVStore(k.storeKey).Set(types.KeyPrefixBlockList, msg.Blob)
return &types.MsgStoreBlockListResponse{}, nil
}
2 changes: 2 additions & 0 deletions x/cronos/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
prefixContractToDenom
paramsKey
prefixAdminToPermissions
prefixBlockList
)

// KVStore key prefixes
Expand All @@ -37,6 +38,7 @@ var (
// ParamsKey is the key for params.
ParamsKey = []byte{paramsKey}
KeyPrefixAdminToPermissions = []byte{prefixAdminToPermissions}
KeyPrefixBlockList = []byte{prefixBlockList}
)

// this line is used by starport scaffolding # ibc/keys/port
Expand Down
60 changes: 60 additions & 0 deletions x/cronos/types/messages.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package types

import (
"bytes"

stderrors "errors"

"cosmossdk.io/errors"
"filippo.io/age"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -14,6 +19,7 @@ const (
TypeMsgUpdateParams = "UpdateParams"
TypeMsgTurnBridge = "TurnBridge"
TypeMsgUpdatePermissions = "UpdatePermissions"
TypeMsgStoreBlockList = "StoreBlockList"
)

var (
Expand All @@ -23,6 +29,7 @@ var (
_ sdk.Msg = &MsgUpdateParams{}
_ sdk.Msg = &MsgTurnBridge{}
_ sdk.Msg = &MsgUpdatePermissions{}
_ sdk.Msg = &MsgStoreBlockList{}
)

func NewMsgConvertVouchers(address string, coins sdk.Coins) *MsgConvertVouchers {
Expand Down Expand Up @@ -318,3 +325,56 @@ func (msg *MsgUpdatePermissions) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

func NewMsgStoreBlockList(from string, blob []byte) *MsgStoreBlockList {
return &MsgStoreBlockList{
From: from,
Blob: blob,
}
}

var errDummyIdentity = stderrors.New("dummy")

type dummyIdentity struct{}

func (i *dummyIdentity) Unwrap(stanzas []*age.Stanza) ([]byte, error) {
return nil, errDummyIdentity
}

func (msg *MsgStoreBlockList) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err)
}

_, err = age.Decrypt(bytes.NewBuffer(msg.Blob), new(dummyIdentity))
if err != nil && err != errDummyIdentity {
return err
}
return nil
}

func (msg *MsgStoreBlockList) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
panic(err)
}

return []sdk.AccAddress{addr}
}

// GetSignBytes ...
func (msg *MsgStoreBlockList) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

// Route ...
func (msg MsgStoreBlockList) Route() string {
return RouterKey
}

// Type ...
func (msg MsgStoreBlockList) Type() string {
return TypeMsgStoreBlockList
}
Loading

0 comments on commit 2a32e8b

Please sign in to comment.