getter_utils.gno
1.13 Kb · 39 lines
1package governance
2
3import (
4 "gno.land/r/gnoswap/gov/governance"
5)
6
7// The functions below are makeEntrySafeFn transformations handed to
8// rotree.Wrap. They run on every read through a read-only view, so each one
9// returns a value the caller may hold without reaching realm-owned state.
10
11// cloneProposalEntry is the transformation for the proposal tree. Proposal is
12// mutable and its setters are declared in /r/gnoswap/gov/governance, so an
13// external holder of the stored pointer could write realm state through it.
14//
15// Proposal.Clone leaves the type-specific data nil; see it for which lookups
16// read it instead.
17func cloneProposalEntry(value any) any {
18 proposal, ok := value.(*governance.Proposal)
19 if !ok {
20 return nil
21 }
22
23 return proposal.Clone()
24}
25
26// cloneProposalIDsEntry is the transformation for the user proposal tree. The
27// stored value is a slice, so hand out a copy rather than letting a reader
28// write through the shared backing array.
29func cloneProposalIDsEntry(value any) any {
30 proposalIDs, ok := value.([]int64)
31 if !ok {
32 return nil
33 }
34
35 cloned := make([]int64, len(proposalIDs))
36 copy(cloned, proposalIDs)
37
38 return cloned
39}