Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(unspents): add docs/input-costs.md, generation tool #3651

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions modules/unspents/bin/generate_tables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as fs from 'fs/promises';
import * as utxolib from '@bitgo/utxo-lib';

import { Dimensions } from '../src';

const headers = [
'Script Type',
'Chain Codes',
'Spend Type',
'Input Size (Virtual Bytes)',
'Relative Size (p2trMusig2 = 1.00)',
];

const relativeCostRef = Dimensions.fromScriptType('taprootKeyPathSpend').getInputsVSize();

type Row = [string, string, string, string, string];

function formatMarkdownTable(headers: string[], rows: string[][]): string {
return [headers, headers.map(() => '---'), ...rows].map((row) => `| ${row.join(' | ')} |`).join('\n');
}

function generateRowsForScriptType(
headers: string[],
t: utxolib.bitgo.outputScripts.ScriptType2Of3,
params?: {
spendTypeName: string;
scriptTypeParams: { scriptPathLevel?: number };
}
): Row[] {
const chainCode = utxolib.bitgo.toChainPair(t);

if (!params) {
if (t === 'p2tr') {
return [
...generateRowsForScriptType(headers, t, {
spendTypeName: 'Script Path, Level 2 (Backup/User, Backup/BitGo)',
scriptTypeParams: { scriptPathLevel: 2 },
}),
...generateRowsForScriptType(headers, t, {
spendTypeName: 'Script Path, Level 1 (User/BitGo)',
scriptTypeParams: { scriptPathLevel: 1 },
}),
];
}

if (t === 'p2trMusig2') {
return [
...generateRowsForScriptType(headers, t, {
spendTypeName: 'Script Path (Backup/User, Backup/BitGo)',
scriptTypeParams: { scriptPathLevel: 1 },
}),
...generateRowsForScriptType(headers, t, {
spendTypeName: 'Key Path (User/BitGo)',
scriptTypeParams: { scriptPathLevel: undefined },
}),
];
}
}

const inputVSize = Dimensions.fromScriptType(t, params?.scriptTypeParams).getInputsVSize();
const row: Row = [
t,
chainCode.join(`/`),
params?.spendTypeName ?? 'all',
inputVSize.toString(),
(inputVSize / relativeCostRef).toFixed(2),
];
return [row];
}

function generateTables() {
const scriptTypes = [...utxolib.bitgo.outputScripts.scriptTypes2Of3];
return formatMarkdownTable(
headers,
scriptTypes.flatMap((s) => generateRowsForScriptType(headers, s))
);
}

function generateDocument() {
return [
'# Input Costs',
'This document contains the worst-case input costs for various script types and spend types.',
'The input costs are calculated using the `Dimensions` class from `@bitgo/unspents`.',
'',
generateTables(),
].join('\n');
}

if (require.main === module) {
const outfile = 'docs/input-costs.md';
fs.writeFile(outfile, generateDocument())
.then(() => console.log('wrote to', outfile))
.catch((e) => console.error(e));
}
13 changes: 13 additions & 0 deletions modules/unspents/docs/input-costs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Input Costs
This document contains the worst-case input costs for various script types and spend types.
The input costs are calculated using the `Dimensions` class from `@bitgo/unspents`.

| Script Type | Chain Codes | Spend Type | Input Size (Virtual Bytes) | Relative Size (p2trMusig2 = 1.00) |
| --- | --- | --- | --- | --- |
| p2sh | 0/1 | all | 298 | 5.14 |
| p2shP2wsh | 10/11 | all | 140 | 2.41 |
| p2wsh | 20/21 | all | 105 | 1.81 |
| p2tr | 30/31 | Script Path, Level 2 (Backup/User, Backup/BitGo) | 116 | 2.00 |
| p2tr | 30/31 | Script Path, Level 1 (User/BitGo) | 108 | 1.86 |
| p2trMusig2 | 40/41 | Script Path (Backup/User, Backup/BitGo) | 108 | 1.86 |
| p2trMusig2 | 40/41 | Key Path (User/BitGo) | 58 | 1.00 |
2 changes: 1 addition & 1 deletion modules/unspents/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"allowJs": false,
"strict": true
},
"include": ["src/**/*", "test/**/*"]
"include": ["src/**/*", "test/**/*", "bin/**/*"]
}
Loading