-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelSandbox.js
More file actions
58 lines (47 loc) · 1.93 KB
/
levelSandbox.js
File metadata and controls
58 lines (47 loc) · 1.93 KB
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
/* ===== Persist data with LevelDB ===================================
| Learn more: level: https://github.com/Level/level |
| =============================================================*/
const level = require('level');
const chainDB = './chaindata';
const db = level(chainDB);
// Add data to levelDB with key/value pair
function addLevelDBData(key,value){
db.put(key, value, function(err) {
if (err) return console.log('Block ' + key + ' submission failed', err);
})
}
// Get data from levelDB with key
function getLevelDBData(key){
db.get(key, function(err, value) {
if (err) return console.log('Not found!', err);
if (value) return value;
console.log('Value = ' + value);
})
}
// Add data to levelDB with value
function addDataToLevelDB(newBlock) {
key = newBlock.height;
addLevelDBData(key, );
console.log('Block #' + key + ' saved');
}
/* ===== Testing ==============================================================|
| - Self-invoking function to add blocks to chain |
| - Learn more: |
| https://scottiestech.info/2014/07/01/javascript-fun-looping-with-a-delay/ |
| |
| * 100 Milliseconds loop = 36,000 blocks per hour |
| (13.89 hours for 500,000 blocks) |
| Bitcoin blockchain adds 8640 blocks per day |
| ( new block every 10 minutes ) |
| ===========================================================================*/
// (function theLoop (i) {
// setTimeout(function () {
// addDataToLevelDB('Testing data');
// if (--i) theLoop(i);
// }, 100);
// })(10);
module.exports = {
addDataToLevelDB: addDataToLevelDB,
getLevelDBData: getLevelDBData,
addLevelDBData: addLevelDBData,
}