Skip to content

Commit

Permalink
feat: debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
krigga committed Aug 14, 2024
1 parent c79864b commit e305fa2
Show file tree
Hide file tree
Showing 13 changed files with 2,244 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on:
push:
branches:
- main
- debugger
name: Publish to NPM
jobs:
publish:
Expand Down Expand Up @@ -29,4 +29,4 @@ jobs:
- name: Publish
env:
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
run: yarn npm publish --access public
run: yarn npm publish --access public --tag debugger
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ton/sandbox",
"version": "0.20.0",
"version": "0.21.0-debugger.0",
"description": "TON transaction emulator",
"main": "dist/index.js",
"license": "MIT",
Expand All @@ -13,12 +13,14 @@
"url": "git+https://github.com/ton-org/sandbox"
},
"devDependencies": {
"@ton/blueprint": "0.23.0-debugger.0",
"@ton/core": "^0.56.0",
"@ton/crypto": "3.2.0",
"@ton/test-utils": "^0.3.1",
"@ton/ton": "^13.11.0",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.11",
"@vscode/debugprotocol": "^1.66.0",
"jest": "^29.5.0",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
Expand All @@ -35,5 +37,8 @@
"build": "rm -rf dist && yarn wasm:pack && yarn test && tsc && yarn wasm:copy",
"config:pack": "ts-node ./scripts/pack-config.ts"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"dependencies": {
"@vscode/debugadapter": "^1.66.0"
}
}
9 changes: 9 additions & 0 deletions src/blockchain/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export class Blockchain {
protected lock = new AsyncLock()
protected contractFetches = new Map<string, Promise<SmartContract>>()
protected nextCreateWalletIndex = 0
protected shouldDebug = false

readonly executor: IExecutor

Expand Down Expand Up @@ -211,6 +212,14 @@ export class Blockchain {
this.nextCreateWalletIndex = snapshot.nextCreateWalletIndex
}

get debug() {
return this.shouldDebug
}

set debug(value: boolean) {
this.shouldDebug = value
}

/**
* @returns Current time in blockchain
*/
Expand Down
56 changes: 51 additions & 5 deletions src/blockchain/SmartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
TupleItem, TupleReader
} from "@ton/core";
import {getSelectorForMethod} from "../utils/selector";
import { EmulationResult, ExecutorVerbosity, RunCommonArgs, TickOrTock } from "../executor/Executor";
import { EmulationResult, Executor, ExecutorVerbosity, GetMethodArgs, RunCommonArgs, TickOrTock, GetMethodResult as ExecutorGetMethodResult, RunTransactionArgs } from "../executor/Executor";
import { debugGetMethod, debugTransaction } from "../debugger/debug";
import { defaultSourceMapCache } from "../debugger/SourceMapCache";

export function createShardAccount(args: { address?: Address, code: Cell, data: Cell, balance: bigint, workchain?: number }): ShardAccount {
let wc = args.workchain ?? 0
Expand Down Expand Up @@ -178,6 +180,7 @@ export class SmartContract {
#parsedAccount?: ShardAccount
#lastTxTime: number
#verbosity?: Partial<LogsVerbosity>
#debug?: boolean

constructor(shardAccount: ShardAccount, blockchain: Blockchain) {
this.address = shardAccount.account!.addr
Expand Down Expand Up @@ -278,10 +281,33 @@ export class SmartContract {
now: this.blockchain.now,
...params,
}
return await this.runCommon(() => this.blockchain.executor.runTransaction({
const args: RunTransactionArgs = {
...this.createCommonArgs(params),
message: beginCell().store(storeMessage(message)).endCell(),
}))
}
if (this.debug) {
const acc = this.account
if (acc.account === undefined || acc.account === null) {
console.log('Debugging uninitialized accounts is unsupported in debugger beta')
return await this.runCommon(() => this.blockchain.executor.runTransaction(args))
}
if (acc.account.storage.state.type !== 'active') {
console.log('Debugging uninitialized accounts is unsupported in debugger beta')
return await this.runCommon(() => this.blockchain.executor.runTransaction(args))
}
const code = acc.account.storage.state.state.code
if (code === undefined || code === null) {
console.log('Debugging uninitialized accounts is unsupported in debugger beta')
return await this.runCommon(() => this.blockchain.executor.runTransaction(args))
}
const sm = defaultSourceMapCache.get(code.hash().toString('base64'))
if (sm === undefined) {
return await this.runCommon(() => this.blockchain.executor.runTransaction(args))
}
return await this.runCommon(() => debugTransaction(this.blockchain.executor as Executor, args, code, sm))
} else {
return await this.runCommon(() => this.blockchain.executor.runTransaction(args))
}
}

async runTickTock(which: TickOrTock, params?: MessageParams) {
Expand Down Expand Up @@ -335,7 +361,7 @@ export class SmartContract {
throw new Error('Trying to run get method on non-active contract')
}

const res = await this.blockchain.executor.runGetMethod({
const args: GetMethodArgs = {
code: this.account.account?.storage.state.state.code!,
data: this.account.account?.storage.state.state.data!,
methodId: typeof method === 'string' ? getSelectorForMethod(method) : method,
Expand All @@ -349,7 +375,19 @@ export class SmartContract {
randomSeed: params?.randomSeed ?? Buffer.alloc(32),
gasLimit: params?.gasLimit ?? 10_000_000n,
debugEnabled: this.verbosity.debugLogs,
})
}

let res: ExecutorGetMethodResult
if (this.debug) {
const sm = defaultSourceMapCache.get(args.code.hash().toString('base64'))
if (sm === undefined) {
res = await this.blockchain.executor.runGetMethod(args)
} else {
res = await debugGetMethod(this.blockchain.executor as Executor, args, sm)
}
} else {
res = await this.blockchain.executor.runGetMethod(args)
}

if (this.verbosity.print && this.verbosity.blockchainLogs && res.logs.length > 0) {
console.log(res.logs)
Expand Down Expand Up @@ -412,4 +450,12 @@ export class SmartContract {
this.#verbosity = verbosity
}
}

get debug() {
return this.#debug ?? this.blockchain.debug
}

setDebug(debug: boolean | undefined) {
this.#debug = debug
}
}
Loading

0 comments on commit e305fa2

Please sign in to comment.