-
Notifications
You must be signed in to change notification settings - Fork 374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ExecAsPkg #644
Closed
Closed
feat: ExecAsPkg #644
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// PKGPATH: gno.land/r/std_test | ||
package std_test | ||
|
||
import ( | ||
"fmt" | ||
"std" | ||
) | ||
|
||
func printOrigCaller() { | ||
println(std.GetOrigCaller()) | ||
} | ||
|
||
func main() { | ||
printOrigCaller() | ||
std.ExecAsPkg(printOrigCaller) | ||
printOrigCaller() | ||
} | ||
|
||
// Output: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need more tests for this feature: multiple levels; reused func pointers; touching a variable outside of the scope; panic; probably more. |
||
// g1fahslvxakc058u0wa98ytfc9fd6trhe5qfh9ld | ||
// g157y5v3k529jyzhjjz4fn49tzzhf4gess6v39xg | ||
// g1fahslvxakc058u0wa98ytfc9fd6trhe5qfh9ld |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still uncertain, so I'd appreciate additional input. However, it appears that we have three primary options.
std.ExecAsPkg
std.UnsafeExecAsPkg
unsafe.ExecAsPkg
I believe it is essential to reassess our understanding of safety and risk when drafting contracts. After all, isn't the default assumption that everything is potentially unsafe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I havn't removed your comments.
But i think we should keep
std.ExecAsPkg
, it's a "normal feature" for a smart-contract engine to be able to do actions as the smart-contract addrThe use of the word
Unsafe
could scare contract writers, and make them think as a bad practice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@albttx
Gnolang support pass function as parameters and interface callback. It does exactly what current std.ExecAsPkg's implementation tries to do, and is more flexible and safer.
Not sure if std.ExecAsPkg implementation does type checking on the function parameters.
pn.DefineNative("ExecAsPkg",
// TODO: rename UnsafeExecAsPkg?
gno.Flds( // params
"fn", gno.FuncT(nil, nil),
),
gno/stdlibs/stdlibs.go
Line 281 in d59c320
On the other hand, Gnolang, as in go, supports the passing functions as parameters does type checking of the passed functions' parameter types
To solve #634, as you mentioned, you can modify GRC20 with interface callback or pass a callback function as a parameter to allow the airdrop contract to alter the state of your token contract. In addition, you can add a static allowed list or even try to verify interface types for approval.
That is more idiomatic in go programming, safe and auditable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gno/tests/files/zrealm_std6.gno
Line 21 in d59c320
Testing case is not robust. It exposes a safety issue for the contract to use std.ExecAsPkg. All three lines should have output the same address without knowing call path context has been changed.
It sideloads the behavior of std.GetOrigCaller() and makes contracts hard to audit. The safety and security of smart contracts have very much to do with readability and auditability.
More importantly, It makes checking std.GetOrigCaller() lose its purpose when we call std.ExecAsPkg(anyFunction).
Here is why:
std.GetOrigCaller() suppose to get the first caller's address of the entire call path. Usually, it is end users who initiate a function call on a smart contract. In Ethereum, that is an external owned account (EOA). In gno, we do not separate it since the main package can also be the OrigCaller.
No matter where std.GetOrigCaller() is called in the call path; it should always return the same address.
However, std.ExecAsPkg changes the context of the call path in the middle. It makes std.GetOrigCaller() returns the address of the contract that std.ExecAsPkg is called in.
gno/stdlibs/stdlibs.go
Line 308 in d59c320
It makes checking std.GetOrigCaller() lose its purpose, when we call std.ExecAsPkg( any function ) in the same contract.