-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapiBlock.go
More file actions
127 lines (119 loc) · 3.04 KB
/
Copy pathapiBlock.go
File metadata and controls
127 lines (119 loc) · 3.04 KB
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
package goVsysSdk
import (
"strconv"
"sort"
"encoding/json"
"strings"
)
func (sdk *VsysApi) GetBlockHeight() (h int,errMsg string){
b,err:= sdk.httpGet("/blocks/height")
if err!=nil{
return 0,"qkw9bg6ufq ["+err.Error()+"]"
}
var respObj struct{
Height int `json:"height"`
}
err = json.Unmarshal(b, &respObj)
if err!=nil{
return 0,"69dszw2fx4 ["+err.Error()+"]"
}
return respObj.Height,""
}
func (sdk *VsysApi) MustGetBlockHeight() int{
b,err:= sdk.httpGet("/blocks/height")
if err!=nil{
panic("qkw9bg6ufq ["+err.Error()+"]")
}
var respObj struct{
Height int `json:"height"`
}
mustJsonUnmarshal(b,&respObj)
return respObj.Height
}
func (sdk *VsysApi) MustApiCallBlockSeq(from int,to int) (output []Block){
b,err:= sdk.httpGet("/blocks/seq/"+urlv(strconv.Itoa(from))+"/"+urlv(strconv.Itoa(to)))
if err!=nil{
panic("qkw9bg6ufq ["+err.Error()+"]")
}
mustJsonUnmarshal(b,&output)
return output
}
func (sdk *VsysApi) MustGetBlockCallbackDesc(cb func(block Block) bool) {
thisHeight:=sdk.MustGetBlockHeight()
const OneBatchNum = 99
for{
low:=thisHeight-OneBatchNum
if low<=0{
low = 0
}
blockList:=sdk.MustApiCallBlockSeq(low,thisHeight)
sort.Slice(blockList,func(i int,j int)bool{
return blockList[i].Height>blockList[j].Height
})
for _,block:=range blockList{
isNext:=cb(block)
if isNext==false{
return
}
}
thisHeight = low
if thisHeight<=0{
break
}
}
return
}
type Block struct {
Version int `json:"version"`
Timestamp int64 `json:"timestamp"`
Reference string `json:"reference"`
SPOSConsensus struct {
MintTime int64 `json:"mintTime"`
MintBalance int64 `json:"mintBalance"`
} `json:"SPOSConsensus"`
ResourcePricingData struct {
Computation int64 `json:"computation"`
Storage int64 `json:"storage"`
Memory int64 `json:"memory"`
RandomIO int64 `json:"randomIO"`
SequentialIO int64 `json:"sequentialIO"`
} `json:"resourcePricingData"`
TransactionMerkleRoot string `json:"TransactionMerkleRoot"`
Transactions []HistoryTransaction `json:"transactions"`
Generator string `json:"generator"`
Signature string `json:"signature"`
Fee int64 `json:"fee"`
Blocksize int64 `json:"blocksize"`
Height int64 `json:"height"`
TransactionCount int64 `json:"transaction count"`
}
func (sdk *VsysApi) MustApiGetBlockLast() Block{
b,err:= sdk.httpGet("/blocks/last")
if err!=nil{
panic("g9ggn6pamr ["+err.Error()+"]")
}
var respObj Block
mustJsonUnmarshal(b,&respObj)
return respObj
}
func (sdk *VsysApi) MustApiGetBlockHeightBySignature(signature string) int64{
b,err:= sdk.httpGet("/blocks/height/"+urlv(signature))
if err!=nil{
errMsg:=err.Error()
if strings.Contains(errMsg,"block does not exist"){
return 0
}
panic("8jby83bsa3 ["+errMsg+"]")
}
var respObj struct{
Height int64 `json:"height"`
}
err = json.Unmarshal(b, &respObj)
if err!=nil{
panic("s6mqh3s93w ["+err.Error()+"]")
}
return respObj.Height
//var respObj Block
//mustJsonUnmarshal(b,&respObj)
//return respObj
}