Skip to content

Commit d55c5a0

Browse files
committed
feat(std): add isMsgRun function and update AssertOriginCall to handle MsgRun cases
1 parent 5742e10 commit d55c5a0

File tree

7 files changed

+58
-23
lines changed

7 files changed

+58
-23
lines changed

gno.land/pkg/integration/testdata/assertorigincall.txtar

+19-19
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
# | 7 | | through /p/demo/bar | bar.A() | PANIC |
1313
# | 8 | | | bar.B() | pass |
1414
# | 9 | | | bar.C() | PANIC |
15-
# | 10 | MsgRun | wallet direct | myrealm.A() | pass |
15+
# | 10 | MsgRun | wallet direct | myrealm.A() | PANIC |
1616
# | 11 | | | myrealm.B() | pass |
17-
# | 12 | | | myrealm.C() | pass |
17+
# | 12 | | | myrealm.C() | PANIC |
1818
# | 13 | | through /r/foo | myrealm.A() | PANIC |
1919
# | 14 | | | myrealm.B() | pass |
2020
# | 15 | | | myrealm.C() | PANIC |
21-
# | 16 | | through /p/demo/bar | bar.A() | pass |
21+
# | 16 | | through /p/demo/bar | bar.A() | PANIC |
2222
# | 17 | | | bar.B() | pass |
2323
# | 18 | | | bar.C() | PANIC |
2424
# | 19 | MsgCall | wallet direct | std.AssertOriginCall() | pass |
25-
# | 20 | MsgRun | wallet direct | std.AssertOriginCall() | pass |
25+
# | 20 | MsgRun | wallet direct | std.AssertOriginCall() | PANIC |
2626

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

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

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

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

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

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

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

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

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

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

119119

120120
-- r/myrlm/rlm.gno --

gnovm/stdlibs/generated.go

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gnovm/stdlibs/std/native.gno

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ package std
66
// It also panic every time when the transaction is broadcasted via
77
// MsgRun.
88
func AssertOriginCall() {
9-
if !PrevRealm().IsUser() {
9+
if !PrevRealm().IsUser() || isMsgRun() {
1010
panic("invalid non-origin call")
1111
}
1212
}
13-
1413
func GetChainID() string // injected
1514
func GetChainDomain() string // injected
1615
func GetHeight() int64 // injected
@@ -53,3 +52,4 @@ func origPkgAddr() string
5352
func callerAt(n int) string
5453
func getRealm(height int) (address string, pkgPath string)
5554
func assertCallerIsRealm()
55+
func isMsgRun() bool

gnovm/stdlibs/std/native.go

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ func GetChainDomain(m *gno.Machine) string {
1414
return GetContext(m).ChainDomain
1515
}
1616

17+
func X_isMsgRun(m *gno.Machine) bool {
18+
n := m.NumFrames()
19+
if n == 0 {
20+
return false
21+
}
22+
23+
return m.Frames[0].LastPackage.PkgPath != "main"
24+
}
25+
1726
func GetHeight(m *gno.Machine) int64 {
1827
return GetContext(m).Height
1928
}

gnovm/tests/files/std5.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
// callerAt<VPBlock(3,52)>(n<VPBlock(1,0)>)
1717
// gonative:std.callerAt
1818
// std<VPBlock(2,0)>.GetCallerAt(2)
19-
// std/native.gno:46
19+
// std/native.gno:45
2020
// main<VPBlock(1,0)>()
2121
// main/files/std5.gno:10
2222

gnovm/tests/files/std8.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626
// callerAt<VPBlock(3,52)>(n<VPBlock(1,0)>)
2727
// gonative:std.callerAt
2828
// std<VPBlock(2,0)>.GetCallerAt(4)
29-
// std/native.gno:46
29+
// std/native.gno:45
3030
// fn<VPBlock(1,0)>()
3131
// main/files/std8.gno:16
3232
// testutils<VPBlock(2,1)>.WrapCall(inner<VPBlock(3,0)>)

gnovm/tests/stdlibs/std/std.gno

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ func TestSkipHeights(count int64) // injected
55
func TestSetOrigCaller(addr Address) { testSetOrigCaller(string(addr)) }
66
func TestSetOrigPkgAddr(addr Address) { testSetOrigPkgAddr(string(addr)) }
77

8+
func AssertOriginCall() {
9+
if !PrevRealm().IsUser() {
10+
panic("invalid non-origin call")
11+
}
12+
}
13+
814
// TestSetRealm sets the realm for the current frame.
915
// After calling TestSetRealm, calling CurrentRealm() in the test function will yield the value of
1016
// rlm, while if a realm function is called, using PrevRealm() will yield rlm.

0 commit comments

Comments
 (0)