-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
303 lines (263 loc) · 6.98 KB
/
util.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
package utari
import (
"crypto/sha256"
"encoding/json"
"fmt"
aero "github.com/aerospike/aerospike-client-go"
)
// blockテーブル用のkeyを取得する関数
func getBlockKey(hash string) (*aero.Key, error) {
namespace := GetAerospikeNamespace()
table := GetAerospikeBlockTable()
key, err := aero.NewKey(namespace, table, hash)
if err != nil {
return nil, err
}
return key, nil
}
// transactionテーブル用のkeyを取得する関数
func getTransactionKey(hash string) (*aero.Key, error) {
namespace := GetAerospikeNamespace()
table := GetAerospikeTxTable()
key, err := aero.NewKey(namespace, table, hash)
if err != nil {
return nil, err
}
return key, nil
}
func getBalanceKey(address string) (*aero.Key, error) {
namespace := GetAerospikeNamespace()
table := "balance"
key, err := aero.NewKey(namespace, table, address)
if err != nil {
return nil, err
}
return key, nil
}
func getUnsettledKey(address string) (*aero.Key, error) {
namespace := GetAerospikeNamespace()
table := "unsettled"
key, err := aero.NewKey(namespace, table, address)
if err != nil {
return nil, err
}
return key, nil
}
// GetHash はkeyとして利用するhash値を取得する関数
func GetHash(v interface{}) string {
// 構造体を[]byteに変換
byteData, err := json.Marshal(v)
if err != nil {
panic(err)
}
// ハッシュ関数にかける
checksum := sha256.Sum256(byteData)
// 文字列として取得する
hash := fmt.Sprintf("%x", checksum)
return hash
}
// Block構造体 to binmap
func blockToBinMap(b Block) aero.BinMap {
return aero.BinMap{
"Id": b.Id,
"Version": b.Version,
"Prehash": b.Prehash,
"Merkleroot": b.Merkleroot,
"Timestamp": b.Timestamp,
"Level": b.Level,
"Nonce": b.Nonce,
"Size": b.Size,
"Txcount": b.Txcount,
"TxidList": b.TxidList,
}
}
// Transaction構造体 to binmap
func transactionToBinMap(t Transaction) aero.BinMap {
return aero.BinMap{
"Txid": t.Txid,
"Output": t.Output,
"Input": t.Input,
"Amount": t.Amount,
"Timestamp": t.Timestamp,
"Sign": t.Sign,
"Pubkey": t.Pubkey,
}
}
// binmap to Block構造体
func binMapToBlock(record *aero.Record) (Block, error) {
var block Block
binMap := record.Bins
// Idの型アサーション
id, ok := binMap["Id"].(string)
if !ok {
return Block{}, fmt.Errorf("failed Id assertion")
}
block.Id = id
// Versionの型アサーション
version, ok := binMap["Version"].(int)
if !ok {
return Block{}, fmt.Errorf("failed Version assertion")
}
block.Version = int32(version)
// Prehashの型アサーション
prehash, ok := binMap["Prehash"].(string)
if !ok {
return Block{}, fmt.Errorf("failed Prehash assertion")
}
block.Prehash = prehash
// Merklerootの型アサーション
merkleroot, ok := binMap["Merkleroot"].(string)
if !ok {
return Block{}, fmt.Errorf("failed Merkleroot assertion")
}
block.Merkleroot = merkleroot
// Timestampの型アサーション
timestamp, ok := binMap["Timestamp"].(string)
if !ok {
return Block{}, fmt.Errorf("failed Timestamp assertion")
}
block.Timestamp = timestamp
// Levelの型アサーション
level, ok := binMap["Level"].(string)
if !ok {
return Block{}, fmt.Errorf("failed Level assertion")
}
block.Level = level
// Nonceの型アサーション
nonce, ok := binMap["Nonce"].(int)
if !ok {
return Block{}, fmt.Errorf("failed Nonce assertion")
}
block.Nonce = uint32(nonce)
// Sizeの型アサーション
size, ok := binMap["Size"].(int)
if !ok {
return Block{}, fmt.Errorf("failed Size assertion")
}
block.Size = int64(size)
// Txcountの型アサーション
txcount, ok := binMap["Txcount"].(int)
if !ok {
return Block{}, fmt.Errorf("failed Txcount assertion")
}
block.Txcount = int64(txcount)
// TxidListの型アサーション
var txidList []string
// まずはスライスの型アサーション
interfaceSlice, ok := binMap["TxidList"].([]interface{})
if !ok {
return Block{}, fmt.Errorf("failed TxidList assertion")
}
// スライスの中身を型アサーション
for _, value := range interfaceSlice {
txid, ok := value.(string)
if !ok {
return Block{}, fmt.Errorf("failed TxidList assertion")
}
txidList = append(txidList, txid)
}
block.TxidList = txidList
return block, nil
}
// binmap to Transaction構造体
func binMapToTransaction(record *aero.Record) (Transaction, error) {
var tx Transaction
binMap := record.Bins
// txidの型アサーション
txid, ok := binMap["Txid"].(string)
if !ok {
return Transaction{}, fmt.Errorf("failed Txid assertion")
}
tx.Txid = txid
// outputの型アサーション
output, ok := binMap["Output"].(string)
if !ok {
return Transaction{}, fmt.Errorf("failed output assertion")
}
tx.Output = output
// Inputの型アサーション
input, ok := binMap["Input"].(string)
if !ok {
return Transaction{}, fmt.Errorf("failed input assertion")
}
tx.Input = input
// Amountの型アサーション
amountFloat, ok := binMap["Amount"].(float64)
if !ok {
amountInt, ok := binMap["Amount"].(int)
if !ok {
return Transaction{}, fmt.Errorf("failed Amount assertion")
}
amountFloat = float64(amountInt)
}
tx.Amount = amountFloat
// Timestampの型アサーション
timestamp, ok := binMap["Timestamp"].(string)
if !ok {
return Transaction{}, fmt.Errorf("failed Timestamp assertion")
}
tx.Timestamp = timestamp
// Signの型アサーション
sign, ok := binMap["Sign"].(string)
if !ok {
return Transaction{}, fmt.Errorf("failed sign assertion")
}
tx.Sign = sign
// Pubkeyの型アサーション
pubkey, ok := binMap["Pubkey"].(string)
if !ok {
return Transaction{}, fmt.Errorf("failed pubkey assertion")
}
tx.Pubkey = pubkey
return tx, nil
}
func binMapToBalance(record *aero.Record) (Balance, error) {
var balance Balance
binMap := record.Bins
balanceFloat, ok := binMap["Balance"].(float64)
if !ok {
balanceInt, ok := binMap["Balance"].(int)
if !ok {
return Balance{}, fmt.Errorf("failed Balance assertion")
}
balanceFloat = float64(balanceInt)
}
balance.Balance = balanceFloat
address, ok := binMap["Address"].(string)
if !ok {
return Balance{}, fmt.Errorf("failed Address assertion")
}
balance.Address = address
return balance, nil
}
func balanceToBinMap(b Balance) aero.BinMap {
return aero.BinMap{
"Address": b.Address,
"Balance": b.Balance,
}
}
func binMapToUnsettled(record *aero.Record) (Unsettled, error) {
var unsettled Unsettled
binMap := record.Bins
unsettledFloat, ok := binMap["Unsettled"].(float64)
if !ok {
unsettledInt, ok := binMap["Unsettled"].(int)
if !ok {
return Unsettled{}, fmt.Errorf("failed Unssetled assertion")
}
unsettledFloat = float64(unsettledInt)
}
unsettled.Unsettled = unsettledFloat
address, ok := binMap["Address"].(string)
if !ok {
return Unsettled{}, fmt.Errorf("failed Address assertion")
}
unsettled.Address = address
return unsettled, nil
}
func unsettledToBinMap(b Unsettled) aero.BinMap {
return aero.BinMap{
"Address": b.Address,
"Unsettled": b.Unsettled,
}
}