Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

contracts: Add host function tracing #13593

Closed
4 tasks
athei opened this issue Mar 14, 2023 · 5 comments · Fixed by #13648
Closed
4 tasks

contracts: Add host function tracing #13593

athei opened this issue Mar 14, 2023 · 5 comments · Fixed by #13648
Assignees
Labels
J0-enhancement An additional feature request. Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder

Comments

@athei
Copy link
Member

athei commented Mar 14, 2023

For contract (language) authors it can be a helpful debugging tool to see which host functions (functions exposed by pallet-contracts) are called with which arguments and what was the result. It is of similar use as the popular strace tool on linux.

This can be implemented within the define_env macro. We should use the log create to emit a log line after each host function was called. We do it after because then we can also display the result on the same line. All code is panic free and can't divert. So there is no way that we miss a host function call on error.

Example of how it might look in the macro:

log::trace!(target: "runtime::contracts::strace", "#func_name(#arg_name0: {arg_val0}, #arg_name1: {arg_val1})={result}")

We use the trace log level and a sub target of contracts. In general, all log levels are compiled into a node (substrate as a library does not supply any features to log). At runtime trace is disabled by default. Having it disabled/enabled at runtime is fine for our use case. It just adds some code and data to the runtime binary but the expensive string formatting isn't performed.

Size conscious runtimes can always supply a feature to log in their build to remove those logs.

  • Implement feature
  • Add section to README about host function tracing
  • Rebench to make sure this actually doesn't affect performance
  • Compare sizes of the kitchensink runtime before and after the change

Assign to @pgherveou as soon as he is added to paritytech.

cc @xermicus @cmichi

@athei athei added J0-enhancement An additional feature request. Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder labels Mar 14, 2023
@github-project-automation github-project-automation bot moved this to Backlog 🗒 in Smart Contracts Mar 14, 2023
@xermicus
Copy link
Member

xermicus commented Mar 14, 2023

In solang we implemented this feature in the contract directly (opt-in) via the debug buffer. What are the benefits of emitting this information on the host only? I can see unified format and behavior regardless of the programming language / compiler being used as a plus. But then why not adding it into the debug buffer too, and emitting the debug buffer as structured data JSON (for example), so that it can be parsed by frontend, debuggers and other tools? Emitting valid JSON structure into the debug buffer from the contract is not possible, as it might trap or run out of gas and can't finish the formatting. Downside of doing it on the host is that it's missing information about the contract source code

@pgherveou pgherveou self-assigned this Mar 14, 2023
@athei
Copy link
Member Author

athei commented Mar 15, 2023

I just forgot about the debug buffer to be honest. You are right. We should write the information into the debug buffer instead of using log directly. We can guard this with log_enabled.

@pgherveou
Copy link
Contributor

Nice will look into this, after I wrap up #13600
is there a special encoding / message format to use for interpreting the raw bytes from the debug_buffer on the frontend?

@athei
Copy link
Member Author

athei commented Mar 15, 2023

There is no structure to the debug buffer except that it is UTF-8 encoded. So just adding lines to it is the way to go.

@athei athei moved this from Backlog 🗒 to In Progress 🛠 in Smart Contracts Mar 19, 2023
pgherveou added a commit that referenced this issue Mar 20, 2023
@pgherveou
Copy link
Contributor

pgherveou commented Mar 20, 2023

@athei @xermicus
added a PR for the implementation of the host function tracing calls here #13648 , I have a few more TODOs to complete on the PR but let me know if that match what you had in mind for the feature before I wrap it up.

I added a few screenshots with the trace output.

@pgherveou pgherveou moved this from In Progress 🛠 to Code in review 🧐 in Smart Contracts Mar 20, 2023
@athei athei moved this from Code in review 🧐 to In Progress 🛠 in Smart Contracts Mar 22, 2023
pgherveou added a commit that referenced this issue Mar 23, 2023
pgherveou added a commit that referenced this issue Mar 24, 2023
@pgherveou pgherveou moved this from In Progress 🛠 to Code in review 🧐 in Smart Contracts Mar 24, 2023
@github-project-automation github-project-automation bot moved this from Code in review 🧐 to Done ✅ in Smart Contracts Mar 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
J0-enhancement An additional feature request. Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

3 participants