Skip to content

Commit

Permalink
feat(std): add isMsgRun function and update AssertOriginCall to handl…
Browse files Browse the repository at this point in the history
…e MsgRun cases
  • Loading branch information
omarsy committed Jan 18, 2025
1 parent 5742e10 commit 8fb3cc6
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 22 deletions.
36 changes: 18 additions & 18 deletions gno.land/pkg/integration/testdata/assertorigincall.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
# | 7 | | through /p/demo/bar | bar.A() | PANIC |
# | 8 | | | bar.B() | pass |
# | 9 | | | bar.C() | PANIC |
# | 10 | MsgRun | wallet direct | myrealm.A() | pass |
# | 10 | MsgRun | wallet direct | myrealm.A() | PANIC |
# | 11 | | | myrealm.B() | pass |
# | 12 | | | myrealm.C() | pass |
# | 13 | | through /r/foo | myrealm.A() | PANIC |
# | 14 | | | myrealm.B() | pass |
# | 15 | | | myrealm.C() | PANIC |
# | 16 | | through /p/demo/bar | bar.A() | pass |
# | 16 | | through /p/demo/bar | bar.A() | PANIC |
# | 17 | | | bar.B() | pass |
# | 18 | | | bar.C() | PANIC |
# | 19 | MsgCall | wallet direct | std.AssertOriginCall() | pass |
# | 20 | MsgRun | wallet direct | std.AssertOriginCall() | pass |
# | 20 | MsgRun | wallet direct | std.AssertOriginCall() | PANIC |

# Init
## set up and start a new node
Expand Down Expand Up @@ -71,17 +71,17 @@ stderr 'invalid non-origin call'
## ! gnokey maketx call -pkgpath gno.land/p/demo/bar -func C -gas-fee 100000ugnot -gas-wanted 5000000 -broadcast -chainid tendermint_test test1
## stderr 'invalid non-origin call'

## 10. MsgRun -> run.main -> myrlm.A: PASS
gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmA.gno
stdout 'OK!'
## 10. MsgRun -> run.main -> myrlm.A: PANIC
! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmA.gno
## stderr 'invalid non-origin call'

## 11. MsgRun -> run.main -> myrlm.B: PASS
gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmB.gno
stdout 'OK!'

## 12. MsgRun -> run.main -> myrlm.C: PASS
## gnokey maketx run -gas-fee 100000ugnot -gas-wanted 5500000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmC.gno
stdout 'OK!'
## 12. MsgRun -> run.main -> myrlm.C: PANIC
! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 5500000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmC.gno
stderr 'invalid non-origin call'

## 13. MsgRun -> run.main -> foo.A: PANIC
! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 5500000 -broadcast -chainid tendermint_test test1 $WORK/run/fooA.gno
Expand All @@ -95,26 +95,26 @@ stdout 'OK!'
! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 5500000 -broadcast -chainid tendermint_test test1 $WORK/run/fooC.gno
stderr 'invalid non-origin call'

## 16. MsgRun -> run.main -> bar.A: PASS
gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/barA.gno
stdout 'OK!'
## 16. MsgRun -> run.main -> bar.A: PANIC
! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/barA.gno
stderr 'invalid non-origin call'

## 17. MsgRun -> run.main -> bar.B: PASS
gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/barB.gno
stdout 'OK!'

## 18. MsgRun -> run.main -> bar.C: PASS
gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/barC.gno
stdout 'OK!'
## 18. MsgRun -> run.main -> bar.C: PANIC
! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/barC.gno
stderr 'invalid non-origin call'

## remove testcase 19 due to maketx call forced to call a realm
## 19. MsgCall -> std.AssertOriginCall: PASS
## gnokey maketx call -pkgpath std -func AssertOriginCall -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1
## stdout 'OK!'

## 20. MsgRun -> std.AssertOriginCall: PASS
gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/baz.gno
stdout 'OK!'
## 20. MsgRun -> std.AssertOriginCall: PANIC
! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 10000000 -broadcast -chainid tendermint_test test1 $WORK/run/baz.gno
stderr 'invalid non-origin call'


-- r/myrlm/rlm.gno --
Expand Down
20 changes: 20 additions & 0 deletions gnovm/stdlibs/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gnovm/stdlibs/std/native.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ package std
// It also panic every time when the transaction is broadcasted via
// MsgRun.
func AssertOriginCall() {
if !PrevRealm().IsUser() {
if !PrevRealm().IsUser() || isMsgRun() {
panic("invalid non-origin call")
}
}

func GetChainID() string // injected
func GetChainDomain() string // injected
func GetHeight() int64 // injected
Expand Down Expand Up @@ -53,3 +52,4 @@ func origPkgAddr() string
func callerAt(n int) string
func getRealm(height int) (address string, pkgPath string)
func assertCallerIsRealm()
func isMsgRun() bool
9 changes: 9 additions & 0 deletions gnovm/stdlibs/std/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ func GetChainDomain(m *gno.Machine) string {
return GetContext(m).ChainDomain
}

func X_isMsgRun(m *gno.Machine) bool {
n := m.NumFrames()
if n == 0 {
return false
}

return m.Frames[0].LastPackage.PkgPath != "main"
}

func GetHeight(m *gno.Machine) int64 {
return GetContext(m).Height
}
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/std5.gno
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
// callerAt<VPBlock(3,52)>(n<VPBlock(1,0)>)
// gonative:std.callerAt
// std<VPBlock(2,0)>.GetCallerAt(2)
// std/native.gno:46
// std/native.gno:45
// main<VPBlock(1,0)>()
// main/files/std5.gno:10

Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/std8.gno
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
// callerAt<VPBlock(3,52)>(n<VPBlock(1,0)>)
// gonative:std.callerAt
// std<VPBlock(2,0)>.GetCallerAt(4)
// std/native.gno:46
// std/native.gno:45
// fn<VPBlock(1,0)>()
// main/files/std8.gno:16
// testutils<VPBlock(2,1)>.WrapCall(inner<VPBlock(3,0)>)
Expand Down

0 comments on commit 8fb3cc6

Please sign in to comment.