-
Notifications
You must be signed in to change notification settings - Fork 19
/
fblock_test.go
201 lines (184 loc) · 11 KB
/
fblock_test.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
// 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_test
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
. "github.com/FactomProject/factom"
)
func TestUnmarshalFBlock(t *testing.T) {
js := []byte(`{"fblock":{"bodymr":"0b6823522198d47689065e7b492baafbf817f0036934afffd1c968f2533a3e84","prevkeymr":"48c432b586b1737bc8ea0349ec319e41f07b28bc89d94b2e970e09f494eb8e04","prevledgerkeymr":"7a7c9851d9bcfb00f4d3d4cd0179adb43e47aabed628e7fceaf0ca718853045b","exchrate":90900,"dbheight":20002,"transactions":[{"txid":"fab98df81a80b1177c5226ff307be7ecc77c30666c63f06623a606424d41fe72","blockheight":0,"millitimestamp":1453149000985,"inputs":[],"outputs":[],"outecs":[],"rcds":[],"sigblocks":[]},{"txid":"1ec91421e01d95267f3deb9b9d5f29d3438387a0280a5ffa5e9a60f235212ae8","blockheight":0,"millitimestamp":1453149058599,"inputs":[{"amount":26268275436,"address":"3d956f129c08ac413025be3f6e47e3fb26461df35c9ccaf2fe4d53373e52536b","useraddress":"FA2SCdYb8iBYmMcmeUjHB8NhKx6DqH3wDovkumgbKt4oNkD3TJMg"}],"outputs":[{"amount":26267184636,"address":"ccf82cf94557f08a6859d8bf4a9b3ce361d0abae1e3bf5136b24638b74d32bc6","useraddress":"FA3XME5vdcjG8jPT188UFkum9BeAJJLgwyCkGB12QLsDA2qQaBET"}],"outecs":[],"rcds":["016664074524dd6a58e6593780717233b56d381a6798e5ee5ba75564bde589a6bf"],"sigblocks":[{"signatures":["efdab088b50d56ea2dfd4f600d5727a06cd7e9f3c353288e6898723ea32f4f044d27a80a199cfefec06cf53e18ea863b05b1075001d592b913e7f32c3d3f2204"]}]}],"chainid":"000000000000000000000000000000000000000000000000000000000000000f","keymr":"cfcac07b29ccfa413aeda646b5d386006468189939dfdfa6415b97cc35f2ea1a","ledgerkeymr":"a47da86f6ac8111da8a7d2a64fbaed1f74839722276acc5773b908963d01a029"},"rawdata":"000000000000000000000000000000000000000000000000000000000000000f0b6823522198d47689065e7b492baafbf817f0036934afffd1c968f2533a3e8448c432b586b1737bc8ea0349ec319e41f07b28bc89d94b2e970e09f494eb8e047a7c9851d9bcfb00f4d3d4cd0179adb43e47aabed628e7fceaf0ca718853045b000000000001631400004e220000000002000000c9020152566e1519000000020152566ef627010100e1edd8a56c3d956f129c08ac413025be3f6e47e3fb26461df35c9ccaf2fe4d53373e52536be1ed95db7cccf82cf94557f08a6859d8bf4a9b3ce361d0abae1e3bf5136b24638b74d32bc6016664074524dd6a58e6593780717233b56d381a6798e5ee5ba75564bde589a6bfefdab088b50d56ea2dfd4f600d5727a06cd7e9f3c353288e6898723ea32f4f044d27a80a199cfefec06cf53e18ea863b05b1075001d592b913e7f32c3d3f220400000000000000000000"}`)
// Create temporary struct to unmarshal json object
wrap := new(struct {
FBlock *FBlock `json:"fblock"`
RawData []byte `json:"rawdata"`
})
err := json.Unmarshal(js, wrap)
if err != nil {
t.Error(err)
}
t.Log(wrap.FBlock)
}
func TestGetFBlock(t *testing.T) {
factomdResponse := `{
"jsonrpc": "2.0",
"id": 0,
"result": {
"fblock": {
"bodymr": "0b6823522198d47689065e7b492baafbf817f0036934afffd1c968f2533a3e84",
"prevkeymr": "48c432b586b1737bc8ea0349ec319e41f07b28bc89d94b2e970e09f494eb8e04",
"prevledgerkeymr": "7a7c9851d9bcfb00f4d3d4cd0179adb43e47aabed628e7fceaf0ca718853045b",
"exchrate": 90900,
"dbheight": 20002,
"transactions": [
{
"txid": "fab98df81a80b1177c5226ff307be7ecc77c30666c63f06623a606424d41fe72",
"blockheight": 0,
"millitimestamp": 1453149000985,
"inputs": [],
"outputs": [],
"outecs": [],
"rcds": [],
"sigblocks": []
},
{
"txid": "1ec91421e01d95267f3deb9b9d5f29d3438387a0280a5ffa5e9a60f235212ae8",
"blockheight": 0,
"millitimestamp": 1453149058599,
"inputs": [
{
"amount": 26268275436,
"address": "3d956f129c08ac413025be3f6e47e3fb26461df35c9ccaf2fe4d53373e52536b",
"useraddress": "FA2SCdYb8iBYmMcmeUjHB8NhKx6DqH3wDovkumgbKt4oNkD3TJMg"
}
],
"outputs": [
{
"amount": 26267184636,
"address": "ccf82cf94557f08a6859d8bf4a9b3ce361d0abae1e3bf5136b24638b74d32bc6",
"useraddress": "FA3XME5vdcjG8jPT188UFkum9BeAJJLgwyCkGB12QLsDA2qQaBET"
}
],
"outecs": [],
"rcds": [
"016664074524dd6a58e6593780717233b56d381a6798e5ee5ba75564bde589a6bf"
],
"sigblocks": [
{
"signatures": [
"efdab088b50d56ea2dfd4f600d5727a06cd7e9f3c353288e6898723ea32f4f044d27a80a199cfefec06cf53e18ea863b05b1075001d592b913e7f32c3d3f2204"
]
}
]
}
],
"chainid": "000000000000000000000000000000000000000000000000000000000000000f",
"keymr": "cfcac07b29ccfa413aeda646b5d386006468189939dfdfa6415b97cc35f2ea1a",
"ledgerkeymr": "a47da86f6ac8111da8a7d2a64fbaed1f74839722276acc5773b908963d01a029"
},
"rawdata": "000000000000000000000000000000000000000000000000000000000000000f0b6823522198d47689065e7b492baafbf817f0036934afffd1c968f2533a3e8448c432b586b1737bc8ea0349ec319e41f07b28bc89d94b2e970e09f494eb8e047a7c9851d9bcfb00f4d3d4cd0179adb43e47aabed628e7fceaf0ca718853045b000000000001631400004e220000000002000000c9020152566e1519000000020152566ef627010100e1edd8a56c3d956f129c08ac413025be3f6e47e3fb26461df35c9ccaf2fe4d53373e52536be1ed95db7cccf82cf94557f08a6859d8bf4a9b3ce361d0abae1e3bf5136b24638b74d32bc6016664074524dd6a58e6593780717233b56d381a6798e5ee5ba75564bde589a6bfefdab088b50d56ea2dfd4f600d5727a06cd7e9f3c353288e6898723ea32f4f044d27a80a199cfefec06cf53e18ea863b05b1075001d592b913e7f32c3d3f220400000000000000000000"
}
}`
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, factomdResponse)
}))
defer ts.Close()
SetFactomdServer(ts.URL[7:])
fb, err := GetFBlock("cfcac07b29ccfa413aeda646b5d386006468189939dfdfa6415b97cc35f2ea1a")
if err != nil {
t.Error(err)
}
t.Log(fb)
}
func TestGetFBlockByHeight(t *testing.T) {
factomdResponse := `{
"jsonrpc": "2.0",
"id": 0,
"result": {
"fblock": {
"bodymr": "0b6823522198d47689065e7b492baafbf817f0036934afffd1c968f2533a3e84",
"prevkeymr": "48c432b586b1737bc8ea0349ec319e41f07b28bc89d94b2e970e09f494eb8e04",
"prevledgerkeymr": "7a7c9851d9bcfb00f4d3d4cd0179adb43e47aabed628e7fceaf0ca718853045b",
"exchrate": 90900,
"dbheight": 20002,
"transactions": [
{
"txid": "fab98df81a80b1177c5226ff307be7ecc77c30666c63f06623a606424d41fe72",
"blockheight": 0,
"millitimestamp": 1453149000985,
"inputs": [],
"outputs": [],
"outecs": [],
"rcds": [],
"sigblocks": []
},
{
"txid": "1ec91421e01d95267f3deb9b9d5f29d3438387a0280a5ffa5e9a60f235212ae8",
"blockheight": 0,
"millitimestamp": 1453149058599,
"inputs": [
{
"amount": 26268275436,
"address": "3d956f129c08ac413025be3f6e47e3fb26461df35c9ccaf2fe4d53373e52536b",
"useraddress": "FA2SCdYb8iBYmMcmeUjHB8NhKx6DqH3wDovkumgbKt4oNkD3TJMg"
}
],
"outputs": [
{
"amount": 26267184636,
"address": "ccf82cf94557f08a6859d8bf4a9b3ce361d0abae1e3bf5136b24638b74d32bc6",
"useraddress": "FA3XME5vdcjG8jPT188UFkum9BeAJJLgwyCkGB12QLsDA2qQaBET"
}
],
"outecs": [],
"rcds": [
"016664074524dd6a58e6593780717233b56d381a6798e5ee5ba75564bde589a6bf"
],
"sigblocks": [
{
"signatures": [
"efdab088b50d56ea2dfd4f600d5727a06cd7e9f3c353288e6898723ea32f4f044d27a80a199cfefec06cf53e18ea863b05b1075001d592b913e7f32c3d3f2204"
]
}
]
}
],
"chainid": "000000000000000000000000000000000000000000000000000000000000000f",
"keymr": "cfcac07b29ccfa413aeda646b5d386006468189939dfdfa6415b97cc35f2ea1a",
"ledgerkeymr": "a47da86f6ac8111da8a7d2a64fbaed1f74839722276acc5773b908963d01a029"
},
"rawdata": "000000000000000000000000000000000000000000000000000000000000000f0b6823522198d47689065e7b492baafbf817f0036934afffd1c968f2533a3e8448c432b586b1737bc8ea0349ec319e41f07b28bc89d94b2e970e09f494eb8e047a7c9851d9bcfb00f4d3d4cd0179adb43e47aabed628e7fceaf0ca718853045b000000000001631400004e220000000002000000c9020152566e1519000000020152566ef627010100e1edd8a56c3d956f129c08ac413025be3f6e47e3fb26461df35c9ccaf2fe4d53373e52536be1ed95db7cccf82cf94557f08a6859d8bf4a9b3ce361d0abae1e3bf5136b24638b74d32bc6016664074524dd6a58e6593780717233b56d381a6798e5ee5ba75564bde589a6bfefdab088b50d56ea2dfd4f600d5727a06cd7e9f3c353288e6898723ea32f4f044d27a80a199cfefec06cf53e18ea863b05b1075001d592b913e7f32c3d3f220400000000000000000000"
}
}`
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, factomdResponse)
}))
defer ts.Close()
SetFactomdServer(ts.URL[7:])
ab, err := GetFBlockByHeight(20000)
if err != nil {
t.Error(err)
}
t.Log("FBlock:", ab)
}
func TestFBTransaction_MarshalJSON(t *testing.T) {
original := []byte(`[{"txid":"fab98df81a80b1177c5226ff307be7ecc77c30666c63f06623a606424d41fe72","blockheight":0,"millitimestamp":1453149000985,"inputs":[],"outputs":[],"outecs":[],"rcds":[],"sigblocks":[]},{"txid":"1ec91421e01d95267f3deb9b9d5f29d3438387a0280a5ffa5e9a60f235212ae8","blockheight":0,"millitimestamp":1453149058599,"inputs":[{"amount":26268275436,"address":"3d956f129c08ac413025be3f6e47e3fb26461df35c9ccaf2fe4d53373e52536b","useraddress":"FA2SCdYb8iBYmMcmeUjHB8NhKx6DqH3wDovkumgbKt4oNkD3TJMg"}],"outputs":[{"amount":26267184636,"address":"ccf82cf94557f08a6859d8bf4a9b3ce361d0abae1e3bf5136b24638b74d32bc6","useraddress":"FA3XME5vdcjG8jPT188UFkum9BeAJJLgwyCkGB12QLsDA2qQaBET"}],"outecs":[],"rcds":["016664074524dd6a58e6593780717233b56d381a6798e5ee5ba75564bde589a6bf"],"sigblocks":[{"signatures":["efdab088b50d56ea2dfd4f600d5727a06cd7e9f3c353288e6898723ea32f4f044d27a80a199cfefec06cf53e18ea863b05b1075001d592b913e7f32c3d3f2204"]}]}]`)
var tr []*FBTransaction
if err := json.Unmarshal(original, &tr); err != nil {
t.Errorf("error unmarshalling transactions: %v", err)
}
rem, err := json.Marshal(tr)
if err != nil {
t.Errorf("error re-marshalling transactions: %v", err)
}
if !bytes.Equal(rem, original) {
t.Errorf("re-marshall doesn't match original. got = %v, want = %v", string(rem), string(original))
}
}