Skip to content

Commit

Permalink
Problem: mempool logic should be kept (crypto-org-chain#1432)
Browse files Browse the repository at this point in the history
* Problem: mempool logic should be kept

* revert integration test
  • Loading branch information
yihuang authored May 10, 2024
1 parent 57b190e commit 01dca97
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
37 changes: 36 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"crypto/sha256"
"encoding/hex"
stderrors "errors"
"fmt"
"io"
Expand All @@ -9,6 +11,7 @@ import (
"net/http"
"os"
"path/filepath"
"sort"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
Expand Down Expand Up @@ -175,6 +178,8 @@ const (
//
// NOTE: In the SDK, the default value is 255.
AddrLen = 20

FlagBlockedAddresses = "blocked-addresses"
)

var Forks = []Fork{}
Expand Down Expand Up @@ -918,6 +923,7 @@ func New(
app.SetEndBlocker(app.EndBlocker)
if err := app.setAnteHandler(encodingConfig.TxConfig,
cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted)),
cast.ToStringSlice(appOpts.Get(FlagBlockedAddresses)),
); err != nil {
panic(err)
}
Expand Down Expand Up @@ -965,7 +971,35 @@ func New(
}

// use Ethermint's custom AnteHandler
func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64) error {
func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64, blacklist []string) error {
if len(blacklist) > 0 {
sort.Strings(blacklist)
// hash blacklist concatenated
h := sha256.New()
for _, addr := range blacklist {
_, err := h.Write([]byte(addr))
if err != nil {
panic(err)
}
}
app.Logger().Error("Setting ante handler with blacklist", "size", len(blacklist), "hash", hex.EncodeToString(h.Sum(nil)))
for _, addr := range blacklist {
app.Logger().Error("Blacklisted address", "address", addr)
}
} else {
app.Logger().Error("Setting ante handler without blacklist")
}
blockedMap := make(map[string]struct{}, len(blacklist))
for _, str := range blacklist {
addr, err := sdk.AccAddressFromBech32(str)
if err != nil {
return fmt.Errorf("invalid bech32 address: %s, err: %w", str, err)
}

blockedMap[string(addr)] = struct{}{}
}
blockAddressDecorator := NewBlockAddressesDecorator(blockedMap)

options := evmante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
Expand All @@ -982,6 +1016,7 @@ func (app *App) setAnteHandler(txConfig client.TxConfig, maxGasWanted uint64) er
sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}),
sdk.MsgTypeURL(&vestingtypes.MsgCreateVestingAccount{}),
},
ExtraDecorators: []sdk.AnteDecorator{blockAddressDecorator},
}

anteHandler, err := evmante.NewAnteHandler(options)
Expand Down
31 changes: 31 additions & 0 deletions app/block_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package app

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// BlockAddressesDecorator block addresses from sending transactions
type BlockAddressesDecorator struct {
blockedMap map[string]struct{}
}

func NewBlockAddressesDecorator(blacklist map[string]struct{}) BlockAddressesDecorator {
return BlockAddressesDecorator{
blockedMap: blacklist,
}
}

func (bad BlockAddressesDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if ctx.IsCheckTx() {
for _, msg := range tx.GetMsgs() {
for _, signer := range msg.GetSigners() {
if _, ok := bad.blockedMap[string(signer)]; ok {
return ctx, fmt.Errorf("signer is blocked: %s", signer.String())
}
}
}
}
return next(ctx, tx, simulate)
}
3 changes: 3 additions & 0 deletions integration_tests/configs/long_timeout_commit.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ default {
timeout_commit: '15s',
},
},
'app-config'+: {
'blocked-addresses': ['crc16z0herz998946wr659lr84c8c556da55dc34hh'],
},
},
}
7 changes: 7 additions & 0 deletions integration_tests/test_mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ def test_mempool(cronos_mempool):
break
wait_for_new_blocks(cli, 1, sleep=0.1)
assert len(sended_hash_set) == 0


def test_blocked_address(cronos_mempool):
cli = cronos_mempool.cosmos_cli(0)
rsp = cli.transfer("signer1", cli.address("validator"), "1basecro")
assert rsp["code"] != 0
assert "signer is blocked" in rsp["raw_log"]

0 comments on commit 01dca97

Please sign in to comment.