Skip to content
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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkgs/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,9 @@ const (
OpValueDecl Op = 0x90 // var/const ...
OpTypeDecl Op = 0x91 // type ...

/* Context operators */
OpSetContext Op = 0xA0

/* Loop (sticky) operators (>= 0xD0) */
OpSticky Op = 0xD0 // not a real op.
OpBody Op = 0xD1 // if/block/switch/select.
Expand Down Expand Up @@ -938,6 +941,9 @@ const (
OpCPUValueDecl = 1
OpCPUTypeDecl = 1

/* Context operators */
OpCPUContext = 1

/* Loop (sticky) operators (>= 0xD0) */
OpCPUSticky = 1
OpCPUBody = 1
Expand Down Expand Up @@ -1275,6 +1281,9 @@ func (m *Machine) Run() {
case OpReturnCallDefers:
m.incrCPU(OpCPUReturnCallDefers)
m.doOpReturnCallDefers()
case OpSetContext:
m.incrCPU(OpCPUContext)
m.doOpSetContext()
default:
panic(fmt.Sprintf("unexpected opcode %s", op.String()))
}
Expand Down
5 changes: 5 additions & 0 deletions pkgs/gnolang/op_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,8 @@ func (m *Machine) doOpPanic2() {
m.PushOp(OpReturnCallDefers) // XXX rename, not return?
}
}

func (m *Machine) doOpSetContext() {
cv := m.PopValue().V.(ContextValue)
m.Context = cv.Context
}
13 changes: 13 additions & 0 deletions pkgs/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (*PackageValue) assertValue() {}
func (*NativeValue) assertValue() {}
func (*Block) assertValue() {}
func (RefValue) assertValue() {}
func (ContextValue) assertValue() {}

const (
nilStr = "nil"
Expand All @@ -62,6 +63,7 @@ var (
_ Value = &NativeValue{}
_ Value = &Block{}
_ Value = RefValue{}
_ Value = ContextValue{}
)

// ----------------------------------------
Expand Down Expand Up @@ -2184,6 +2186,17 @@ func (tv *TypedValue) GetSlice2(alloc *Allocator, low, high, max int) TypedValue
}
}

// ----------------------------------------
// ContextValue

type ContextValue struct {
Context interface{}
}

func (ctx ContextValue) String() string {
return fmt.Sprintf("%v", ctx.Context)
}

// ----------------------------------------
// Block
//
Expand Down
40 changes: 40 additions & 0 deletions stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,46 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
m.PushValue(res0)
},
)
pn.DefineNative("ExecAsPkg",
// TODO: rename UnsafeExecAsPkg?
Copy link
Member

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?

Copy link
Member Author

@albttx albttx Mar 24, 2023

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 addr

The use of the word Unsafe could scare contract writers, and make them think as a bad practice.

Copy link
Contributor

@piux2 piux2 Mar 25, 2023

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 addr

The use of the word Unsafe could scare contract writers, and make them think as a bad practice.

@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),
),

"fn", gno.FuncT(nil, nil),

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.

Copy link
Contributor

@piux2 piux2 Mar 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// g157y5v3k529jyzhjjz4fn49tzzhf4gess6v39xg

  • 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.

m.PushOp(gno.OpSetContext)

// Push an alternate context with OrigCaller=OrigPkgAddr, this will
// affect the function call above
m.PushOp(gno.OpSetContext)
ctx := m.Context.(ExecContext)
ctx.OrigCaller = ctx.OrigPkgAddr

It makes checking std.GetOrigCaller() lose its purpose, when we call std.ExecAsPkg( any function ) in the same contract.

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.

// Push original context first, so it will be reset after the function
// call.
m.PushOp(gno.OpSetContext)
m.PushValue(gno.TypedValue{
V: gno.ContextValue{
Context: m.Context,
},
})

// Push a function call, to execute the function in parameter.
m.PushOp(gno.OpPrecall)
m.PushExpr(gno.Call(gno.FuncT(nil, nil)))
tv := m.LastBlock().GetParams1().TV
m.PushValue(*tv)

// Push an alternate context with OrigCaller=OrigPkgAddr, this will
// affect the function call above
m.PushOp(gno.OpSetContext)
ctx := m.Context.(ExecContext)
ctx.OrigCaller = ctx.OrigPkgAddr
m.PushValue(gno.TypedValue{
V: gno.ContextValue{
Context: ctx,
},
})
},
)
pn.DefineNative("GetCallerAt",
gno.Flds( // params
"n", "int",
Expand Down
4 changes: 4 additions & 0 deletions stdlibs/stdshim/stdshim.gno
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ func DecodeBech32(addr Address) (prefix string, bytes [20]byte, ok bool) {
func DerivePkgAddr(pkgPath string) (addr Address) {
panic(shimWarn)
}

func ExecAsPkg(fn func()) {
panic(shimWarn)
}
2 changes: 1 addition & 1 deletion tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestMachine(store gno.Store, stdout io.Writer, pkgPath string) *gno.Machine
func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAlloc int64, send std.Coins) *gno.Machine {
// FIXME: create a better package to manage this, with custom constructors
pkgAddr := gno.DerivePkgAddr(pkgPath) // the addr of the pkgPath called.
caller := gno.DerivePkgAddr(pkgPath) // NOTE: for the purpose of testing, the caller is generally the "main" package, same as pkgAddr.
caller := gno.DerivePkgAddr("caller") // NOTE: for the purpose of testing, the caller is generally the "main" package, same as pkgAddr.
pkgCoins := std.MustParseCoins("200000000ugnot").Add(send) // >= send.
banker := newTestBanker(pkgAddr.Bech32(), pkgCoins)
ctx := stdlibs.ExecContext{
Expand Down
22 changes: 22 additions & 0 deletions tests/files/zrealm_std6.gno
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:
Copy link
Member

Choose a reason for hiding this comment

The 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