-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package app | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"filippo.io/age" | ||
|
||
abci "github.com/cometbft/cometbft/abci/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
) | ||
|
||
type BlockList struct { | ||
Addresses []string `mapstructure:"addresses"` | ||
} | ||
|
||
type ProposalHandler struct { | ||
txDecoder sdk.TxDecoder | ||
identity age.Identity | ||
blocklist map[string]struct{} | ||
} | ||
|
||
func NewProposalHandler(txDecoder sdk.TxDecoder, identity age.Identity) *ProposalHandler { | ||
return &ProposalHandler{ | ||
txDecoder: txDecoder, | ||
identity: identity, | ||
blocklist: make(map[string]struct{}), | ||
} | ||
} | ||
|
||
func (h *ProposalHandler) SetBlockList(blob []byte) error { | ||
reader, err := age.Decrypt(bytes.NewBuffer(blob), h.identity) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
data, err := io.ReadAll(reader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var blocklist BlockList | ||
if err := json.Unmarshal(data, &blocklist); err != nil { | ||
return err | ||
} | ||
|
||
// convert to map | ||
m := make(map[string]struct{}, len(blocklist.Addresses)) | ||
for _, s := range blocklist.Addresses { | ||
addr, err := sdk.AccAddressFromBech32(s) | ||
if err != nil { | ||
return fmt.Errorf("invalid bech32 address: %s, err: %w", s, err) | ||
} | ||
m[string(addr)] = struct{}{} | ||
} | ||
|
||
h.blocklist = m | ||
return nil | ||
} | ||
|
||
func (h ProposalHandler) ValidateTransactions(txs [][]byte) error { | ||
for _, txBz := range txs { | ||
tx, err := h.txDecoder(txBz) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sigTx, ok := tx.(signing.SigVerifiableTx) | ||
if !ok { | ||
return fmt.Errorf("tx of type %T does not implement SigVerifiableTx", tx) | ||
} | ||
|
||
for _, signer := range sigTx.GetSigners() { | ||
if _, ok := h.blocklist[string(signer)]; ok { | ||
return fmt.Errorf("signer is blocked: %s", sdk.AccAddress(signer).String()) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (h ProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHandler { | ||
return func(ctx sdk.Context, req abci.RequestPrepareProposal) abci.ResponsePrepareProposal { | ||
if err := h.ValidateTransactions(req.Txs); err != nil { | ||
panic(err) | ||
} | ||
|
||
return abci.ResponsePrepareProposal{Txs: req.Txs} | ||
} | ||
} | ||
|
||
func (h ProposalHandler) ProcessProposalHandler() sdk.ProcessProposalHandler { | ||
return func(ctx sdk.Context, req abci.RequestProcessProposal) abci.ResponseProcessProposal { | ||
if err := h.ValidateTransactions(req.Txs); err != nil { | ||
panic(err) | ||
} | ||
|
||
return abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_ACCEPT} | ||
} | ||
} |