-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleChain.js
127 lines (95 loc) · 3.51 KB
/
simpleChain.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
/* ===== Executable Test ==================================
| Use this file to test your project.
| =========================================================*/
const BlockChain = require('./BlockChain.js');
const Block = require('./Block.js');
let myBlockChain = new BlockChain.Blockchain();
setTimeout(function () {
console.log("Waiting...")
}, 10000);
/******************************************
** Function for Create Tests Blocks ****
******************************************/
(function theLoop (i) {
setTimeout(function () {
let blockTest = new Block.Block("Test Block - " + (i + 1));
// Be careful this only will work if your method 'addBlock' in the Blockchain.js file return a Promise
myBlockChain.addBlock(blockTest).then((result) => {
console.log(result);
i++;
if (i < 10) theLoop(i);
}, (err) => {console.log(err)});
}, 10000);
})(0);
/***********************************************
** Function to get the Height of the Chain ****
***********************************************/
// Be careful this only will work if `getBlockHeight` method in Blockchain.js file return a Promise
myBlockChain.getBlockHeight().then((height) => {
console.log(height);
}).catch((err) => { console.log(err);});
/***********************************************
******** Function to Get a Block *************
***********************************************/
// Be careful this only will work if `getBlock` method in Blockchain.js file return a Promise
myBlockChain.getBlock(0).then((block) => {
console.log(JSON.stringify(block));
}).catch((err) => { console.log(err);});
/***********************************************
***************** Validate Block *************
***********************************************/
// Be careful this only will work if `validateBlock` method in Blockchain.js file return a Promise
myBlockChain.validateBlock(0).then((valid) => {
console.log(valid);
})
.catch((error) => {
console.log(error);
})
/** Tampering a Block this is only for the purpose of testing the validation methods */
/*
myBlockChain.getBlock(5).then((block) => {
let blockAux = block;
blockAux.body = "Tampered Block";
myBlockChain._modifyBlock(blockAux.height, blockAux).then((blockModified) => {
if(blockModified){
myBlockChain.validateBlock(blockAux.height).then((valid) => {
console.log(`Block #${blockAux.height}, is valid? = ${valid}`);
})
.catch((error) => {
console.log(error);
})
} else {
console.log("The Block wasn't modified");
}
}).catch((err) => { console.log(err);});
}).catch((err) => { console.log(err);});
myBlockChain.getBlock(6).then((block) => {
let blockAux = block;
blockAux.previousBlockHash = "jndininuud94j9i3j49dij9ijij39idj9oi";
myBlockChain._modifyBlock(blockAux.height, blockAux).then((blockModified) => {
if(blockModified){
console.log("The Block was modified");
} else {
console.log("The Block wasn't modified");
}
}).catch((err) => { console.log(err);});
}).catch((err) => { console.log(err);}); */
/***********************************************
***************** Validate Chain *************
***********************************************/
// Be careful this only will work if `validateChain` method in Blockchain.js file return a Promise
/*
myBlockChain.validateChain().then((errorLog) => {
if(errorLog.length > 0){
console.log("The chain is not valid:");
errorLog.forEach(error => {
console.log(error);
});
} else {
console.log("No errors found, The chain is Valid!");
}
})
.catch((error) => {
console.log(error);
})
*/