-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnodeos.js
More file actions
221 lines (184 loc) · 7.57 KB
/
nodeos.js
File metadata and controls
221 lines (184 loc) · 7.57 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
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
const { getDB, getStartBlock, serialize, deserialize, handleHashesDB, pruneDB } = require("./db");
const { append, annotateIncrementalMerkleTree } = require("./functions");
const { blocksDB, rootDB, statusDB } = getDB();
const { asBinary } = require('lmdb');
const axios = require('axios');
const sleep = s => new Promise(resolve=>setTimeout(resolve,s*1000));
let batchBlocks = 800;
let head;
const startNodeos = async () => {
//catch up to lib
await catchUpToLib();
//handle reversible blocks
const start_block_num = await getStartBlock();
console.log("start_block_num",start_block_num);
let currentBlock = start_block_num;
let previousBlockId;
if(currentBlock > 1) {
let previousBuffer = await blocksDB.getBinary(currentBlock-1);
if(!previousBuffer){
console.log(`Cannot find previous block #${currentBlock-1} in lightproof-db`);
process.exit()
}
let previous = await deserialize(previousBuffer);
previousBlockId = previous.id;
console.log("previousBlockId",previousBlockId)
}
while (true){
let block;
while (!block){
try{ block = (await fetchBlockInfo(currentBlock)).data; }
catch(ex){ await sleep(0.5) }
}
if(block.previous === previousBlockId){
await processBlock(block);
previousBlockId = block.id;
currentBlock++;
}
else {
console.log("fork detected, block previous doesnt match the previous blocks id")
currentBlock--;
let previousBuffer = await blocksDB.getBinary(currentBlock-1);
if(!previousBuffer){
console.log(`Cannot find previous block #${currentBlock-1} in lightproof-db`);
process.exit()
}
let previous = await deserialize(previousBuffer);
previousBlockId = previous.id;
}
}
}
const catchUpToLib = async () =>{
head = (await fetchHeadInfo()).data;
let nodeosLib = head.last_irreversible_block_num;
const start_block_num = await getStartBlock();
console.log("start_block_num",start_block_num);
console.log("nodeosLib",nodeosLib);
if (nodeosLib <= start_block_num) return;
let currentBlock = start_block_num;
console.time("start");
//handle irreeversible blocks in bulk
while (currentBlock+batchBlocks < nodeosLib){
let promises = [];
for (var i=0;i<batchBlocks;i++) promises.push(fetchBlockInfo(currentBlock + i))
let blockLot;
while (!blockLot){
try{ blockLot = (await Promise.all(promises)).map(r=>r.data); }
catch(ex){ await sleep(5) }
}
await processIrreversibleLot(blockLot);
currentBlock+=batchBlocks;
}
//handle remaining irreeversible blocks (<batchBlocks)
let promises = [];
for (var i=0;i<nodeosLib-currentBlock + 1;i++) promises.push(fetchBlockInfo(currentBlock + i));
let blockLot;
while (!blockLot){
try{ blockLot = (await Promise.all(promises)).map(r=>r.data); }
catch(ex){await sleep(5)}
}
await processIrreversibleLot(blockLot);
currentBlock=nodeosLib;
console.log("done catching up to lib")
console.timeEnd("start");
console.log("Total blocks processed",nodeosLib - start_block_num )
}
const processIrreversibleLot = async blockLot =>{
if (!blockLot.length) return;
return rootDB.transaction(async () => {
// let block_num, block_id;
for (var block of blockLot) {
let block_num = block.block_num;
let block_id = block.id;
let blockExists = blocksDB.getBinary(block_num);
if (blockExists){
console.log(`overwriting block #${block_num} in lightproof-db`);
let existingBlock
try{ existingBlock = deserialize(blockExists); }
catch(ex){ console.log("ex deserializing",ex, blockExists); }
//remove the hash if not used in another block, or reduce its instance count by 1 to roll back the block
if(existingBlock) for (var node of existingBlock.nodes) await handleHashesDB(node);
}
if (!(block_num % 5000)){
head = (await fetchHeadInfo()).data;
const progress = (100 * ((block_num) / head.head_block_num)).toFixed(2)
console.log(`NODEOS: ${progress}% (${block_num}/${head.head_block_num})`);
await pruneDB();
}
let nodes, aliveUntil=0;
//if starting form genesis
if(block_num==1) nodes = [];
//if lightproof-db stored the previous block or starting from a snapshot
else {
let previousBuffer = blocksDB.getBinary(block_num-1);
//if starting from snapshot
if(!previousBuffer){
console.log(`Cannot find previous block #${block_num-1} in lightproof-db`);
process.exit()
}
//if lightproof-db has the previous block nodes
let previous = deserialize(previousBuffer);
const previousNodeCount = block_num-2;
const merkleTree = append(previous.id, previous.nodes, previousNodeCount);
nodes = merkleTree.activeNodes;
const tree = {...merkleTree, nodeCount: block_num-1};
const { blockToEdit } = annotateIncrementalMerkleTree(JSON.parse(JSON.stringify(tree)), false);
if (blockToEdit) aliveUntil = blockToEdit.aliveUntil;
}
var buffer = serialize(block_id, nodes, aliveUntil);
blocksDB.put(block_num, asBinary(buffer));
}
await statusDB.put("lib", blockLot[blockLot.length-1].block_num);
console.log("finished",blockLot.length, blockLot[0].block_num,"->",blockLot[blockLot.length-1].block_num)
});
}
const processBlock = async block =>{
return rootDB.transaction(async () => {
let block_num = block.block_num;
let block_id = block.id;
// console.log("Processing",block_num)
let blockExists = blocksDB.getBinary(block_num);
if (blockExists){
console.log(`overwriting block #${block_num} in lightproof-db`);
let existingBlock
try{ existingBlock = deserialize(blockExists); }
catch(ex){ console.log("ex deserializing",ex, blockExists); }
//remove the hash if not used in another block, or reduce its instance count by 1 to roll back the block
if(existingBlock) for (var node of existingBlock.nodes) await handleHashesDB(node);
}
if (!(block_num % 5000)){
head = (await fetchHeadInfo()).data;
const progress = (100 * ((block_num) / head.head_block_num)).toFixed(2)
console.log(`NODEOS: ${progress}% (${block_num}/${head.head_block_num})`);
statusDB.put("lib", block.block_num - 600);
await pruneDB();
}
let nodes, aliveUntil=0;
//if starting form genesis
if(block_num==1) nodes = [];
//if lightproof-db stored the previous block or starting from a snapshot
else {
let previousBuffer = blocksDB.getBinary(block_num-1);
//if starting from snapshot
if(!previousBuffer){
console.log(`Cannot find previous block #${block_num-1} in lightproof-db`);
process.exit()
}
//if lightproof-db has the previous block nodes
let previous = deserialize(previousBuffer);
const previousNodeCount = block_num-2
const merkleTree = append(previous.id, previous.nodes, previousNodeCount);
nodes = merkleTree.activeNodes;
const tree = {...merkleTree, nodeCount: block_num-1};
const { blockToEdit } = annotateIncrementalMerkleTree(JSON.parse(JSON.stringify(tree)), false);
if (blockToEdit) aliveUntil = blockToEdit.aliveUntil;
}
var buffer = serialize(block_id, nodes, aliveUntil);
blocksDB.put(block_num, asBinary(buffer));
});
}
const fetchBlockInfo = block_num => axios.post(`${process.env.NODEOS_HTTP}/v1/chain/get_block_info`, JSON.stringify({block_num}));
const fetchHeadInfo = () => axios(`${process.env.NODEOS_HTTP}/v1/chain/get_info`);
module.exports = {
startNodeos
}