forked from ethereum/mist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeSync.js
More file actions
204 lines (152 loc) · 5.86 KB
/
nodeSync.js
File metadata and controls
204 lines (152 loc) · 5.86 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
/**
The nodeSync module,
checks the current node whether its synching or not and how much it kept up already.
@module nodeSync
*/
const _ = global._;
const Q = require('bluebird');
const EventEmitter = require('events').EventEmitter;
const { ipcMain: ipc } = require('electron');
const ethereumNode = require('./ethereumNode');
const log = require('./utils/logger').create('NodeSync');
const SYNC_CHECK_INTERVAL_MS = 2000;
class NodeSync extends EventEmitter {
constructor() {
super();
ethereumNode.on('state', _.bind(this._onNodeStateChanged, this));
}
/**
* @return {Promise}
*/
start() {
if (this._syncPromise) {
log.warn('Sync already in progress, returning Promise');
return Q.resolve(this._syncPromise);
}
this._syncPromise = Q.try(() => {
if (!ethereumNode.isIpcConnected) {
throw new Error('Cannot sync - Ethereum node not yet connected');
}
return new Q((resolve, reject) => {
log.info('Starting sync loop');
this._syncInProgress = true;
this._onSyncDone = resolve;
this._onSyncError = reject;
this.emit('starting');
ipc.on('backendAction_skipSync', () => {
ipc.removeAllListeners('backendAction_skipSync');
log.info('Sync has been skipped');
this._onSyncDone();
});
this._sync();
});
})
.then(() => {
this.emit('finished');
})
.catch((err) => {
log.error('Sync error', err);
this.emit('error', err);
})
.finally(() => {
log.info('Sync loop ended');
this._clearState();
});
return this._syncPromise;
}
/**
* @return {Promise}
*/
stop() {
return Q.try(() => {
if (!this._syncInProgress) {
log.debug('Sync not already in progress.');
} else {
log.info('Stopping sync loop');
this._clearState();
return Q.delay(SYNC_CHECK_INTERVAL_MS)
.then(() => {
this.emit('stopped');
});
}
});
}
_clearState() {
ipc.removeAllListeners('backendAction_skipSync');
this._syncInProgress
= this._syncPromise
= this._onSyncDone
= this._onSyncError
= false;
}
_sync() {
_.delay(() => {
if (!this._syncInProgress) {
log.debug('Sync no longer in progress, so ending sync loop.');
return;
}
log.trace('Check sync status');
ethereumNode.send('eth_syncing', [])
.then((ret) => {
const result = ret.result;
// got a result, check for error
if (result) {
log.trace('Sync status', result);
// got an error?
if (result.error) {
if (result.error.code === -32601) {
log.warn('Sync method not implemented, skipping sync.');
return this._onSyncDone();
}
throw new Error(`Unexpected error: ${result.error}`);
} else { // no error, so call again in a bit
this.emit('nodeSyncing', result);
return this._sync();
}
} else { // got no result, let's check the block number
log.debug('Check latest block number');
return ethereumNode.send('eth_getBlockByNumber', ['latest', false])
.then((ret2) => {
const blockResult = ret2.result;
const now = Math.floor(new Date().getTime() / 1000);
if (!blockResult) {
return this._sync();
}
log.debug(`Last block: ${Number(blockResult.number)}; timestamp: ${blockResult.timestamp}`);
const diff = now - +blockResult.timestamp;
// need sync if > 1 minute
if (diff > 60) {
this.emit('nodeSyncing', result);
log.trace('Keep syncing...');
return this._sync();
}
log.info('No more sync necessary');
return this._onSyncDone();
});
}
})
.catch((err) => {
log.error('Node crashed while syncing?', err);
this._onSyncError(err);
});
}, SYNC_CHECK_INTERVAL_MS);
}
_onNodeStateChanged(state) {
switch (state) { // eslint-disable-line default-case
// stop syncing when node about to be stopped
case ethereumNode.STATES.STOPPING:
log.info('Ethereum node stopping, so stop sync');
this.stop();
break;
// auto-sync whenever node gets connected
case ethereumNode.STATES.CONNECTED:
log.info('Ethereum node connected, re-start sync');
// stop syncing, then start again
this.stop().then(() => {
this.start();
});
break;
}
}
}
module.exports = new NodeSync();