Skip to content

Commit

Permalink
add cronos msg
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed May 2, 2024
1 parent 0562eaf commit 6366747
Show file tree
Hide file tree
Showing 13 changed files with 970 additions and 132 deletions.
41 changes: 22 additions & 19 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ import (
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
ethparams "github.com/ethereum/go-ethereum/params"

"github.com/99designs/keyring"
e2ee "github.com/crypto-org-chain/cronos/v2/x/e2ee"
e2eekeeper "github.com/crypto-org-chain/cronos/v2/x/e2ee/keeper"
e2eekeyring "github.com/crypto-org-chain/cronos/v2/x/e2ee/keyring"
Expand Down Expand Up @@ -385,6 +384,8 @@ type App struct {
configurator module.Configurator

qms storetypes.MultiStore

blockProposalHandler *ProposalHandler
}

// New returns a reference to an initialized chain.
Expand All @@ -406,34 +407,29 @@ func New(
if err != nil {
panic(err)
}
if _, err = kr.Get(e2eetypes.DefaultKeyringName); err != nil {
if err == keyring.ErrKeyNotFound {
k, err := age.GenerateX25519Identity()
if err != nil {
panic(err)
}
secret := k.String()
if err := kr.Set(e2eetypes.DefaultKeyringName, []byte(secret)); err != nil {
panic(err)
}
if identity, err = age.ParseX25519Identity(secret); err != nil {
panic(err)
}
} else {
bz, err := kr.Get(e2eetypes.DefaultKeyringName)
if err != nil {
logger.Error("e2ee identity not found", "error", err)
} else {

identity, err = age.ParseX25519Identity(string(bz))
if err != nil {
panic(err)
}
}
}

baseAppOptions = memiavlstore.SetupMemIAVL(logger, homePath, appOpts, false, false, baseAppOptions)

blockProposalHandler := NewProposalHandler(encodingConfig.TxConfig.TxDecoder(), identity)

// NOTE we use custom transaction decoder that supports the sdk.Tx interface instead of sdk.StdTx
// Setup Mempool and Proposal Handlers
baseAppOptions = append(baseAppOptions, func(app *baseapp.BaseApp) {
mempool := mempool.NoOpMempool{}
app.SetMempool(mempool)
handler := NewProposalHandler(encodingConfig.TxConfig.TxDecoder(), identity)
app.SetPrepareProposal(handler.PrepareProposalHandler())
app.SetProcessProposal(handler.ProcessProposalHandler())
app.SetPrepareProposal(blockProposalHandler.PrepareProposalHandler())
app.SetProcessProposal(blockProposalHandler.ProcessProposalHandler())
})
bApp := baseapp.NewBaseApp(Name, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)

Expand Down Expand Up @@ -1050,7 +1046,14 @@ func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.R

// EndBlocker application updates every end block
func (app *App) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.mm.EndBlock(ctx, req)
rsp := app.mm.EndBlock(ctx, req)

// refresh blocklist
if err := app.blockProposalHandler.SetBlockList(app.CronosKeeper.GetBlockList(ctx)); err != nil {

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

path flow from Begin/EndBlock to a panic call
app.Logger().Error("failed to update blocklist", "error", err)
}

return rsp
}

// InitChainer application update at chain initialization
Expand Down
16 changes: 13 additions & 3 deletions app/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type BlockList struct {
}

type ProposalHandler struct {
txDecoder sdk.TxDecoder
identity age.Identity
blocklist map[string]struct{}
txDecoder sdk.TxDecoder
identity age.Identity
blocklist map[string]struct{}
lastBlockList []byte
}

func NewProposalHandler(txDecoder sdk.TxDecoder, identity age.Identity) *ProposalHandler {
Expand All @@ -32,6 +33,15 @@ func NewProposalHandler(txDecoder sdk.TxDecoder, identity age.Identity) *Proposa
}

func (h *ProposalHandler) SetBlockList(blob []byte) error {
if h.identity == nil {
return nil
}

if bytes.Equal(h.lastBlockList, blob) {
return nil
}
h.lastBlockList = blob

reader, err := age.Decrypt(bytes.NewBuffer(blob), h.identity)
if err != nil {
return err
Expand Down
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;
}
12 changes: 11 additions & 1 deletion proto/cronos/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ service Msg {

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

rpc StoreBlockList(MsgStoreBlockList) returns (MsgStoreBlockListResponse);
}

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

// this line is used by starport scaffolding # proto/tx/message
// MsgStoreBlockList
message MsgStoreBlockList {
string from = 1;
bytes blob = 2;
}

// MsgStoreBlockListResponse
message MsgStoreBlockListResponse {
}
2 changes: 0 additions & 2 deletions proto/e2ee/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ service Msg {

// MsgRegisterEncryptionKey defines the Msg/RegisterEncryptionKey request type
message MsgRegisterEncryptionKey {
option (cosmos.msg.v1.signer) = "address";

string address = 1;
string key = 2;
}
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 @@ -146,3 +146,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 @@ -360,3 +360,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 @@ -123,3 +123,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
Loading

0 comments on commit 6366747

Please sign in to comment.