-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcommit-pool.js
More file actions
51 lines (44 loc) · 1.35 KB
/
commit-pool.js
File metadata and controls
51 lines (44 loc) · 1.35 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
const ChainUtil = require("./chain-util");
class CommitPool {
// list object is mapping that holds a list commit messages for a hash of a block
constructor() {
this.list = {};
}
// commit function initialize a list of commit message for a prepare message
// and adds the commit message for the current node and
// returns it
commit(prepare, wallet) {
let commit = this.createCommit(prepare, wallet);
this.list[prepare.blockHash] = [];
this.list[prepare.blockHash].push(commit);
return commit;
}
// creates a commit message for the given prepare message
createCommit(prepare, wallet) {
let commit = {};
commit.blockHash = prepare.blockHash;
commit.publicKey = wallet.getPublicKey();
commit.signature = wallet.sign(prepare.blockHash);
return commit;
}
// checks if the commit message already exists
existingCommit(commit) {
let exists = this.list[commit.blockHash].find(
p => p.publicKey === commit.publicKey
);
return exists;
}
// checks if the commit message is valid or not
isValidCommit(commit) {
return ChainUtil.verifySignature(
commit.publicKey,
commit.signature,
commit.blockHash
);
}
// pushes the commit message for a block hash into the list
addCommit(commit) {
this.list[commit.blockHash].push(commit);
}
}
module.exports = CommitPool;