forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.js
51 lines (43 loc) · 907 Bytes
/
layout.js
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
/*!
* layout.js - mempool data layout for bcoin
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
* https://github.com/bcoin-org/bcoin
*/
'use strict';
const assert = require('assert');
/*
* Database Layout:
* R -> tip hash
* V -> db version
* e[id][hash] -> entry
*/
const layout = {
binary: true,
R: Buffer.from([0x52]),
V: Buffer.from([0x76]),
F: Buffer.from([0x46]),
e: function e(hash) {
const key = Buffer.allocUnsafe(33);
key[0] = 0x65;
write(key, hash, 1);
return key;
},
ee: function ee(key) {
assert(Buffer.isBuffer(key));
assert(key.length === 33);
return key.toString('hex', 1, 33);
}
};
/*
* Helpers
*/
function write(data, str, off) {
if (Buffer.isBuffer(str))
return str.copy(data, off);
assert(typeof str === 'string');
return data.write(str, off, 'hex');
}
/*
* Expose
*/
module.exports = layout;