NomicFoundation / edr

An Ethereum development runtime implementation that can be reused to build new developer tools.
MIT License
57 stars 12 forks source link

Bitwise string logging of words from console.sol (%b ?) #459

Open CryptoKiddies opened 10 months ago

CryptoKiddies commented 10 months ago

Describe the feature

I have this utility created to see the bitwise values in a word for testing and debugging contract logic, particularly when using bit masks. Wondering if you'd be interested in adding support directly in console.sol with this util? I'm suggesting a feature indicated with a %b flag perhaps.

This is great for low level developers (who are still commonplace for now) in the solidity ecosystem. Any bitwise operations or state packing optimization testing would benefit from being able to see the word printed out as 1s and 0s., like Here's my result 11101001.... Wonder if I can contribute, but definitely agree this is pre-processed as discussed here with discussed here with @fvictorio.

code: https://github.com/YOLOrekt/community-bug-bounty/blob/bounty-head/contracts/utils/LogBinary.sol

Search terms

console.sol console log debugging

CryptoKiddies commented 5 months ago

just a bump on this; if you guys want help, let me know @fvictorio.

fvictorio commented 5 months ago

Hi @CryptoKiddies!

I still think this is a valuable feature, but to be honest the implementation is not quite easy.

The way Hardhat's console.log works is by delegating the formating to node's util.format which, AFAIK, doesn't support binary specifiers (nor octal, nor hex).

One way to work around this is to do something like:

  1. Suppose you have a format string %s %b %i and arguments ["foo", 31, 5678]. You'd like this to be logged as foo 0b11111 5678.
  2. If you just pass that to util.format, the result will be foo %b 31 5678, which is obviously not what you want.
  3. For this to work, you'd have to do two things: replace %b with %s, and replace 31 with the string "0b11111". And you need to do this for all possible %bs in the format string.
  4. To do that, you basically have to "parse" the whole format string, because you need to figure out the positions of all the %b in it.

This is doable with a simple state machine, but it may be more effort that what you'd like to invest here.

On top of that, we will eventually move this functionality to EDR, and we'll probably implement our own formatter there. Supporting %b while/after doing that seems better than temporarily doing it in javascript just to discard it later.

So my proposal would be to transfer this issue to the EDR repo, and for us to do it when the time comes to port the console.log feature. Wdyt?