-
Notifications
You must be signed in to change notification settings - Fork 145
/
limits.go
78 lines (68 loc) · 2.02 KB
/
limits.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
package gas
import (
"errors"
"fmt"
"io"
"github.com/iotaledger/wasp/packages/util/rwutil"
)
type Limits struct {
MaxGasPerBlock uint64 `json:"maxGasPerBlock" swagger:"desc(The maximum gas per block),required"`
MinGasPerRequest uint64 `json:"minGasPerRequest" swagger:"desc(The minimum gas per request),required"`
MaxGasPerRequest uint64 `json:"maxGasPerRequest" swagger:"desc(The maximum gas per request),required"`
MaxGasExternalViewCall uint64 `json:"maxGasExternalViewCall" swagger:"desc(The maximum gas per external view call),required"`
}
var LimitsDefault = &Limits{
MaxGasPerBlock: 1_000_000_000,
MinGasPerRequest: 10_000,
MaxGasPerRequest: 50_000_000, // 20 requests per block max
MaxGasExternalViewCall: 50_000_000,
}
func LimitsFromBytes(data []byte) (*Limits, error) {
return rwutil.ReadFromBytes(data, new(Limits))
}
func (gl *Limits) IsValid() bool {
if gl.MaxGasPerBlock == 0 {
return false
}
if gl.MinGasPerRequest == 0 || gl.MinGasPerRequest > gl.MaxGasPerBlock {
return false
}
if gl.MaxGasPerRequest < gl.MinGasPerRequest {
return false
}
if gl.MaxGasExternalViewCall == 0 {
return false
}
return true
}
func (gl *Limits) Bytes() []byte {
return rwutil.WriteToBytes(gl)
}
func (gl *Limits) String() string {
return fmt.Sprintf(
"GasLimits(max/block: %d, min/req: %d, max/req: %d, max/view: %d",
gl.MaxGasPerBlock,
gl.MinGasPerRequest,
gl.MaxGasPerRequest,
gl.MaxGasExternalViewCall,
)
}
func (gl *Limits) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
gl.MaxGasPerBlock = rr.ReadGas64()
gl.MinGasPerRequest = rr.ReadGas64()
gl.MaxGasPerRequest = rr.ReadGas64()
gl.MaxGasExternalViewCall = rr.ReadGas64()
if rr.Err == nil && !gl.IsValid() {
rr.Err = errors.New("invalid gas limits")
}
return rr.Err
}
func (gl *Limits) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.WriteGas64(gl.MaxGasPerBlock)
ww.WriteGas64(gl.MinGasPerRequest)
ww.WriteGas64(gl.MaxGasPerRequest)
ww.WriteGas64(gl.MaxGasExternalViewCall)
return ww.Err
}