-
Notifications
You must be signed in to change notification settings - Fork 379
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
feat: ExecAsPkg #644
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,6 +275,48 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { | |
m.PushValue(res0) | ||
}, | ||
) | ||
pn.DefineNative("ExecAsPkg", | ||
// TODO: rename UnsafeExecAsPkg? | ||
gno.Flds( // params | ||
"fn", gno.FuncT(nil, nil), | ||
), | ||
gno.Flds( // results | ||
), | ||
func(m *gno.Machine) { | ||
// TODO: limit stack size: 1? 32?. | ||
// TODO: limit usage in goroutines? | ||
// TODO: build a new sub-context instead of patching the root one. | ||
// to support multiple concurrent contexts and advanced flows. | ||
arg0 := m.LastBlock().GetParams1().TV | ||
fn := arg0.GetFunc() | ||
|
||
ctx := m.Context.(ExecContext) | ||
ctx.OrigCaller = ctx.OrigPkgAddr | ||
|
||
backupContext := m.Context.(ExecContext) | ||
|
||
m.PushValue(gno.TypedValue{ | ||
T: &gno.InterfaceType{}, | ||
V: gno.ContextValue{ | ||
Context: backupContext, | ||
}, | ||
}) | ||
m.PushOp(gno.OpSetContext) | ||
|
||
cx := gno.Call(gno.FuncT(nil, nil)) | ||
m.PushValue(gno.TypedValue{}) | ||
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 are still unsure why we need to add this useless value in the values' stack. The thing is, if we don't, the stack is truncated by one value (when We'll try to figure out why this value is truncated. 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. After some investigation, in a normal context, this truncated value is the Finally, instead of using |
||
m.PushFrameCall(cx, fn, gno.TypedValue{}) | ||
m.PushOp(gno.OpCall) | ||
|
||
m.PushValue(gno.TypedValue{ | ||
T: &gno.InterfaceType{}, | ||
V: gno.ContextValue{ | ||
Context: ctx, | ||
}, | ||
}) | ||
m.PushOp(gno.OpSetContext) | ||
}, | ||
) | ||
pn.DefineNative("GetCallerAt", | ||
gno.Flds( // params | ||
"n", "int", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// PKGPATH: gno.land/r/std_test | ||
package std_test | ||
|
||
import ( | ||
"fmt" | ||
"std" | ||
) | ||
|
||
func main() { | ||
println(std.GetOrigCaller()) | ||
std.ExecAsPkg(func() { | ||
println(std.GetOrigCaller()) | ||
}) | ||
println(std.GetOrigCaller()) | ||
} | ||
|
||
// 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 |
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.