Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

subhost.gno

2.90 Kb · 67 lines
 1// Package subhost is a test fixture for realm.Sub sub-identity
 2// presentation across a real package boundary. It plays two roles:
 3//
 4//   - Host: ActAsSub mints a sub of its OWN live cur and crosses into an
 5//     observer with it.
 6//   - Observer: Observe reads the presented identity (cur.Previous) that
 7//     a caller crossed in with, and asserts unsafe.PreviousRealm parity.
 8//
 9// Consumers:
10//   - gnovm/tests/files/zrealm_sub_foreign_present.gno crosses into
11//     Observe directly with a sub minted in the caller's own realm, to
12//     check cross-realm identity presentation + Subpath() through a cross.
13//   - gno.land/pkg/integration/testdata/subrealm_run_parity.txtar enters
14//     via MsgRun, crosses into ActAsSub, which mints a sub and crosses
15//     into Observe — the MsgRun-entry counterpart to the MsgCall-entry
16//     parity filetest zrealm_sub1.gno.
17package subhost
18
19import (
20	"chain"
21	"chain/runtime/unsafe"
22)
23
24const pkgPath = "gno.land/r/tests/vm/subhost"
25
26// Observe reports what the immediate caller presented as its identity.
27// The three cur.Previous() views (address, synthesized pkgpath, subpath)
28// are the callee-side surface a sub-token must present; the two parity
29// booleans assert unsafe.PreviousRealm() reads the SAME identity (v2
30// §5.4: unsafe.* must agree with cur.Previous()). prevParity is the
31// load-bearing assertion for the identity-chain walk; curParity guards
32// height 0.
33func Observe(cur realm) {
34	prev := cur.Previous()
35	println("observed prev.PkgPath:", prev.PkgPath())
36	println("observed prev.Subpath:", prev.Subpath())
37	println("observed prev.IsUserCall:", prev.IsUserCall())
38	// Short-circuit the primary case FIRST: DerivePkgSubAddr panics on an
39	// empty subpath, so it must not be evaluated when prev is a primary.
40	println("prev.Address == DerivePkgSubAddr(host, subpath):",
41		prev.Subpath() == "" ||
42			string(prev.Address()) == string(chain.DerivePkgSubAddr(pkgPathOf(prev.PkgPath()), prev.Subpath())))
43	println("unsafe prev parity:",
44		string(prev.Address()) == string(unsafe.PreviousRealm().Address()) &&
45			prev.PkgPath() == unsafe.PreviousRealm().PkgPath())
46	println("unsafe cur parity:",
47		string(cur.Address()) == string(unsafe.CurrentRealm().Address()) &&
48			cur.PkgPath() == unsafe.CurrentRealm().PkgPath())
49}
50
51// pkgPathOf returns the host portion of a possibly-synthesized pkgpath,
52// so DerivePkgSubAddr (which takes the host) can be recomputed from the
53// callee's observation alone.
54func pkgPathOf(p string) string {
55	host, _, _ := chain.SplitPkgSubPath(p)
56	return host
57}
58
59// ActAsSub mints a sub-identity of subhost's OWN live cur and crosses
60// with it into Observe. Used by the MsgRun-entry parity test: the
61// outermost entry is a `/e/<addr>/run` ephemeral, but subhost's cur is a
62// first-class primary here, so cur.Sub is legal and the sub presents to
63// Observe exactly as in the MsgCall path.
64func ActAsSub(cur realm, subpath string) {
65	sub := cur.Sub(subpath)
66	Observe(cross(sub))
67}