-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockUtils.ts
279 lines (270 loc) · 7.95 KB
/
blockUtils.ts
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* @author Michael D. Nath, Kenan Hasanaliyev
* @email [email protected], [email protected]
* @file blockUtils.ts
* @desc blockUtils.ts contains all logic pertinent to block validation.
*/
import {
Transaction,
Block,
VerificationResponse,
TransactionRequest,
Outpoint,
HashObjectMessage,
} from "./types";
import {
isCoinbase,
isHex,
validateCoinbase,
validateTransaction,
} from "./transactionUtils";
import * as sha256 from "fast-sha256";
import * as db from "./db";
import * as CONSTANTS from "./constants";
import { applyBlockToUTXO } from "./utxoUtils";
const T_VALUE =
"00000002af000000000000000000000000000000000000000000000000000000";
const BLOCK_REWARD = 50000000000000;
const canonicalize = require("canonicalize");
export function createObjectID(object: Block | Transaction): string {
const canonicalizedJSON = canonicalize(object);
const uint8arr = new TextEncoder().encode(canonicalizedJSON);
const hash = sha256.hash(uint8arr);
return Buffer.from(hash).toString("hex");
}
export function validateBlockFormat(block: Object): VerificationResponse {
// Block must have desired keys
const requiredKeys = ["txids", "nonce", "previd", "T", "created", "type"];
const optionalKeys = ["miner", "note"];
let missingAnyKeys = false;
let keysMissing: string[] = [];
for (let key of requiredKeys) {
if (block[key] === undefined) {
missingAnyKeys = true;
keysMissing.push(key);
}
}
if (missingAnyKeys) {
return {
valid: false,
msg: `Not all required keys in block. Missing: ${keysMissing}`,
};
}
// Blocks must only contain keys either in requiredKeys or optionalKeys
for (const [key, _] of Object.entries(block)) {
if (!requiredKeys.includes(key) && !optionalKeys.includes(key)) {
return { valid: false, msg: `Unknown key: ${key}` };
}
}
const blockifiedBlock = block as Block;
// Txids must be an array of strings
if (!Array.isArray(blockifiedBlock["txids"])) {
return { valid: false, msg: "txids must be an array" };
}
for (let txid of blockifiedBlock["txids"]) {
if (!(typeof txid == "string")) {
return { valid: false, msg: "txids must be an array of strings" };
}
}
// Nonce must be hex string
if (!isHex(blockifiedBlock["nonce"])) {
return { valid: false, msg: "nonce must be hex string" };
}
// Previd must be hex string
if (!isHex(blockifiedBlock["previd"])) {
return { valid: false, msg: "previd must be hex string" };
}
// 'Created' key must be non-negative integer seconds
if (
typeof blockifiedBlock["created"] != "number" ||
blockifiedBlock["created"] < 0 ||
Math.floor(blockifiedBlock["created"]) != blockifiedBlock["created"]
) {
return { valid: false, msg: "created key must be integer timestamp" };
}
if (!isHex(blockifiedBlock["T"])) {
return { valid: false, msg: "target must be hex string" };
}
if (
blockifiedBlock["miner"] != undefined &&
(typeof blockifiedBlock["miner"] != "string" ||
blockifiedBlock["miner"].length > 128 ||
!/^[\x00-\x7F]*$/.test(blockifiedBlock["miner"])) // regex used to test whether string is ascii
) {
return {
valid: false,
msg: "miner key must be valid string of max length 128",
};
}
if (
blockifiedBlock["note"] != undefined &&
(typeof blockifiedBlock["note"] != "string" ||
blockifiedBlock["note"].length > 128 ||
!/^[\x00-\x7F]*$/.test(blockifiedBlock["note"]))
) {
return {
valid: false,
msg: "note key must be valid string of max length 128",
};
}
// Check target
if (blockifiedBlock["T"] == undefined || blockifiedBlock["T"] != T_VALUE) {
return {
valid: false,
msg: "Invalid target",
};
}
//Check proof of work
if (!(createObjectID(blockifiedBlock) < blockifiedBlock["T"])) {
return {
valid: false,
msg: "Proof of work invalid",
};
}
return { valid: true };
}
// Node gives peer TIMEOUT_IN_MILLIS amount of time to provide a parent. Otherwise, parent does not exist in the eye of the node
function parentBlockCallback(previd: string): Promise<string> {
return new Promise((resolve) => {
globalThis.emitter.on(previd, () => {
console.log("PARENT FOUND - PROMISED RESOLVED");
resolve("Parent found");
});
setTimeout(() => {
globalThis.emitter.removeAllListeners(previd);
resolve("Parent not found");
}, CONSTANTS.TIMEOUT_IN_MILLIS);
});
}
export function askForParent(parentid: string) {
const getObjectMessage: HashObjectMessage = {
type: "getobject",
objectid: parentid,
};
(async () => {
for (let peerToInformConnection of globalThis.sockets) {
peerToInformConnection.write(canonicalize(getObjectMessage) + "\n");
}
})();
}
export async function validateBlock(
block: Object
): Promise<VerificationResponse> {
let coinbaseTXID;
let coinbaseOutputValue = 0;
let sumInputValues = 0;
let sumOutputValues = 0;
// Check if parent block exists
const previd = block["previd"];
const existenceResponse = await db.doesHashExist(previd);
if (!existenceResponse.exists) {
askForParent(previd);
const parentBlockValidationResponse = await parentBlockCallback(previd);
if (parentBlockValidationResponse === "Parent found") {
return validateBlock(block);
} else {
return {
valid: false,
msg: "Parent block does not exist",
};
}
} else {
const parent = existenceResponse.obj as Block;
const parentHeight = await db.HEIGHTS.get(previd);
const newHeight = parseInt(parentHeight) + 1;
if (block["created"] <= parent.created) {
return {
valid: false,
msg: "timestamp of created field must be later than that of its parent",
};
}
if (block["created"] > Date.now() / 1000) {
return {
valid: false,
msg: "timestamp of block must be before the current time",
};
}
// Validate each transaction in the block
const txids: [string] = block["txids"];
for (let index = 0; index < txids.length; index++) {
try {
const transaction = (await db.TRANSACTIONS.get(
txids[index]
)) as Transaction;
if (isCoinbase(transaction)) {
const coinbaseResponse = validateCoinbase(
transaction,
index,
newHeight
);
if (!coinbaseResponse.valid) return coinbaseResponse;
coinbaseTXID = txids[index];
coinbaseOutputValue = coinbaseResponse["data"]["value"];
} else {
console.log("TRANSACTION ", transaction);
for (let input of transaction["inputs"]) {
if (input.outpoint.txid == coinbaseTXID) {
return {
valid: false,
msg: "Coinbase transaction cannot be spent in same block",
};
}
}
const transactionResponse = await validateTransaction(transaction);
if (!transactionResponse.valid) return transactionResponse;
sumInputValues += transactionResponse["data"]["inputValues"];
sumOutputValues += transactionResponse["data"]["outputValues"];
}
} catch (err) {
console.log(err);
}
}
if (
coinbaseOutputValue >
BLOCK_REWARD + (sumInputValues - sumOutputValues)
) {
return {
valid: false,
msg: "coinbase transaction does not satisfy law of conservation",
};
}
await db.HEIGHTS.put(createObjectID(block as Block), newHeight);
return { valid: true, data: { height: newHeight } };
}
}
export async function handleIncomingValidatedBlock(
block: Block
): Promise<VerificationResponse> {
var utxoToBeUpdated = (await db.BLOCKUTXOS.get(
block["previd"]
)) as Array<Outpoint>;
const utxoBlockAdditionResponse = await applyBlockToUTXO(
block,
utxoToBeUpdated
);
if (utxoBlockAdditionResponse["valid"]) {
await db.BLOCKUTXOS.put(
createObjectID(block),
utxoBlockAdditionResponse["data"] as Array<Outpoint>
);
return { valid: true };
} else {
return utxoBlockAdditionResponse;
}
}
export async function correspondingTransactionsExist(
txids: [string]
): Promise<TransactionRequest> {
let missingTransactions = new Set<string>();
for (let txid of txids) {
try {
await db.TRANSACTIONS.get(txid);
} catch (err) {
missingTransactions.add(txid);
}
}
if (missingTransactions.size > 0) {
return { missing: true, txids: missingTransactions };
}
return { missing: false, txids: missingTransactions };
}