-
Notifications
You must be signed in to change notification settings - Fork 10
/
nanodb.ksy
executable file
·556 lines (521 loc) · 13.4 KB
/
nanodb.ksy
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# ----------------------------------------------------------------------
# Nano database definition
# ----------------------------------------------------------------------
meta:
id: nanodb
title: Nano Ledger Database Binary Format
license: BSD 2-Clause
endian: le
enums:
# The database version covered by this specification
database_version:
19: value
enum_blocktype:
0x00: invalid
0x01: not_a_block
0x02: send
0x03: receive
0x04: open
0x05: change
0x06: state
enum_epoch:
0x00: invalid
0x01: unspecified
0x02: epoch_0
0x03: epoch_1
0x04: epoch_2
enum_signature_verification:
0x0: unknown
0x1: invalid
0x2: valid
0x3: valid_epoch
# Keys in the meta table. Note that keys are converted to 32-byte integers by the node.
meta_key:
1: version
types:
# -------------------------------------------------------------------
# Table: confirmation_height
# -------------------------------------------------------------------
confirmation_height:
doc: Confirmed height per account
seq:
- id: key
type: confirmation_height_key
- id: value
type: confirmation_height_value
confirmation_height_key:
seq:
- id: account
size: 32
confirmation_height_value:
seq:
- id: height
type: u8le
- id: frontier
size: 32
doc: Hash of confirmed frontier block at this height
# -------------------------------------------------------------------
# Table: frontiers
# -------------------------------------------------------------------
frontiers:
doc: Mapping from block hash to account. This is not used after epoch 1.
seq:
- id: key
type: frontiers_key
- id: value
type: frontiers_value
frontiers_key:
seq:
- id: hash
size: 32
frontiers_value:
seq:
- id: account
size: 32
# -------------------------------------------------------------------
# Table: meta
# Structure of value may depends on key in the future, hence a
# separate definition for each key (currently only version is used)
# -------------------------------------------------------------------
meta_key:
seq:
- id: key
size: 32
meta_version:
doc: Value of key meta_key#version
seq:
- id: database_version
size: 32
doc: 32-byte big endian integer representing the database version
# -------------------------------------------------------------------
# Table: online_weight
# -------------------------------------------------------------------
online_weight:
doc: Stores online weight trended over time
seq:
- id: key
type: online_weight_key
- id: value
type: online_weight_value
online_weight_key:
seq:
- id: timestamp
type: u8be
doc: Unix epoch
online_weight_value:
seq:
- id: amount
size: 16
doc: 128 bit amount representing online weight at timestamp
# -------------------------------------------------------------------
# Table: peers
# -------------------------------------------------------------------
peers:
doc: Peer cache table. All data is stored in the key.
seq:
- id: address
doc: IPv6 address bytes, big endian
size: 16
- id: port
doc: Port number, big endian
type: u2be
# -------------------------------------------------------------------
# Table: pending
# -------------------------------------------------------------------
pending:
doc: Pending table
seq:
- id: key
type: pending_key
- id: value
type: pending_value
pending_key:
seq:
- id: account
size: 32
- id: hash
doc: Block hash
size: 32
pending_value:
seq:
- id: source
doc: Source account
size: 32
- id: amount
size: 16
- id: epoch
type: u1
enum: enum_epoch
# -------------------------------------------------------------------
# Table: blocks
# -------------------------------------------------------------------
blocks:
seq:
- id: key
type: blocks_key
- id: value
type: blocks_value
blocks_key:
seq:
- id: hash
size: 32
blocks_value:
seq:
- id: block_type
type: u1
enum: enum_blocktype
- id: block_value
type:
switch-on: block_type
cases:
'enum_blocktype::send': send_value
'enum_blocktype::receive': receive_value
'enum_blocktype::open': open_value
'enum_blocktype::change': change_value
'enum_blocktype::state': state_value
_: ignore_until_eof
change_value:
seq:
- id: block
type: block_change
- id: sideband
type: change_sideband
change_sideband:
seq:
- id: successor
size: 32
doc: Hash of successor block, if any
- id: account
size: 32
doc: Public key to identify which account chain the block belongs to
- id: height
type: u8be
doc: Block height, big endian
- id: balance
size: 16
doc: 128 bit balance in raw
- id: timestamp
type: u8be
doc: Unix epoch
open_value:
seq:
- id: block
type: block_open
- id: sideband
type: open_sideband
open_sideband:
seq:
- id: successor
size: 32
doc: Hash of successor block, if any
- id: balance
size: 16
doc: 128 bit balance in raw
- id: timestamp
type: u8be
doc: Unix epoch, big endian
receive_value:
seq:
- id: block
type: block_receive
- id: sideband
type: receive_sideband
receive_sideband:
seq:
- id: successor
size: 32
doc: Hash of successor block, if any
- id: account
size: 32
doc: Public key to identify which account chain the block belongs to
- id: height
type: u8be
doc: Block height, big endian
- id: balance
size: 16
doc: 128 bit balance in raw
- id: timestamp
type: u8be
doc: Unix epoch, big endian
send_value:
seq:
- id: block
type: block_send
- id: sideband
type: send_sideband
send_sideband:
seq:
- id: successor
size: 32
doc: Hash of successor block, if any
- id: account
size: 32
doc: Public key to identify which account chain the block belongs to
- id: height
type: u8be
doc: Block height, big endian
- id: timestamp
type: u8be
doc: Unix epoch, big endian
state_value:
seq:
- id: block
type: block_state
- id: sideband
type: state_sideband
state_sideband:
seq:
- id: successor
size: 32
doc: Hash of successor block, if any
- id: height
type: u8be
doc: Block height, big endian
- id: timestamp
type: u8be
doc: Unix epoch, big endian
- id: is_send
type: b1
- id: is_receive
type: b1
- id: is_epoch
type: b1
- id: epoch
type: b5
enum: enum_epoch
doc: Which ledger epoch this block belongs to
# -------------------------------------------------------------------
# Table: unchecked
# -------------------------------------------------------------------
unchecked:
seq:
- id: key
type: unchecked_key
- id: value
type: unchecked_value
unchecked_key:
doc: Key of the unchecked table
seq:
- id: previous
size: 32
- id: hash
size: 32
unchecked_value:
doc: Information about an unchecked block
seq:
- id: block_type
type: u1
enum: enum_blocktype
- id: block
type: block_selector(block_type.to_i)
- id: account
size: 32
- id: modified
type: u8le
- id: verified
doc: Signature verification status
type: u1
enum: enum_signature_verification
# -------------------------------------------------------------------
# Table: vote
# -------------------------------------------------------------------
vote:
seq:
- id: key
type: vote_key
- id: value
type: vote_value
vote_key:
doc: Key of the vote table
seq:
- id: account
size: 32
doc: Public key
vote_value:
doc: Vote and block(s)
seq:
- id: account
size: 32
- id: signature
size: 64
- id: sequence
type: u8le
- id: block_type
type: u1
enum: enum_blocktype
- id: votebyhash
if: block_type == enum_blocktype::not_a_block
type: vote_by_hash
- id: block
if: block_type != enum_blocktype::not_a_block
type: block_selector(block_type.to_i)
vote_by_hash:
doc: A sequence of up to 12 hashes, terminated by EOF.
seq:
- id: hashes
type: vote_by_hash_entry(_index)
repeat: until
repeat-until: _index == 12 or _io.eof
if: not _io.eof
vote_by_hash_entry:
doc: The serialized hash in VBH is prepended by not_a_block
params:
- id: idx
type: u4
seq:
- id: block_type
doc: Always enum_blocktype::not_a_block
type: u1
enum: enum_blocktype
if: idx > 0
- id: block_hash
size: 32
# -------------------------------------------------------------------
# Table: accounts
# -------------------------------------------------------------------
accounts:
seq:
- id: key
type: accounts_key
- id: value
type: accounts_value
accounts_key:
seq:
- id: account
size: 32
doc: Public key
accounts_value:
seq:
- id: head
size: 32
doc: Hash of head block
- id: representative
size: 32
doc: Public key of account representative
- id: open_block
size: 32
doc: Hash of the open block for this account
- id: balance
size: 16
doc: 128 bit balance in raw
- id: modified
type: u8le
doc: Last account modification, in seconds since unix epoch
- id: block_count
type: u8le
doc: Number of blocks on the account chain
# -------------------------------------------------------------------
# Block definitions
# -------------------------------------------------------------------
block_send:
seq:
- id: previous
size: 32
doc: Hash of the previous block
- id: destination
size: 32
doc: Public key of destination account
- id: balance
size: 16
doc: 128-bit big endian balance
- id: signature
size: 64
doc: ed25519 signature
- id: work
type: u8le
doc: Proof of work
block_receive:
seq:
- id: previous
size: 32
doc: Hash of the previous block
- id: source
size: 32
doc: Hash of the source send block
- id: signature
size: 64
doc: ed25519 signature
- id: work
type: u8le
doc: Proof of work
block_open:
seq:
- id: source
size: 32
doc: Hash of the source send block
- id: representative
size: 32
doc: Public key of initial representative account
- id: account
size: 32
doc: Public key of account being opened
- id: signature
size: 64
doc: ed25519 signature
- id: work
type: u8le
doc: Proof of work
block_change:
seq:
- id: previous
size: 32
doc: Hash of the previous block
- id: representative
size: 32
doc: Public key of new representative account
- id: signature
size: 64
doc: ed25519 signature
- id: work
type: u8le
doc: Proof of work
block_state:
seq:
- id: account
size: 32
doc: Public key of account represented by this state block
- id: previous
size: 32
doc: Hash of previous block
- id: representative
size: 32
doc: Public key of the representative account
- id: balance
size: 16
doc: 128-bit big endian balance
- id: link
size: 32
doc: Pairing send's block hash (open/receive), 0 (change) or destination public key (send)
- id: signature
size: 64
doc: ed25519 signature
- id: work
type: u8be
doc: Proof of work (big endian)
doc: State block
# The block selector takes an integer argument representing the block type.
# Note that enum arguments are not yet supported in kaitai, hence the to_i casts.
block_selector:
doc: Selects a block based on the argument
params:
- id: arg_block_type
type: u1
seq:
- id: block
type:
switch-on: arg_block_type
cases:
'enum_blocktype::send.to_i': block_send
'enum_blocktype::receive.to_i': block_receive
'enum_blocktype::open.to_i': block_open
'enum_blocktype::change.to_i': block_change
'enum_blocktype::state.to_i': block_state
_: ignore_until_eof
# Catch-all that ignores until eof
ignore_until_eof:
seq:
- id: empty
type: u1
repeat: until
repeat-until: _io.eof
if: not _io.eof