-
Notifications
You must be signed in to change notification settings - Fork 1
/
NONCE
54 lines (48 loc) · 1.1 KB
/
NONCE
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
// const Bar = require("./Bar.js");
// const crypto = require("crypto");
// class Karchain {
// constructor() { ... }
// get() { ... }
// get latestBar() { ... }
// isValidHashDifficulty(hash) { ... }
// calculateHashForBar(bar) { ... }
// calculateHash(...) { ... }
// mine(data) { ... }
generateNextBar(data) {
const nextIndex = this.latestBar.index + 1;
const previousHash = this.latestBar.hash;
let timestamp = new Date().getTime();
let nonce = 0;
let nextHash = this.calculateHash(
nextIndex,
previousHash,
timestamp,
data,
provider,
nonce
);
while (!this.isValidHashDifficulty(nextHash)) {
nonce = nonce + 1;
timestamp = new Date().getTime();
nextHash = this.calculateHash(
nextIndex,
previousHash,
timestamp,
data,
provider,
nonce
);
}
const nextBlock = new Block(
nextIndex,
previousBlock.hash,
nextTimestamp,
data,
provider,
nextHash,
nonce
);
return nextBar;
}
// };
// module.exports = Blockchain;