This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockChain.js
133 lines (120 loc) · 3.6 KB
/
BlockChain.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ===== Blockchain Class ==========================
| Class with a constructor for new blockchain |
| ================================================ */
const SHA256 = require('crypto-js/sha256')
const LevelSandbox = require('./LevelSandbox.js')
const Block = require('./Block.js')
class Blockchain {
constructor () {
this.bd = new LevelSandbox.LevelSandbox()
this.generateGenesisBlock()
}
// Create genesis block
async generateGenesisBlock () {
const height = await this.getBlockHeight()
if (height < 0) {
const genesisBlock = new Block.Block('Genesis Block')
genesisBlock.time = time()
genesisBlock.hash = hash(genesisBlock)
this.bd.addLevelDBData(genesisBlock.height, JSON.stringify(genesisBlock)).then(_ => console.log('Genesis block created'))
} else {
console.log('Genesis block exists already')
}
}
/* Get the height of the BlockChain
returns: Promise
*/
async getBlockHeight () {
const blocksCount = await this.bd.getBlocksCount() - 1
return blocksCount
}
/* Add new block
arg: Block
returns: Promise
*/
async addBlock (newBlock) {
const height = await this.getBlockHeight()
newBlock.height = height + 1
newBlock.time = time()
if (newBlock.height > 0) {
const previousBlock = await this.getBlock(height)
newBlock.previousBlockHash = previousBlock.hash
} else {
// if no genesis block create one
await this.generateGenesisBlock()
}
newBlock.hash = hash(newBlock)
// Store block within levelDB
return this.bd.addLevelDBData(newBlock.height, JSON.stringify(newBlock))
}
/* get block object from the blockchain
arg: height integer
returns: Promise
*/
async getBlock (height) {
return JSON.parse(await this.bd.getLevelDBData(height))
}
/* Validate block = check if a block has been tampered
arg: height integer
returns: Promise
*/
async validateBlock (height) {
const block = await this.getBlock(height)
const blockHash = block.hash
block.hash = ''
const validBlockHash = hash(block)
if (validBlockHash === blockHash) {
return true
} else {
return false
}
}
/* Validate link
arg: height integer
returns: Promise
*/
async validateLink (height) {
const block = await this.getBlock(height)
const previousBlock = await this.getBlock(height - 1)
return (block.previousBlockHash === previousBlock.hash)
}
// Validate Blockchain (chek if all blocks are valid)
async validateChain () {
const height = await this.getBlockHeight()
const promisesArray = []
// validate genesis block
promisesArray.push(await this.validateBlock(0))
// validate further blocks and their links
for (let i = 1; i < height + 1; i++) {
promisesArray.push(await this.validateBlock(i), await this.validateLink(i))
}
return Promise.all(promisesArray).then(valuesArray => {
return !valuesArray.toString().includes('f')
})
}
// Utility Method to Tamper a Block for Test Validation
// This method is for testing purpose
_modifyBlock (height, block) {
let self = this
return new Promise((resolve, reject) => {
self.bd.addLevelDBData(height, JSON.stringify(block).toString())
.then(blockModified => {
resolve(blockModified)
})
.catch(err => {
console.log(err)
reject(err)
})
})
}
}
// HELPERS
// Get time
function time () {
return new Date().getTime().toString().slice(0, -3)
}
// Hash data
function hash (objData) {
return SHA256(JSON.stringify(objData)).toString()
}
module.exports.Blockchain = Blockchain