-
Notifications
You must be signed in to change notification settings - Fork 19
/
ecblock_test.go
219 lines (204 loc) · 16.8 KB
/
ecblock_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// 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 (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
. "github.com/FactomProject/factom"
"testing"
)
func TestUnmarshalECBlock(t *testing.T) {
js := []byte(`{"ecblock":{"header":{"bodyhash":"541338744c8254641e0df2776dc7af07915c5da009e72e764da2bcbaa29a1bc6","prevheaderhash":"86aa9a8ef0cdb5e7b525fb7f9dd05f8188471cfbea6cf1c7ebab482ec408b6e9","prevfullhash":"af8a96d6e4ce0bd81c327bc49ab96c7e190c08c5ea0257d95a88c0806abf4266","dbheight":10199,"headerexpansionarea":"","objectcount":14,"bodysize":561,"chainid":"000000000000000000000000000000000000000000000000000000000000000c","ecchainid":"000000000000000000000000000000000000000000000000000000000000000c"},"body":{"entries":[{"serverindexnumber":0},{"version":0,"millitime":"0150f7d966a9","chainidhash":"e5f6f7cd369ef90a9872532af2d9755edfcd78124ea140f3417f54949b169aea","weld":"1aa415bfaa978342ef396d7203cde3ad45cf92dab89ec6b34128234cae42ef6f","entryhash":"7b4bc033547fd3ac1055d500752e99048d83ae9e580cc1fa4dcead10db868c73","credits":11,"ecpubkey":"79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92","sig":"34cab18fbc270bc51e9d68adc8cb9c65da5d7021bcc34370598ac6370fb7edde9b5c1a0164055bef53a83fbb1ddeb61a6942491fd8f9a56eb264c1abcc7c3905"},{"version":0,"millitime":"0150f7d8f870","entryhash":"ac43f66ddf733981ce33a15bff872e125fff1a2b640cf99ee7e44b6ca2e96fb6","credits":1,"ecpubkey":"4bcbc1c5ab90e432bd407a51eaa513b4050eecda1fd42bbf6b7050a1d96f94b7","sig":"d06dedddf728f55a011eb6c133bfeebe1669823afd109158f9c6cbeaf012d358e9bc0055850ca639bb78838418465e48aa1f9e03874c948e8520d9064adb9c06"},{"number":1},{"number":2},{"number":3},{"number":4},{"version":0,"millitime":"0150f7dcfb53","chainidhash":"1962219a271a272ff432fb8635ce07269d6f4a974871bbfde9d5ac7ab429a682","weld":"2b5088c89e158f94802459c01a9eb170eca3487f4de26ff8a331a5b5f5dbde4e","entryhash":"8c138dfb419a2c118c58a7ac0e791c3c6c2a67cec732325c2465ce911af41a4e","credits":11,"ecpubkey":"79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92","sig":"ff2a6878ab59da88bd15b94545fbdecbab29fd14f64e7d7cf5fe3eb7f2f08a169aa1cfea415bd5d86d934ff925dfd8567491bdc7d9dff2a38d28bed729364101"},{"number":5},{"number":6},{"number":7},{"number":8},{"number":9},{"number":10}]},"headerhash":"a7baaa24e477a0acef165461d70ec94ff3f33ad15562ecbe937967a761929a17","fullhash":"84339a4a849c3616c7c1a5011f2fe14d000efd3a98309afaabbd2d7c0122094c"},"rawdata":"000000000000000000000000000000000000000000000000000000000000000c541338744c8254641e0df2776dc7af07915c5da009e72e764da2bcbaa29a1bc686aa9a8ef0cdb5e7b525fb7f9dd05f8188471cfbea6cf1c7ebab482ec408b6e9af8a96d6e4ce0bd81c327bc49ab96c7e190c08c5ea0257d95a88c0806abf4266000027d700000000000000000e0000000000000231000002000150f7d966a9e5f6f7cd369ef90a9872532af2d9755edfcd78124ea140f3417f54949b169aea1aa415bfaa978342ef396d7203cde3ad45cf92dab89ec6b34128234cae42ef6f7b4bc033547fd3ac1055d500752e99048d83ae9e580cc1fa4dcead10db868c730b79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac9234cab18fbc270bc51e9d68adc8cb9c65da5d7021bcc34370598ac6370fb7edde9b5c1a0164055bef53a83fbb1ddeb61a6942491fd8f9a56eb264c1abcc7c390503000150f7d8f870ac43f66ddf733981ce33a15bff872e125fff1a2b640cf99ee7e44b6ca2e96fb6014bcbc1c5ab90e432bd407a51eaa513b4050eecda1fd42bbf6b7050a1d96f94b7d06dedddf728f55a011eb6c133bfeebe1669823afd109158f9c6cbeaf012d358e9bc0055850ca639bb78838418465e48aa1f9e03874c948e8520d9064adb9c06010101020103010402000150f7dcfb531962219a271a272ff432fb8635ce07269d6f4a974871bbfde9d5ac7ab429a6822b5088c89e158f94802459c01a9eb170eca3487f4de26ff8a331a5b5f5dbde4e8c138dfb419a2c118c58a7ac0e791c3c6c2a67cec732325c2465ce911af41a4e0b79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92ff2a6878ab59da88bd15b94545fbdecbab29fd14f64e7d7cf5fe3eb7f2f08a169aa1cfea415bd5d86d934ff925dfd8567491bdc7d9dff2a38d28bed72936410101050106010701080109010a"}`)
jsbadentry := []byte(`{"ecblock":{"header":{"bodyhash":"541338744c8254641e0df2776dc7af07915c5da009e72e764da2bcbaa29a1bc6","prevheaderhash":"86aa9a8ef0cdb5e7b525fb7f9dd05f8188471cfbea6cf1c7ebab482ec408b6e9","prevfullhash":"af8a96d6e4ce0bd81c327bc49ab96c7e190c08c5ea0257d95a88c0806abf4266","dbheight":10199,"headerexpansionarea":"","objectcount":14,"bodysize":561,"chainid":"000000000000000000000000000000000000000000000000000000000000000c","ecchainid":"000000000000000000000000000000000000000000000000000000000000000c"},"body":{"entries":[{"badentry":"bad"},{"serverindexnumber":0},{"number":5},{"number":6},{"number":7},{"number":8},{"number":9},{"number":10}]},"headerhash":"a7baaa24e477a0acef165461d70ec94ff3f33ad15562ecbe937967a761929a17","fullhash":"84339a4a849c3616c7c1a5011f2fe14d000efd3a98309afaabbd2d7c0122094c"},"rawdata":"000000000000000000000000000000000000000000000000000000000000000c541338744c8254641e0df2776dc7af07915c5da009e72e764da2bcbaa29a1bc686aa9a8ef0cdb5e7b525fb7f9dd05f8188471cfbea6cf1c7ebab482ec408b6e9af8a96d6e4ce0bd81c327bc49ab96c7e190c08c5ea0257d95a88c0806abf4266000027d700000000000000000e0000000000000231000002000150f7d966a9e5f6f7cd369ef90a9872532af2d9755edfcd78124ea140f3417f54949b169aea1aa415bfaa978342ef396d7203cde3ad45cf92dab89ec6b34128234cae42ef6f7b4bc033547fd3ac1055d500752e99048d83ae9e580cc1fa4dcead10db868c730b79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac9234cab18fbc270bc51e9d68adc8cb9c65da5d7021bcc34370598ac6370fb7edde9b5c1a0164055bef53a83fbb1ddeb61a6942491fd8f9a56eb264c1abcc7c390503000150f7d8f870ac43f66ddf733981ce33a15bff872e125fff1a2b640cf99ee7e44b6ca2e96fb6014bcbc1c5ab90e432bd407a51eaa513b4050eecda1fd42bbf6b7050a1d96f94b7d06dedddf728f55a011eb6c133bfeebe1669823afd109158f9c6cbeaf012d358e9bc0055850ca639bb78838418465e48aa1f9e03874c948e8520d9064adb9c06010101020103010402000150f7dcfb531962219a271a272ff432fb8635ce07269d6f4a974871bbfde9d5ac7ab429a6822b5088c89e158f94802459c01a9eb170eca3487f4de26ff8a331a5b5f5dbde4e8c138dfb419a2c118c58a7ac0e791c3c6c2a67cec732325c2465ce911af41a4e0b79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92ff2a6878ab59da88bd15b94545fbdecbab29fd14f64e7d7cf5fe3eb7f2f08a169aa1cfea415bd5d86d934ff925dfd8567491bdc7d9dff2a38d28bed72936410101050106010701080109010a"}`)
wrap := new(struct {
ECBlock ECBlock `json:"ecblock"`
RawData string `json:"rawdata"`
})
err := json.Unmarshal(js, wrap)
if err != nil {
t.Error(err)
}
err = json.Unmarshal(jsbadentry, wrap)
if err != ErrUnknownECBEntry {
t.Error(err)
}
t.Log("ECBlock:", wrap.ECBlock)
t.Log("RawData:", wrap.RawData)
}
func TestGetECBlock(t *testing.T) {
factomdResponse := `{
"jsonrpc": "2.0",
"id": 1,
"result": {
"ecblock": {
"header": {
"bodyhash": "541338744c8254641e0df2776dc7af07915c5da009e72e764da2bcbaa29a1bc6",
"prevheaderhash": "86aa9a8ef0cdb5e7b525fb7f9dd05f8188471cfbea6cf1c7ebab482ec408b6e9",
"prevfullhash": "af8a96d6e4ce0bd81c327bc49ab96c7e190c08c5ea0257d95a88c0806abf4266",
"dbheight": 10199,
"headerexpansionarea": "",
"objectcount": 14,
"bodysize": 561,
"chainid": "000000000000000000000000000000000000000000000000000000000000000c",
"ecchainid": "000000000000000000000000000000000000000000000000000000000000000c"
},
"body": {
"entries": [
{
"serverindexnumber": 0
}, {
"version": 0,
"millitime": "0150f7d966a9",
"chainidhash": "e5f6f7cd369ef90a9872532af2d9755edfcd78124ea140f3417f54949b169aea",
"weld": "1aa415bfaa978342ef396d7203cde3ad45cf92dab89ec6b34128234cae42ef6f",
"entryhash": "7b4bc033547fd3ac1055d500752e99048d83ae9e580cc1fa4dcead10db868c73",
"credits": 11,
"ecpubkey": "79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92",
"sig": "34cab18fbc270bc51e9d68adc8cb9c65da5d7021bcc34370598ac6370fb7edde9b5c1a0164055bef53a83fbb1ddeb61a6942491fd8f9a56eb264c1abcc7c3905"
}, {
"version": 0,
"millitime": "0150f7d8f870",
"entryhash": "ac43f66ddf733981ce33a15bff872e125fff1a2b640cf99ee7e44b6ca2e96fb6",
"credits": 1,
"ecpubkey": "4bcbc1c5ab90e432bd407a51eaa513b4050eecda1fd42bbf6b7050a1d96f94b7",
"sig": "d06dedddf728f55a011eb6c133bfeebe1669823afd109158f9c6cbeaf012d358e9bc0055850ca639bb78838418465e48aa1f9e03874c948e8520d9064adb9c06"
}, {
"number": 1
}, {
"number": 2
}, {
"number": 3
}, {
"number": 4
}, {
"version": 0,
"millitime": "0150f7dcfb53",
"chainidhash": "1962219a271a272ff432fb8635ce07269d6f4a974871bbfde9d5ac7ab429a682",
"weld": "2b5088c89e158f94802459c01a9eb170eca3487f4de26ff8a331a5b5f5dbde4e",
"entryhash": "8c138dfb419a2c118c58a7ac0e791c3c6c2a67cec732325c2465ce911af41a4e",
"credits": 11,
"ecpubkey": "79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92",
"sig": "ff2a6878ab59da88bd15b94545fbdecbab29fd14f64e7d7cf5fe3eb7f2f08a169aa1cfea415bd5d86d934ff925dfd8567491bdc7d9dff2a38d28bed729364101"
}, {
"number": 5
}, {
"number": 6
}, {
"number": 7
}, {
"number": 8
}, {
"number": 9
}, {
"number": 10
}
]
},
"headerhash": "a7baaa24e477a0acef165461d70ec94ff3f33ad15562ecbe937967a761929a17",
"fullhash": "84339a4a849c3616c7c1a5011f2fe14d000efd3a98309afaabbd2d7c0122094c"
},
"rawdata": "000000000000000000000000000000000000000000000000000000000000000c541338744c8254641e0df2776dc7af07915c5da009e72e764da2bcbaa29a1bc686aa9a8ef0cdb5e7b525fb7f9dd05f8188471cfbea6cf1c7ebab482ec408b6e9af8a96d6e4ce0bd81c327bc49ab96c7e190c08c5ea0257d95a88c0806abf4266000027d700000000000000000e0000000000000231000002000150f7d966a9e5f6f7cd369ef90a9872532af2d9755edfcd78124ea140f3417f54949b169aea1aa415bfaa978342ef396d7203cde3ad45cf92dab89ec6b34128234cae42ef6f7b4bc033547fd3ac1055d500752e99048d83ae9e580cc1fa4dcead10db868c730b79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac9234cab18fbc270bc51e9d68adc8cb9c65da5d7021bcc34370598ac6370fb7edde9b5c1a0164055bef53a83fbb1ddeb61a6942491fd8f9a56eb264c1abcc7c390503000150f7d8f870ac43f66ddf733981ce33a15bff872e125fff1a2b640cf99ee7e44b6ca2e96fb6014bcbc1c5ab90e432bd407a51eaa513b4050eecda1fd42bbf6b7050a1d96f94b7d06dedddf728f55a011eb6c133bfeebe1669823afd109158f9c6cbeaf012d358e9bc0055850ca639bb78838418465e48aa1f9e03874c948e8520d9064adb9c06010101020103010402000150f7dcfb531962219a271a272ff432fb8635ce07269d6f4a974871bbfde9d5ac7ab429a6822b5088c89e158f94802459c01a9eb170eca3487f4de26ff8a331a5b5f5dbde4e8c138dfb419a2c118c58a7ac0e791c3c6c2a67cec732325c2465ce911af41a4e0b79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92ff2a6878ab59da88bd15b94545fbdecbab29fd14f64e7d7cf5fe3eb7f2f08a169aa1cfea415bd5d86d934ff925dfd8567491bdc7d9dff2a38d28bed72936410101050106010701080109010a"
}
}`
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:])
ecb, err := GetECBlock("a7baaa24e477a0acef165461d70ec94ff3f33ad15562ecbe937967a761929a17")
if err != nil {
t.Error(err)
}
t.Log("ECBlock: ", ecb)
}
func TestGetECBlockByHeight(t *testing.T) {
factomdResponse := `{
"jsonrpc": "2.0",
"id": 1,
"result": {
"ecblock": {
"header": {
"bodyhash": "541338744c8254641e0df2776dc7af07915c5da009e72e764da2bcbaa29a1bc6",
"prevheaderhash": "86aa9a8ef0cdb5e7b525fb7f9dd05f8188471cfbea6cf1c7ebab482ec408b6e9",
"prevfullhash": "af8a96d6e4ce0bd81c327bc49ab96c7e190c08c5ea0257d95a88c0806abf4266",
"dbheight": 10199,
"headerexpansionarea": "",
"objectcount": 14,
"bodysize": 561,
"chainid": "000000000000000000000000000000000000000000000000000000000000000c",
"ecchainid": "000000000000000000000000000000000000000000000000000000000000000c"
},
"body": {
"entries": [
{
"serverindexnumber": 0
}, {
"version": 0,
"millitime": "0150f7d966a9",
"chainidhash": "e5f6f7cd369ef90a9872532af2d9755edfcd78124ea140f3417f54949b169aea",
"weld": "1aa415bfaa978342ef396d7203cde3ad45cf92dab89ec6b34128234cae42ef6f",
"entryhash": "7b4bc033547fd3ac1055d500752e99048d83ae9e580cc1fa4dcead10db868c73",
"credits": 11,
"ecpubkey": "79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92",
"sig": "34cab18fbc270bc51e9d68adc8cb9c65da5d7021bcc34370598ac6370fb7edde9b5c1a0164055bef53a83fbb1ddeb61a6942491fd8f9a56eb264c1abcc7c3905"
}, {
"version": 0,
"millitime": "0150f7d8f870",
"entryhash": "ac43f66ddf733981ce33a15bff872e125fff1a2b640cf99ee7e44b6ca2e96fb6",
"credits": 1,
"ecpubkey": "4bcbc1c5ab90e432bd407a51eaa513b4050eecda1fd42bbf6b7050a1d96f94b7",
"sig": "d06dedddf728f55a011eb6c133bfeebe1669823afd109158f9c6cbeaf012d358e9bc0055850ca639bb78838418465e48aa1f9e03874c948e8520d9064adb9c06"
}, {
"number": 1
}, {
"number": 2
}, {
"number": 3
}, {
"number": 4
}, {
"version": 0,
"millitime": "0150f7dcfb53",
"chainidhash": "1962219a271a272ff432fb8635ce07269d6f4a974871bbfde9d5ac7ab429a682",
"weld": "2b5088c89e158f94802459c01a9eb170eca3487f4de26ff8a331a5b5f5dbde4e",
"entryhash": "8c138dfb419a2c118c58a7ac0e791c3c6c2a67cec732325c2465ce911af41a4e",
"credits": 11,
"ecpubkey": "79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92",
"sig": "ff2a6878ab59da88bd15b94545fbdecbab29fd14f64e7d7cf5fe3eb7f2f08a169aa1cfea415bd5d86d934ff925dfd8567491bdc7d9dff2a38d28bed729364101"
}, {
"number": 5
}, {
"number": 6
}, {
"number": 7
}, {
"number": 8
}, {
"number": 9
}, {
"number": 10
}
]
},
"headerhash": "a7baaa24e477a0acef165461d70ec94ff3f33ad15562ecbe937967a761929a17",
"fullhash": "84339a4a849c3616c7c1a5011f2fe14d000efd3a98309afaabbd2d7c0122094c"
},
"rawdata": "000000000000000000000000000000000000000000000000000000000000000c541338744c8254641e0df2776dc7af07915c5da009e72e764da2bcbaa29a1bc686aa9a8ef0cdb5e7b525fb7f9dd05f8188471cfbea6cf1c7ebab482ec408b6e9af8a96d6e4ce0bd81c327bc49ab96c7e190c08c5ea0257d95a88c0806abf4266000027d700000000000000000e0000000000000231000002000150f7d966a9e5f6f7cd369ef90a9872532af2d9755edfcd78124ea140f3417f54949b169aea1aa415bfaa978342ef396d7203cde3ad45cf92dab89ec6b34128234cae42ef6f7b4bc033547fd3ac1055d500752e99048d83ae9e580cc1fa4dcead10db868c730b79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac9234cab18fbc270bc51e9d68adc8cb9c65da5d7021bcc34370598ac6370fb7edde9b5c1a0164055bef53a83fbb1ddeb61a6942491fd8f9a56eb264c1abcc7c390503000150f7d8f870ac43f66ddf733981ce33a15bff872e125fff1a2b640cf99ee7e44b6ca2e96fb6014bcbc1c5ab90e432bd407a51eaa513b4050eecda1fd42bbf6b7050a1d96f94b7d06dedddf728f55a011eb6c133bfeebe1669823afd109158f9c6cbeaf012d358e9bc0055850ca639bb78838418465e48aa1f9e03874c948e8520d9064adb9c06010101020103010402000150f7dcfb531962219a271a272ff432fb8635ce07269d6f4a974871bbfde9d5ac7ab429a6822b5088c89e158f94802459c01a9eb170eca3487f4de26ff8a331a5b5f5dbde4e8c138dfb419a2c118c58a7ac0e791c3c6c2a67cec732325c2465ce911af41a4e0b79a1ad273d890287e5d4f16d2669c06c523b9e48673de1bfde3ea2fda309ac92ff2a6878ab59da88bd15b94545fbdecbab29fd14f64e7d7cf5fe3eb7f2f08a169aa1cfea415bd5d86d934ff925dfd8567491bdc7d9dff2a38d28bed72936410101050106010701080109010a"
}
}`
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:])
ecb, err := GetECBlockByHeight(10199)
if err != nil {
t.Error(err)
}
t.Log("ECBlock: ", ecb)
}