-
Notifications
You must be signed in to change notification settings - Fork 19
/
byHeight.go
74 lines (61 loc) · 1.82 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
// 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 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
}
// GetBlockByHeightRaw fetches the specified block type by height
// Deprecated: use ablock, dblock, eblock, ecblock and fblock instead.
func GetBlockByHeightRaw(blockType string, height int64) (*BlockByHeightRawResponse, error) {
params := heightRequest{Height: height, NoRaw: false} // include raw
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
}