|
| 1 | +import { binToHex } from '../../../../format/format.js'; |
| 2 | +import type { |
| 3 | + AuthenticationInstructionMalformed, |
| 4 | + AuthenticationProgramStateControlStack, |
| 5 | + AuthenticationProgramStateError, |
| 6 | + AuthenticationProgramStateFunctionTable, |
| 7 | + AuthenticationProgramStateMinimum, |
| 8 | + AuthenticationProgramStateStack, |
| 9 | +} from '../../../../lib.js'; |
| 10 | +import { |
| 11 | + applyError, |
| 12 | + authenticationInstructionsAreMalformed, |
| 13 | + decodeAuthenticationInstructions, |
| 14 | + disassembleAuthenticationInstructionMalformed, |
| 15 | + pushToControlStack, |
| 16 | + useOneStackItem, |
| 17 | + useOneVmNumber, |
| 18 | +} from '../../common/common.js'; |
| 19 | + |
| 20 | +import { ConsensusBch2026 } from './bch-2026-consensus.js'; |
| 21 | +import { AuthenticationErrorBch2026 } from './bch-2026-errors.js'; |
| 22 | +import { OpcodesBch2026 } from './bch-2026-opcodes.js'; |
| 23 | + |
| 24 | +export const createOpDefine = |
| 25 | + ({ |
| 26 | + maximumFunctionIdentifier = ConsensusBch2026.maximumFunctionIdentifier, |
| 27 | + minimumFunctionIdentifier = ConsensusBch2026.minimumFunctionIdentifier, |
| 28 | + } = {}) => |
| 29 | + < |
| 30 | + State extends AuthenticationProgramStateError & |
| 31 | + AuthenticationProgramStateFunctionTable & |
| 32 | + AuthenticationProgramStateStack, |
| 33 | + >( |
| 34 | + state: State, |
| 35 | + ) => |
| 36 | + useOneVmNumber(state, (nextState, [providedInteger]) => { |
| 37 | + const functionIdentifier = Number(providedInteger); |
| 38 | + if ( |
| 39 | + functionIdentifier < minimumFunctionIdentifier || |
| 40 | + functionIdentifier > maximumFunctionIdentifier |
| 41 | + ) { |
| 42 | + return applyError( |
| 43 | + nextState, |
| 44 | + AuthenticationErrorBch2026.functionIdentifierInvalid, |
| 45 | + `Function identifier (${functionIdentifier}) is outside of the valid range: ${minimumFunctionIdentifier} to ${maximumFunctionIdentifier} (inclusive).`, |
| 46 | + ); |
| 47 | + } |
| 48 | + if (nextState.functionTable[functionIdentifier] !== undefined) { |
| 49 | + return applyError( |
| 50 | + nextState, |
| 51 | + AuthenticationErrorBch2026.functionIdentifierPreviouslyDefined, |
| 52 | + `Function identifier: ${functionIdentifier}. Existing contents: ${binToHex( |
| 53 | + nextState.functionTable[functionIdentifier], |
| 54 | + )}.`, |
| 55 | + ); |
| 56 | + } |
| 57 | + return useOneStackItem(nextState, (finalState, [functionBody]) => { |
| 58 | + // eslint-disable-next-line functional/no-expression-statements, functional/immutable-data |
| 59 | + finalState.functionTable[functionIdentifier] = functionBody; |
| 60 | + return finalState; |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | +export const opInvoke = < |
| 65 | + State extends AuthenticationProgramStateControlStack & |
| 66 | + AuthenticationProgramStateError & |
| 67 | + AuthenticationProgramStateFunctionTable & |
| 68 | + AuthenticationProgramStateMinimum & |
| 69 | + AuthenticationProgramStateStack, |
| 70 | +>( |
| 71 | + state: State, |
| 72 | +) => |
| 73 | + useOneVmNumber(state, (nextState, [providedInteger]) => { |
| 74 | + const functionTableIndex = Number(providedInteger); |
| 75 | + const functionBody = nextState.functionTable[functionTableIndex]; |
| 76 | + if (functionBody === undefined) { |
| 77 | + return applyError( |
| 78 | + nextState, |
| 79 | + AuthenticationErrorBch2026.functionIdentifierUndefined, |
| 80 | + `Function identifier: ${functionTableIndex}.`, |
| 81 | + ); |
| 82 | + } |
| 83 | + const newInstructions = decodeAuthenticationInstructions(functionBody); |
| 84 | + if (authenticationInstructionsAreMalformed(newInstructions)) { |
| 85 | + return applyError( |
| 86 | + nextState, |
| 87 | + AuthenticationErrorBch2026.malformedFunction, |
| 88 | + `Malformed instruction: ${disassembleAuthenticationInstructionMalformed( |
| 89 | + OpcodesBch2026, |
| 90 | + newInstructions[ |
| 91 | + newInstructions.length - 1 |
| 92 | + ] as AuthenticationInstructionMalformed, |
| 93 | + )}.`, |
| 94 | + ); |
| 95 | + } |
| 96 | + const manuallyAdvance = 1; |
| 97 | + const finalState = pushToControlStack(nextState, { |
| 98 | + instructions: nextState.instructions, |
| 99 | + ip: nextState.ip + manuallyAdvance, |
| 100 | + }); |
| 101 | + finalState.ip = 0 - manuallyAdvance; // eslint-disable-line functional/no-expression-statements, functional/immutable-data |
| 102 | + finalState.instructions = newInstructions; // eslint-disable-line functional/no-expression-statements, functional/immutable-data |
| 103 | + return finalState; |
| 104 | + }); |
0 commit comments