github.com/Finschia/finschia-sdk@v0.48.1/types/handler.go (about)

     1  package types
     2  
     3  // Handler defines the core of the state transition function of an application.
     4  type Handler func(ctx Context, msg Msg) (*Result, error)
     5  
     6  // AnteHandler authenticates transactions, before their internal messages are handled.
     7  // If newCtx.IsZero(), ctx is used instead.
     8  type AnteHandler func(ctx Context, tx Tx, simulate bool) (newCtx Context, err error)
     9  
    10  // AnteDecorator wraps the next AnteHandler to perform custom pre- and post-processing.
    11  type AnteDecorator interface {
    12  	AnteHandle(ctx Context, tx Tx, simulate bool, next AnteHandler) (newCtx Context, err error)
    13  }
    14  
    15  // ChainDecorator chains AnteDecorators together with each AnteDecorator
    16  // wrapping over the decorators further along chain and returns a single AnteHandler.
    17  //
    18  // NOTE: The first element is outermost decorator, while the last element is innermost
    19  // decorator. Decorator ordering is critical since some decorators will expect
    20  // certain checks and updates to be performed (e.g. the Context) before the decorator
    21  // is run. These expectations should be documented clearly in a CONTRACT docline
    22  // in the decorator's godoc.
    23  //
    24  // NOTE: Any application that uses GasMeter to limit transaction processing cost
    25  // MUST set GasMeter with the FIRST AnteDecorator. Failing to do so will cause
    26  // transactions to be processed with an infinite gasmeter and open a DOS attack vector.
    27  // Use `ante.SetUpContextDecorator` or a custom Decorator with similar functionality.
    28  // Returns nil when no AnteDecorator are supplied.
    29  func ChainAnteDecorators(chain ...AnteDecorator) AnteHandler {
    30  	if len(chain) == 0 {
    31  		return nil
    32  	}
    33  
    34  	// handle non-terminated decorators chain
    35  	if (chain[len(chain)-1] != Terminator{}) {
    36  		chain = append(chain, Terminator{})
    37  	}
    38  
    39  	return func(ctx Context, tx Tx, simulate bool) (Context, error) {
    40  		return chain[0].AnteHandle(ctx, tx, simulate, ChainAnteDecorators(chain[1:]...))
    41  	}
    42  }
    43  
    44  // Terminator AnteDecorator will get added to the chain to simplify decorator code
    45  // Don't need to check if next == nil further up the chain
    46  //
    47  //	                      ______
    48  //	                   <((((((\\\
    49  //	                   /      . }\
    50  //	                   ;--..--._|}
    51  //	(\                 '--/\--'  )
    52  //	 \\                | '-'  :'|
    53  //	  \\               . -==- .-|
    54  //	   \\               \.__.'   \--._
    55  //	   [\\          __.--|       //  _/'--.
    56  //	   \ \\       .'-._ ('-----'/ __/      \
    57  //	    \ \\     /   __>|      | '--.       |
    58  //	     \ \\   |   \   |     /    /       /
    59  //	      \ '\ /     \  |     |  _/       /
    60  //	       \  \       \ |     | /        /
    61  //	 snd    \  \      \        /
    62  type Terminator struct{}
    63  
    64  // Simply return provided Context and nil error
    65  func (t Terminator) AnteHandle(ctx Context, _ Tx, _ bool, _ AnteHandler) (Context, error) {
    66  	return ctx, nil
    67  }