1- import { AuthenticationErrorCommon , AuthenticationInstruction , AuthenticationProgramCommon , AuthenticationProgramStateCommon , AuthenticationVirtualMachine , ResolvedTransactionCommon , WalletTemplate , WalletTemplateScriptUnlocking , binToHex , createCompiler , createVirtualMachineBch2025 , decodeAuthenticationInstructions , encodeAuthenticationInstruction , walletTemplateToCompilerConfiguration } from '@bitauth/libauth' ;
1+ import { AuthenticationErrorCommon , AuthenticationInstruction , AuthenticationProgramCommon , AuthenticationProgramStateCommon , AuthenticationVirtualMachine , ResolvedTransactionCommon , WalletTemplate , WalletTemplateScriptUnlocking , binToHex , createCompiler , createVirtualMachineBch2023 , createVirtualMachineBch2025 , createVirtualMachineBch2026 , createVirtualMachineBchSpec , decodeAuthenticationInstructions , encodeAuthenticationInstruction , walletTemplateToCompilerConfiguration } from '@bitauth/libauth' ;
22import { Artifact , LogEntry , Op , PrimitiveType , StackItem , asmToBytecode , bytecodeToAsm , decodeBool , decodeInt , decodeString } from '@cashscript/utils' ;
33import { findLastIndex , toRegExp } from './utils.js' ;
44import { FailedRequireError , FailedTransactionError , FailedTransactionEvaluationError } from './Errors.js' ;
55import { getBitauthUri } from './LibauthTemplate.js' ;
6+ import { VmTarget } from './interfaces.js' ;
67
78export type DebugResult = AuthenticationProgramStateCommon [ ] ;
89export type DebugResults = Record < string , DebugResult > ;
910
11+ /* eslint-disable @typescript-eslint/indent */
12+ type VM = AuthenticationVirtualMachine <
13+ ResolvedTransactionCommon ,
14+ AuthenticationProgramCommon ,
15+ AuthenticationProgramStateCommon
16+ > ;
17+ /* eslint-enable @typescript-eslint/indent */
18+
19+ const createVirtualMachine = ( vmTarget : VmTarget ) : VM => {
20+ switch ( vmTarget ) {
21+ case 'BCH_2023_05' :
22+ return createVirtualMachineBch2023 ( ) ;
23+ case 'BCH_2025_05' :
24+ return createVirtualMachineBch2025 ( ) ;
25+ case 'BCH_2026_05' :
26+ return createVirtualMachineBch2026 ( ) ;
27+ case 'BCH_SPEC' :
28+ // TODO: This typecast is shitty, but it's hard to fix
29+ return createVirtualMachineBchSpec ( ) as unknown as VM ;
30+ default :
31+ throw new Error ( `Debugging is not supported for the ${ vmTarget } virtual machine.` ) ;
32+ }
33+ } ;
34+
1035// debugs the template, optionally logging the execution data
1136export const debugTemplate = ( template : WalletTemplate , artifacts : Artifact [ ] ) : DebugResults => {
1237 // If a contract has the same name, but a different bytecode, then it is considered a name collision
@@ -61,7 +86,7 @@ const debugSingleScenario = (
6186
6287 for ( const log of executedLogs ) {
6388 const inputIndex = extractInputIndexFromScenario ( scenarioId ) ;
64- logConsoleLogStatement ( log , executedDebugSteps , artifact , inputIndex ) ;
89+ logConsoleLogStatement ( log , executedDebugSteps , artifact , inputIndex , vm ) ;
6590 }
6691 }
6792
@@ -157,21 +182,13 @@ const extractInputIndexFromScenario = (scenarioId: string): number => {
157182 return parseInt ( match [ 1 ] ) ;
158183} ;
159184
160- /* eslint-disable @typescript-eslint/indent */
161- type VM = AuthenticationVirtualMachine <
162- ResolvedTransactionCommon ,
163- AuthenticationProgramCommon ,
164- AuthenticationProgramStateCommon
165- > ;
166- /* eslint-enable @typescript-eslint/indent */
167-
168185type Program = AuthenticationProgramCommon ;
169186type CreateProgramResult = { vm : VM , program : Program } ;
170187
171188// internal util. instantiates the virtual machine and compiles the template into a program
172189const createProgram = ( template : WalletTemplate , unlockingScriptId : string , scenarioId : string ) : CreateProgramResult => {
173190 const configuration = walletTemplateToCompilerConfiguration ( template ) ;
174- const vm = createVirtualMachineBch2025 ( ) ;
191+ const vm = createVirtualMachine ( template . supported [ 0 ] as VmTarget ) ;
175192 const compiler = createCompiler ( configuration ) ;
176193
177194 if ( ! template . scripts [ unlockingScriptId ] ) {
@@ -204,13 +221,14 @@ const logConsoleLogStatement = (
204221 debugSteps : AuthenticationProgramStateCommon [ ] ,
205222 artifact : Artifact ,
206223 inputIndex : number ,
224+ vm : VM ,
207225) : void => {
208226 let line = `${ artifact . contractName } .cash:${ log . line } ` ;
209227 const decodedData = log . data . map ( ( element ) => {
210228 if ( typeof element === 'string' ) return element ;
211229
212230 const debugStep = debugSteps . find ( ( step ) => step . ip === element . ip ) ! ;
213- const transformedDebugStep = applyStackItemTransformations ( element , debugStep ) ;
231+ const transformedDebugStep = applyStackItemTransformations ( element , debugStep , vm ) ;
214232 return decodeStackItem ( element , transformedDebugStep . stack ) ;
215233 } ) ;
216234 console . log ( `[Input #${ inputIndex } ] ${ line } ${ decodedData . join ( ' ' ) } ` ) ;
@@ -219,6 +237,7 @@ const logConsoleLogStatement = (
219237const applyStackItemTransformations = (
220238 element : StackItem ,
221239 debugStep : AuthenticationProgramStateCommon ,
240+ vm : VM ,
222241) : AuthenticationProgramStateCommon => {
223242 if ( ! element . transformations ) return debugStep ;
224243
@@ -236,9 +255,10 @@ const applyStackItemTransformations = (
236255 instructions : transformationsAuthenticationInstructions ,
237256 signedMessages : [ ] ,
238257 program : { ...debugStep . program } ,
258+ functionTable : debugStep . functionTable ?? { } ,
259+ functionCount : debugStep . functionCount ?? 0 ,
239260 } ;
240261
241- const vm = createVirtualMachineBch2025 ( ) ;
242262 const transformationsEndState = vm . stateEvaluate ( transformationsStartState ) ;
243263
244264 return transformationsEndState ;
0 commit comments