-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
address_range.go
105 lines (87 loc) · 2.87 KB
/
address_range.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
102
103
104
105
package s4
import (
"bytes"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
)
// AddressRange represents a range of Ethereum addresses.
type AddressRange struct {
// MinAddress (inclusive).
MinAddress *ubig.Big
// MaxAddress (inclusive).
MaxAddress *ubig.Big
}
var (
ErrInvalidIntervals = errors.New("invalid intervals value")
MinAddress = ubig.New(common.BytesToAddress(bytes.Repeat([]byte{0x00}, common.AddressLength)).Big())
MaxAddress = ubig.New(common.BytesToAddress(bytes.Repeat([]byte{0xff}, common.AddressLength)).Big())
)
// NewFullAddressRange creates AddressRange for all address space: 0x00..-0xFF..
func NewFullAddressRange() *AddressRange {
return &AddressRange{
MinAddress: MinAddress,
MaxAddress: MaxAddress,
}
}
// NewSingleAddressRange creates AddressRange for a single address.
func NewSingleAddressRange(address *ubig.Big) (*AddressRange, error) {
if address == nil || address.Cmp(MinAddress) < 0 || address.Cmp(MaxAddress) > 0 {
return nil, errors.New("invalid address")
}
return &AddressRange{
MinAddress: address,
MaxAddress: address,
}, nil
}
// NewInitialAddressRangeForIntervals splits the full address space with intervals,
// and returns a range for the first interval.
// Number of intervals must be > 0 and a power of 2.
func NewInitialAddressRangeForIntervals(intervals uint) (*AddressRange, error) {
if intervals == 0 || (intervals&(intervals-1) != 0) {
return nil, ErrInvalidIntervals
}
if intervals == 1 {
return NewFullAddressRange(), nil
}
divisor := big.NewInt(int64(intervals))
maxPlusOne := MaxAddress.Add(ubig.NewI(1))
interval := ubig.New(new(big.Int).Div(maxPlusOne.ToInt(), divisor))
return &AddressRange{
MinAddress: MinAddress,
MaxAddress: MinAddress.Add(interval).Sub(ubig.NewI(1)),
}, nil
}
// Advances the AddressRange by r.Interval. Has no effect for NewFullAddressRange().
// When it reaches the end of the address space, it resets to the initial state,
// returned by NewAddressRangeForFirstInterval().
func (r *AddressRange) Advance() {
if r == nil {
return
}
interval := r.Interval()
r.MinAddress = r.MinAddress.Add(interval)
r.MaxAddress = r.MaxAddress.Add(interval)
if r.MinAddress.Cmp(MaxAddress) >= 0 {
r.MinAddress = MinAddress
r.MaxAddress = MinAddress.Add(interval).Sub(ubig.NewI(1))
}
if r.MaxAddress.Cmp(MaxAddress) > 0 {
r.MaxAddress = MaxAddress
}
}
// Contains returns true if the given address belongs to the range.
func (r *AddressRange) Contains(address *ubig.Big) bool {
if r == nil {
return false
}
return r.MinAddress.Cmp(address) <= 0 && r.MaxAddress.Cmp(address) >= 0
}
// Interval returns the interval between max and min address plus one.
func (r *AddressRange) Interval() *ubig.Big {
if r == nil {
return nil
}
return r.MaxAddress.Sub(r.MinAddress).Add(ubig.NewI(1))
}