Skip to content

Commit

Permalink
feat: 🎸 Optimize consensus algorithm and improve the BigNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
imfly committed Aug 14, 2020
1 parent 87e91c2 commit e408e2f
Show file tree
Hide file tree
Showing 22 changed files with 541 additions and 168 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ You can donate DDN to the follow address with [DDN Wallet](http://wallet.ddn.lin

## Contribute

Please visit [CONTRIBUTING](./doc/guide/contributing.md)
Please visit [CONTRIBUTING](./docs/guide/contributing.en-US.md)

## License

Expand Down
137 changes: 137 additions & 0 deletions config/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Please use yourself constants file
* Note: Once the mainnet is online, this file can no longer be changed.
*/
export default {
nethash: 'gar0fktt', // 标定该链的版本
tokenName: 'HBL',
tokenPrefix: 'H',
foundAddress: 'HLjrrVwnmMXstcAYVjcrpwyYb3kY1ehABU',
walletUrl: 'http://wallet.hbl.link',
enableMoreLockTypes: true,

interval: 10, // 10ms
delegates: 101, // number
superPeers: 21,
voters: 33,
remoteVoters: 1,
maxAmount: 100000000,
maxPayloadLength: 8 * 1024 * 1024,
blockHeaderLength: 248,
addressLength: 208,
maxAddressesLength: 208 * 128,
maxClientConnections: 100,
numberLength: 100000000,
maxRequests: 10000 * 12,
requestLength: 104,
signatureLength: 196,
maxSignaturesLength: 196 * 256,
maxConfirmations: 77 * 100,
confirmationLength: 77,
fixedPoint: 10 ** 8,
totalAmount: '10000000000000000', // = maxAmount * fixedPoint
maxTxsPerBlock: 500,

testnet: {
rewardRatio: 0.2,
milestones: [
'500000000', // Initial Reward Bignum update
'400000000', // Milestone 1
'300000000', // Milestone 2
'200000000', // Milestone 3
'100000000' // Milestone 4
],
beginDate: new Date(Date.UTC(2017, 10, 20, 12, 20, 20, 20)), // testnet
rewardDistance: 3000000, // Distance between each milestone
rewardOffset: 1, // Start rewards at block (n)60480
compatibleVersion: '0.0.0', // Peer isCompatible?

// Fees for transacton
fees: {
// base
transfer: '0.1',
vote: '0.1',
delegate: '100',
signature: '5',
multiSignature: '5',
lock: 'o.1',

// aob
aob_issuer: '100',
aob_asset: '500',
aob_flag: '0.1',
aob_acl: '0.1',
aob_issue: '0.1',
aob_transfer: '0',

// dapp
dapp: '100',
dapp_out: '0.1',
dapp_in: '0.1',

// todo: 测试中提供的费用
username: '0.1',

// dao
dao_confirmation: '1',
dao_contribution: '1',
dao_exchange: '0.1',
dao_org: '0.1',

// evidence
evidence: '0.1' // fixme
}
},

mainnet: {
rewardRatio: 0.2,
milestones: [
'500000000', // Initial Reward Bignum update
'400000000', // Milestone 1
'300000000', // Milestone 2
'200000000', // Milestone 3
'100000000' // Milestone 4
],
beginDate: new Date(Date.UTC(2017, 11, 20, 4, 0, 0, 0)), // 主网上线:2017年12月20日中午12点(+8)
rewardDistance: 3000000, // Distance between each milestone
rewardOffset: 1, // Start rewards at block (n)
// If you have some different thing, Please set the compatibleVersion.
compatibleVersion: '0.0.0', // Peer isCompatible?

// Fees for transacton
fees: {
// base
transfer: '0.1',
vote: '0.1',
delegate: '100',
signature: '5',
multiSignature: '5',
lock: 'o.1',

// aob
aob_issuer: '100',
aob_asset: '500',
aob_flag: '0.1',
aob_acl: '0.1',
aob_issue: '0.1',
aob_transfer: '0',

// dapp
dapp: '100',
dapp_out: '0.1',
dapp_in: '0.1',

// todo: 测试中提供的费用
username: '0.1',

// dao
dao_confirmation: '1',
dao_contribution: '1',
dao_exchange: '0.1',
dao_org: '0.1',

// evidence
evidence: '0.1' // fixme
}
}
}
4 changes: 2 additions & 2 deletions examples/fun-tests/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module.exports = {
interval: 10, // 10ms
delegates: 101, // number
superPeers: 21,
voters: 0,
remoteVoters: 0,
voters: 33,
remoteVoters: 1,
maxAmount: 100000000,
maxPayloadLength: 8 * 1024 * 1024,
blockHeaderLength: 248,
Expand Down
1 change: 1 addition & 0 deletions packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"bitcore-mnemonic": "^8.20.5",
"bytebuffer": "^5.0.1",
"crypto": "^1.0.1",
"fast-sha256": "^1.3.0",
"ripemd160": "^2.0.2",
"tweetnacl": "^1.0.2"
Expand Down
4 changes: 2 additions & 2 deletions packages/js-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"scripts": {
"test": "mocha test",
"build": "yarn build:dev & yarn build:prod",
"build:dev": "browserify index.js > ./build/index.browserify.js",
"build:prod": "browserify index.js -p tinyify > ./build/index.browserify.min.js"
"build:dev": "browserify index.js > ./build/ddn.browserify.js",
"build:prod": "browserify index.js -p tinyify > ./build/ddn.browserify.min.js"
},
"keywords": [
"ddn",
Expand Down
2 changes: 1 addition & 1 deletion packages/js-sdk/src/config/ddnrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
publicIp: '',
logLevel: 'error',
net: 'testnet',
nethash: 'gar0fktt',
nethash: '0ab796cd',
payloadLimitSize: '8mb',
clientDriftSeconds: 5
}
10 changes: 8 additions & 2 deletions packages/js-sdk/src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*-------------------------------------------------------------------------------------------- */

// import constants from './constants.ddn'
import constants from './constants.hbl'
import ddn from './constants.ddn'

let constants = ddn

// Todo: get it when building
if (process.env.DDN_ENV === 'custom') {
constants = require('./constants.custom').default
}

export default constants
4 changes: 2 additions & 2 deletions packages/node-sdk/src/constants.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default {
interval: 10, // 10ms
delegates: 101, // number
superPeers: 21,
voters: 0,
remoteVoters: 0,
voters: 66,
remoteVoters: 3,
maxAmount: 100000000,
maxPayloadLength: 8 * 1024 * 1024,
blockHeaderLength: 248,
Expand Down
12 changes: 7 additions & 5 deletions packages/peer/src/assets/system/multisignature.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,20 @@ class Multisignature {
}, dbTrans)
}

async applyUnconfirmed ({
asset
}, {
async applyUnconfirmed ({ asset }, {
address,
multisignatures
}, dbTrans) {
if (this._unconfirmedSignatures[address]) {
throw new Error('Signature on this account is pending confirmation')
// todo: 2020.8.13 throw 和 return 不是一样的流程,请确认
// throw new Error(`Signature on this account ${address} is pending confirmation`)
this.logger.info(`Signature on this account ${address} is pending confirmation`)
return
}

if (multisignatures.length) {
throw new Error('Account already has multisignatures enabled')
this.logger.info('Account already has multisignatures enabled')
return
}

this._unconfirmedSignatures[address] = true
Expand Down
47 changes: 32 additions & 15 deletions packages/peer/src/kernal/block/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ class Block {
payload_hash: raw.b_payloadHash, // wxm block database
generator_public_key: raw.b_generatorPublicKey, // wxm block database
generator_id: this.address.generateAddress(raw.b_generatorPublicKey), // imfly
// generator_id: address.generateB÷ase58CheckAddress(raw.b_generatorPublicKey), //wxm block database
block_signature: raw.b_blockSignature, // wxm block database
confirmations: raw.b_confirmations
}
block.totalForged = bignum.plus(block.total_fee, block.reward)
// fixme: 2020.8.12 bignum 的性能可能会影响块同步
block.totalForged = bignum.plus(block.total_fee, block.reward).toString()

return block
}
Expand All @@ -265,6 +265,7 @@ class Block {
* @param {*} dbTrans
*/
async saveBlock (block, dbTrans) {
this.logger.debug('start saveBlock block.height = ', block.height)
await this.serializeBlock2Db(block, dbTrans)

if (block.transactions && block.transactions.length > 0) {
Expand Down Expand Up @@ -397,6 +398,7 @@ class Block {

if (this.runtime.consensus.hasEnoughVotes(totalVotes)) {
const block = this.runtime.consensus.getPendingBlock()
this.logger.debug('receiveVotes getPendingBlock block.height = ', block.height)

const height = block.height
const id = block.id
Expand Down Expand Up @@ -432,7 +434,8 @@ class Block {

await new Promise((resolve, reject) => {
this.sequence.add(async cb => {
if (this._lastPropose && this._lastPropose.height === propose.height &&
// if (this._lastPropose && this._lastPropose.height === propose.height &&
if (this._lastPropose && bignum.isEqualTo(this._lastPropose.height, propose.height) &&
this._lastPropose.generator_public_key === propose.generator_public_key &&
this._lastPropose.id !== propose.id) {
this.logger.warn(`generate different block with the same height, generator: ${propose.generator_public_key}`)
Expand Down Expand Up @@ -543,7 +546,7 @@ class Block {
const transaction = sortedTrs[i]
const updatedAccountInfo = await this.runtime.account.setAccount({
publicKey: transaction.senderPublicKey,
isGenesis: block.height === 1
isGenesis: bignum.isEqualTo(block.height, 1)
}, dbTrans)

const accountInfo = await this.runtime.account.getAccountByAddress(updatedAccountInfo.address)
Expand Down Expand Up @@ -572,7 +575,7 @@ class Block {
if (err) {
applyedTrsIdSet.clear() // wxm TODO 清除上面未处理的交易记录
this.balanceCache.rollback()
if (!result) {
if (!result) { // fixme 2020.8.13 这里的 result 是 ????????? 重要, 按照上面 done() 的回调, result为null
this.logger.error(`回滚失败或者提交异常,出块失败: ${err}`)
process.exit(1)
} else { // 回滚成功
Expand Down Expand Up @@ -705,7 +708,7 @@ class Block {
block.height = bignum.plus(this._lastBlock.height, 1).toString()

if (typeof this._lastBlock.height === 'undefined') {
this.logger.debug(`verifyBlock, block: ${block}, pre-block: ${this._lastBlock}`)
this.logger.debug(`verifyBlock, block: ${JSON.stringify(block)}, pre-block: ${JSON.stringify(this._lastBlock)}`)
}
this.logger.debug(`verifyBlock, id: ${block.id}, pre-h: ${this._lastBlock.height}, h: ${block.height}`)

Expand All @@ -715,7 +718,8 @@ class Block {

const expectedReward = this._blockStatus.calcReward(block.height)

if (block.height !== 1 && !bignum.isEqualTo(expectedReward, block.reward)) {
// if (block.height !== 1 && !bignum.isEqualTo(expectedReward, block.reward)) {
if (!bignum.isEqualTo(block.height, 1) && !bignum.isEqualTo(expectedReward, block.reward)) {
throw new Error('Invalid block reward')
}

Expand All @@ -730,7 +734,8 @@ class Block {
// FIXME: 每次重启服务都会出现该错误 2020.6.2
if (block.previous_block !== this._lastBlock.id) {
await this.runtime.delegate.fork(block, 1)
this.logger.error('Incorrect previous block hash', block.previous_block, this._lastBlock.id)
this.logger.debug('Incorrect previous block hash, block.previous_block', block.previous_block)
this.logger.debug('Incorrect previous block hash, this._lastBlock.id', this._lastBlock.id)
throw new Error('Incorrect previous block hash')
}

Expand Down Expand Up @@ -993,7 +998,11 @@ class Block {

this.logger.info(`Generate new block at height ${(parseInt(this._lastBlock.height) + 1)}`)

await this.verifyBlock(block, null)
try {
await this.verifyBlock(block, null)
} catch (error) {
this.logger.debug(`Add try/catch to handle verifyBlock not passed ${error}`)
}

// 本地 keypairs
const activeKeypairs = await this.runtime.delegate.getActiveDelegateKeypairs(block.height)
Expand Down Expand Up @@ -1105,24 +1114,32 @@ class Block {
async _popLastBlock (oldLastBlock) {
return new Promise((resolve, reject) => {
this.balancesSequence.add(async cb => {
function done (err, previousBlock) {
if (err) {
const finalErr = 'popLastBlock err: ' + err
cb(finalErr)
} else {
cb(null, previousBlock)
}
}

this.logger.info(`begin to pop block ${oldLastBlock.height} ${oldLastBlock.id}`)

// wxm TODO 这里查询条件用的id = previous_block,但过来的previous_block肯定有问题怎么会查出来呢,所以改成按照height-1来查上一个,但不知道会不会有问题
let previousBlock = await this.runtime.dataquery.queryFullBlockData({
height: bignum.minus(oldLastBlock.height, 1).toString()
id: oldLastBlock.previous_block
}, 1, 0, [
['height', 'asc']
]) // {id: oldLastBlock.previous_block}
])
if (!previousBlock || !previousBlock.length) {
return cb('previousBlock is null')
return done('previousBlock is null')
}

previousBlock = previousBlock[0]

let transactions = this._sortTransactions(oldLastBlock.transactions)
transactions = transactions.reverse()

this.dao.transaction(async (dbTrans, done) => {
this.dao.transaction(async (dbTrans, cb) => {
try {
for (let i = 0; i < transactions.length; i++) {
const transaction = transactions[i]
Expand Down Expand Up @@ -1239,7 +1256,7 @@ class Block {
if (lastBlock && lastBlock.id) {
await this.verifyBlock(block, null)
}
// fixme: 获取到块之后,isSaveBlock 应该是 true ?? 2020.8.7
// fixme: 获取到块之后,isSaveBlock 应该是 true
await this.applyBlock(block, null, false, false)
} else {
this.setLastBlock(block)
Expand Down
Loading

0 comments on commit e408e2f

Please sign in to comment.