-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
43 lines (38 loc) · 868 Bytes
/
database.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
/*
Helper functions
*/
const level = require('level');
const chainDB = './chainData';
const db = level(chainDB);
// var exports = module.exports = {};
// Add block to DB
exports.addBlock = (key, value) => {
return new Promise((resolve, reject) => {
db.put(key, value, (err) => {
if (err) reject('Block ' + key + ' submission failed', err);
resolve(key);
})
});
}
// chain length
exports.getChainLength = () => {
return new Promise((resolve, reject) => {
let height = 0
db.createReadStream().on('data', (data) => {
height ++
}).on('error', (err) => {
reject(err)
}).on('close', () => {
resolve(height)
})
})
}
// Get Block
exports.getBlock = (key) => {
return new Promise((resolve, reject) => {
db.get(key, (err, value) => {
if (err) reject('Block ' + key + ' not found');
resolve(value)
})
})
}