Skip to content

Commit

Permalink
feat: 🎸 Optimize logger to make DDN debug more friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
limsbase committed Oct 30, 2020
1 parent 61be42d commit 83be9c8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 171 deletions.
1 change: 0 additions & 1 deletion packages/peer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"sequelize": "^5.21.3",
"socket.io": "^2.2.0",
"sqlite3": "^5.0.0",
"strftime": "^0.10.0",
"tracer": "^0.8.11",
"util-extend": "1.0.3",
"valid-url": "1.0.9"
Expand Down
164 changes: 95 additions & 69 deletions packages/peer/src/kernal/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import ip from 'ip'
import extend from 'extend2'
import DdnCrypto from '@ddn/crypto'
import DdnUtils from '@ddn/utils'
import tracer from 'tracer'

import { Logger } from '../logger'
import Context from './context'
import Block from './block/block'
import Transaction from './transaction/transaction'
Expand Down Expand Up @@ -38,10 +38,21 @@ process.env.UV_THREADPOOL_SIZE = 20 // max: 128

class Program {
async _init (options) {
options.logger = Logger({
filename: path.join(options.baseDir, 'logs', 'debug.log'),
echo: options.isDaemonMode ? null : options.configObject.logLevel,
errorLevel: options.configObject.logLevel
options.logger = tracer.colorConsole({
level: options.configObject.logLevel,
format: [
'{{title}} {{timestamp}} {{message}} (in {{file}}:{{line}})', // default format
{
error: '{{title}} {{timestamp}} {{message}} (in {{file}}:{{line}})\nCall Stack:\n{{stack}}' // error format
}
],
dateformat: 'HH:MM:ss.L',
transport: function (data) {
console.log(data.output)
fs.appendFile(path.join(options.baseDir, 'logs', 'debug.log'), data.rawoutput + '\n', err => {
if (err) throw err
})
}
})

if (!options.configObject.publicIp) {
Expand Down Expand Up @@ -83,8 +94,8 @@ class Program {
}

/**
* 文件锁,保证系统只能运行一份
*/
* 文件锁,保证系统只能运行一份
*/
_checkProcessState () {
if (this._context.isDaemonMode) {
try {
Expand All @@ -104,8 +115,8 @@ class Program {
}

/**
* 释放文件锁
*/
* 释放文件锁
*/
_resetProcessState () {
try {
if (fs.existsSync(this._pid_file)) {
Expand All @@ -117,8 +128,8 @@ class Program {
}

/**
* 升级数据库结构
*/
* 升级数据库结构
*/
async _applyDatabaseUpgrade () {
return new Promise((resolve, reject) => {
dbUpgrade.upgrade(this._context, (err, result) => {
Expand All @@ -132,8 +143,8 @@ class Program {
}

/**
* 校验创世区块的数据
*/
* 校验创世区块的数据
*/
async _checkGenesisBlock () {
const block = this._context.genesisblock

Expand Down Expand Up @@ -192,7 +203,7 @@ class Program {
process.emit('cleanup')
})

if (typeof (gc) === 'function') {
if (typeof gc === 'function') {
setInterval(() => {
// eslint-disable-next-line no-undef
gc()
Expand All @@ -211,7 +222,7 @@ class Program {
await this._applyDatabaseUpgrade()

// 验证创世区块数据是否合法
if (!await this._checkGenesisBlock()) {
if (!(await this._checkGenesisBlock())) {
process.exit(1)
}

Expand Down Expand Up @@ -269,23 +280,26 @@ class Program {
}

async _blockchainReady () {
if (!this._blockchainReadyFired &&
this._context.runtime.state === DdnUtils.runtimeState.Ready) {
if (!this._blockchainReadyFired && this._context.runtime.state === DdnUtils.runtimeState.Ready) {
// 通知资产系统已就绪事件
await this._context.runtime.transaction.execAssetFunc('onBlockchainReady')
this._blockchainReadyFired = true
}
}

/**
* 获取一个有效节点(非本机自己)
*/
* 获取一个有效节点(非本机自己)
*/
async getValidPeer () {
try {
const publicIp = this._context.config.publicIp || '127.0.0.1'
const publicIpLongValue = ip.toLong(publicIp)
const port = this._context.config.port
const result = await this._context.runtime.peer.queryList(null, { state: { $gt: 0 }, $not: { ip: publicIpLongValue, port: port } }, 1)
const result = await this._context.runtime.peer.queryList(
null,
{ state: { $gt: 0 }, $not: { ip: publicIpLongValue, port: port } },
1
)
if (result && result.length) {
return result[0]
}
Expand All @@ -296,8 +310,8 @@ class Program {
}

/**
* 同步节点列表 & 维护本地节点状态(轮询)
*/
* 同步节点列表 & 维护本地节点状态(轮询)
*/
async startPeerSyncTask () {
const validPeer = await this.getValidPeer()
if (validPeer) {
Expand All @@ -323,8 +337,8 @@ class Program {
}

/**
* 签名同步任务(轮询)
*/
* 签名同步任务(轮询)
*/
async startSignaturesSyncTask () {
const validPeer = await this.getValidPeer()
if (validPeer) {
Expand All @@ -343,8 +357,8 @@ class Program {
}

/**
* 同步未确认交易(轮询)
*/
* 同步未确认交易(轮询)
*/
async startUnconfirmedTransactionSyncTask () {
const validPeer = await this.getValidPeer()
if (validPeer) {
Expand All @@ -367,8 +381,8 @@ class Program {
}

/**
* 同步节点区块数据(轮询)
*/
* 同步节点区块数据(轮询)
*/
async startBlockDataSyncTask () {
const validPeer = await this.getValidPeer()
if (validPeer) {
Expand All @@ -385,28 +399,31 @@ class Program {

this._context.logger.debug('startSyncBlocks enter')

await new Promise((resolve) => {
this._context.sequence.add(async (cb) => {
try {
const syncCompleted = await this._context.runtime.peer.syncBlocks()
cb(null, syncCompleted)
} catch (syncErr) {
cb(syncErr)
await new Promise(resolve => {
this._context.sequence.add(
async cb => {
try {
const syncCompleted = await this._context.runtime.peer.syncBlocks()
cb(null, syncCompleted)
} catch (syncErr) {
cb(syncErr)
}
},
async (err, syncCompleted) => {
err && this._context.logger.error('loadBlocks timer:', err)
this._context.logger.debug('startSyncBlocks end')

if (syncCompleted) {
this._context.runtime.state = DdnUtils.runtimeState.Ready
await this._blockchainReady()
} else {
this._context.logger.debug('startSyncBlocks not complete change state pending')
this._context.runtime.state = DdnUtils.runtimeState.Pending
}

resolve()
}
}, async (err, syncCompleted) => {
err && this._context.logger.error('loadBlocks timer:', err)
this._context.logger.debug('startSyncBlocks end')

if (syncCompleted) {
this._context.runtime.state = DdnUtils.runtimeState.Ready
await this._blockchainReady()
} else {
this._context.logger.debug('startSyncBlocks not complete change state pending')
this._context.runtime.state = DdnUtils.runtimeState.Pending
}

resolve()
})
)
})
}
})()
Expand All @@ -425,8 +442,8 @@ class Program {
}

/**
* 尝试铸造区块(轮询)
*/
* 尝试铸造区块(轮询)
*/
async startForgeBlockTask () {
// const lastBlock = this._context.runtime.block.getLastBlock()
// if (lastBlock.height > 3) {
Expand Down Expand Up @@ -464,32 +481,41 @@ class Program {
return
}

const forgeDelegateInfo = await this._context.runtime.delegate.getForgeDelegateWithCurrentTime(currentSlot, DdnUtils.bignum.plus(lastBlock.height, 1))
const forgeDelegateInfo = await this._context.runtime.delegate.getForgeDelegateWithCurrentTime(
currentSlot,
DdnUtils.bignum.plus(lastBlock.height, 1)
)
if (forgeDelegateInfo === null) {
this._context.logger.trace('Loop:', 'skipping slot')
return
}

await new Promise((resolve) => {
this._context.sequence.add(async (cb) => {
if (this._context.runtime.slot.getSlotNumber(forgeDelegateInfo.time) === this._context.runtime.slot.getSlotNumber() &&
this._context.runtime.block.getLastBlock().timestamp < forgeDelegateInfo.time) {
try {
await this._context.runtime.block.generateBlock(forgeDelegateInfo.keypair, forgeDelegateInfo.time)
} catch (err) {
this._context.logger.error('Forged new block failed: ' + DdnUtils.system.getErrorMsg(err))
cb('Forged new block failed: ' + err) // Added: 2020.9.4
await new Promise(resolve => {
this._context.sequence.add(
async cb => {
if (
this._context.runtime.slot.getSlotNumber(forgeDelegateInfo.time) ===
this._context.runtime.slot.getSlotNumber() &&
this._context.runtime.block.getLastBlock().timestamp < forgeDelegateInfo.time
) {
try {
await this._context.runtime.block.generateBlock(forgeDelegateInfo.keypair, forgeDelegateInfo.time)
} catch (err) {
this._context.logger.error('Forged new block failed: ' + DdnUtils.system.getErrorMsg(err))
cb('Forged new block failed: ' + err) // Added: 2020.9.4
}
}
}

cb()
}, (err) => {
if (err) {
this._context.logger.error('Failed generate block within slot:', err)
}
cb()
},
err => {
if (err) {
this._context.logger.error('Failed generate block within slot:', err)
}

resolve()
})
resolve()
}
)
})
})()

Expand Down
Loading

0 comments on commit 83be9c8

Please sign in to comment.