-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.js
205 lines (167 loc) · 6.74 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"use strict";
const assert = require('chai').assert;
const utils = require('./utils.js');
const Block = require('./block.js');
const Blockchain = require('./blockchain.js');
const Client = require('./client.js');
const Miner = require('./miner.js');
const Transaction = require('./transaction.js');
// Generating keypair for multiple test cases, since key generation is slow.
const kp = utils.generateKeypair();
let addr = utils.calcAddress(kp.public);
// Adding a POW target that should be trivial to match.
const EASY_POW_TARGET = BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
// Setting blockchain configuration. (Usually this would be done during the creation of the genesis block.)
Blockchain.createInstance({ blockClass: Block, transactionClass: Transaction });
describe('utils', () => {
describe('.verifySignature', () => {
let sig = utils.sign(kp.private, "hello");
it('should accept a valid signature', () => {
assert.ok(utils.verifySignature(kp.public, "hello", sig));
});
it('should reject an invalid signature', () => {
assert.ok(!utils.verifySignature(kp.public, "goodbye", sig));
});
});
});
describe("Transaction", () => {
let outputs = [{amount: 20, address: "ffff"},
{amount: 40, address: "face"}];
let t = new Transaction({from: addr, pubKey: kp.public, outputs: outputs, fee: 1, nonce: 1});
t.sign(kp.private);
describe("#totalOutput", () => {
it('should sum up all of the outputs and the transaction fee', () => {
assert.equal(t.totalOutput(), 61);
});
});
});
describe('Block', () => {
let prevBlock = new Block("8e7912");
prevBlock.balances = new Map([ [addr, 500], ["ffff", 100], ["face", 99] ]);
let outputs = [{amount: 20, address: "ffff"}, {amount: 40, address: "face"}];
let t = new Transaction({from: addr, pubKey: kp.public, outputs: outputs, fee: 1, nonce: 0});
describe('#addTransaction', () => {
it("should fail if a transaction is not signed.", () => {
let b = new Block(addr, prevBlock);
let tx = new Transaction(t);
assert.isFalse(b.addTransaction(tx));
});
it("should fail if the 'from' account does not have enough gold.", () => {
let b = new Block(addr, prevBlock);
let tx = new Transaction(t);
tx.outputs = [{amount:20000000000000, address: "ffff"}];
tx.sign(kp.private);
assert.isFalse(b.addTransaction(tx));
});
it("should transfer gold from the sender to the receivers.", () => {
let b = new Block(addr, prevBlock);
let tx = new Transaction(t);
tx.sign(kp.private);
b.addTransaction(tx);
assert.equal(b.balances.get(addr), 500-61); // Extra 1 for transaction fee.
assert.equal(b.balances.get("ffff"), 100+20);
assert.equal(b.balances.get("face"), 99+40);
});
it("should ignore any transactions that were already received in a previous block.", () => {
let b = new Block(addr, prevBlock);
let tx = new Transaction(t);
tx.sign(kp.private);
b.addTransaction(tx);
// Attempting to add transaction to subsequent block.
let b2 = new Block(addr, b);
b2.addTransaction(tx);
assert.isEmpty(b2.transactions);
});
});
describe('#rerun', () => {
it("should redo transactions to return to the same block.", () => {
let b = new Block(addr, prevBlock);
let tx = new Transaction(t);
tx.sign(kp.private);
b.addTransaction(tx);
// Wiping out balances and then rerunning the block
b.balances = new Map();
b.rerun(prevBlock);
// Verifying prevBlock's balances are unchanged.
assert.equal(prevBlock.balances.get(addr), 500);
assert.equal(prevBlock.balances.get("ffff"), 100);
assert.equal(prevBlock.balances.get("face"), 99);
// Verifying b's balances are correct.
assert.equal(b.balances.get(addr), 500-61);
assert.equal(b.balances.get("ffff"), 100+20);
assert.equal(b.balances.get("face"), 99+40);
});
it("should take a serialized/deserialized block and get back the same block.", () => {
let b = new Block(addr, prevBlock);
let tx = new Transaction(t);
tx.sign(kp.private);
b.addTransaction(tx);
let hash = b.hashVal();
let serialBlock = b.serialize();
let o = JSON.parse(serialBlock);
let b2 = Blockchain.deserializeBlock(o);
b2.rerun(prevBlock);
// Verify hashes still match
assert.equal(b2.hashVal(), hash);
assert.equal(b2.balances.get(addr), 500-61);
assert.equal(b2.balances.get("ffff"), 100+20);
assert.equal(b2.balances.get("face"), 99+40);
});
});
});
describe('Client', () => {
let genesis = new Block("8e7912");
genesis.balances = new Map([ [addr, 500], ["ffff", 100], ["face", 99] ]);
let net = { broadcast: function(){} };
let outputs = [{amount: 20, address: "ffff"}, {amount: 40, address: "face"}];
let t = new Transaction({from: addr, pubKey: kp.public, outputs: outputs, fee: 1, nonce: 0});
t.sign(kp.private);
let outputs2 = [{amount: 10, address: "face"}];
let t2 = new Transaction({from: addr, pubKey: kp.public, outputs: outputs2, fee: 1, nonce: 1});
t2.sign(kp.private);
let clint = new Client({net: net, startingBlock: genesis});
clint.log = function(){};
let miner = new Miner({name: "Minnie", net: net, startingBlock: genesis});
miner.log = function(){};
describe('#receiveBlock', () => {
it("should reject any block without a valid proof.", () => {
let b = new Block(addr, genesis);
b.addTransaction(t);
// Receiving and verifying block
b = clint.receiveBlock(b);
assert.isNull(b);
});
it("should store all valid blocks, but only change lastBlock if the newer block is better.", () => {
let b = new Block(addr, genesis, EASY_POW_TARGET);
b.addTransaction(t);
// Finding a proof.
miner.currentBlock = b;
b.proof = 0;
miner.findProof(true);
// Receiving and verifying block
clint.receiveBlock(b);
assert.equal(clint.blocks.get(b.id), b);
assert.equal(clint.lastBlock, b);
let b2 = new Block(addr, b, EASY_POW_TARGET);
b2.addTransaction(t2);
// Finding a proof.
miner.currentBlock = b2;
b2.proof = 0;
miner.findProof(true);
// Receiving and verifying block
clint.receiveBlock(b2);
assert.equal(clint.blocks.get(b2.id), b2);
assert.equal(clint.lastBlock, b2);
let bAlt = new Block(addr, genesis, EASY_POW_TARGET);
bAlt.addTransaction(t2);
// Finding a proof.
miner.currentBlock = bAlt;
bAlt.proof = 0;
miner.findProof(true);
// Receiving and verifying block
clint.receiveBlock(bAlt);
assert.equal(clint.blocks.get(bAlt.id), bAlt);
assert.equal(clint.lastBlock, b2);
});
});
});