Skip to content
This repository was archived by the owner on Oct 19, 2022. It is now read-only.

Commit bc6a0e5

Browse files
authored
Merge pull request #15 from dashevo/develop
Release v1.0.1
2 parents 1c12ada + 615f7a2 commit bc6a0e5

18 files changed

+1398
-1416
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
/libs/spv-dash/node_modules/buffers/index.js
2-
apply this fix https://github.com/vlocityinc/node-buffers/commit/eb663888b693bcca3ce715eaadef8748d3e0f835
1+
# dash-spv
2+
3+
> Temporary repo until spv functions moved into dashcore-lib
4+
5+
No new features should be added to this repo.
6+
7+
## License
8+
9+
[MIT](LICENSE) © Dash Core Group, Inc.

config/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
const DashUtil = require('@dashevo/dash-util');
12
const utils = require('../lib/utils');
2-
const DashUtil = require('dash-util');
33

44
module.exports = {
55
getLowDiffGenesis() {

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const spvChain = require('./lib/spvchain');
2-
const merkleProof = require('./lib/merkleproof');
2+
const merkleProof = require('./lib/merkleproofs');
33

44
module.exports = {
55
SpvChain: spvChain,

lib/blockstore.js

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
1-
// In memory storage of header chain
21
const levelup = require('levelup');
3-
const utils = require('./utils');
42
const memdown = require('memdown');
5-
const bitcore = require('bitcore-lib-dash');
3+
const dashcore = require('@dashevo/dashcore-lib');
64

7-
const DGWBlockCount = 25;
8-
9-
10-
const BlockStore = class {
11-
constructor(tipHash) {
5+
class BlockStore {
6+
constructor() {
127
this.db = levelup(memdown(), {
138
keyAsBuffer: false,
149
valueAsBuffer: false,
1510
valueEncoding: 'json',
1611
});
17-
this.block = bitcore.BlockHeader;
18-
this.tipHash = tipHash;
19-
this.dgwCache = [];
12+
this.block = dashcore.BlockHeader;
2013
}
2114

2215
put(header) {
23-
/* eslint no-underscore-dangle: ["error", { "allow": ["_getHash"] }] */
24-
this.tipHash = utils.getCorrectedHash(header._getHash());
25-
26-
const self = this;
27-
2816
return new Promise((resolve, reject) => {
29-
this.db.put(self.tipHash, JSON.stringify(header.toObject()), (err) => {
17+
this.db.put(header.hash, JSON.stringify(header.toObject()), (err) => {
3018
if (!err) {
31-
this.pushDgwCache(header);
32-
resolve(self.tipHash);
19+
resolve(header.hash);
3320
} else {
34-
// Todo update tiphash now incorrect
3521
reject(err);
3622
}
3723
});
@@ -54,19 +40,6 @@ const BlockStore = class {
5440
});
5541
}
5642

57-
pushDgwCache(header) {
58-
if (this.dgwCache.length > DGWBlockCount) { this.dgwCache.shift(); }
59-
this.dgwCache.push(header);
60-
}
61-
62-
getDgwCache() {
63-
return this.dgwCache;
64-
}
65-
66-
getTipHash() {
67-
return this.tipHash;
68-
}
69-
7043
close() {
7144
this.db.close();
7245
}
@@ -78,7 +51,7 @@ const BlockStore = class {
7851
isOpen() {
7952
return this.db.isOpen();
8053
}
81-
};
54+
}
8255

8356

8457
module.exports = BlockStore;

lib/consensus.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const dgwTarget = require('dark-gravity-wave-js').getTarget;
1+
const { isValidTarget } = require('@dashevo/dark-gravity-wave');
22
const utils = require('./utils');
33

44
module.exports = {
55

66
isValidBlockHeader(dgwHeaders, newHeader) {
7-
return newHeader.validProofOfWork() &&
8-
newHeader.validTimestamp() &&
9-
newHeader.bits <= dgwTarget(dgwHeaders.map(h => utils.getDgwBlock(h)));
7+
return newHeader.validProofOfWork()
8+
&& newHeader.validTimestamp()
9+
&& isValidTarget(newHeader.bits, dgwHeaders.map(h => utils.getDgwBlock(h)));
1010
},
11-
1211
};

lib/forkedchain.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

lib/merkleproof.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

lib/merkleproofs.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const dashcore = require('@dashevo/dashcore-lib');
2+
const calculateMnListMerkleRoot = require('./mnlistmerkleroot');
3+
4+
const merkleproofs = {
5+
6+
validateTxProofs: (merkleBlock, transactions) => merkleBlock.validMerkleTree()
7+
&& transactions.filter(t => merkleBlock.hasTransaction(t)).length === transactions.length,
8+
9+
validateMnProofs(header, flags, hashes, numTransactions, cbTxHash) {
10+
const merkleBlock = new dashcore.MerkleBlock({
11+
header,
12+
numTransactions,
13+
hashes,
14+
flags,
15+
});
16+
17+
return merkleBlock.validMerkleTree() && merkleBlock.hasTransaction(cbTxHash);
18+
},
19+
20+
validateMnListMerkleRoot(mnListMerkleRoot, mnList) {
21+
return calculateMnListMerkleRoot(mnList) === mnListMerkleRoot;
22+
},
23+
24+
};
25+
26+
module.exports = merkleproofs;

lib/mnlistmerkleroot.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const dashcore = require('@dashevo/dashcore-lib');
2+
3+
// from dashcore/lib/block/block.js
4+
function getMerkleTree(tree) {
5+
let j = 0;
6+
for (let size = tree.length; size > 1; size = Math.floor((size + 1) / 2)) {
7+
for (let i = 0; i < size; i += 2) {
8+
const i2 = Math.min(i + 1, size - 1);
9+
const buf = Buffer.concat([tree[j + i], tree[j + i2]]);
10+
tree.push(dashcore.crypto.Hash.sha256sha256(buf));
11+
}
12+
j += size;
13+
}
14+
15+
return tree;
16+
}
17+
18+
function calculateMnListMerkleRoot(mnList) {
19+
return getMerkleTree(mnList.sort((m1, m2) => m1.proRegTxHash > m2.proRegTxHash)
20+
.map(m => new dashcore.SimplifiedMNListEntry(m).getHash()))
21+
.slice(-1)[0]
22+
.toString('hex');
23+
}
24+
25+
module.exports = calculateMnListMerkleRoot;

0 commit comments

Comments
 (0)