Skip to content

Commit

Permalink
Improve VM perf, support schnorr multisig, expand support for limit, …
Browse files Browse the repository at this point in the history
…loops, and bigint, expand VMB benchmark tooling and tests
  • Loading branch information
bitjson committed Jul 26, 2024
1 parent d0d54b9 commit 333cbfa
Show file tree
Hide file tree
Showing 90 changed files with 46,509 additions and 31,110 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"cashaddr",
"CASHTOKENS",
"Chaingraph",
"checkbits",
"CHECKDATASIG",
"CHECKDATASIGVERIFY",
"CHECKLOCKTIMEVERIFY",
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ coverage
report.*.json
scratch
gitignore.*
*.cpuprofile

src/lib/bin/**/*.html
src/lib/bin/**/*.js
Expand Down
30 changes: 28 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,35 @@
"runtimeExecutable": "yarn",
"runtimeArgs": ["run", "ava"],
"args": ["${file}"],
// "args": ["--serial", "${file}"],
"outputCapture": "std",
"skipFiles": ["<node_internals>/**", "**/node_modules/**"]
"console": "integratedTerminal",
"skipFiles": ["<node_internals>/**"]
},
{
"type": "node",
"request": "launch",
"name": "Debug VMB Test",
"runtimeExecutable": "yarn",
"runtimeArgs": ["run", "test:unit:vmb_test"],
"args": ["${input:vmVersion}", "${input:vmbTestShortId}", "-v"],
"args": ["${input:vmVersion}", "${input:vmbTestShortId}"],
// "args": ["${input:vmVersion}", "${input:vmbTestShortId}", "-v"],
"outputCapture": "std",
"console": "integratedTerminal",
"skipFiles": ["<node_internals>/**"],
"preLaunchTask": "npm: build"
},
{
"type": "node",
"request": "launch",
"name": "Debug VMB Benchmark",
"runtimeExecutable": "yarn",
"runtimeArgs": ["run", "bench:vmb_tests"],
"env": {
"BENCH_DEBUG_VM": "${input:vmVersion}",
"BENCH_DEBUG_TEST": "${input:vmbTestShortId}",
"BENCH_DEBUG_PROFILE": "${input:collectProfile}"
},
"outputCapture": "std",
"console": "integratedTerminal",
"skipFiles": ["<node_internals>/**"],
Expand Down Expand Up @@ -72,6 +91,13 @@
"bch_spec_nonstandard"
],
"default": "bch_spec_standard"
},
{
"id": "collectProfile",
"description": "Collect a CPU profile?",
"type": "pickString",
"options": ["Yes", "No"],
"default": "Yes"
}
]
}
1 change: 0 additions & 1 deletion config/.ava.bench.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export default {
files: ['build/**/*.bench.js'],
workerThreads: false,
verbose: true,
};
2 changes: 1 addition & 1 deletion docs/verify-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const instructionSet = createInstructionSetBch(true);
*/
const opUnRot = <State extends AuthenticationProgramStateStack>(state: State) =>
useThreeStackItems(state, (nextState, [a, b, c]) =>
pushToStack(nextState, c, a, b),
pushToStack(nextState, [c, a, b]),
);

/* We assign "OP_UNROT" at the index held by "OP_RESERVED1" */
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"test:unit:vmb_tests": "c8 ava src/lib/vmb-tests/bch-vmb-tests.spec.ts --serial",
"test:unit:vmb_test": "node --enable-source-maps 'build/lib/vmb-tests/run-bch-vmb-test.spec.helper.js'",
"dev:vmb_tests": "yarn gen:vmb_tests && yarn build:tsc && ava src/lib/vmb-tests/bch-vmb-tests.spec.ts --serial --fail-fast",
"dev:vmb_tests:info": "echo 'example: export FILTER_VMB_TESTS=benchmark; export FILTER_VMB_TEST_VM=2025; yarn dev:vmb_tests && say sucess || say failure",
"bench:vmb_tests": "ava --config config/.ava.vmb_bench.config.js src/lib/vmb-tests/benchmark-bch-vmb-tests.spec.ts --serial",
"bench": "yarn build && yarn bench:browser-deps && yarn bench:vmb_tests && yarn bench:test",
"bench:test": "ava --config config/.ava.bench.config.js --serial --timeout=2m 2>&1 | tee bench.log",
Expand Down Expand Up @@ -156,7 +157,7 @@
"SECURITY.md"
],
"ava": {
"timeout": "60s",
"timeout": "120s",
"typescript": {
"compile": false,
"rewritePaths": {
Expand All @@ -165,10 +166,11 @@
},
"nodeArguments": [
"--experimental-json-modules",
"--experimental-global-webcrypto",
"# ^ needed for node v18"
"--experimental-global-webcrypto"
],
"files": ["!src/lib/vmb-tests/benchmark-bch-vmb-tests.spec.ts"]
"files": [
"!src/lib/vmb-tests/benchmark-bch-vmb-tests.spec.ts"
]
},
"config": {
"commitizen": {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/compiler/compiler-bch/compiler-bch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test('[BCH compiler] createCompilerBch: generateBytecode', (t) => {
);
});

test('[BCH compiler] createCompilerBch: debug', (t) => {
test.failing('[BCH compiler] createCompilerBch: debug', (t) => {
const compiler = createCompilerBch({
scripts: {
lock: 'OP_DUP OP_HASH160 <$(<a.public_key> OP_HASH160)> OP_EQUALVERIFY OP_CHECKSIG',
Expand Down
6 changes: 3 additions & 3 deletions src/lib/compiler/compiler-bch/compiler-bch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
} from '../../lib.js';
import { encodeTokenPrefix } from '../../message/message.js';
import {
createVirtualMachineBch,
createVirtualMachineBchSpec,
generateBytecodeMap,
generateSigningSerializationBch,
OpcodesBchSpec,
Expand All @@ -40,7 +40,7 @@ import {
} from '../compiler-utils.js';

export type CompilerOperationsKeyBch =
| 'data_signature'
| 'ecdsa_data_signature'
| 'ecdsa_signature'
| 'public_key'
| 'schnorr_data_signature'
Expand Down Expand Up @@ -753,7 +753,7 @@ export const createCompilerBch = <
secp256k1: internalSecp256k1,
sha256: internalSha256,
sha512: internalSha512,
vm: configuration.vm ?? createVirtualMachineBch(),
vm: configuration.vm ?? createVirtualMachineBchSpec(),
},
...configuration,
});
Expand Down
8 changes: 7 additions & 1 deletion src/lib/compiler/compiler-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
sha256 as internalSha256,
sha512 as internalSha512,
} from '../crypto/crypto.js';
import { flattenBinArray } from '../format/format.js';
import { compileScript } from '../language/language.js';
import type {
AnyCompilerConfiguration,
Expand All @@ -24,6 +25,7 @@ import type {
WalletTemplate,
} from '../lib.js';
import {
encodeDataPush,
generateBytecodeMap,
Opcodes,
OpcodesBchSpec,
Expand Down Expand Up @@ -123,6 +125,7 @@ export const compilerConfigurationToCompiler =
compilerConfigurationToCompilerBch;

const nullHashLength = 32;
const maximumValidOpReturnPushLength = 9996;

/**
* A common {@link createAuthenticationProgram} implementation for
Expand Down Expand Up @@ -162,7 +165,10 @@ export const createAuthenticationProgramEvaluationCommon = (
locktime: 0,
outputs: [
{
lockingBytecode: Uint8Array.of(),
lockingBytecode: flattenBinArray([
Uint8Array.of(Opcodes.OP_RETURN),
encodeDataPush(new Uint8Array(maximumValidOpReturnPushLength)),
]),
valueSatoshis: 0n,
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/lib/docs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ test('verify-transactions.md: add OP_UNROT', (t) => {
state: State,
) =>
useThreeStackItems(state, (nextState, [a, b, c]) =>
pushToStack(nextState, c, a, b),
pushToStack(nextState, [c, a, b]),
);

/* We assign "OP_UNROT" at the index held by "OP_RESERVED1" */
Expand Down
7 changes: 7 additions & 0 deletions src/lib/language/language-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@ export type EvaluationSample<ProgramState> = {
instruction: AuthenticationInstruction;
state: ProgramState;
}[];
/**
* Defined only if an `OP_BEGIN ... OP_UNTIL` loop causes this sample to be
* evaluated more than once; each successive iteration of this sample's
* `instruction` is appended to the `iterations` array. (Note, this array
* excludes the first iteration, which is assigned to the `state` property.)
*/
iterations?: ProgramState[];
/**
* The range over which this sample was defined in the source script.
*/
Expand Down
Loading

0 comments on commit 333cbfa

Please sign in to comment.