-
Notifications
You must be signed in to change notification settings - Fork 12
/
block.js
256 lines (224 loc) · 8.16 KB
/
block.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"use strict";
const BigInteger = require('jsbn').BigInteger;
const Transaction = require('./transaction.js');
const utils = require('./utils.js');
const POW_BASE_TARGET = new BigInteger("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
const POW_TARGET = POW_BASE_TARGET.shiftRight(20);
const COINBASE_AMT_ALLOWED = 25;
/**
* A block is a collection of transactions, with a hash connecting it
* to a previous block.
*
* The block also stores a list of UTXOs, organizing them by their
* transaction IDs.
*/
module.exports = class Block {
/**
* This method is designed to produce the very first block, known as the
* genesis block, which does not follow normal rules. It role is to
* establish all starting funds for different parties.
*
* @param {Array} clientInitialFunds - A list of pairs specifying a client
* and the amount of coins that client should start with.
*/
static makeGenesisBlock(clientInitialFunds) {
// Creating outputs
let outputs = [];
clientInitialFunds.forEach(({ client, amount }) => {
let addr = client.wallet.makeAddress();
let out = { address: addr, amount: amount };
outputs.push(out);
});
// Adding funds to clients' wallets
let tx = new Transaction({outputs: outputs});
clientInitialFunds.forEach(({client}, i) => {
client.wallet.addUTXO(outputs[i], tx.id, i);
});
// Creating block
let genesis = new Block();
genesis.addTransaction(tx, true);
return genesis;
}
/**
* Converts a string representation of a block to a new Block instance.
* We assume that a serialized block intentended for deserialization
* (in other words, sharing over the network) always includes the UTXOs.
*
* @param {string} str - A string representing a block in JSON format.
*/
static deserialize(str) {
let b = new Block();
let o = JSON.parse(str);
b.prevBlockHash = o.prevBlockHash;
b.timestamp = o.timestamp;
b.proof = o.proof;
b.chainLength = parseInt(o.chainLength);
// Serializing the UTXOs simplifies things, but should probably be eliminated.
b.utxos = o.utxos;
// Transactions need to be recreated and restored in a map.
b.transactions = new Map();
o.transactions.forEach(([txID,txJson]) => {
let { outputs, inputs } = txJson;
let tx = new Transaction({outputs, inputs});
tx.id = txID;
b.transactions.set(txID, tx);
});
return b;
}
/**
* Creates a new Block. Note that the previous block will not be stored;
* instead, its hash value will be maintained in this block.
*
* @param {String} rewardAddr - The address to receive all mining rewards for this block.
* @param {Block} prevBlock - The previous block in the blockchain.
* @param {number} target - The POW target. The miner must find a proof that
* produces a smaller value when hashed.
*/
constructor(rewardAddr, prevBlock, target) {
this.prevBlockHash = prevBlock ? prevBlock.hashVal() : null;
this.target = target || POW_TARGET;
// Storing transactions in a Map to preserve key order.
this.transactions = new Map();
// Used to determine the winner between competing chains.
// Note that this is a little simplistic -- an attacker
// make a long, but low-work chain. However, this works
// well enough for us.
this.chainLength = prevBlock ? prevBlock.chainLength+1 : 1;
this.timestamp = Date.now();
// Caching unspent transactions for quick lookup.
// Each block serves as a snapshot of available coins.
// Note that we need to do a deep clone of the object.
this.utxos = prevBlock ? JSON.parse(JSON.stringify(prevBlock.utxos)) : {};
// Add the initial coinbase reward.
if (rewardAddr) {
let output = { address: rewardAddr, amount: COINBASE_AMT_ALLOWED};
// The coinbase transaction will be updated to capture transaction fees.
this.coinbaseTX = new Transaction({ outputs: [output] });
this.addTransaction(this.coinbaseTX, true);
}
}
/**
* The genesis block has special rules. The coinbase transaction can have
* limitless outputs and is still valid. Note that a new Genesis block will
* be ignored by miners who have a longer chain already.
*/
isGenesisBlock() {
return !this.prevBlockHash;
}
/**
* Returns true if the hash of the block is less than the target
* proof of work value.
*/
verifyProof() {
let h = utils.hash(this.serialize());
let n = new BigInteger(h, 16);
return n.compareTo(this.target) < 0;
}
/**
* Converts a Block into string form. Some fields are deliberately omitted.
*/
serialize(includeUTXOs=false) {
return `{ "transactions": ${JSON.stringify(Array.from(this.transactions.entries()))},` +
(includeUTXOs ? ` "utxos": ${JSON.stringify(this.utxos)},` : '') +
` "prevBlockHash": "${this.prevBlockHash}",` +
` "timestamp": "${this.timestamp}",` +
` "target": "${this.target}",` +
` "proof": "${this.proof}",` +
` "chainLength": "${this.chainLength}" }`;
}
/**
* Returns the cryptographic hash of the current block.
*/
hashVal() {
return utils.hash(this.serialize());
}
/**
* Determines whether the block would accept the transaction.
* A block will accept a transaction if it is not a duplicate
* and all inputs are valid (meaning they have a matching UTXO).
*
* @param {Transaction} tx - The transaction to validate.
*/
willAcceptTransaction(tx) {
if (this.transactions.get(tx.id)) {
//console.log(`${tx.id} is a duplicate`);
return false;
} else if (!tx.isValid(this.utxos)) {
//console.log(`${tx.id} is invalid`);
return false;
}
return true;
}
/**
* Accepts a new transaction if it is valid. The validity is determined
* by the Transaction class.
*
* @param {Transaction} tx - The transaction to add to the block.
* @param {boolean} forceAccept - Accept the transaction without validating.
* This setting is useful for coinbase transactions.
*/
addTransaction(tx, forceAccept) {
if (!forceAccept && !this.willAcceptTransaction(tx)) {
throw new Error(`Transaction ${tx.id} is invalid.`);
}
//
// **YOUR CODE HERE**
//
// First, store the transaction.
//
// Next, you will need to update the UTXOs to account for changes made by the transaction.
// That means you will need to:
// 1) Delete the spent UTXOs from the UTXO set.
// 2) Add newly created UTXOs to the UTXO set.
// 3) Calculate the miner's transaction fee, determined by the difference between the inputs and the outputs.
// The addTransactionFee method might help you with this part.
}
/**
* Adds the transaction fee to the miner's coinbase transaction.
*
* @param {number} fee - The miner's reward for including a given transaction.
*/
addTransactionFee(fee) {
// Either the transaction was a coinbase transaction, or there was no transaction fee.
if (fee <= 0) return;
if (this.coinbaseTX) {
// Rather than create a new key, we accumulate all rewards in the same transaction.
this.coinbaseTX.addFee(fee);
}
}
/**
* A block is valid if all transactions (except for the coinbase transaction) are
* valid and the total outputs equal the total inputs plus the coinbase reward.
*/
isValid(utxos=this.utxos) {
// The genesis block is automatically valid.
if (this.isGenesisBlock()) return true;
// Calculating total inputs.
let totalIn = COINBASE_AMT_ALLOWED;
this.transactions.forEach((tx) => {
tx.inputs.forEach((input, txID) => {
let txUXTOs = utxos[txID];
if (txUXTOs[input.outputIndex]) {
totalIn += txUXTOs[input.outputIndex].amount;
}
});
});
// Calculating total outputs.
let totalOut = 0;
this.transactions.forEach((tx) => {
totalOut += tx.totalOutput();
});
return totalIn === totalOut;
}
/**
* Prints out the value of all UTXOs in the system.
*/
displayUTXOs() {
Object.keys(this.utxos).forEach(txID => {
let txUTXOs = this.utxos[txID];
txUTXOs.forEach(utxo => {
console.log(JSON.stringify(utxo));
});
});
}
}