-
Notifications
You must be signed in to change notification settings - Fork 0
/
levelSandbox.js
54 lines (47 loc) · 1.93 KB
/
levelSandbox.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
52
53
54
/* ===== Persist data with LevelDB ==================
| Learn more: level: https://github.com/Level/level |
/===================================================*/
let level = require('level');
let chainDB = './chaindata';
let 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);
console.log('Value = ' + value);
})
}
// Add data to levelDB with value
function addDataToLevelDB(value) {
let i = 0;
db.createReadStream().on('data', function(data) {
i++;
}).on('error', function(err) {
return console.log('Unable to read data stream!', err)
}).on('close', function() {
console.log('Block #' + i);
addLevelDBData(i, value);
});
}
/* ===== 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);