-
Notifications
You must be signed in to change notification settings - Fork 193
/
bytesblock.go
38 lines (32 loc) · 926 Bytes
/
bytesblock.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
package core
import (
"fmt"
"math"
)
// BytesBlock tracks the amount of bytes available during execution of the transactions
// in a block. The zero value is a block with zero bytes available.
type BytesBlock uint64
// SetLimit makes bytes available to use.
func (bp *BytesBlock) SetLimit(amount uint64) *BytesBlock {
if uint64(*bp) > math.MaxUint64-amount {
panic("block's bytes pushed above uint64")
}
*(*uint64)(bp) += amount
return bp
}
// SubBytes deducts the given amount from the block if enough bytes are
// available and returns an error otherwise.
func (bp *BytesBlock) SubBytes(amount uint64) error {
if uint64(*bp) < amount {
return ErrBytesLimitReached
}
*(*uint64)(bp) -= amount
return nil
}
// BytesLeft returns the amount of gas remaining in the pool.
func (bp *BytesBlock) BytesLeft() uint64 {
return uint64(*bp)
}
func (bp *BytesBlock) String() string {
return fmt.Sprintf("%d", *bp)
}