50 lines
1.8 KiB
Go
50 lines
1.8 KiB
Go
package main
|
|
|
|
// The switch on the controller's lease (novox/hq ADR 0561 §3): minting is on only while the lease is handed to a holder
|
|
// that is no controller, and off for a controller's value, a key deleted or expired, or one that cannot be read. The
|
|
// write that hands it away turns it on and mints every wanted account the vault holds no value for; a lease handed back
|
|
// turns it off. No clock and no setting turns it. A key's age passing is heard by no watch, so the lease is read afresh
|
|
// before every mint (LeaseRead): a lease that expired since mints nothing.
|
|
|
|
import "git.novox.be/novox/mesh-sdk/go/buscompose"
|
|
|
|
// LeaseHeard is the watch of the controller's lease. When minting turns on or off, every wanted account is judged again:
|
|
// minted when on, held back with the new why when off.
|
|
func (v *Vault) LeaseHeard(raw []byte, found bool, readErr error) {
|
|
if !v.setLease(raw, found, readErr) {
|
|
return
|
|
}
|
|
on, why := v.mintingOn()
|
|
v.log("minting is %s: %s", map[bool]string{true: "ON", false: "off"}[on], why)
|
|
for _, user := range v.wantedUsers() {
|
|
if w, ok := v.wanted(user); ok {
|
|
if _, err := v.mintWanted(w, "the lease write"); err != nil {
|
|
v.log("%s is not minted: %v", user, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// setLease records what the lease says, and answers whether minting turned on or off by it.
|
|
func (v *Vault) setLease(raw []byte, found bool, readErr error) bool {
|
|
on, why := buscompose.LeaseHandedAway(raw, found, readErr)
|
|
m := &v.minting
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
flipped := on != m.on || m.why == ""
|
|
if flipped {
|
|
m.since = v.Now().UTC()
|
|
}
|
|
m.on, m.why = on, why
|
|
return flipped
|
|
}
|
|
|
|
// leaseAfresh reads the lease again before a mint, where the vault can: a key that expired since is heard by no watch.
|
|
func (v *Vault) leaseAfresh() {
|
|
if v.LeaseRead == nil {
|
|
return
|
|
}
|
|
raw, found, err := v.LeaseRead()
|
|
v.setLease(raw, found, err)
|
|
}
|