forked from FactomProject/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
byHeight.go
178 lines (148 loc) · 4.26 KB
/
byHeight.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
// 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"
)
type BlockByHeightResponse struct {
//TODO: implement all of the blocks as proper structures
DBlock map[string]interface{} `json:"dblock,omitempty"`
ABlock map[string]interface{} `json:"ablock,omitempty"`
FBlock map[string]interface{} `json:"fblock,omitempty"`
ECBlock map[string]interface{} `json:"ecblock,omitempty"`
RawData string `json:"rawdata,omitempty"`
}
func (f *BlockByHeightResponse) String() string {
var s string
if f.DBlock != nil {
j, _ := json.Marshal(f.DBlock)
s += fmt.Sprintln("DBlock:", string(j))
} else if f.ABlock != nil {
j, _ := json.Marshal(f.ABlock)
s += fmt.Sprintln("ABlock:", string(j))
} else if f.FBlock != nil {
j, _ := json.Marshal(f.FBlock)
s += fmt.Sprintln("FBlock:", string(j))
} else if f.ECBlock != nil {
j, _ := json.Marshal(f.ECBlock)
s += fmt.Sprintln("ECBlock:", string(j))
}
return s
}
type JStruct struct {
data []byte
}
func (e *JStruct) MarshalJSON() ([]byte, error) {
return e.data, nil
}
func (e *JStruct) UnmarshalJSON(b []byte) error {
e.data = b
return nil
}
type BlockByHeightRawResponse struct {
//TODO: implement all of the blocks as proper structures
DBlock *JStruct `json:"dblock,omitempty"`
ABlock *JStruct `json:"ablock,omitempty"`
FBlock *JStruct `json:"fblock,omitempty"`
ECBlock *JStruct `json:"ecblock,omitempty"`
RawData string `json:"rawdata,omitempty"`
}
func (f *BlockByHeightRawResponse) String() string {
var s string
if f.DBlock != nil {
j, _ := f.DBlock.MarshalJSON()
s += fmt.Sprintln("DBlock:", string(j))
} else if f.ABlock != nil {
j, _ := f.ABlock.MarshalJSON()
s += fmt.Sprintln("ABlock:", string(j))
} else if f.FBlock != nil {
j, _ := f.FBlock.MarshalJSON()
s += fmt.Sprintln("FBlock:", string(j))
} else if f.ECBlock != nil {
j, _ := f.ECBlock.MarshalJSON()
s += fmt.Sprintln("ECBlock:", string(j))
}
return s
}
func GetBlockByHeightRaw(blockType string, height int64) (*BlockByHeightRawResponse, error) {
params := heightRequest{Height: height}
req := NewJSON2Request(fmt.Sprintf("%vblock-by-height", blockType), APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
block := new(BlockByHeightRawResponse)
if err := json.Unmarshal(resp.JSONResult(), block); err != nil {
return nil, err
}
return block, nil
}
func GetDBlockByHeight(height int64) (*BlockByHeightResponse, error) {
params := heightRequest{Height: height}
req := NewJSON2Request("dblock-by-height", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
block := new(BlockByHeightResponse)
if err := json.Unmarshal(resp.JSONResult(), block); err != nil {
return nil, err
}
return block, nil
}
func GetECBlockByHeight(height int64) (*BlockByHeightResponse, error) {
params := heightRequest{Height: height}
req := NewJSON2Request("ecblock-by-height", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
block := new(BlockByHeightResponse)
if err := json.Unmarshal(resp.JSONResult(), block); err != nil {
return nil, err
}
return block, nil
}
func GetFBlockByHeight(height int64) (*BlockByHeightResponse, error) {
params := heightRequest{Height: height}
req := NewJSON2Request("fblock-by-height", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
block := new(BlockByHeightResponse)
if err := json.Unmarshal(resp.JSONResult(), block); err != nil {
return nil, err
}
return block, nil
}
func GetABlockByHeight(height int64) (*BlockByHeightResponse, error) {
params := heightRequest{Height: height}
req := NewJSON2Request("ablock-by-height", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
block := new(BlockByHeightResponse)
if err := json.Unmarshal(resp.JSONResult(), block); err != nil {
return nil, err
}
return block, nil
}