forked from dominant-strategies/go-quai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal_address.go
169 lines (150 loc) · 4.21 KB
/
external_address.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package common
import (
"bytes"
"database/sql/driver"
"encoding/hex"
"fmt"
"reflect"
"github.com/dominant-strategies/go-quai/common/hexutil"
"golang.org/x/crypto/sha3"
)
type ExternalAddress [AddressLength]byte
// Bytes gets the string representation of the underlying address.
func (a ExternalAddress) Bytes() []byte { return a[:] }
// Hash converts an address to a hash by left-padding it with zeros.
func (a ExternalAddress) Hash() Hash { return BytesToHash(a[:]) }
// Hex returns a hex string representation of the address.
func (a ExternalAddress) Hex() string {
return string(a.checksumHex())
}
// String implements fmt.Stringer.
func (a ExternalAddress) String() string {
return a.Hex()
}
func (a *ExternalAddress) checksumHex() []byte {
buf := a.hex()
// compute checksum
sha := sha3.NewLegacyKeccak256()
sha.Write(buf[2:])
hash := sha.Sum(nil)
for i := 2; i < len(buf); i++ {
hashByte := hash[(i-2)/2]
if i%2 == 0 {
hashByte = hashByte >> 4
} else {
hashByte &= 0xf
}
if buf[i] > '9' && hashByte > 7 {
buf[i] -= 32
}
}
return buf[:]
}
func (a ExternalAddress) hex() []byte {
var buf [len(a)*2 + 2]byte
copy(buf[:2], "0x")
hex.Encode(buf[2:], a[:])
return buf[:]
}
// Format implements fmt.Formatter.
// Address supports the %v, %s, %v, %x, %X and %d format verbs.
func (a ExternalAddress) Format(s fmt.State, c rune) {
switch c {
case 'v', 's':
s.Write(a.checksumHex())
case 'q':
q := []byte{'"'}
s.Write(q)
s.Write(a.checksumHex())
s.Write(q)
case 'x', 'X':
// %x disables the checksum.
hex := a.hex()
if !s.Flag('#') {
hex = hex[2:]
}
if c == 'X' {
hex = bytes.ToUpper(hex)
}
s.Write(hex)
case 'd':
fmt.Fprint(s, ([len(a)]byte)(a))
default:
fmt.Fprintf(s, "%%!%c(address=%x)", c, a)
}
}
// SetBytes sets the address to the value of b.
// If b is larger than len(a), b will be cropped from the left.
func (a *ExternalAddress) setBytes(b []byte) {
if len(b) > len(a) {
b = b[len(b)-AddressLength:]
}
copy(a[AddressLength-len(b):], b)
}
// MarshalText returns the hex representation of a.
func (a ExternalAddress) MarshalText() ([]byte, error) {
return hexutil.Bytes(a[:]).MarshalText()
}
// UnmarshalText parses a hash in hex syntax.
func (a *ExternalAddress) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedText("Address", input, a[:])
}
// UnmarshalJSON parses a hash in hex syntax.
func (a *ExternalAddress) UnmarshalJSON(input []byte) error {
return hexutil.UnmarshalFixedJSON(reflect.TypeOf(ExternalAddress{}), input, a[:])
}
// Scan implements Scanner for database/sql.
func (a *ExternalAddress) Scan(src interface{}) error {
srcB, ok := src.([]byte)
if !ok {
return fmt.Errorf("can't scan %T into Address", src)
}
if len(srcB) != AddressLength {
return fmt.Errorf("can't scan []byte of len %d into Address, want %d", len(srcB), AddressLength)
}
copy(a[:], srcB)
return nil
}
// Value implements valuer for database/sql.
func (a ExternalAddress) Value() (driver.Value, error) {
return a[:], nil
}
// Location looks up the chain location which contains this address
func (a ExternalAddress) Location() *Location {
R, Z, D := 0, 0, HierarchyDepth
if NodeLocation.HasRegion() {
R = NodeLocation.Region()
}
if NodeLocation.HasZone() {
Z = NodeLocation.Zone()
}
// Search zone->region->prime address spaces in-slice first, and then search
// zone->region out-of-slice address spaces next. This minimizes expected
// search time under the following assumptions:
// * a node is more likely to encounter a TX from its slice than from another
// * we expect `>= Z` `zone` TXs for every `region` TX
// * we expect `>= R` `region` TXs for every `prime` TX
// * (and by extension) we expect `>= R*Z` `zone` TXs for every `prime` TX
primeChecked := false
for r := 0; r < NumRegionsInPrime; r++ {
for z := 0; z < NumZonesInRegion; z++ {
l := Location{byte((r + R) % D), byte((z + Z) % D)}
if l.ContainsAddress(Address{&a}) {
return &l
}
}
l := Location{byte((r + R) % D)}
if l.ContainsAddress(Address{&a}) {
return &l
}
// Check prime on first pass through slice, but not again
if !primeChecked {
primeChecked = true
l := Location{}
if l.ContainsAddress(Address{&a}) {
return &l
}
}
}
return nil
}