-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathutils.go
166 lines (143 loc) · 4.4 KB
/
utils.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
// Copyright 2021 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package codec
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"
"github.com/streamingfast/eth-go"
pbeth "github.com/streamingfast/firehose-ethereum/types/pb/sf/ethereum/type/v2"
"google.golang.org/protobuf/types/known/timestamppb"
)
func FromInt32(input string, tag string) int32 {
value, err := strconv.ParseInt(input, 10, 32)
if err != nil {
panic(fmt.Errorf("%s failed to parse: %s", tag, err))
}
return int32(value)
}
func FromUint32(input string, tag string) uint32 {
value, err := strconv.ParseUint(input, 10, 32)
if err != nil {
panic(fmt.Errorf("%s failed to parse: %s", tag, err))
}
return uint32(value)
}
func FromUint64(input string, tag string) uint64 {
value, err := strconv.ParseUint(input, 10, 64)
if err != nil {
panic(fmt.Errorf("%s failed to parse: %s", tag, err))
}
return value
}
func FromHex(input string, tag string) []byte {
// The `.` means the value is not present for this field, so let's skip it
if len(input) == 0 || input == "." {
return nil
}
bytes, err := DecodeHex(input)
if err != nil {
panic(fmt.Errorf("%s unable to decode hex string %q: %s", tag, input, err))
}
return bytes
}
func DecodeHex(input string) ([]byte, error) {
bytes, err := hex.DecodeString(SanitizeHex(input))
if err != nil {
return nil, err
}
return bytes, nil
}
// CanonicalHex receives an input and return it's canonical form,
// i.e. the single unique well-formed which in our case is an all-lower
// case version with even number of characters.
//
// The only differences with `SanitizeHexInput` here is an additional
// call to `strings.ToLower` before returning the result.
func CanonicalHex(input string) string {
return strings.ToLower(SanitizeHex(input))
}
// PrefixedHex is CanonicalHex but with 0x prefix
func PrefixedHex(input string) string {
return "0x" + CanonicalHex(input)
}
// ConcatHex concatenates sanitized hex strings
func ConcatHex(with0x bool, in ...string) (out string) {
if with0x {
out = "0x"
}
for _, s := range in {
out += SanitizeHex(s)
}
return
}
// SanitizeHex removes the prefix `0x` if it exists
// and ensures there is an even number of characters in the string,
// padding on the left of the string is it's not the case.
func SanitizeHex(input string) string {
if Has0xPrefix(input) {
input = input[2:]
}
if len(input)%2 != 0 {
input = "0" + input
}
return strings.ToLower(input)
}
func FromHeader(header *BlockHeader) *pbeth.BlockHeader {
return &pbeth.BlockHeader{
ParentHash: header.ParentHash,
UncleHash: header.UncleHash,
Coinbase: header.Coinbase,
StateRoot: header.Root,
TransactionsRoot: header.TxHash,
ReceiptRoot: header.ReceiptHash,
LogsBloom: header.Bloom,
Difficulty: pbeth.BigIntFromBytes(header.Difficulty),
Number: uint64(header.Number),
GasLimit: uint64(header.GasLimit),
GasUsed: uint64(header.GasUsed),
Timestamp: timestamppb.New(time.Unix(int64(header.Time), 0)),
ExtraData: header.Extra,
MixHash: header.MixDigest,
Nonce: uint64(header.Nonce),
Hash: header.Hash,
BaseFeePerGas: pbeth.BigIntFromBytes(header.BaseFeePerGas),
WithdrawalsRoot: header.WithdrawalsHash,
TxDependency: Uint64NestedArrayFromEthUint(header.TxDependency),
BlobGasUsed: (*uint64)(header.BlobGasUsed),
ExcessBlobGas: (*uint64)(header.ExcessBlobGas),
ParentBeaconRoot: header.ParentBeaconRoot,
}
}
func Uint64NestedArrayFromEthUint(in [][]eth.Uint64) *pbeth.Uint64NestedArray {
if in == nil {
return nil
}
out := &pbeth.Uint64NestedArray{}
for _, v := range in {
out.Val = append(out.Val, &pbeth.Uint64Array{
Val: toUint64Array(v),
})
}
return out
}
func toUint64Array(in []eth.Uint64) []uint64 {
out := make([]uint64, len(in))
for i, el := range in {
out[i] = uint64(el)
}
return out
}