-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.js
More file actions
67 lines (62 loc) · 1.84 KB
/
Copy pathblock.js
File metadata and controls
67 lines (62 loc) · 1.84 KB
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
62
63
64
65
66
67
const hexToBinary = require("hex-to-binary");
const {GENESIS_DATA, MINE_RATE} = require("./config");
const cryptoHash = require("./crypto-hash");
class Block{
constructor({timestamp,prevHash,hash,data,nonce,difficulty}){ //passing parameters as objects
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;
let nonce =0;
do {
nonce++;
timestamp=Date.now();
difficulty = Block.adjustDifficulty({
orginalBlock:prevBlock,
timestamp,
});
hash= cryptoHash(timestamp,prevHash,data,nonce,difficulty)
} while (hexToBinary(hash).substring(0,difficulty)!=='0'.repeat(difficulty));
return new this({
timestamp,
prevHash,
data,
difficulty,
nonce,
hash,
});
}
static adjustDifficulty({orginalBlock,timestamp}){
const {difficulty}=orginalBlock;
if(difficulty<1) return 1;
const difference = timestamp - orginalBlock.timestamp;
if (difference>MINE_RATE) {
return difficulty -1;
}
else
{
return difficulty +1;
}
}
}
const block1 = new Block
({
hash : "0xacb",
timestamp : "24/06/23",
prevHash :"0xc12",
data:"hillo",
});
//console.log(block1);
//const result= Block.mineBlock({prevBlock:block1,data:"block 2"})
//console.log(result);
module.exports = Block;