Skip to content

Commit c2700f5

Browse files
committed
set with ante
1 parent fa6497f commit c2700f5

File tree

4 files changed

+101
-139
lines changed

4 files changed

+101
-139
lines changed

app/app.go

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
tmjson "github.com/cometbft/cometbft/libs/json"
2525
"github.com/cometbft/cometbft/libs/log"
2626
tmos "github.com/cometbft/cometbft/libs/os"
27-
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
2827
"github.com/cosmos/cosmos-sdk/client"
2928
"github.com/cosmos/cosmos-sdk/codec/types"
3029
"github.com/gorilla/mux"
@@ -328,6 +327,7 @@ type App struct {
328327
interfaceRegistry types.InterfaceRegistry
329328

330329
invCheckPeriod uint
330+
blockedMap map[string]struct{}
331331

332332
// keys to access the substores
333333
keys map[string]*storetypes.KVStoreKey
@@ -385,8 +385,6 @@ type App struct {
385385
configurator module.Configurator
386386

387387
qms storetypes.MultiStore
388-
389-
blockProposalHandler *ProposalHandler
390388
}
391389

392390
// New returns a reference to an initialized chain.
@@ -396,7 +394,7 @@ func New(
396394
homePath string, invCheckPeriod uint, encodingConfig ethermint.EncodingConfig,
397395
// this line is used by starport scaffolding # stargate/app/newArgument
398396
appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp),
399-
) *App {
397+
) (app *App) {
400398
appCodec := encodingConfig.Codec
401399
cdc := encodingConfig.Amino
402400
interfaceRegistry := encodingConfig.InterfaceRegistry
@@ -423,34 +421,41 @@ func New(
423421

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

426-
blockProposalHandler := NewProposalHandler(encodingConfig.TxConfig.TxDecoder(), identity)
424+
keys, memKeys, tkeys := StoreKeys(skipGravity)
425+
cronosKey := keys[cronostypes.StoreKey]
427426

427+
txConfig := encodingConfig.TxConfig
428+
maxTxGasWanted := cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted))
428429
// NOTE we use custom transaction decoder that supports the sdk.Tx interface instead of sdk.StdTx
429430
// Setup Mempool and Proposal Handlers
430-
baseAppOptions = append(baseAppOptions, func(app *baseapp.BaseApp) {
431+
baseAppOptions = append(baseAppOptions, func(bapp *baseapp.BaseApp) {
431432
mempool := mempool.NoOpMempool{}
432-
app.SetMempool(mempool)
433-
app.SetPrepareProposal(blockProposalHandler.PrepareProposalHandler())
434-
app.SetProcessProposal(blockProposalHandler.ProcessProposalHandler())
433+
bapp.SetMempool(mempool)
434+
blockProposalHandler := NewProposalHandler(
435+
appCodec,
436+
cronosKey,
437+
txConfig.TxDecoder(),
438+
identity,
439+
func(blacklist []string) { app.setAnteHandler(txConfig, maxTxGasWanted, blacklist) },
440+
)
441+
bapp.SetPrepareProposal(blockProposalHandler.PrepareProposalHandler())
442+
bapp.SetProcessProposal(blockProposalHandler.ProcessProposalHandler())
435443
})
436444
bApp := baseapp.NewBaseApp(Name, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
437445

438446
bApp.SetCommitMultiStoreTracer(traceStore)
439447
bApp.SetVersion(version.Version)
440448
bApp.SetInterfaceRegistry(interfaceRegistry)
441449

442-
keys, memKeys, tkeys := StoreKeys(skipGravity)
443-
444-
app := &App{
445-
BaseApp: bApp,
446-
cdc: cdc,
447-
appCodec: appCodec,
448-
interfaceRegistry: interfaceRegistry,
449-
invCheckPeriod: invCheckPeriod,
450-
keys: keys,
451-
tkeys: tkeys,
452-
memKeys: memKeys,
453-
blockProposalHandler: blockProposalHandler,
450+
app = &App{
451+
BaseApp: bApp,
452+
cdc: cdc,
453+
appCodec: appCodec,
454+
interfaceRegistry: interfaceRegistry,
455+
invCheckPeriod: invCheckPeriod,
456+
keys: keys,
457+
tkeys: tkeys,
458+
memKeys: memKeys,
454459
}
455460

456461
// init params keeper and subspaces
@@ -606,7 +611,7 @@ func New(
606611

607612
app.CronosKeeper = *cronoskeeper.NewKeeper(
608613
appCodec,
609-
keys[cronostypes.StoreKey],
614+
cronosKey,
610615
keys[cronostypes.MemStoreKey],
611616
app.BankKeeper,
612617
app.TransferKeeper,
@@ -955,10 +960,6 @@ func New(
955960
tmos.Exit(fmt.Sprintf("versiondb version %d lag behind iavl version %d", v1, v2))
956961
}
957962
}
958-
959-
if err := app.RefreshBlockList(app.NewUncachedContext(false, tmproto.Header{})); err != nil {
960-
panic(err)
961-
}
962963
}
963964

964965
app.ScopedIBCKeeper = scopedIBCKeeper
@@ -970,8 +971,11 @@ func New(
970971
return app
971972
}
972973

973-
// use Ethermint's custom AnteHandler
974-
func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64, blacklist []string) error {
974+
func (app *App) getBlockedMap() map[string]struct{} {
975+
return app.blockedMap
976+
}
977+
978+
func (app *App) setBlacklist(blacklist []string) error {
975979
if len(blacklist) > 0 {
976980
sort.Strings(blacklist)
977981
// hash blacklist concatenated
@@ -991,14 +995,16 @@ func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64, bl
991995
}
992996
blockedMap := make(map[string]struct{}, len(blacklist))
993997
for _, str := range blacklist {
994-
addr, err := sdk.AccAddressFromBech32(str)
995-
if err != nil {
996-
return fmt.Errorf("invalid bech32 address: %s, err: %w", str, err)
997-
}
998-
999-
blockedMap[string(addr)] = struct{}{}
998+
blockedMap[str] = struct{}{}
1000999
}
1001-
blockAddressDecorator := NewBlockAddressesDecorator(blockedMap)
1000+
app.blockedMap = blockedMap
1001+
return nil
1002+
}
1003+
1004+
// use Ethermint's custom AnteHandler
1005+
func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64, blacklist []string) error {
1006+
app.setBlacklist(blacklist)
1007+
blockAddressDecorator := NewBlockAddressesDecorator(app.getBlockedMap)
10021008
options := evmante.HandlerOptions{
10031009
AccountKeeper: app.AccountKeeper,
10041010
BankKeeper: app.BankKeeper,
@@ -1053,22 +1059,7 @@ func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.R
10531059

10541060
// EndBlocker application updates every end block
10551061
func (app *App) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
1056-
rsp := app.mm.EndBlock(ctx, req)
1057-
1058-
if err := app.RefreshBlockList(ctx); err != nil {
1059-
app.Logger().Error("failed to update blocklist", "error", err)
1060-
}
1061-
1062-
return rsp
1063-
}
1064-
1065-
func (app *App) RefreshBlockList(ctx sdk.Context) error {
1066-
if app.blockProposalHandler.Identity == nil {
1067-
return nil
1068-
}
1069-
1070-
// refresh blocklist
1071-
return app.blockProposalHandler.SetBlockList(app.CronosKeeper.GetBlockList(ctx))
1062+
return app.mm.EndBlock(ctx, req)
10721063
}
10731064

10741065
// InitChainer application update at chain initialization

app/block_address.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ import (
88

99
// BlockAddressesDecorator block addresses from sending transactions
1010
type BlockAddressesDecorator struct {
11-
blockedMap map[string]struct{}
11+
blockedMapGetter func() map[string]struct{}
1212
}
1313

14-
func NewBlockAddressesDecorator(blacklist map[string]struct{}) BlockAddressesDecorator {
15-
return BlockAddressesDecorator{
16-
blockedMap: blacklist,
14+
func NewBlockAddressesDecorator(blockedMapGetter func() map[string]struct{}) *BlockAddressesDecorator {
15+
return &BlockAddressesDecorator{
16+
blockedMapGetter: blockedMapGetter,
1717
}
1818
}
1919

2020
func (bad BlockAddressesDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
21+
blockedMap := bad.blockedMapGetter()
2122
if ctx.IsCheckTx() {
2223
for _, msg := range tx.GetMsgs() {
2324
for _, signer := range msg.GetSigners() {
24-
if _, ok := bad.blockedMap[string(signer)]; ok {
25+
if _, ok := blockedMap[signer.String()]; ok {
2526
return ctx, fmt.Errorf("signer is blocked: %s", signer.String())
2627
}
2728
}

app/proposal.go

Lines changed: 44 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,111 +3,76 @@ package app
33
import (
44
"bytes"
55
"encoding/json"
6-
"fmt"
76
"io"
87

98
"filippo.io/age"
109

1110
abci "github.com/cometbft/cometbft/abci/types"
11+
"github.com/cosmos/cosmos-sdk/codec"
12+
storetypes "github.com/cosmos/cosmos-sdk/store/types"
1213
sdk "github.com/cosmos/cosmos-sdk/types"
13-
"github.com/cosmos/cosmos-sdk/x/auth/signing"
14+
cronostypes "github.com/crypto-org-chain/cronos/v2/x/cronos/types"
1415
)
1516

16-
type BlockList struct {
17-
Addresses []string `mapstructure:"addresses"`
18-
}
19-
2017
type ProposalHandler struct {
21-
TxDecoder sdk.TxDecoder
22-
Identity age.Identity
23-
Blocklist map[string]struct{}
24-
LastBlockList []byte
25-
}
26-
27-
func NewProposalHandler(txDecoder sdk.TxDecoder, identity age.Identity) *ProposalHandler {
28-
return &ProposalHandler{
29-
TxDecoder: txDecoder,
30-
Identity: identity,
31-
Blocklist: make(map[string]struct{}),
32-
}
18+
cdc codec.BinaryCodec
19+
cronosKey *storetypes.KVStoreKey
20+
TxDecoder sdk.TxDecoder
21+
Identity age.Identity
22+
LastBlockList []byte
23+
refreshBlocklistHandler refreshBlocklistHandler
3324
}
3425

35-
func (h *ProposalHandler) SetBlockList(blob []byte) error {
36-
if h.Identity == nil {
37-
return nil
38-
}
39-
40-
if bytes.Equal(h.LastBlockList, blob) {
41-
return nil
42-
}
43-
h.LastBlockList = blob
44-
45-
reader, err := age.Decrypt(bytes.NewBuffer(blob), h.Identity)
46-
if err != nil {
47-
return err
48-
}
49-
50-
data, err := io.ReadAll(reader)
51-
if err != nil {
52-
return err
53-
}
54-
55-
var blocklist BlockList
56-
if err := json.Unmarshal(data, &blocklist); err != nil {
57-
return err
58-
}
59-
60-
// convert to map
61-
m := make(map[string]struct{}, len(blocklist.Addresses))
62-
for _, s := range blocklist.Addresses {
63-
addr, err := sdk.AccAddressFromBech32(s)
64-
if err != nil {
65-
return fmt.Errorf("invalid bech32 address: %s, err: %w", s, err)
66-
}
67-
m[addr.String()] = struct{}{}
68-
}
69-
70-
h.Blocklist = m
71-
return nil
26+
type blockList struct {
27+
Addresses []string `mapstructure:"addresses"`
7228
}
7329

74-
func (h *ProposalHandler) ValidateTransactions(txs [][]byte) error {
75-
for _, txBz := range txs {
76-
tx, err := h.TxDecoder(txBz)
77-
if err != nil {
78-
return err
79-
}
80-
81-
sigTx, ok := tx.(signing.SigVerifiableTx)
82-
if !ok {
83-
return fmt.Errorf("tx of type %T does not implement SigVerifiableTx", tx)
84-
}
30+
type refreshBlocklistHandler func(blacklist []string)
8531

86-
for _, signer := range sigTx.GetSigners() {
87-
if _, ok := h.Blocklist[signer.String()]; ok {
88-
return fmt.Errorf("signer is blocked: %s", signer.String())
89-
}
90-
}
32+
func NewProposalHandler(
33+
cdc codec.BinaryCodec,
34+
cronosKey *storetypes.KVStoreKey,
35+
txDecoder sdk.TxDecoder,
36+
identity age.Identity,
37+
refreshBlocklistHandler refreshBlocklistHandler,
38+
) *ProposalHandler {
39+
return &ProposalHandler{
40+
cdc: cdc,
41+
cronosKey: cronosKey,
42+
TxDecoder: txDecoder,
43+
Identity: identity,
44+
refreshBlocklistHandler: refreshBlocklistHandler,
9145
}
92-
return nil
9346
}
9447

9548
func (h *ProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHandler {
9649
return func(ctx sdk.Context, req abci.RequestPrepareProposal) abci.ResponsePrepareProposal {
97-
if err := h.ValidateTransactions(req.Txs); err != nil {
98-
panic(err)
99-
}
100-
10150
return abci.ResponsePrepareProposal{Txs: req.Txs}
10251
}
10352
}
10453

10554
func (h *ProposalHandler) ProcessProposalHandler() sdk.ProcessProposalHandler {
10655
return func(ctx sdk.Context, req abci.RequestProcessProposal) abci.ResponseProcessProposal {
107-
if err := h.ValidateTransactions(req.Txs); err != nil {
108-
panic(err)
56+
if h.Identity != nil {
57+
reject := abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}
58+
blob := ctx.KVStore(h.cronosKey).Get(cronostypes.KeyPrefixBlockList)
59+
if !bytes.Equal(h.LastBlockList, blob) {
60+
reader, err := age.Decrypt(bytes.NewBuffer(blob), h.Identity)
61+
if err != nil {
62+
return reject
63+
}
64+
data, err := io.ReadAll(reader)
65+
if err != nil {
66+
return reject
67+
}
68+
var blocklist blockList
69+
if err := json.Unmarshal(data, &blocklist); err != nil {
70+
return reject
71+
}
72+
h.LastBlockList = blob
73+
h.refreshBlocklistHandler(blocklist.Addresses)
74+
}
10975
}
110-
11176
return abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_ACCEPT}
11277
}
11378
}

integration_tests/test_gov_update_params.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,15 @@ def test_gov_update_params(cronos, tmp_path):
9797

9898
# gen two keys for two accounts
9999
name = "e2ee-identity"
100+
cli1 = cronos.cosmos_cli(1)
100101
pubkey0 = cli.keygen(keyring_name=name)
102+
pubkey1 = cli.keygen(keyring_name=name)
101103
cli.register_e2ee_key(pubkey0, _from="validator")
104+
cli1.register_e2ee_key(pubkey1, _from="validator")
102105
assert cli.query_e2ee_key(cli.address("validator")) == pubkey0
103-
cronos.supervisorctl("stop", "cronos_777-1-node0")
104-
cronos.supervisorctl("start", "cronos_777-1-node0")
106+
assert cli1.query_e2ee_key(cli1.address("validator")) == pubkey1
107+
cronos.supervisorctl("stop", "all")
108+
cronos.supervisorctl("start", "cronos_777-1-node0", "cronos_777-1-node1")
105109
wait_for_port(ports.evmrpc_port(cronos.base_port(0)))
106110

107111
addr = cli.address("user")
@@ -111,11 +115,12 @@ def test_gov_update_params(cronos, tmp_path):
111115
cli.encrypt(
112116
plainfile,
113117
cli.address("validator"),
118+
cli1.address("validator"),
114119
output=cipherfile,
115120
)
116121
rsp = cli.store_blocklist(cipherfile, from_="validator")
117122
assert rsp["code"] == 0, rsp["raw_log"]
118-
wait_for_new_blocks(cli, 4)
119-
rsp = cli.transfer(addr, cli.address("validator"), "1basecro")
123+
wait_for_new_blocks(cli, 2)
124+
rsp = cli.transfer(addr, cli.address("validator"), "1basetcro")
120125
assert rsp["code"] != 0
121126
assert "signer is blocked" in rsp["raw_log"]

0 commit comments

Comments
 (0)