-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.js
62 lines (54 loc) · 1.54 KB
/
block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const {GENESIS_DATA , MINE_RATE} = require('./config');
const cryptoHash = require('./cryptoHash');
const hexToBinary = require('hex-to-binary');
class Block {
constructor({ timestamp, prevHash, hash, data, nonce, difficulty }) {
this.timestamp = timestamp;
this.prevHash = prevHash;
this.hash = hash;
this.data = data;
this.nonce = nonce;
this.difficulty = difficulty;
}
static genesis(){
return new this(GENESIS_DATA);
}
static mineBlock({prevBlock, data}){
let hash,timestamp;
const prevHash = prevBlock.hash;
let difficulty = prevBlock.difficulty;
let nonce =0;
do{
nonce++;
timestamp = Date.now();
difficulty = Block.adjustDifficulty({
lastBlock: prevBlock,
timestamp,
})
hash = cryptoHash(timestamp,prevHash,data,nonce,difficulty);
}while(hexToBinary(hash).substring(0,difficulty) !== '0'.repeat(difficulty));
return new Block({
timestamp,
prevHash,
data,
hash,
difficulty,
nonce,
});
}
static adjustDifficulty({lastBlock,timestamp}){
const { difficulty } = lastBlock;
if(difficulty < 1) return 1;
const difference = timestamp - lastBlock.timestamp;
if(difference > MINE_RATE){
return difficulty - 1;
}
return difficulty + 1;
}
}
const genesisBlock = Block.genesis();
//console.log(genesisBlock)
// const result = Block.mineBlock({prevBlock:genesisBlock,data:"Bobade"});
// console.log(result);
// console.log(block1);
module.exports = Block;