forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircuit.go
More file actions
45 lines (36 loc) · 1.41 KB
/
Copy pathcircuit.go
File metadata and controls
45 lines (36 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ante
import (
"context"
"github.com/cockroachdb/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// CircuitBreaker is an interface that defines the methods for a circuit breaker.
type CircuitBreaker interface {
IsAllowed(ctx context.Context, typeURL string) (bool, error)
}
// CircuitBreakerDecorator is an AnteDecorator that checks if the transaction type is allowed to enter the mempool or be executed
type CircuitBreakerDecorator struct {
circuitKeeper CircuitBreaker
}
func NewCircuitBreakerDecorator(ck CircuitBreaker) CircuitBreakerDecorator {
return CircuitBreakerDecorator{
circuitKeeper: ck,
}
}
// If you copy this as reference and your app has the authz module enabled, you must either:
// - recessively check for nested authz.Exec messages in this function.
// - or error early if a nested authz grant is found.
// The circuit AnteHandler handles this with baseapp's service router: https://github.com/cosmos/cosmos-sdk/issues/18632.
func (cbd CircuitBreakerDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
// loop through all the messages and check if the message type is allowed
for _, msg := range tx.GetMsgs() {
isAllowed, err := cbd.circuitKeeper.IsAllowed(ctx, sdk.MsgTypeURL(msg))
if err != nil {
return ctx, err
}
if !isAllowed {
return ctx, errors.New("tx type not allowed")
}
}
return next(ctx, tx, simulate)
}