forked from nicholascc/steem-state
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (146 loc) · 4.81 KB
/
index.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
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
/*
args:
client: A dsteem client to use to get blocks, etc. [REQUIRED]
steem: A dsteem instance. [REQIURED]
currentBlockNumber: The last block that has been processed by this client; should be
loaded from some sort of storage file. Default is block 1.
blockComputeSpeed: The amount of milliseconds to wait before processing
another block (not used when streaming)
prefix: The prefix to use for each transaction id, to identify the DApp which
is using these transactions (interfering transaction with other Dappsids could cause
errors)
*/
module.exports = function(client, steem, currentBlockNumber=1, blockComputeSpeed=1000, prefix='', mode='latest') {
var onCustomJsonOperation = {}; // Stores the function to be run for each operation id.
var onOperation = {};
var onNewBlock = function() {};
var onStreamingStart = function() {};
var isStreaming;
var stream;
var stopping = false;
var stopCallback;
// Returns the block number of the last block on the chain or the last irreversible block depending on mode.
function getHeadOrIrreversibleBlockNumber(callback) {
client.database.getDynamicGlobalProperties().then(function(result) {
if(mode === 'latest') {
callback(result.head_block_number);
} else {
callback(result.last_irreversible_block_num);
}
})
}
function isAtRealTime(callback) {
getHeadOrIrreversibleBlockNumber(function(result) {
if(currentBlockNumber >= result) {
callback(true);
} else {
callback(false);
}
})
}
function beginBlockComputing() {
function computeBlock() {
var blockNum = currentBlockNumber;// Helper variable to prevent race condition
// in getBlock()
client.database.getBlock(blockNum)
.then((result) => {
processBlock(result, blockNum);
})
.catch((err) => {
console.log("Error getting block:", err);
})
currentBlockNumber++;
if(!stopping) {
isAtRealTime(function(result) {
if(!result) {
setTimeout(computeBlock, blockComputeSpeed);
} else {
beginBlockStreaming();
}
})
} else {
setTimeout(stopCallback,1000);
}
}
computeBlock();
}
function beginBlockStreaming() {
isStreaming = true;
onStreamingStart();
if(mode === 'latest') {
stream = client.blockchain.getBlockStream({mode: steem.BlockchainMode.Latest});
} else {
stream = client.blockchain.getBlockStream();
}
stream.on('data', function(block) {
var blockNum = parseInt(block.block_id.slice(0,8), 16);
if(blockNum >= currentBlockNumber) {
processBlock(block, blockNum);
currentBlockNumber = blockNum+1;
}
})
stream.on('end', function() {
console.error("Block stream ended unexpectedly.")
})
}
function processBlock(block, num) {
onNewBlock(num, block);
var transactions = block.transactions;
for(var i = 0; i < transactions.length; i++) {
for(var j = 0; j < transactions[i].operations.length; j++) {
var op = transactions[i].operations[j];
if(op[0] === 'custom_json') {
if(typeof onCustomJsonOperation[op[1].id] === 'function') {
onCustomJsonOperation[op[1].id](JSON.parse(op[1].json), op[1].required_posting_auths[0]);
}
} else if(onOperation[op[0]] !== undefined) {
onOperation[op[0]](op[1]);
}
}
}
}
return {
/*
Determines a state update to be called when a new operation of the id
operationId (with added prefix) is computed.
*/
on: function(operationId, callback) {
onCustomJsonOperation[prefix + operationId] = callback;
},
onOperation: function(type, callback) {
onOperation[type] = callback;
},
onNoPrefix: function(operationId, callback) {
onCustomJsonOperation[operationId] = callback;
},
/*
Determines a state update to be called when a new block is computed.
*/
onBlock: function(callback) {
onNewBlock = callback;
},
start: function() {
beginBlockComputing();
isStreaming = false;
},
getCurrentBlockNumber: function() {
return currentBlockNumber;
},
isStreaming: function() {
return isStreaming;
},
onStreamingStart: function(callback) {
onStreamingStart = callback;
},
stop: function(callback) {
if(isStreaming){
stopping = true;
stream.pause();
setTimeout(callback,1000);
} else {
stopping = true;
stopCallback = callback;
}
}
}
}