github.com/cosmos/cosmos-sdk@v0.50.10/types/handler.go (about)

     1  package types
     2  
     3  // AnteHandler authenticates transactions, before their internal messages are handled.
     4  // If newCtx.IsZero(), ctx is used instead.
     5  type AnteHandler func(ctx Context, tx Tx, simulate bool) (newCtx Context, err error)
     6  
     7  // PostHandler like AnteHandler but it executes after RunMsgs. Runs on success
     8  // or failure and enables use cases like gas refunding.
     9  type PostHandler func(ctx Context, tx Tx, simulate, success bool) (newCtx Context, err error)
    10  
    11  // AnteDecorator wraps the next AnteHandler to perform custom pre-processing.
    12  type AnteDecorator interface {
    13  	AnteHandle(ctx Context, tx Tx, simulate bool, next AnteHandler) (newCtx Context, err error)
    14  }
    15  
    16  // PostDecorator wraps the next PostHandler to perform custom post-processing.
    17  type PostDecorator interface {
    18  	PostHandle(ctx Context, tx Tx, simulate, success bool, next PostHandler) (newCtx Context, err error)
    19  }
    20  
    21  // ChainAnteDecorators ChainDecorator chains AnteDecorators together with each AnteDecorator
    22  // wrapping over the decorators further along chain and returns a single AnteHandler.
    23  //
    24  // NOTE: The first element is outermost decorator, while the last element is innermost
    25  // decorator. Decorator ordering is critical since some decorators will expect
    26  // certain checks and updates to be performed (e.g. the Context) before the decorator
    27  // is run. These expectations should be documented clearly in a CONTRACT docline
    28  // in the decorator's godoc.
    29  //
    30  // NOTE: Any application that uses GasMeter to limit transaction processing cost
    31  // MUST set GasMeter with the FIRST AnteDecorator. Failing to do so will cause
    32  // transactions to be processed with an infinite gasmeter and open a DOS attack vector.
    33  // Use `ante.SetUpContextDecorator` or a custom Decorator with similar functionality.
    34  // Returns nil when no AnteDecorator are supplied.
    35  func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler {
    36  	if len(chain) == 0 {
    37  		return nil
    38  	}
    39  
    40  	handlerChain := make([]AnteHandler, len(chain)+1)
    41  	// set the terminal AnteHandler decorator
    42  	handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate bool) (Context, error) {
    43  		return ctx, nil
    44  	}
    45  	for i := 0; i < len(chain); i++ {
    46  		ii := i
    47  		handlerChain[ii] = func(ctx Context, tx Tx, simulate bool) (Context, error) {
    48  			return chain[ii].AnteHandle(ctx, tx, simulate, handlerChain[ii+1])
    49  		}
    50  	}
    51  
    52  	return handlerChain[0]
    53  }
    54  
    55  // ChainPostDecorators chains PostDecorators together with each PostDecorator
    56  // wrapping over the decorators further along chain and returns a single PostHandler.
    57  //
    58  // NOTE: The first element is outermost decorator, while the last element is innermost
    59  // decorator. Decorator ordering is critical since some decorators will expect
    60  // certain checks and updates to be performed (e.g. the Context) before the decorator
    61  // is run. These expectations should be documented clearly in a CONTRACT docline
    62  // in the decorator's godoc.
    63  func ChainPostDecorators(chain ...PostDecorator) PostHandler {
    64  	if len(chain) == 0 {
    65  		return nil
    66  	}
    67  
    68  	handlerChain := make([]PostHandler, len(chain)+1)
    69  	// set the terminal PostHandler decorator
    70  	handlerChain[len(chain)] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
    71  		return ctx, nil
    72  	}
    73  	for i := 0; i < len(chain); i++ {
    74  		ii := i
    75  		handlerChain[ii] = func(ctx Context, tx Tx, simulate, success bool) (Context, error) {
    76  			return chain[ii].PostHandle(ctx, tx, simulate, success, handlerChain[ii+1])
    77  		}
    78  	}
    79  	return handlerChain[0]
    80  }
    81  
    82  // Terminator AnteDecorator will get added to the chain to simplify decorator code
    83  // Don't need to check if next == nil further up the chain
    84  //
    85  //	                      ______
    86  //	                   <((((((\\\
    87  //	                   /      . }\
    88  //	                   ;--..--._|}
    89  //	(\                 '--/\--'  )
    90  //	 \\                | '-'  :'|
    91  //	  \\               . -==- .-|
    92  //	   \\               \.__.'   \--._
    93  //	   [\\          __.--|       //  _/'--.
    94  //	   \ \\       .'-._ ('-----'/ __/      \
    95  //	    \ \\     /   __>|      | '--.       |
    96  //	     \ \\   |   \   |     /    /       /
    97  //	      \ '\ /     \  |     |  _/       /
    98  //	       \  \       \ |     | /        /
    99  //	 snd    \  \      \        /
   100  //
   101  // Deprecated: Terminator is retired (ref https://github.com/cosmos/cosmos-sdk/pull/16076).
   102  type Terminator struct{}
   103  
   104  // AnteHandle returns the provided Context and nil error
   105  func (t Terminator) AnteHandle(ctx Context, _ Tx, _ bool, _ AnteHandler) (Context, error) {
   106  	return ctx, nil
   107  }
   108  
   109  // PostHandle returns the provided Context and nil error
   110  func (t Terminator) PostHandle(ctx Context, _ Tx, _, _ bool, _ PostHandler) (Context, error) {
   111  	return ctx, nil
   112  }