Skip to content

Commit

Permalink
WIP: add initial DAA support
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcanfly committed May 2, 2018
1 parent 7c8965d commit b02fbec
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
58 changes: 58 additions & 0 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ class Chain extends AsyncEmitter {
state.flags = Script.flags.VERIFY_SIGHASH_FORKID;
}

// DAA is now enabled.
if (height >= this.network.block.daaheight)
state.daa = true;

// CHECKSEQUENCEVERIFY and median time
// past locktimes are now usable (bip9 & bip113).
if (await this.isActive(prev, deployments.csv)) {
Expand Down Expand Up @@ -2172,6 +2176,27 @@ class Chain extends AsyncEmitter {
return this.getTarget(this.network.now(), this.tip);
}

/**
* Get median of timestamp last three blocks based on timestamp.
* @param {ChainEntry} prev - Previous entry.
* @returns {Promise} - returns Entry
*/

async getSuitableBlock(prev) {
const median = {};

for (let i = 0; i < 2; i++) {
median[prev.time] = prev;
prev= await this.getPrevious(prev);

This comment has been minimized.

Copy link
@bucko13

bucko13 May 2, 2018

Contributor

Space before =

}

const times = Object.keys(median);
times.sort(cmp);

const mid = times[times.length >>> 1];
return median[mid];
}

/**
* Calculate the next target.
* @param {Number} time - Next block timestamp.
Expand All @@ -2189,6 +2214,29 @@ class Chain extends AsyncEmitter {
return pow.bits;
}

// IsDAAEnabled
if (this.state.hasDAA()) {
// Do not retarget
if (pow.targetReset) {
// Special behavior for testnet:
if (time > prev.time + pow.targetSpacing * 2)
return pow.bits;
}

// Compute the difficulty on the full adjustment interval.
assert(prev.height >= pow.retargetInterval);

// Get the last suitable block of the difficulty interval.
const last = await this.getSuitableBlock(prev);

// Get the first suitable block of the difficulty interval.
const height = prev.height - 144;
const first = await this.getSuitableBlock(await this.getAncestor(prev, height));

// Compute the target based on time eand work done during the interval.
return this.retarget(last, first);
}

if ((prev.height + 1) % pow.retargetInterval === 0) {
// Back 2 weeks
const height = prev.height - (pow.retargetInterval - 1);
Expand Down Expand Up @@ -2755,6 +2803,7 @@ class DeploymentState {
this.bip34 = false;
this.bip91 = false;
this.bip148 = false;
this.daa = false;
}

/**
Expand Down Expand Up @@ -2837,6 +2886,15 @@ class DeploymentState {
hasBIP148() {
return this.bip148;
}

/**
* Test whether DAA is active.
* @returns {Boolean}
*/

hasDAA() {
return this.daa;
}
}

/**
Expand Down
26 changes: 24 additions & 2 deletions lib/protocol/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ main.checkpointMap = {
460000: '8c25fc7e414d3e868d6ce0ec473c30ad44e7e8bc1b75ef000000000000000000',
470000: '89756d1ed75901437300af10d5ab69070a282e729c536c000000000000000000',
// UAHF fork block:
478559: 'ec5e1a193601f25ff1d94b421ddead0dbefcb99cf91e65000000000000000000'
478559: 'ec5e1a193601f25ff1d94b421ddead0dbefcb99cf91e65000000000000000000',
// DAA fork block
504031: '9cabb6ee1b1a4c3b659d70be75810be83d0a0db665bf1e010000000000000000'
};

/**
Expand Down Expand Up @@ -275,6 +277,18 @@ main.block = {

bip66hash: '3109b588941188a9f1c2576aae462d729b8cce9da1ea79030000000000000000',

/**
* Height at which DAA was activated.
*/

daaheight: 504031,

/**
* Hash of the block that activated DAA
*/

daahash: '9cabb6ee1b1a4c3b659d70be75810be83d0a0db665bf1e010000000000000000',

/**
* Safe height to start pruning.
*/
Expand Down Expand Up @@ -497,7 +511,9 @@ testnet.checkpointMap = {
780000: '0381582e34c3755964dc2813e2b33e521e5596367144e1670851050000000000',
840000: 'dac1648107bd4394e57e4083c86d42b548b1cfb119665f179ea80a0000000000',
900000: '9bd8ac418beeb1a2cf5d68c8b5c6ebaa947a5b766e5524898d6f350000000000',
1050000: 'd8190cf0af7f08e179cab51d67db0b44b87951a78f7fdc31b4a01a0000000000'
1050000: 'd8190cf0af7f08e179cab51d67db0b44b87951a78f7fdc31b4a01a0000000000',
1155875: '38f1ae7f0ea8c1b589884c5fbd0b83721e3ab6759a4b897206857cf100000000',
1188697: 'fb47e0ab0d2448f71192a09fe61bc9c46cd3b4e7bd778091d00e170000000000'
};

testnet.lastCheckpoint = 1050000;
Expand Down Expand Up @@ -551,6 +567,8 @@ testnet.block = {
bip65hash: 'b61e864fbec41dfaf09da05d1d76dc068b0dd82ee7982ff255667f0000000000',
bip66height: 330776,
bip66hash: '82a14b9e5ea81d4832b8e2cd3c2a6092b5a3853285a8995ec4c8042100000000',
daaheight: 1188697,
daahash: 'fb47e0ab0d2448f71192a09fe61bc9c46cd3b4e7bd778091d00e170000000000',
pruneAfterHeight: 1000,
keepBlocks: 10000,
maxTipAge: 24 * 60 * 60,
Expand Down Expand Up @@ -692,6 +710,8 @@ regtest.block = {
bip65hash: null,
bip66height: 1251,
bip66hash: null,
daaheight: 0,
daahash: null,
pruneAfterHeight: 1000,
keepBlocks: 10000,
maxTipAge: 0xffffffff,
Expand Down Expand Up @@ -835,6 +855,8 @@ simnet.block = {
bip65hash: 'f67ad7695d9b662a72ff3d8edbbb2de0bfa67b13974bb9910d116d5cbd863e68',
bip66height: 0,
bip66hash: 'f67ad7695d9b662a72ff3d8edbbb2de0bfa67b13974bb9910d116d5cbd863e68',
daaheight: 0,
daahash: null,
pruneAfterHeight: 1000,
keepBlocks: 10000,
maxTipAge: 0xffffffff,
Expand Down

0 comments on commit b02fbec

Please sign in to comment.