-
Notifications
You must be signed in to change notification settings - Fork 19
/
ecblock.go
397 lines (345 loc) · 9.93 KB
/
ecblock.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// 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/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"regexp"
)
var (
ErrECIDUndefined = errors.New("ECID type undefined")
ErrUnknownECBEntry = errors.New("Unknown Entry Credit Block Entry type")
)
// ECID defines the type of an Entry Credit Block Entry
type ECID byte
// Available ECID types
const (
ECIDServerIndexNumber ECID = iota // 0
ECIDMinuteNumber // 1
ECIDChainCommit // 2
ECIDEntryCommit // 3
ECIDBalanceIncrease // 4
)
func (id ECID) String() string {
switch id {
case ECIDServerIndexNumber:
return "ServerIndexNumber"
case ECIDMinuteNumber:
return "MinuteNumber"
case ECIDChainCommit:
return "ChainCommit"
case ECIDEntryCommit:
return "EntryCommit"
case ECIDBalanceIncrease:
return "BalanceIncrease"
default:
return "ECIDUndefined"
}
}
// ECBlock (Entry Credit Block) holds transactions that create Chains and
// Entries, and fund Entry Credit Addresses.
type ECBlock struct {
Header struct {
BodyHash string `json:"bodyhash"`
PrevHeaderHash string `json:"prevheaderhash"`
PrevFullHash string `json:"prevfullhash"`
DBHeight int64 `json:"dbheight"`
HeaderExpansionArea []byte `json:"headerexpansionarea,omitempty"`
} `json:"header"`
HeaderHash string `json:"headerhash"`
FullHash string `json:"fullhash"`
Entries []ECBEntry `json:"body"`
}
func (e *ECBlock) String() string {
var s string
s += fmt.Sprintln("HeaderHash:", e.HeaderHash)
s += fmt.Sprintln("PrevHeaderHash:", e.Header.PrevHeaderHash)
s += fmt.Sprintln("FullHash:", e.FullHash)
s += fmt.Sprintln("PrevFullHash:", e.Header.PrevFullHash)
s += fmt.Sprintln("BodyHash:", e.Header.BodyHash)
s += fmt.Sprintln("DBHeight:", e.Header.DBHeight)
if e.Header.HeaderExpansionArea != nil {
s += fmt.Sprintf("HeaderExpansionArea: %x\n", e.Header.HeaderExpansionArea)
}
s += fmt.Sprintln("Entries:")
for _, v := range e.Entries {
s += fmt.Sprintln(v)
}
return s
}
func (e *ECBlock) UnmarshalJSON(js []byte) error {
tmp := new(struct {
Header struct {
BodyHash string `json:"bodyhash"`
PrevHeaderHash string `json:"prevheaderhash"`
PrevFullHash string `json:"prevfullhash"`
DBHeight int64 `json:"dbheight"`
} `json:"header"`
HeaderHash string `json:"headerhash"`
FullHash string `json:"fullhash"`
Body struct {
Entries []json.RawMessage `json:"entries"`
} `json:"body"`
})
err := json.Unmarshal(js, tmp)
if err != nil {
return err
}
e.Header.BodyHash = tmp.Header.BodyHash
e.Header.PrevHeaderHash = tmp.Header.PrevHeaderHash
e.Header.PrevFullHash = tmp.Header.PrevFullHash
e.Header.DBHeight = tmp.Header.DBHeight
e.HeaderHash = tmp.HeaderHash
e.FullHash = tmp.FullHash
// the entry block entry type is not specified in the json data, so detect
// the entry type by regex and umarshal into the correct type.
for _, v := range tmp.Body.Entries {
switch {
case regexp.MustCompile(`"serverindexnumber":`).MatchString(string(v)):
a := new(ECServerIndexNumber)
err := json.Unmarshal(v, a)
if err != nil {
return err
}
e.Entries = append(e.Entries, a)
case regexp.MustCompile(`"number":`).MatchString(string(v)):
a := new(ECMinuteNumber)
err := json.Unmarshal(v, a)
if err != nil {
return err
}
e.Entries = append(e.Entries, a)
case regexp.MustCompile(`"entryhash":`).MatchString(string(v)):
if regexp.MustCompile(`"chainidhash":`).MatchString(string(v)) {
a := new(ECChainCommit)
err := json.Unmarshal(v, a)
if err != nil {
return err
}
e.Entries = append(e.Entries, a)
} else {
a := new(ECEntryCommit)
err := json.Unmarshal(v, a)
if err != nil {
return err
}
e.Entries = append(e.Entries, a)
}
case regexp.MustCompile(`"numec":`).MatchString(string(v)):
a := new(ECBalanceIncrease)
err := json.Unmarshal(v, a)
if err != nil {
return err
}
e.Entries = append(e.Entries, a)
default:
return ErrUnknownECBEntry
}
}
return nil
}
// an ECBEntry is an individual member of the Entry Credit Block.
type ECBEntry interface {
Type() ECID
String() string
}
// ECServerIndexNumber shows the index of the server that acknowledged the
// following ECBEntries.
type ECServerIndexNumber struct {
ServerIndexNumber int `json:"serverindexnumber"`
}
func (i *ECServerIndexNumber) Type() ECID {
return ECIDServerIndexNumber
}
func (i *ECServerIndexNumber) String() string {
return fmt.Sprintln("ServerIndexNumber:", i.ServerIndexNumber)
}
// ECMinuteNumber represents the end of a minute minute [1-10] in the order of
// the ECBEntries.
type ECMinuteNumber struct {
Number int `json:"number"`
}
func (m *ECMinuteNumber) Type() ECID {
return ECIDMinuteNumber
}
func (m *ECMinuteNumber) String() string {
return fmt.Sprintln("MinuteNumber:", m.Number)
}
// ECChainCommit pays for and reserves a new chain in Factom.
type ECChainCommit struct {
Version int `json:"version"`
MilliTime int64 `json:"millitime"`
ChainIDHash string `json:"chainidhash"`
Weld string `json:"weld"`
EntryHash string `json:"entryhash"`
Credits int `json:"credits"`
ECPubKey string `json:"ecpubkey"`
Sig string `json:"sig"`
}
func (c *ECChainCommit) UnmarshalJSON(js []byte) error {
tmp := new(struct {
Version int `json:"version"`
MilliTime string `json:"millitime"`
ChainIDHash string `json:"chainidhash"`
Weld string `json:"weld"`
EntryHash string `json:"entryhash"`
Credits int `json:"credits"`
ECPubKey string `json:"ecpubkey"`
Sig string `json:"sig"`
})
err := json.Unmarshal(js, tmp)
if err != nil {
return err
}
// convert 6 byte MilliTime into int64
m := make([]byte, 8)
if p, err := hex.DecodeString(tmp.MilliTime); err != nil {
return err
} else {
// copy p into the last 6 bytes
copy(m[2:], p)
}
c.MilliTime = int64(binary.BigEndian.Uint64(m))
c.Version = tmp.Version
c.ChainIDHash = tmp.ChainIDHash
c.Weld = tmp.Weld
c.EntryHash = tmp.EntryHash
c.Credits = tmp.Credits
c.ECPubKey = tmp.ECPubKey
c.Sig = tmp.Sig
return nil
}
func (c *ECChainCommit) Type() ECID {
return ECIDChainCommit
}
func (c *ECChainCommit) String() string {
var s string
s += fmt.Sprintln("ChainCommit {")
s += fmt.Sprintln(" Version:", c.Version)
s += fmt.Sprintln(" Millitime:", c.MilliTime)
s += fmt.Sprintln(" ChainIDHash:", c.ChainIDHash)
s += fmt.Sprintln(" Weld:", c.Weld)
s += fmt.Sprintln(" EntryHash:", c.EntryHash)
s += fmt.Sprintln(" Credits:", c.Credits)
s += fmt.Sprintln(" ECPubKey:", c.ECPubKey)
s += fmt.Sprintln(" Signature:", c.Sig)
s += fmt.Sprintln("}")
return s
}
// ECEntryCommit pays for and reserves a new entry in Factom.
type ECEntryCommit struct {
Version int `json:"version"`
MilliTime int64 `json:"millitime"`
EntryHash string `json:"entryhash"`
Credits int `json:"credits"`
ECPubKey string `json:"ecpubkey"`
Sig string `json:"sig"`
}
func (e *ECEntryCommit) UnmarshalJSON(js []byte) error {
tmp := new(struct {
Version int `json:"version"`
MilliTime string `json:"millitime"`
EntryHash string `json:"entryhash"`
Credits int `json:"credits"`
ECPubKey string `json:"ecpubkey"`
Sig string `json:"sig"`
})
err := json.Unmarshal(js, tmp)
if err != nil {
return err
}
// convert 6 byte MilliTime into int64
m := make([]byte, 8)
if p, err := hex.DecodeString(tmp.MilliTime); err != nil {
return err
} else {
// copy p into the last 6 bytes
copy(m[2:], p)
}
e.MilliTime = int64(binary.BigEndian.Uint64(m))
e.Version = tmp.Version
e.EntryHash = tmp.EntryHash
e.Credits = tmp.Credits
e.ECPubKey = tmp.ECPubKey
e.Sig = tmp.Sig
return nil
}
func (e *ECEntryCommit) Type() ECID {
return ECIDEntryCommit
}
func (e *ECEntryCommit) String() string {
var s string
s += fmt.Sprintln("EntryCommit {")
s += fmt.Sprintln(" Version:", e.Version)
s += fmt.Sprintln(" Millitime:", e.MilliTime)
s += fmt.Sprintln(" EntryHash:", e.EntryHash)
s += fmt.Sprintln(" Credits:", e.Credits)
s += fmt.Sprintln(" ECPubKey:", e.ECPubKey)
s += fmt.Sprintln(" Signature:", e.Sig)
s += fmt.Sprintln("}")
return s
}
// ECBalanceIncrease pays for and reserves a new entry in Factom.
type ECBalanceIncrease struct {
ECPubKey string `json:"ecpubkey"`
TXID string `json:"txid"`
Index uint64 `json:"index"`
NumEC uint64 `json:"numec"`
}
func (e *ECBalanceIncrease) Type() ECID {
return ECIDBalanceIncrease
}
func (e *ECBalanceIncrease) String() string {
var s string
s += fmt.Sprintln("BalanceIncrease {")
s += fmt.Sprintln(" ECPubKey:", e.ECPubKey)
s += fmt.Sprintln(" TXID:", e.TXID)
s += fmt.Sprintln(" Index:", e.TXID)
s += fmt.Sprintln(" NumEC:", e.NumEC)
s += fmt.Sprintln("}")
return s
}
// GetECBlock requests a specified Entry Credit Block from the factomd API
func GetECBlock(keymr string) (ecblock *ECBlock, err error) {
params := keyMRRequest{KeyMR: keymr, NoRaw: true}
req := NewJSON2Request("entrycredit-block", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return
}
if resp.Error != nil {
return nil, resp.Error
}
// create a wraper construct for the ECBlock API return
wrap := new(struct {
ECBlock *ECBlock `json:"ecblock"`
})
err = json.Unmarshal(resp.JSONResult(), wrap)
if err != nil {
return
}
return wrap.ECBlock, nil
}
// GetECBlockByHeight request an Entry Credit Block of a given height
func GetECBlockByHeight(height int64) (ecblock *ECBlock, err error) {
params := heightRequest{Height: height, NoRaw: true}
req := NewJSON2Request("ecblock-by-height", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return
}
if resp.Error != nil {
return nil, resp.Error
}
wrap := new(struct {
ECBlock *ECBlock `json:"ecblock"`
})
if err = json.Unmarshal(resp.JSONResult(), wrap); err != nil {
return
}
return wrap.ECBlock, nil
}