-
Notifications
You must be signed in to change notification settings - Fork 178
/
config.go
101 lines (84 loc) · 2.93 KB
/
config.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package collection
import (
"github.com/onflow/flow-go/model/flow"
)
const (
DefaultMaxCollectionSize uint = 100 // max 100 transactions per collection
DefaultExpiryBuffer uint = 15 // 15 blocks for collections to be included
DefaultMaxPayerTransactionRate float64 = 0 // no rate limiting
DefaultMaxCollectionByteSize uint64 = 1750000 // ~1.75MB. This is slightly higher than the limit on single tx size, which is 1.5MB
DefaultMaxCollectionTotalGas uint64 = 10000000 // 10M
)
// Config is the configurable options for the collection builder.
type Config struct {
// MaxCollectionSize is the maximum size of collections.
MaxCollectionSize uint
// ExpiryBuffer is how much buffer we add when considering transaction
// expiry. If the buffer is set to 10, and a transaction actually expires
// in 15 blocks, we consider it expired in 5 (15-10) blocks. This accounts
// for the time between the collection being built and being included in
// block.
ExpiryBuffer uint
// MaxPayerTransactionRate is the maximum number of transactions per payer
// per collection. Fractional values greater than 1 are rounded down.
// Fractional values 0<k<1 mean that only 1 transaction every ceil(1/k)
// collections is allowed.
//
// A negative value or 0 indicates no rate limiting.
MaxPayerTransactionRate float64
// UnlimitedPayer is a set of addresses which are not affected by per-payer
// rate limiting.
UnlimitedPayers map[flow.Address]struct{}
// MaxCollectionByteSize is the maximum byte size of a collection.
MaxCollectionByteSize uint64
// MaxCollectionTotalGas is the maximum of total of gas per collection (sum of maxGasLimit over transactions)
MaxCollectionTotalGas uint64
}
func DefaultConfig() Config {
return Config{
MaxCollectionSize: DefaultMaxCollectionSize,
ExpiryBuffer: DefaultExpiryBuffer,
MaxPayerTransactionRate: DefaultMaxPayerTransactionRate,
UnlimitedPayers: make(map[flow.Address]struct{}), // no unlimited payers
MaxCollectionByteSize: DefaultMaxCollectionByteSize,
MaxCollectionTotalGas: DefaultMaxCollectionTotalGas,
}
}
type Opt func(config *Config)
func WithMaxCollectionSize(size uint) Opt {
return func(c *Config) {
c.MaxCollectionSize = size
}
}
func WithExpiryBuffer(buf uint) Opt {
return func(c *Config) {
c.ExpiryBuffer = buf
}
}
func WithMaxPayerTransactionRate(rate float64) Opt {
return func(c *Config) {
if rate < 0 {
rate = 0
}
c.MaxPayerTransactionRate = rate
}
}
func WithUnlimitedPayers(payers ...flow.Address) Opt {
lookup := make(map[flow.Address]struct{})
for _, payer := range payers {
lookup[payer] = struct{}{}
}
return func(c *Config) {
c.UnlimitedPayers = lookup
}
}
func WithMaxCollectionByteSize(limit uint64) Opt {
return func(c *Config) {
c.MaxCollectionByteSize = limit
}
}
func WithMaxCollectionTotalGas(limit uint64) Opt {
return func(c *Config) {
c.MaxCollectionTotalGas = limit
}
}