-
Foundry is great because you don't have to context switch between Solidity and (for example) javascript when writing contracts. Sometimes, testing external to a contract is not enough. You want to get access to the state of variables inside a contract function as it's executing; and you want to do so outside a debugger. I.e., all you want to do is log some output. So, my idea was to do something like this:
This outputs:
Somewhat surprising, since the docs would imply msg.sender for calls from the test functions is
Changing the address cast to ITest() to msg.sender doesn't fix the problem (it's that 0x00a... address) nor does explicitly inserting the literal 0x00a... It looks like making one of the functions external results in an actual deployed library contract into which a delegatecall is made (for whois()). Note that the name of the call is not logged (instead its selector cb6ab4fa is used). Further, the call back into 0xb... fails. Any thoughts? Also, the "bigger" issue is "what's Foundry's position on the insertion of debugging code into the developed contracts and libraries?" PS: This methodology works "ok" (it's ugly but ...) until you try to call a logging function from inside a view function. Then, all hell breaks loose. Perhaps in the future Foundry should not use emits for logging, but rather, should provide a library that has a bunch of external view functions that implement logging and other useful things, e.g. at |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
This is not the case, I think you might have confused the sender and the test address. The docs state that the test is at address 0xb4c79daB8f259C7Aee6E5b2Aa729821864227e84, and 0x00a329c0648769a73afac7f9381e08fb43dbea72 is the address that calls the test. When you call a library function it is usually a delegatecall. I'm not sure what it is for an internal function, maybe a normal call? Anyway, the reason you see a different
You can either:
|
Beta Was this translation helpful? Give feedback.
-
FYI, performing a search (using the looking glass icon) on the Foundry book brings up only two mentions of
Given no mention of other special addresses it therefore seemed reasonable to assume that the msg.sender would also be that address (through some hack for the purposes of the test environment). I saw someone comment that the Foundry book is considered out of date, but perhaps some key edits should still be made (like mentioning this issue). Separately, no, directly emitting the log is not an option because emits are state changing, and therefore cannot be done in view functions. I'll look for console2.sol (it's not in the forge-std I pulled from github). Thank you for the pointer! This is what I was looking for. I won't go looking now to confirm, but forge-std was not something I ran into right away when diving into Foundry. It probably should be mentioned in README.md if it isn't. Separately, I'm not sure I understand why the call back into 0xb4's logging function after the delegatecall into the library failed. It seems to me that should have worked. |
Beta Was this translation helpful? Give feedback.
-
It is no longer super out of date. That passage also says:
So,
We mostly rely on Forge Std in the book now, so it is mentioned there |
Beta Was this translation helpful? Give feedback.
This is not the case, I think you might have confused the sender and the test address. The docs state that the test is at address 0xb4c79daB8f259C7Aee6E5b2Aa729821864227e84, and 0x00a329c0648769a73afac7f9381e08fb43dbea72 is the address that calls the test.
When you call a library function it is usually a delegatecall. I'm not sure what it is for an internal function, maybe a normal call? Anyway, the reason you see a different
msg.sender
when you mark the functionexternal
is because the function is now called via delegatecall, so msg.sender is preser…