-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathburgerBlock.js
More file actions
66 lines (56 loc) · 2.38 KB
/
Copy pathburgerBlock.js
File metadata and controls
66 lines (56 loc) · 2.38 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
const HashProvider = require('./hashProvider');
class BurgerBlock {
constructor(index, transactions, difficulty, prevBlockHash, minedBy) {
this.index = index; //We will pass the appoppriate index
this.transactions = transactions;
this.difficulty = difficulty;
this.minedBy = minedBy;
this.prevBlockHash = prevBlockHash;
this.blockDataHash = this.calculateBlockDataHash();
this.nonce = 0;
this.dateCreated = new Date().toISOString();
this.blockHash = null;
}
static createNewInstance(blockData) {
const burgerBlockInstance = new BurgerBlock();
Object.assign(burgerBlockInstance, blockData);
return burgerBlockInstance;
}
calculateBlockDataHash() {
const blockData = {
index: this.index,
transactions: this.transactions,
difficulty: this.difficulty,
prevBlockHash: this.prevBlockHash,
minedBy: this.minedBy
};
return HashProvider.calculateBlockDataHash(blockData);
}
static validateBlock(block) {
const burgerBlockKeys = Object.keys(new BurgerBlock());
const currentBlockKeys = Object.keys(block);
const areKeysEqual = JSON.stringify(burgerBlockKeys) === JSON.stringify(currentBlockKeys);
const isIndexANumber = typeof block.index === 'number';
const isTransactionsAnArray = Array.isArray(block.transactions);
const isDifficultyANumber = typeof block.difficulty === 'number';
const isPrevBlockhashAString = typeof block.prevBlockHash === 'string';
// TODO: genesis block minedBy shouldn't default to zero?
const isMinedByAString = typeof block.minedBy === 'string';
const isBlockDataHashAString = typeof block.blockDataHash === 'string';
const isNonceANumber = typeof block.nonce === 'number';
const isDateCreatedAnObject = typeof block.dateCreated === 'string';
// TODO: genesis blockhash should not be null
const isBlockHashAString = typeof block.blockHash === 'string';
return areKeysEqual
&& isIndexANumber
&& isTransactionsAnArray
&& isDifficultyANumber
&& isPrevBlockhashAString
&& isMinedByAString
&& isBlockDataHashAString
&& isNonceANumber
&& isDateCreatedAnObject
&& isBlockDataHashAString;
}
}
module.exports = BurgerBlock;