-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.ts
215 lines (183 loc) · 6.01 KB
/
utils.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
import colors from 'colors/safe';
import cbor from 'cbor';
import { fromHex, type SpendingValidator, toHex, TxSigned } from 'translucent-cardano/index';
import crypto from 'crypto';
import blueprint from '../plutus.json' assert { type: 'json' };
const MAX_TX_EX_STEPS = 10000000000;
const MAX_TX_EX_MEM = 14000000;
const MAX_TX_SIZE = 16384;
export async function sha256(input: Uint8Array): Promise<Uint8Array> {
const hash = await crypto.subtle.digest('SHA-256', input);
return new Uint8Array(hash);
}
export function readValidator(): SpendingValidator {
const validator = blueprint.validators[0];
return {
type: 'PlutusV2',
script: toHex(cbor.encode(fromHex(validator.compiledCode))),
};
}
export function printExecutionDetails(tx: TxSigned, name: string) {
const redeemers = tx.txSigned.witness_set().redeemers()!;
let steps = 0;
let mem = 0;
for (let i = 0; i < redeemers.len(); i++) {
const red = redeemers.get(i);
steps += parseInt(red.ex_units().steps().toString(), 10);
mem += parseInt(red.ex_units().mem().toString(), 10);
}
const remainingMem = MAX_TX_EX_MEM - mem;
const remainingSteps = MAX_TX_EX_STEPS - steps;
const txBytes = tx.txSigned.to_bytes().length;
const remainingTxBytes = MAX_TX_SIZE - txBytes;
const fee = tx.txSigned.body().fee().toString();
const text = `
${colors.bold(colors.magenta(name))} - ${colors.green('passed')}
${colors.bold(colors.blue('mem'))}: ${colors.green(mem.toString())}
${colors.bold(colors.blue('remaining'))}: ${colors.cyan(remainingMem.toString())}
${colors.bold(colors.blue('cpu'))}: ${colors.green(steps.toString())}
${colors.bold(colors.blue('remaining'))}: ${colors.cyan(remainingSteps.toString())}
${colors.bold(colors.blue('tx size'))}: ${colors.green(txBytes.toString())}
${colors.bold(colors.blue('remaining'))}: ${colors.cyan(remainingTxBytes.toString())}
${colors.bold(colors.blue('fee'))}: ${colors.green(fee.toUpperCase())}`;
console.log(text);
if (remainingMem < 0) {
console.log(colors.red(' Out of mem'));
}
if (remainingSteps < 0) {
console.log(colors.red(' Out of cpu'));
}
if (remainingTxBytes < 0) {
console.log(colors.red(' Out of tx space'));
}
}
export function randomAssetId() {
const bytes = new Uint8Array(48);
crypto.getRandomValues(bytes);
return toHex(bytes);
}
export function getDifficulty(hash: Uint8Array): {
leadingZeros: bigint;
difficulty_number: bigint;
} {
let leadingZeros = 0;
let difficulty_number = 0;
for (const [indx, chr] of hash.entries()) {
if (chr !== 0) {
if ((chr & 0x0f) === chr) {
leadingZeros += 1;
difficulty_number += chr * 4096;
difficulty_number += hash[indx + 1] * 16;
difficulty_number += Math.floor(hash[indx + 2] / 16);
return {
leadingZeros: BigInt(leadingZeros),
difficulty_number: BigInt(difficulty_number),
};
} else {
difficulty_number += chr * 256;
difficulty_number += hash[indx + 1];
return {
leadingZeros: BigInt(leadingZeros),
difficulty_number: BigInt(difficulty_number),
};
}
} else {
leadingZeros += 2;
}
}
return { leadingZeros: 32n, difficulty_number: 0n };
}
export function incrementU8Array(x: Uint8Array) {
for (let i = 0; i < x.length; i++) {
if (x[i] === 255) {
x[i] = 0;
} else {
x[i] += 1;
break;
}
}
}
export function halfDifficultyNumber(a: { leadingZeros: bigint; difficulty_number: bigint }): {
leadingZeros: bigint;
difficulty_number: bigint;
} {
const new_a = a.difficulty_number / 2n;
if (new_a < 4096n) {
return {
leadingZeros: a.leadingZeros + 1n,
difficulty_number: new_a * 16n,
};
} else {
return {
leadingZeros: a.leadingZeros,
difficulty_number: new_a,
};
}
}
export function getDifficultyAdjustement(
total_epoch_time: bigint,
epoch_target: bigint,
): { numerator: bigint; denominator: bigint } {
if (epoch_target / total_epoch_time >= 4 && epoch_target % total_epoch_time > 0) {
return { numerator: 1n, denominator: 4n };
} else if (total_epoch_time / epoch_target >= 4 && total_epoch_time % epoch_target > 0) {
return { numerator: 4n, denominator: 1n };
} else {
return { numerator: total_epoch_time, denominator: epoch_target };
}
}
export function calculateDifficultyNumber(
a: { leadingZeros: bigint; difficulty_number: bigint },
numerator: bigint,
denominator: bigint,
): { leadingZeros: bigint; difficulty_number: bigint } {
const new_padded_difficulty = (a.difficulty_number * 16n * numerator) / denominator;
const new_difficulty = new_padded_difficulty / 16n;
if (new_padded_difficulty / 65536n == 0n) {
if (a.leadingZeros >= 62n) {
return { difficulty_number: 4096n, leadingZeros: 62n };
} else {
return {
difficulty_number: new_padded_difficulty,
leadingZeros: a.leadingZeros + 1n,
};
}
} else if (new_difficulty / 65536n > 0n) {
if (a.leadingZeros <= 2) {
return { difficulty_number: 65535n, leadingZeros: 2n };
} else {
return {
difficulty_number: new_difficulty / 16n,
leadingZeros: a.leadingZeros - 1n,
};
}
} else {
return {
difficulty_number: new_difficulty,
leadingZeros: a.leadingZeros,
};
}
}
export function calculateInterlink(
currentHash: string,
a: { leadingZeros: bigint; difficulty_number: bigint },
b: { leadingZeros: bigint; difficulty_number: bigint },
currentInterlink: string[],
): string[] {
let b_half = halfDifficultyNumber(b);
const interlink: string[] = currentInterlink;
let currentIndex = 0;
while (
b_half.leadingZeros < a.leadingZeros ||
(b_half.leadingZeros == a.leadingZeros && b_half.difficulty_number > a.difficulty_number)
) {
if (currentIndex < interlink.length) {
interlink[currentIndex] = currentHash;
} else {
interlink.push(currentHash);
}
b_half = halfDifficultyNumber(b_half);
currentIndex += 1;
}
return interlink;
}