|
|
@ -1,4 +1,6 @@ |
|
|
|
var http = require('http'); |
|
|
|
var http = require('http'); |
|
|
|
|
|
|
|
const fs = require('fs-ext'); |
|
|
|
|
|
|
|
|
|
|
|
var models = require('../models'); |
|
|
|
var models = require('../models'); |
|
|
|
var rpcConfig = require('../config/config')['rpc']; |
|
|
|
var rpcConfig = require('../config/config')['rpc']; |
|
|
|
|
|
|
|
|
|
|
@ -150,19 +152,38 @@ async function getSyncedHeight() { |
|
|
|
return height; |
|
|
|
return height; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function acquireLock() { |
|
|
|
|
|
|
|
let fd = fs.openSync('sync.lock', 'w'); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
fs.flockSync(fd, 'exnb'); |
|
|
|
|
|
|
|
} catch(ex) { |
|
|
|
|
|
|
|
if (ex.code === 'EAGAIN') { |
|
|
|
|
|
|
|
console.log('Synchronization is already running'); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
console.log('Could\'nt lock file', ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
throw ex; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function syncBlockchain() { |
|
|
|
async function syncBlockchain() { |
|
|
|
let syncedHeight = await getSyncedHeight(); |
|
|
|
|
|
|
|
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight is', syncedHeight); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let currentHeight = await getCurrentHeight(); |
|
|
|
|
|
|
|
console.log('\x1b[36m%s\x1b[0m', 'currentHeight is', currentHeight); |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await acquireLock(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let syncedHeight = await getSyncedHeight(); |
|
|
|
|
|
|
|
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight is', syncedHeight); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let currentHeight = await getCurrentHeight(); |
|
|
|
|
|
|
|
console.log('\x1b[36m%s\x1b[0m', 'currentHeight is', currentHeight); |
|
|
|
|
|
|
|
|
|
|
|
while (syncedHeight < currentHeight) { |
|
|
|
while (syncedHeight < currentHeight) { |
|
|
|
syncedHeight = await syncNextBlock(syncedHeight); |
|
|
|
syncedHeight = await syncNextBlock(syncedHeight); |
|
|
|
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight: ', syncedHeight) |
|
|
|
console.log('\x1b[36m%s\x1b[0m', 'syncedHeight: ', syncedHeight) |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
console.log('=====', e); |
|
|
|
console.log(e); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
models.sequelize.close().then(() => process.exit(0)); |
|
|
|
models.sequelize.close().then(() => process.exit(0)); |
|
|
|
} |
|
|
|
} |
|
|
|