Skip to content

Commit 01dca97

Browse files
authored
Problem: mempool logic should be kept (crypto-org-chain#1432)
* Problem: mempool logic should be kept * revert integration test
1 parent 57b190e commit 01dca97

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

app/app.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package app
22

33
import (
4+
"crypto/sha256"
5+
"encoding/hex"
46
stderrors "errors"
57
"fmt"
68
"io"
@@ -9,6 +11,7 @@ import (
911
"net/http"
1012
"os"
1113
"path/filepath"
14+
"sort"
1215

1316
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
1417
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
@@ -175,6 +178,8 @@ const (
175178
//
176179
// NOTE: In the SDK, the default value is 255.
177180
AddrLen = 20
181+
182+
FlagBlockedAddresses = "blocked-addresses"
178183
)
179184

180185
var Forks = []Fork{}
@@ -918,6 +923,7 @@ func New(
918923
app.SetEndBlocker(app.EndBlocker)
919924
if err := app.setAnteHandler(encodingConfig.TxConfig,
920925
cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted)),
926+
cast.ToStringSlice(appOpts.Get(FlagBlockedAddresses)),
921927
); err != nil {
922928
panic(err)
923929
}
@@ -965,7 +971,35 @@ func New(
965971
}
966972

967973
// use Ethermint's custom AnteHandler
968-
func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64) error {
974+
func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64, blacklist []string) error {
975+
if len(blacklist) > 0 {
976+
sort.Strings(blacklist)
977+
// hash blacklist concatenated
978+
h := sha256.New()
979+
for _, addr := range blacklist {
980+
_, err := h.Write([]byte(addr))
981+
if err != nil {
982+
panic(err)
983+
}
984+
}
985+
app.Logger().Error("Setting ante handler with blacklist", "size", len(blacklist), "hash", hex.EncodeToString(h.Sum(nil)))
986+
for _, addr := range blacklist {
987+
app.Logger().Error("Blacklisted address", "address", addr)
988+
}
989+
} else {
990+
app.Logger().Error("Setting ante handler without blacklist")
991+
}
992+
blockedMap := make(map[string]struct{}, len(blacklist))
993+
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{}{}
1000+
}
1001+
blockAddressDecorator := NewBlockAddressesDecorator(blockedMap)
1002+
9691003
options := evmante.HandlerOptions{
9701004
AccountKeeper: app.AccountKeeper,
9711005
BankKeeper: app.BankKeeper,
@@ -982,6 +1016,7 @@ func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64) er
9821016
sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}),
9831017
sdk.MsgTypeURL(&vestingtypes.MsgCreateVestingAccount{}),
9841018
},
1019+
ExtraDecorators: []sdk.AnteDecorator{blockAddressDecorator},
9851020
}
9861021

9871022
anteHandler, err := evmante.NewAnteHandler(options)

app/block_address.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package app
2+
3+
import (
4+
"fmt"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
)
8+
9+
// BlockAddressesDecorator block addresses from sending transactions
10+
type BlockAddressesDecorator struct {
11+
blockedMap map[string]struct{}
12+
}
13+
14+
func NewBlockAddressesDecorator(blacklist map[string]struct{}) BlockAddressesDecorator {
15+
return BlockAddressesDecorator{
16+
blockedMap: blacklist,
17+
}
18+
}
19+
20+
func (bad BlockAddressesDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
21+
if ctx.IsCheckTx() {
22+
for _, msg := range tx.GetMsgs() {
23+
for _, signer := range msg.GetSigners() {
24+
if _, ok := bad.blockedMap[string(signer)]; ok {
25+
return ctx, fmt.Errorf("signer is blocked: %s", signer.String())
26+
}
27+
}
28+
}
29+
}
30+
return next(ctx, tx, simulate)
31+
}

integration_tests/configs/long_timeout_commit.jsonnet

+3
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ default {
77
timeout_commit: '15s',
88
},
99
},
10+
'app-config'+: {
11+
'blocked-addresses': ['crc16z0herz998946wr659lr84c8c556da55dc34hh'],
12+
},
1013
},
1114
}

integration_tests/test_mempool.py

+7
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,10 @@ def test_mempool(cronos_mempool):
7676
break
7777
wait_for_new_blocks(cli, 1, sleep=0.1)
7878
assert len(sended_hash_set) == 0
79+
80+
81+
def test_blocked_address(cronos_mempool):
82+
cli = cronos_mempool.cosmos_cli(0)
83+
rsp = cli.transfer("signer1", cli.address("validator"), "1basecro")
84+
assert rsp["code"] != 0
85+
assert "signer is blocked" in rsp["raw_log"]

0 commit comments

Comments
 (0)