Skip to content
Draft
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ build-*/
# Direnv or shell hook + Nix Flake generated files
.direnv/
result
result-bin
result-man

docs/book/book
src/index.js
Expand Down Expand Up @@ -205,4 +207,4 @@ csources_v1/
.aider*

# Temporary .patch file, used to coppy results from codex and applied using git apply <file name>.patch
*.patch
*.patch
7 changes: 7 additions & 0 deletions appimage-scripts/build_appimage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ nix build "${ROOT_PATH}#packages.${CURRENT_NIX_SYSTEM}.cargo-stylus"
CARGO_STYLUS=$(nix eval --raw "${ROOT_PATH}#packages.${CURRENT_NIX_SYSTEM}.cargo-stylus.out")
cp -L "${CARGO_STYLUS}"/bin/cargo-stylus "${APP_DIR}"/bin

# cargo-expand
nix build "${ROOT_PATH}#packages.${CURRENT_NIX_SYSTEM}.cargo-expand"

CARGO_EXPAND=$(nix eval --raw "${ROOT_PATH}#packages.${CURRENT_NIX_SYSTEM}.cargo-expand.out")
cp -L "${CARGO_EXPAND}"/bin/cargo-expand "${APP_DIR}"/bin

# ctags
cp -Lr "${ROOT_PATH}/src/links/ctags" "${APP_DIR}/bin/"
chmod +x "${APP_DIR}/bin/ctags"
Expand Down Expand Up @@ -261,6 +267,7 @@ patchelf --set-interpreter "${INTERPRETER_PATH}" "${APP_DIR}"/bin/wazero
patchelf --set-interpreter "${INTERPRETER_PATH}" "${APP_DIR}"/bin/ctags
patchelf --set-interpreter "${INTERPRETER_PATH}" "${APP_DIR}"/bin/curl
patchelf --set-interpreter "${INTERPRETER_PATH}" "${APP_DIR}"/bin/cargo-stylus
patchelf --set-interpreter "${INTERPRETER_PATH}" "${APP_DIR}"/bin/cargo-expand
patchelf --set-interpreter "${INTERPRETER_PATH}" "${APP_DIR}"/bin/node
patchelf --set-interpreter "${INTERPRETER_PATH}" "${APP_DIR}"/ruby/bin/ruby

Expand Down
50 changes: 25 additions & 25 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
};

wazero = {
url = "github:metacraft-labs/codetracer-wasm-recorder?ref=wasm-tracing";
url = "github:metacraft-labs/codetracer-wasm-recorder?ref=feat-decode-events"; # TODO: restore to wasm-tracing
inputs.nixpkgs.follows = "nixpkgs-unstable";
flake = true;
};
Expand Down
3 changes: 3 additions & 0 deletions nix/packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
cargo-stylus =
inputs.nix-blockchain-development.outputs.legacyPackages.${system}.metacraft-labs.cargo-stylus;

cargo-expand = pkgs.cargo-expand;

# curl = pkgs.curl;
inherit (pkgs) curl;

Expand Down Expand Up @@ -320,6 +322,7 @@
name = "runtime-deps";

paths = [
pkgs.cargo-expand
cargo-stylus
resources-derivation
db-backend
Expand Down
1 change: 1 addition & 0 deletions nix/shells/main.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mkShell {

# stylus
ourPkgs.cargo-stylus
cargo-expand

yarn
yarn2nix
Expand Down
6 changes: 6 additions & 0 deletions src/ct/codetracerconf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ type
desc: "Path to a stylus emv trace json file"
.} : string

recordStylusSignatureMap* {.
name: "stylus-signature-map"
defaultValue: ""
desc: "Path to a stylus signature map json file"
.} : string

recordAddress* {.
name: "address"
abbr: "a"
Expand Down
21 changes: 17 additions & 4 deletions src/ct/db_backend_record.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ proc recordDb(
lang: Lang, vmExe: string,
program: string, args: seq[string],
backend: string, traceFolder: string, stylusTrace: string,
stylusSignatureMap: string,
traceId: int): Trace =

createDir(traceFolder)
Expand All @@ -83,6 +84,9 @@ proc recordDb(
if stylusTrace.len > 0:
vmArgs.add("-stylus")
vmArgs.add(stylusTrace)
if stylusSignatureMap.len > 0:
vmArgs.add("--stylus-signature-map")
vmArgs.add(stylusSignatureMap)
vmArgs = vmArgs.concat(@["--trace-dir", traceFolder, program])
vmArgs
of LangNoir:
Expand Down Expand Up @@ -154,6 +158,7 @@ proc recordDb(
proc record(
cmd: string, args: seq[string], compileCommand: string,
langArg: Lang, backend: string, stylusTrace: string,
stylusSignatureMap: string,
test = false, basic = false,
traceIDRecord: int = -1, customPath: string = "", outputFolderArg: string = ""): Trace =
var traceID: int
Expand Down Expand Up @@ -225,7 +230,7 @@ proc record(

try:
if lang == LangRubyDb:
return recordDb(LangRubyDb, rubyExe, executable, args, backend, outputFolder, "", traceId)
return recordDb(LangRubyDb, rubyExe, executable, args, backend, outputFolder, "", "", traceId)
elif lang in {LangNoir, LangRustWasm, LangCppWasm}:
if lang == LangNoir:
# TODO: base the first arg: source folder for record symbols on
Expand All @@ -238,9 +243,9 @@ proc record(
# echo "wasm vm path ", vmPath
else:
vmPath = noirExe
return recordDb(lang, vmPath, executable, args, backend, outputFolder, stylusTrace, traceId)
return recordDb(lang, vmPath, executable, args, backend, outputFolder, stylusTrace, stylusSignatureMap, traceId)
elif lang == LangSmall:
return recordDb(LangSmall, smallExe, executable, args, backend, outputFolder, stylusTrace, traceId)
return recordDb(LangSmall, smallExe, executable, args, backend, outputFolder, stylusTrace, stylusSignatureMap, traceId)
else:
echo fmt"ERROR: unsupported lang {lang}"
quit(1)
Expand Down Expand Up @@ -323,6 +328,7 @@ proc main*(): Trace =
# [--backend <backend>]
# [-e/--export <export-zip>] [-c/--cleanup-output-folder]
# [-t/--stylus-trace <trace-path>]
# [--stylus-signature-map <signature-map-path>]
# [-a/--address <address>] [--socket <socket-path>]
# <program> [<args>]
let args = os.commandLineParams()
Expand All @@ -341,6 +347,7 @@ proc main*(): Trace =
var exportZipPath = ""
var backend = ""
var stylusTrace = ""
var stylusSignatureMap = ""
var address = ""
var socketPath = ""
var isExportedWithArg = false
Expand Down Expand Up @@ -385,6 +392,12 @@ proc main*(): Trace =
return
stylusTrace = args[i + 1]
i += 2
elif arg == "--stylus-signature-map":
if args.len() < i + 2:
displayHelp()
return
stylusSignatureMap = args[i + 1]
i += 2
elif arg == "--address" or arg == "-a":
if args.len() < i + 2:
displayHelp()
Expand Down Expand Up @@ -483,7 +496,7 @@ proc main*(): Trace =

try:
var trace = record(
program, recordArgs, "", lang, backend, stylusTrace,
program, recordArgs, "", lang, backend, stylusTrace, stylusSignatureMap,
traceIDRecord=traceID, outputFolderArg=outputFolder)
traceId = trace.id
outputFolder = trace.outputFolder
Expand Down
1 change: 1 addition & 0 deletions src/ct/launch/launch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ proc runInitial*(conf: CodetracerConf) =
discard record(
conf.recordLang, conf.recordOutputFolder,
conf.recordExportFile, conf.recordStylusTrace,
conf.recordStylusSignatureMap,
conf.recordAddress, conf.recordSocket,
conf.recordProgram, conf.recordArgs)
of StartupCommand.run:
Expand Down
6 changes: 3 additions & 3 deletions src/ct/stylus/arb_node_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import ../../common/[types, paths]
const DEFAULT_NODE_URL* = "http://localhost:8547"

# TODO: get name from config? Maybe use SQLite?
let
CONTRACT_WASM_PATH* = getHomeDir() / ".local" / "share" / "codetracer" / "contract-debug-wasm"
let
CONTRACT_DEBUG_DATA_PATH* = getHomeDir() / ".local" / "share" / "codetracer" / "contract-debug-data"
EVM_TRACE_DIR_PATH* = codetracerTmpPath

proc jsonRpcRequest(methodParam: string, params: JsonNode): JsonNode {.raises: [IOError, ValueError].} =
Expand Down Expand Up @@ -55,7 +55,7 @@ proc getPermittedToHashes(): HashSet[string] {.raises: [].} =
init(toHashes)

try:
for file in walkDir(CONTRACT_WASM_PATH):
for file in walkDir(CONTRACT_DEBUG_DATA_PATH):
if file.kind == pcDir:
let toAddr = splitPath(file.path)[1]
toHashes.incl(toAddr)
Expand Down
54 changes: 46 additions & 8 deletions src/ct/stylus/deploy.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[os, osproc, streams, strutils]
import std/[json, os, osproc, re, streams, strutils, tables]
import arb_node_utils

# NOTE: remove CatchableError if using custom exception
Expand Down Expand Up @@ -91,18 +91,56 @@ proc doDebugBuild(): string {.raises: [OSError, IOError, CatchableError, Excepti

return possibleFiles[0]

proc saveContractDebugWasm(deploymentAddr: string, wasmWithDebug: string) {.raises: [OSError, IOError].} =
let currDir = CONTRACT_WASM_PATH / deploymentAddr
let currFile = currDir / "debug.wasm"
# NOTE: remove CatchableError if using custom exception
proc doSignatureMap(): Table[string, string] {.raises: [OSError, IOError, CatchableError, Exception].} =
let process = startProcess(
"cargo",
args=["expand", "--lib"],
options={poEchoCmd, poUsePath, poStdErrToStdOut}
)
defer: process.close()

let outStream = process.outputStream

var output = ""

while process.running:
output.add(outStream.readAll())

output.add(outStream.readAll())

let exitCode = process.peekExitCode()

if exitCode != 0:
echo "Can't extract event signatures! cargo expand output:"
echo output
return

let signatureRegex = re"Event with signature `(.*)` and selector `(.*)`.\n```solidity\n(.*)\n```"
var matches = @["", "", ""]

for eventComment in output.findAll(signatureRegex):
discard eventComment.match(signatureRegex, matches)
result[matches[1]] = matches[2]

proc saveContractDebugWasm(deploymentAddr: string, wasmWithDebug: string, signatureMapJson: string) {.raises: [OSError, IOError].} =
let debugDataDir = CONTRACT_DEBUG_DATA_PATH / deploymentAddr
let debugWasmFile = debugDataDir / "debug.wasm"
let signatureMapFile = debugDataDir / "signature_map.json"

createDir(debugDataDir)

copyFile(wasmWithDebug, debugWasmFile)
echo "Debug executable for ", deploymentAddr, " saved at ", debugWasmFile

createDir(currDir)
copyFile(wasmWithDebug, currFile)
writeFile(signatureMapFile, signatureMapJson)
echo "Signature map for ", deploymentAddr, " saved at ", signatureMapFile

echo "Debug executable for ", deploymentAddr, " saved at ", currFile

# NOTE: remove CatchableError if using custom exception
proc deployStylus*() {.raises: [OSError, IOError, CatchableError, Exception].} =
let wasmWithDebug = doDebugBuild()
let signatureMapJson = $(%doSignatureMap())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Json.encode if possible (from status json serialization lib)

let deploymentAddr = doDeploy()

saveContractDebugWasm(deploymentAddr, wasmWithDebug)
saveContractDebugWasm(deploymentAddr, wasmWithDebug, signatureMapJson)
Loading
Loading