-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
139 lines (107 loc) · 6.4 KB
/
test.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
134
135
136
137
138
139
import BlockChain from './blockchain';
import * as Controller from './controller';
let chai = require('chai');
let sinon = require('sinon');
let expect = chai.expect;
let request = require('request');
import uuidv4 from 'uuid/v4';
import rp from 'request-promise';
global.request = request;
global.rp = rp;
require.cache.request = request;
require.cache.rp = rp;
describe("Blockchain Class", function() {
let blockChain;
before(function() {
const nodeId = uuidv4();
console.log('GENESIS env var: ' + process.env.GENESIS);
blockChain = new BlockChain(nodeId, 'localhost', 5000);
});
it('should be initialized with a genesis block', function() {
expect(blockChain.chain[0].nonce).to.equal(100);
});
});
describe("Controller", function () {
this.timeout(5000); // only necessary for hashing effect in console
let nodes, valChain = [{"index":0,"timestamp":1547531020021,"transactions":[{"sender":0,"recipient":"facc6b77-206f-424a-a8e1-5ffccce858a3","amount":25,"fee":0,"timestamp":1547531020021,"type":"genesis"}],"nonce":100,"prevHash":"1"},{"index":1,"timestamp":1547531099018,"transactions":[{"sender":"facc6b77-206f-424a-a8e1-5ffccce858a3","recipient":"415faffb-60d3-464a-86be-52787684d05d","amount":1,"fee":1,"timestamp":1547531081738,"type":"regular"},{"sender":"0","recipient":"facc6b77-206f-424a-a8e1-5ffccce858a3","amount":1,"fee":0,"timestamp":1547531099018,"type":"reward"},{"sender":"0","recipient":"facc6b77-206f-424a-a8e1-5ffccce858a3","amount":1,"fee":0,"timestamp":1547531099018,"type":"fee"}],"nonce":106201,"prevHash":"e3960783729884fc8e47a360c746f91dd51c72a99edfa8b49df85541592039ac"},{"index":2,"timestamp":1547531144868,"transactions":[{"sender":"415faffb-60d3-464a-86be-52787684d05d","recipient":"facc6b77-206f-424a-a8e1-5ffccce858a3","amount":1,"fee":1,"timestamp":1547531139763,"type":"regular"},{"sender":"0","recipient":"415faffb-60d3-464a-86be-52787684d05d","amount":1,"fee":0,"timestamp":1547531144868,"type":"reward"},{"sender":"0","recipient":"415faffb-60d3-464a-86be-52787684d05d","amount":1,"fee":0,"timestamp":1547531144868,"type":"fee"}],"nonce":14075,"prevHash":"f5ad6772303b6aba31614a8ad996c4a2e23cd2ff6697053d0af09e1c825fa500"},{"index":3,"timestamp":1547531175263,"transactions":[{"sender":"facc6b77-206f-424a-a8e1-5ffccce858a3","recipient":"415faffb-60d3-464a-86be-52787684d05d","amount":1,"fee":2,"timestamp":1547531170368,"type":"regular"},{"sender":"0","recipient":"facc6b77-206f-424a-a8e1-5ffccce858a3","amount":1,"fee":0,"timestamp":1547531175263,"type":"reward"},{"sender":"0","recipient":"facc6b77-206f-424a-a8e1-5ffccce858a3","amount":2,"fee":0,"timestamp":1547531175263,"type":"fee"}],"nonce":16820,"prevHash":"ea8c3d5a41fc3545a7b2f0fa566c401fdea2ba9f9616c81d29f73d19bbb133cc"}];
before(function () {
function makeAddress(node) {
return `http://${node.host}:${node.port}`;
}
nodes = [{ host: 'host1', port: 5000, isHost: true }, { host: 'host2', port: 5000 }, { host: 'host1', port: 5001 }];
let chain1 = valChain.slice(0,3);
let chain2 = valChain.slice(0,4);
let chain3 = valChain.slice(0,5);
let stub = sinon
.stub(global.request, 'get');
stub.withArgs({ url: makeAddress(nodes[0]) + '/chain', json: true})
.yields(null, null, JSON.stringify(chain1))
.withArgs({ url: makeAddress(nodes[1]) + '/chain', json: true})
.yields(null, null, JSON.stringify(chain2))
.withArgs({ url: makeAddress(nodes[2]) + '/chain', json: true})
.yields(null, null, JSON.stringify(chain3))
.withArgs({ url: makeAddress(nodes[0]) + '/id', json: true})
.yields(null, null, '"eee6ea6e-ecc2-496e-b505-f63dc1382d7a"')
.withArgs({ url: makeAddress(nodes[1]) + '/id', json: true})
.yields(null, null, '"eee6ea6e-ecc2-496e-b505-f63dc1382d7a"')
.withArgs({ url: makeAddress(nodes[2]) + '/id', json: true})
.yields(null, null, '"eee6ea6e-ecc2-496e-b505-f63dc1382d7a"')
.withArgs({ url: makeAddress(nodes[0]) + '/transactions', json: true})
.yields(null, null, JSON.stringify(chain1.transactions))
.withArgs({ url: makeAddress(nodes[1]) + '/transactions', json: true})
.yields(null, null, JSON.stringify(chain2.transactions))
.withArgs({ url: makeAddress(nodes[2]) + '/transactions', json: true})
.yields(null, null, JSON.stringify(chain3.transactions));
let stub2 = sinon
.stub(global.rp, 'get');
stub2.withArgs({ url: makeAddress(nodes[0]) + '/chain', json: true})
.resolves(JSON.stringify(chain1))
.withArgs({ url: makeAddress(nodes[1]) + '/chain', json: true})
.resolves(JSON.stringify(chain2))
.withArgs({ url: makeAddress(nodes[2]) + '/chain', json: true})
.resolves(JSON.stringify(chain3))
.withArgs({ url: makeAddress(nodes[1]) + '/transactions', json: true})
.resolves(JSON.stringify(chain2.transactions))
});
it("should return the new blocked that was forged", function () {
expect(Controller.getChain().length).to.equal(1);
let forged = Controller.mine();
expect(forged.nonce).to.not.be.null;
expect(Controller.getChain().length).to.equal(2);
let transactions = forged.transactions;
expect(transactions[0].recipient).to.equal(Controller.nodeId);
});
it("should return the id of a new transaction", function() {
let idx = Controller.newTransaction("me", "you", 245);
expect(idx).to.equal(2);
});
it("should include any new transactions in the block that was just forged. " +
"(length includes mining reward transaction)", function() {
let throwaway = Controller.mine();
Controller.newTransaction("you", "me", 56);
Controller.newTransaction("me", "you", 74);
let forged = Controller.mine();
expect(forged.transactions.length).to.equal(4);
let nextBlock = Controller.mine();
expect(nextBlock.transactions.length).to.equal(2);
});
// it("should allow p2p nodes to be registered", function() {
// let count = Controller.registerNodes(['http://loca', 'foo', 'bar']);
// expect(count).to.equal(3);
// });
it('should validate chains', function() {
expect(Controller.validateChain(valChain)).to.equal(true);
});
it("should resolve conflicts with other nodes", function (done) {
Controller.resetChain();
Controller.registerNodes(nodes);
Controller.resolveConflicts(function (msg) {
expect(msg).to.equal('Chain was replaced');
done();
});
});
after(function () {
global.request.get.restore();
global.rp.get.restore();
});
});