forked from meshplus/bitxhub-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.go
341 lines (288 loc) · 7.83 KB
/
basic.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package types
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"hash"
"math/big"
"sync"
"github.com/axiomesh/axiom-kit/types/pb"
mt "github.com/cbergoon/merkletree"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"golang.org/x/crypto/sha3"
)
// hasherPool holds LegacyKeccak256 hashers for rlpHash.
var (
hasherPool = sync.Pool{
New: func() interface{} { return sha3.NewLegacyKeccak256() },
}
ErrSyntax = fmt.Errorf("invalid hex string")
ErrMissingPrefix = fmt.Errorf("hex string without 0x prefix")
ErrOddLength = fmt.Errorf("hex string of odd length")
)
type KeccakState interface {
hash.Hash
Read([]byte) (int, error)
}
// Lengths of hashes and addresses in bytes.
const (
BloomByteLength = 256
badNibble = ^uint64(0)
)
type Hash struct {
rawHash common.Hash
hashStr string
}
func NewHash(b []byte) *Hash {
return &Hash{
rawHash: common.BytesToHash(b),
}
}
func decodeHash(data []byte) (*Hash, error) {
if len(data) != common.HashLength && len(data) != 0 {
return nil, fmt.Errorf("decode hash from bytes failed, bytes size must be %v", common.HashLength)
}
if len(data) == 0 {
return &Hash{}, nil
}
return &Hash{
rawHash: common.BytesToHash(data),
}, nil
}
func NewHashByStr(s string) *Hash {
return &Hash{
rawHash: common.HexToHash(s),
}
}
func (h *Hash) MarshalJSON() ([]byte, error) {
return []byte("\"" + h.String() + "\""), nil
}
func (h *Hash) UnmarshalJSON(data []byte) error {
if !(len(data) > 2 && data[0] == '"' && data[len(data)-1] == '"') {
return errors.New("decode hash failed, invalid format")
}
h.rawHash = common.HexToHash(string(data[1 : len(data)-1]))
return nil
}
// CalculateHash hashes the values of a TestContent
func (h *Hash) CalculateHash() ([]byte, error) {
return h.Bytes(), nil
}
// Equals tests for equality of two Contents
func (h *Hash) Equals(other mt.Content) (bool, error) {
tOther, ok := other.(*Hash)
if !ok {
return false, errors.New("parameter should be type TransactionHash")
}
return bytes.Equal(h.rawHash[:], tOther.rawHash[:]), nil
}
func (h *Hash) SetBytes(b []byte) {
h.rawHash.SetBytes(b)
h.hashStr = ""
}
func (h *Hash) ETHHash() common.Hash {
return h.rawHash
}
func (h *Hash) Bytes() []byte {
if h == nil {
return []byte{}
}
return h.rawHash.Bytes()
}
func (h *Hash) String() string {
if h.hashStr == "" {
// if hash field is empty, initialize it for only once
h.hashStr = h.rawHash.String()
}
return h.hashStr
}
func (h *Hash) Size() int {
return common.HashLength
}
type Address struct {
rawAddress common.Address
addressStr string
}
func decodeAddress(data []byte) (*Address, error) {
if len(data) != common.AddressLength && len(data) != 0 {
return nil, fmt.Errorf("decode address from bytes failed, bytes size must be %v", common.AddressLength)
}
if len(data) == 0 {
return &Address{}, nil
}
return &Address{
rawAddress: common.BytesToAddress(data),
}, nil
}
// BytesToAddress returns Address with value b.
// If b is larger than len(h), b will be cropped address the left.
func NewAddress(b []byte) *Address {
return &Address{
rawAddress: common.BytesToAddress(b),
}
}
func NewAddressByStr(s string) *Address {
return &Address{
rawAddress: common.HexToAddress(s),
}
}
func (a *Address) MarshalJSON() ([]byte, error) {
return []byte("\"" + a.String() + "\""), nil
}
func (a *Address) UnmarshalJSON(data []byte) error {
if !(len(data) > 2 && data[0] == '"' && data[len(data)-1] == '"') {
return errors.New("decode address failed, invalid format")
}
a.rawAddress = common.HexToAddress(string(data[1 : len(data)-1]))
return nil
}
// SetBytes sets the address to the value of b.
// If b is larger than len(a) it will panic.
func (a *Address) SetBytes(b []byte) {
a.rawAddress.SetBytes(b)
a.addressStr = ""
}
func (h *Address) ETHAddress() common.Address {
return h.rawAddress
}
func (a *Address) Bytes() []byte {
if a == nil {
return []byte{}
}
return a.rawAddress.Bytes()
}
// String returns an EIP55-compliant hex string representation of the address.
func (a *Address) String() string {
if a.addressStr == "" {
// if address field is empty, initialize it for only once
a.addressStr = a.rawAddress.String()
}
return a.addressStr
}
type Bloom [BloomByteLength]byte
func decodeBloom(data []byte) (*Bloom, error) {
if len(data) != BloomByteLength && len(data) != 0 {
return nil, fmt.Errorf("decode bloom from bytes failed, bytes size must be %v", BloomByteLength)
}
b := Bloom{}
if len(data) == 0 {
return &b, nil
}
copy(b[:], data)
return &b, nil
}
// Add adds d to the filter. Future calls of Test(d) will return true.
func (b *Bloom) Add(d []byte) {
b.add(d, make([]byte, 6))
}
// add is internal version of Add, which takes a scratch buffer for reuse (needs to be at least 6 bytes)
func (b *Bloom) add(d []byte, buf []byte) {
i1, v1, i2, v2, i3, v3 := bloomValues(d, buf)
b[i1] |= v1
b[i2] |= v2
b[i3] |= v3
}
// Test checks if the given topic is present in the bloom filter
func (b *Bloom) Test(topic []byte) bool {
i1, v1, i2, v2, i3, v3 := bloomValues(topic, make([]byte, 6))
return v1 == v1&b[i1] &&
v2 == v2&b[i2] &&
v3 == v3&b[i3]
}
func (b *Bloom) Bytes() []byte {
if b == nil {
return nil
}
return b[:]
}
// bloomValues returns the bytes (index-value pairs) to set for the given data
func bloomValues(data []byte, hashbuf []byte) (uint, byte, uint, byte, uint, byte) {
sha := hasherPool.Get().(KeccakState)
sha.Reset()
sha.Write(data)
sha.Read(hashbuf)
hasherPool.Put(sha)
// The actual bits to flip
v1 := byte(1 << (hashbuf[1] & 0x7))
v2 := byte(1 << (hashbuf[3] & 0x7))
v3 := byte(1 << (hashbuf[5] & 0x7))
// The indices for the bytes to OR in
i1 := BloomByteLength - uint((binary.BigEndian.Uint16(hashbuf)&0x7ff)>>3) - 1
i2 := BloomByteLength - uint((binary.BigEndian.Uint16(hashbuf[2:])&0x7ff)>>3) - 1
i3 := BloomByteLength - uint((binary.BigEndian.Uint16(hashbuf[4:])&0x7ff)>>3) - 1
return i1, v1, i2, v2, i3, v3
}
// OrBloom executes an Or operation on the bloom
func (b *Bloom) OrBloom(bl *Bloom) {
bin := new(big.Int).SetBytes(b[:])
input := new(big.Int).SetBytes(bl[:])
bin.Or(bin, input)
b.SetBytes(bin.Bytes())
}
// SetBytes sets the content of b to the given bytes.
// It panics if d is not of suitable size.
func (b *Bloom) SetBytes(d []byte) {
if len(b) < len(d) {
panic(fmt.Sprintf("bloom bytes too big %d %d", len(b), len(d)))
}
copy(b[BloomByteLength-len(d):], d)
}
func (b *Bloom) ETHBloom() types.Bloom {
if b == nil {
emptyBytes := [BloomByteLength]byte{}
return types.BytesToBloom(emptyBytes[:])
}
return types.BytesToBloom(b[:])
}
type CodecObject interface {
Marshal() ([]byte, error)
Unmarshal(buf []byte) error
}
type CodecObjectConstraint[T any] interface {
*T
CodecObject
}
func MarshalObjects[T any, Constraint CodecObjectConstraint[T]](objs []*T) ([]byte, error) {
var objsRaw [][]byte
for _, obj := range objs {
objRaw, err := Constraint(obj).Marshal()
if err != nil {
return nil, err
}
objsRaw = append(objsRaw, objRaw)
}
helper := pb.BytesSlice{
Slice: objsRaw,
}
return helper.MarshalVTStrict()
}
func UnmarshalObjects[T any, Constraint CodecObjectConstraint[T]](data []byte) ([]*T, error) {
helper := &pb.BytesSlice{}
err := helper.UnmarshalVT(data)
if err != nil {
return nil, err
}
var objs []*T
for _, objRaw := range helper.Slice {
obj := new(T)
if err := Constraint(obj).Unmarshal(objRaw); err != nil {
return nil, err
}
objs = append(objs, obj)
}
return objs, nil
}
func MarshalTransactions(objs []*Transaction) ([]byte, error) {
return MarshalObjects(objs)
}
func UnmarshalTransactions(data []byte) ([]*Transaction, error) {
return UnmarshalObjects[Transaction, *Transaction](data)
}
func MarshalReceipts(objs []*Receipt) ([]byte, error) {
return MarshalObjects(objs)
}
func UnmarshalReceipts(data []byte) ([]*Receipt, error) {
return UnmarshalObjects[Receipt, *Receipt](data)
}