github.com/koko1123/flow-go-1@v0.29.6/module/builder/collection/config.go (about)

     1  package collection
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/model/flow"
     5  )
     6  
     7  const (
     8  	DefaultExpiryBuffer            uint    = 15 // 15 blocks for collections to be included
     9  	DefaultMaxPayerTransactionRate float64 = 0  // no rate limiting
    10  )
    11  
    12  // Config is the configurable options for the collection builder.
    13  type Config struct {
    14  
    15  	// MaxCollectionSize is the maximum size of collections.
    16  	MaxCollectionSize uint
    17  
    18  	// ExpiryBuffer is how much buffer we add when considering transaction
    19  	// expiry. If the buffer is set to 10, and a transaction actually expires
    20  	// in 15 blocks, we consider it expired in 5 (15-10) blocks. This accounts
    21  	// for the time between the collection being built and being included in
    22  	// block.
    23  	ExpiryBuffer uint
    24  
    25  	// DryRunRateLimit will, when enabled, log when a transaction would have
    26  	// been omitted from a collection due to rate limiting settings. Rate
    27  	// limiting settings are not actually enforced while dry-run is true.
    28  	DryRunRateLimit bool
    29  
    30  	// MaxPayerTransactionRate is the maximum number of transactions per payer
    31  	// per collection. Fractional values greater than 1 are rounded down.
    32  	// Fractional values 0<k<1 mean that only 1 transaction every ceil(1/k)
    33  	// collections is allowed.
    34  	//
    35  	// A negative value or 0 indicates no rate limiting.
    36  	MaxPayerTransactionRate float64
    37  
    38  	// UnlimitedPayer is a set of addresses which are not affected by per-payer
    39  	// rate limiting.
    40  	UnlimitedPayers map[flow.Address]struct{}
    41  
    42  	// MaxCollectionByteSize is the maximum byte size of a collection.
    43  	MaxCollectionByteSize uint64
    44  
    45  	// MaxCollectionTotalGas is the maximum of total of gas per collection (sum of maxGasLimit over transactions)
    46  	MaxCollectionTotalGas uint64
    47  }
    48  
    49  func DefaultConfig() Config {
    50  	return Config{
    51  		MaxCollectionSize:       flow.DefaultMaxCollectionSize,
    52  		ExpiryBuffer:            DefaultExpiryBuffer,
    53  		DryRunRateLimit:         false,
    54  		MaxPayerTransactionRate: DefaultMaxPayerTransactionRate,
    55  		UnlimitedPayers:         make(map[flow.Address]struct{}), // no unlimited payers
    56  		MaxCollectionByteSize:   flow.DefaultMaxCollectionByteSize,
    57  		MaxCollectionTotalGas:   flow.DefaultMaxCollectionTotalGas,
    58  	}
    59  }
    60  
    61  type Opt func(config *Config)
    62  
    63  func WithMaxCollectionSize(size uint) Opt {
    64  	return func(c *Config) {
    65  		c.MaxCollectionSize = size
    66  	}
    67  }
    68  
    69  func WithExpiryBuffer(buf uint) Opt {
    70  	return func(c *Config) {
    71  		c.ExpiryBuffer = buf
    72  	}
    73  }
    74  
    75  func WithRateLimitDryRun(dryRun bool) Opt {
    76  	return func(c *Config) {
    77  		c.DryRunRateLimit = dryRun
    78  	}
    79  }
    80  
    81  func WithMaxPayerTransactionRate(rate float64) Opt {
    82  	return func(c *Config) {
    83  		if rate < 0 {
    84  			rate = 0
    85  		}
    86  		c.MaxPayerTransactionRate = rate
    87  	}
    88  }
    89  
    90  func WithUnlimitedPayers(payers ...flow.Address) Opt {
    91  	lookup := make(map[flow.Address]struct{})
    92  	for _, payer := range payers {
    93  		lookup[payer] = struct{}{}
    94  	}
    95  	return func(c *Config) {
    96  		c.UnlimitedPayers = lookup
    97  	}
    98  }
    99  
   100  func WithMaxCollectionByteSize(limit uint64) Opt {
   101  	return func(c *Config) {
   102  		c.MaxCollectionByteSize = limit
   103  	}
   104  }
   105  
   106  func WithMaxCollectionTotalGas(limit uint64) Opt {
   107  	return func(c *Config) {
   108  		c.MaxCollectionTotalGas = limit
   109  	}
   110  }