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

lock.gno

0.96 Kb · 30 lines
 1package pool
 2
 3import "errors"
 4
 5// assertPoolUnlocked is the read-only reentrancy guard. It reports the global
 6// pool lock state without mutating it, so it is safe to call before the access
 7// checks: a call that aborts on authorization afterwards leaves no persisted
 8// lock behind (which would otherwise leak under interrealm v2, where a panic
 9// crossing the realm boundary skips the deferred unlockPool). It also keeps the
10// "cannot modify pool while locked" message as the first failure for any
11// entrypoint invoked while a swap holds the lock (reentrancy protection).
12func (i *poolV1) assertPoolUnlocked() {
13	if i.store.HasUnlocked() && !i.store.GetUnlocked() {
14		panic(errors.New(errLockedPool))
15	}
16}
17
18func (i *poolV1) lockPool(_ int, rlm realm) {
19	i.assertPoolUnlocked()
20
21	if err := i.store.SetUnlocked(0, rlm, false); err != nil {
22		panic(err)
23	}
24}
25
26func (i *poolV1) unlockPool(_ int, rlm realm) {
27	if err := i.store.SetUnlocked(0, rlm, true); err != nil {
28		panic(err)
29	}
30}