This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update deps * update package lock * rem mnListDiff * add isValidTimestamp fn to consensus * add timestamp check * Timestamps: median instead of avg * update dark-gravity-wave * bump minor version * update dashcore-lib dep * update package lock * merge changes from master * update consensus * rem mnlist merkleproofs & test * correct validProofOfWork * rem unnecessary property .pow * typos in tests * add deserialized raw headers for tests * add network argument * add network property * check hasValidTarget only when enough headers * fix tests with deserialized headers * fix serialized tests * update switch case devnet * tests for adding many headers
- Loading branch information
Showing
11 changed files
with
5,281 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,44 @@ | ||
const { isValidTarget } = require('@dashevo/dark-gravity-wave'); | ||
const { hasValidTarget } = require('@dashevo/dark-gravity-wave'); | ||
const utils = require('./utils'); | ||
|
||
module.exports = { | ||
const MIN_TIMESTAMP_HEADERS = 11; | ||
const MIN_DGW_HEADERS = 24; | ||
|
||
function getMedianTimestamp(headers) { | ||
const timestamps = headers.map(h => h.time); | ||
const median = (arr) => { | ||
const mid = Math.floor(arr.length / 2); | ||
const nums = [...arr].sort((a, b) => a - b); | ||
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; | ||
}; | ||
return median(timestamps); | ||
} | ||
|
||
// Must be strictly greater than the median time of the previous 11 blocks. | ||
// https://dash-docs.github.io/en/developer-reference#block-headers | ||
function hasGreaterThanMedianTimestamp(newHeader, previousHeaders) { | ||
if (previousHeaders.length < MIN_TIMESTAMP_HEADERS) return true; | ||
const headerNormalised = utils.normalizeHeader(newHeader); | ||
const normalizedLatestHeaders = previousHeaders.slice( | ||
Math.max(previousHeaders.length - MIN_TIMESTAMP_HEADERS, 0), | ||
).map(h => utils.normalizeHeader(h)); | ||
return getMedianTimestamp(normalizedLatestHeaders) < headerNormalised.time; | ||
} | ||
|
||
isValidBlockHeader(dgwHeaders, newHeader) { | ||
function isValidBlockHeader(newHeader, previousHeaders, network = 'mainnet') { | ||
if (previousHeaders.length > MIN_DGW_HEADERS) { | ||
return newHeader.validProofOfWork() | ||
&& newHeader.validTimestamp() | ||
&& isValidTarget(newHeader.bits, dgwHeaders.map(h => utils.getDgwBlock(h))); | ||
}, | ||
&& hasGreaterThanMedianTimestamp(newHeader, previousHeaders) | ||
&& hasValidTarget( | ||
utils.getDgwBlock(newHeader), previousHeaders.map(h => utils.getDgwBlock(h)), network, | ||
); | ||
} | ||
return newHeader.validProofOfWork() | ||
&& newHeader.validTimestamp() | ||
&& hasGreaterThanMedianTimestamp(newHeader, previousHeaders); | ||
} | ||
|
||
module.exports = { | ||
isValidBlockHeader, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,7 @@ | ||
const dashcore = require('@dashevo/dashcore-lib'); | ||
const calculateMnListMerkleRoot = require('./mnlistmerkleroot'); | ||
|
||
const merkleproofs = { | ||
|
||
validateTxProofs: (merkleBlock, transactions) => merkleBlock.validMerkleTree() | ||
&& transactions.filter(t => merkleBlock.hasTransaction(t)).length === transactions.length, | ||
|
||
validateMnProofs(header, flags, hashes, numTransactions, cbTxHash) { | ||
const merkleBlock = new dashcore.MerkleBlock({ | ||
header, | ||
numTransactions, | ||
hashes, | ||
flags, | ||
}); | ||
|
||
return merkleBlock.validMerkleTree() && merkleBlock.hasTransaction(cbTxHash); | ||
}, | ||
|
||
validateMnListMerkleRoot(mnListMerkleRoot, mnList) { | ||
return calculateMnListMerkleRoot(mnList) === mnListMerkleRoot; | ||
}, | ||
|
||
}; | ||
|
||
module.exports = merkleproofs; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.