forked from FactomProject/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
463 lines (381 loc) · 10.9 KB
/
get.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom
import (
"encoding/json"
"fmt"
)
// GetECBalance returns the balance in factoshi (factoid * 1e8) of a given Entry
// Credit Public Address.
func GetECBalance(addr string) (int64, error) {
type balanceResponse struct {
Balance int64 `json:"balance"`
}
params := addressRequest{Address: addr}
req := NewJSON2Request("entry-credit-balance", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return -1, err
}
if resp.Error != nil {
return -1, resp.Error
}
balance := new(balanceResponse)
if err := json.Unmarshal(resp.JSONResult(), balance); err != nil {
return -1, err
}
return balance.Balance, nil
}
// GetFactoidBalance returns the balance in factoshi (factoid * 1e8) of a given
// Factoid Public Address.
func GetFactoidBalance(addr string) (int64, error) {
type balanceResponse struct {
Balance int64 `json:"balance"`
}
params := addressRequest{Address: addr}
req := NewJSON2Request("factoid-balance", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return -1, err
}
if resp.Error != nil {
return -1, resp.Error
}
balance := new(balanceResponse)
if err := json.Unmarshal(resp.JSONResult(), balance); err != nil {
return -1, err
}
return balance.Balance, nil
}
// GetBalanceTotals return the total value of Factoids and Entry Credits in the
// wallet according to the the server acknowledgement and the value saved in the
// blockchain.
func GetBalanceTotals() (fSaved, fAcknowledged, eSaved, eAcknowledged int64, err error) {
type multiBalanceResponse struct {
FactoidAccountBalances struct {
Ack int64 `json:"ack"`
Saved int64 `json:"saved"`
} `json:"fctaccountbalances"`
EntryCreditAccountBalances struct {
Ack int64 `json:"ack"`
Saved int64 `json:"saved"`
} `json:"ecaccountbalances"`
}
req := NewJSON2Request("wallet-balances", APICounter(), nil)
resp, err := walletRequest(req)
if err != nil {
return
} else if resp.Error != nil {
err = resp.Error
return
}
balances := new(multiBalanceResponse)
err = json.Unmarshal(resp.JSONResult(), balances)
if err != nil {
return
}
fSaved = balances.FactoidAccountBalances.Saved
fAcknowledged = balances.FactoidAccountBalances.Ack
eSaved = balances.EntryCreditAccountBalances.Saved
eAcknowledged = balances.EntryCreditAccountBalances.Ack
return
}
// GetRate returns the number of factoshis per entry credit
func GetRate() (uint64, error) {
type rateResponse struct {
Rate uint64 `json:"rate"`
}
req := NewJSON2Request("entry-credit-rate", APICounter(), nil)
resp, err := factomdRequest(req)
if err != nil {
return 0, err
}
if resp.Error != nil {
return 0, resp.Error
}
rate := new(rateResponse)
if err := json.Unmarshal(resp.JSONResult(), rate); err != nil {
return 0, err
}
return rate.Rate, nil
}
// GetDBlock requests a Directory Block from factomd by its Key Merkle Root
func GetDBlock(keymr string) (*DBlock, error) {
params := keyMRRequest{KeyMR: keymr}
req := NewJSON2Request("directory-block", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
db := new(DBlock)
if err := json.Unmarshal(resp.JSONResult(), db); err != nil {
return nil, err
}
return db, nil
}
func GetDBlockHead() (string, error) {
req := NewJSON2Request("directory-block-head", APICounter(), nil)
resp, err := factomdRequest(req)
if err != nil {
return "", err
}
if resp.Error != nil {
return "", resp.Error
}
head := new(DBHead)
if err := json.Unmarshal(resp.JSONResult(), head); err != nil {
return "", err
}
return head.KeyMR, nil
}
func GetHeights() (*HeightsResponse, error) {
req := NewJSON2Request("heights", APICounter(), nil)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
heights := new(HeightsResponse)
if err := json.Unmarshal(resp.JSONResult(), heights); err != nil {
return nil, err
}
return heights, nil
}
// GetEntry requests an Entry from factomd by its Entry Hash
func GetEntry(hash string) (*Entry, error) {
params := hashRequest{Hash: hash}
req := NewJSON2Request("entry", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
e := new(Entry)
if err := json.Unmarshal(resp.JSONResult(), e); err != nil {
return nil, err
}
return e, nil
}
// GetChainHead only returns the chainhead part of the response, so you are losing information
// returned by the api. GetChainHeadAndStatus returns the full repsonse.
// TODO: Depreciate this call, or make it return an error when the chainhead == ""
// When (chainhead == "" && err == nil ) the ChainInProcessList == true, and we could
// return an error indicating there is no chainhead found, but it will be created in the
// next block.
func GetChainHead(chainid string) (string, error) {
ch, err := getChainHead(chainid)
if err != nil {
return "", err
}
return ch.ChainHead, nil
}
type chainHeadResponse struct {
ChainHead string `json:"chainhead"`
ChainInProcessList bool `json:"chaininprocesslist"`
}
func GetChainHeadAndStatus(chainid string) (*chainHeadResponse, error) {
return getChainHead(chainid)
}
func getChainHead(chainid string) (*chainHeadResponse, error) {
params := chainIDRequest{ChainID: chainid}
req := NewJSON2Request("chain-head", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
head := new(chainHeadResponse)
if err := json.Unmarshal(resp.JSONResult(), head); err != nil {
return nil, err
}
return head, nil
}
// GetAllEBlockEntries requests every Entry in a specific Entry Block
func GetAllEBlockEntries(keymr string) ([]*Entry, error) {
es := make([]*Entry, 0)
eb, err := GetEBlock(keymr)
if err != nil {
return es, err
}
for _, v := range eb.EntryList {
e, err := GetEntry(v.EntryHash)
if err != nil {
return es, err
}
es = append(es, e)
}
return es, nil
}
// GetEBlock requests an Entry Block from factomd by its Key Merkle Root
func GetEBlock(keymr string) (*EBlock, error) {
params := keyMRRequest{KeyMR: keymr}
req := NewJSON2Request("entry-block", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
eb := new(EBlock)
if err := json.Unmarshal(resp.JSONResult(), eb); err != nil {
return nil, err
}
return eb, nil
}
func GetRaw(keymr string) ([]byte, error) {
params := hashRequest{Hash: keymr}
req := NewJSON2Request("raw-data", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
raw := new(RawData)
if err := json.Unmarshal(resp.JSONResult(), raw); err != nil {
return nil, err
}
return raw.GetDataBytes()
}
func GetAllChainEntries(chainid string) ([]*Entry, error) {
es := make([]*Entry, 0)
head, err := GetChainHeadAndStatus(chainid)
if err != nil {
return es, err
}
if head.ChainHead == "" && head.ChainInProcessList {
return nil, fmt.Errorf("Chain not yet included in a Directory Block")
}
for ebhash := head.ChainHead; ebhash != ZeroHash; {
eb, err := GetEBlock(ebhash)
if err != nil {
return es, err
}
s, err := GetAllEBlockEntries(ebhash)
if err != nil {
return es, err
}
es = append(s, es...)
ebhash = eb.Header.PrevKeyMR
}
return es, nil
}
func GetAllChainEntriesAtHeight(chainid string, height int64) ([]*Entry, error) {
es := make([]*Entry, 0)
head, err := GetChainHeadAndStatus(chainid)
if err != nil {
return es, err
}
if head.ChainHead == "" && head.ChainInProcessList {
return nil, fmt.Errorf("Chain not yet included in a Directory Block")
}
for ebhash := head.ChainHead; ebhash != ZeroHash; {
eb, err := GetEBlock(ebhash)
if err != nil {
return es, err
}
if eb.Header.DBHeight > height {
ebhash = eb.Header.PrevKeyMR
continue
}
s, err := GetAllEBlockEntries(ebhash)
if err != nil {
return es, err
}
es = append(s, es...)
ebhash = eb.Header.PrevKeyMR
}
return es, nil
}
func GetFirstEntry(chainid string) (*Entry, error) {
e := new(Entry)
head, err := GetChainHeadAndStatus(chainid)
if err != nil {
return e, err
}
if head.ChainHead == "" && head.ChainInProcessList {
return nil, fmt.Errorf("Chain not yet included in a Directory Block")
}
eb, err := GetEBlock(head.ChainHead)
if err != nil {
return e, err
}
for eb.Header.PrevKeyMR != ZeroHash {
ebhash := eb.Header.PrevKeyMR
eb, err = GetEBlock(ebhash)
if err != nil {
return e, err
}
}
return GetEntry(eb.EntryList[0].EntryHash)
}
func GetProperties() (string, string, string, string, string, string, string, string) {
type propertiesResponse struct {
FactomdVersion string `json:"factomdversion"`
FactomdVersionErr string `json:"factomdversionerr"`
FactomdAPIVersion string `json:"factomdapiversion"`
FactomdAPIVersionErr string `json:"factomdapiversionerr"`
WalletVersion string `json:"walletversion"`
WalletVersionErr string `json:"walletversionerr"`
WalletAPIVersion string `json:"walletapiversion"`
WalletAPIVersionErr string `json:"walletapiversionerr"`
}
props := new(propertiesResponse)
wprops := new(propertiesResponse)
req := NewJSON2Request("properties", APICounter(), nil)
wreq := NewJSON2Request("properties", APICounter(), nil)
resp, err := factomdRequest(req)
if err != nil {
props.FactomdVersionErr = err.Error()
} else if resp.Error != nil {
props.FactomdVersionErr = resp.Error.Error()
} else if jerr := json.Unmarshal(resp.JSONResult(), props); jerr != nil {
props.FactomdVersionErr = jerr.Error()
}
wresp, werr := walletRequest(wreq)
if werr != nil {
wprops.WalletVersionErr = werr.Error()
} else if wresp.Error != nil {
wprops.WalletVersionErr = wresp.Error.Error()
} else if jwerr := json.Unmarshal(wresp.JSONResult(), wprops); jwerr != nil {
wprops.WalletVersionErr = jwerr.Error()
}
return props.FactomdVersion, props.FactomdVersionErr, props.FactomdAPIVersion, props.FactomdAPIVersionErr, wprops.WalletVersion, wprops.WalletVersionErr, wprops.WalletAPIVersion, wprops.WalletAPIVersionErr
}
func GetPendingEntries() (string, error) {
req := NewJSON2Request("pending-entries", APICounter(), nil)
resp, err := factomdRequest(req)
if err != nil {
return "", err
}
if resp.Error != nil {
return "", err
}
rBytes := resp.JSONResult()
return string(rBytes), nil
}
func GetPendingTransactions() (string, error) {
req := NewJSON2Request("pending-transactions", APICounter(), nil)
resp, err := factomdRequest(req)
if err != nil {
return "", err
}
if resp.Error != nil {
return "", err
}
//fmt.Println("factom resp=", resp)
transList := resp.JSONResult()
return string(transList), nil
}