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

curchain_b.gno

1.72 Kb · 50 lines
 1// Package curchain_b is the terminal hop in the multi-level cross-call
 2// chain test helpers. It prints the full captured chain visible from this
 3// realm, and asserts parity with unsafe.CurrentRealm() /
 4// unsafe.PreviousRealm() at the cross-callee position.
 5package curchain_b
 6
 7import (
 8	"chain/runtime/unsafe"
 9)
10
11// B is a crossing function that prints the captured chain reached via
12// .Previous() walks and asserts parity with unsafe.CurrentRealm() /
13// unsafe.PreviousRealm() at the cross-callee position. The chain-root
14// case is covered separately by zrealm_cur_parity.
15//
16// Chain layout for callsite main -> curchain_a.A -> curchain_b.B:
17//
18//	cur                  = B
19//	cur.Previous()       = A
20//	cur.Previous()^2     = main
21//	cur.Previous()^3     = EOA origin (PkgPath() == "")
22//	cur.Previous()^4     = panics (origin has no further previous)
23func B(cur realm) {
24	println("B cur.PkgPath:", cur.PkgPath())
25	p1 := cur.Previous()
26	println("B Previous().PkgPath:", p1.PkgPath())
27	p2 := p1.Previous()
28	println("B Previous().Previous().PkgPath:", p2.PkgPath())
29	p3 := p2.Previous()
30	println("B Previous()^3 PkgPath empty (EOA origin):", p3.PkgPath() == "")
31	func() {
32		defer func() {
33			r := recover()
34			println("B Previous()^4 panics past origin:", r != nil)
35		}()
36		_ = p3.Previous()
37	}()
38
39	// Parity at cross-callee position: cur agrees with
40	// unsafe.CurrentRealm(), and cur.Previous() (the caller realm
41	// A) agrees with unsafe.PreviousRealm().
42	rc := unsafe.CurrentRealm()
43	rp := unsafe.PreviousRealm()
44	println("B parity cur==Current:",
45		string(cur.Address()) == string(rc.Address()) &&
46			cur.PkgPath() == rc.PkgPath())
47	println("B parity prev==Previous:",
48		string(p1.Address()) == string(rp.Address()) &&
49			p1.PkgPath() == rp.PkgPath())
50}