store.gno
4.88 Kb · 167 lines
1package launchpad
2
3import (
4 "strconv"
5
6 "gno.land/p/gnoswap/store"
7 bptree "gno.land/p/nt/bptree/v0"
8 ufmt "gno.land/p/nt/ufmt/v0"
9)
10
11// NewBPTreeN allocates a BP-tree under /r/gnoswap/launchpad's realm context
12// (the realm that declares Project/Deposit/RewardManager). The tree's PkgID is
13// therefore /r/gnoswap/launchpad, matching the domain values it stores, so
14// tree.Set leaf-slot writes clear the readonly-taint gate regardless of which
15// realm (launchpad/v1, mock, tests) calls Set. Implementations, mocks, and
16// tests must allocate launchpad trees through here rather than calling
17// bptree.NewBPTreeN directly in their own realm.
18func NewBPTreeN(fanout int) *bptree.BPTree {
19 return bptree.NewBPTreeN(fanout)
20}
21
22type StoreKey string
23
24func (s StoreKey) String() string {
25 return string(s)
26}
27
28const (
29 StoreKeyProjects StoreKey = "projects" // Projects tree
30 StoreKeyProjectTierRewardManagers StoreKey = "projectTierRewardManagers" // Project tier reward managers tree
31 StoreKeyDepositCounter StoreKey = "depositCounter" // Deposit counter
32 StoreKeyDeposits StoreKey = "deposits" // Deposits tree
33)
34
35type launchpadStore struct {
36 kvStore store.KVStore
37}
38
39// HasProjectsKey checks if the projects key exists in the store.
40func (s *launchpadStore) HasProjectsKey() bool {
41 return s.kvStore.Has(StoreKeyProjects.String())
42}
43
44// GetProjects retrieves the projects tree.
45func (s *launchpadStore) GetProjects() *bptree.BPTree {
46 result, err := s.kvStore.Get(StoreKeyProjects.String())
47 if err != nil {
48 panic(err)
49 }
50
51 projects, ok := result.(*bptree.BPTree)
52 if !ok {
53 panic(ufmt.Sprintf("failed to cast result to *bptree.BPTree: %T", result))
54 }
55
56 return projects
57}
58
59// SetProjects stores the projects tree.
60func (s *launchpadStore) SetProjects(_ int, rlm realm, projects *bptree.BPTree) error {
61 if !rlm.IsCurrent() {
62 return ErrSpoofedRealm
63 }
64
65 return s.kvStore.Set(0, rlm, StoreKeyProjects.String(), projects)
66}
67
68// HasProjectTierRewardManagersKey checks if the project tier reward managers key exists in the store.
69func (s *launchpadStore) HasProjectTierRewardManagersKey() bool {
70 return s.kvStore.Has(StoreKeyProjectTierRewardManagers.String())
71}
72
73// GetProjectTierRewardManagers retrieves the project tier reward managers tree.
74func (s *launchpadStore) GetProjectTierRewardManagers() *bptree.BPTree {
75 result, err := s.kvStore.Get(StoreKeyProjectTierRewardManagers.String())
76 if err != nil {
77 panic(err)
78 }
79
80 managers, ok := result.(*bptree.BPTree)
81 if !ok {
82 panic(ufmt.Sprintf("failed to cast result to *bptree.BPTree: %T", result))
83 }
84
85 return managers
86}
87
88// SetProjectTierRewardManagers stores the project tier reward managers tree.
89func (s *launchpadStore) SetProjectTierRewardManagers(_ int, rlm realm, managers *bptree.BPTree) error {
90 if !rlm.IsCurrent() {
91 return ErrSpoofedRealm
92 }
93
94 return s.kvStore.Set(0, rlm, StoreKeyProjectTierRewardManagers.String(), managers)
95}
96
97// HasDepositCounterStoreKey checks if the deposit counter key exists in the store.
98func (s *launchpadStore) HasDepositCounterStoreKey() bool {
99 return s.kvStore.Has(StoreKeyDepositCounter.String())
100}
101
102// GetDepositCounter retrieves the deposit counter.
103func (s *launchpadStore) GetDepositCounter() *Counter {
104 result, err := s.kvStore.Get(StoreKeyDepositCounter.String())
105 if err != nil {
106 panic(err)
107 }
108
109 counter, ok := result.(*Counter)
110 if !ok {
111 panic(ufmt.Sprintf("failed to cast result to Counter: %T", result))
112 }
113
114 return counter
115}
116
117func (s *launchpadStore) SetDepositCounter(_ int, rlm realm, counter *Counter) error {
118 if !rlm.IsCurrent() {
119 return ErrSpoofedRealm
120 }
121
122 return s.kvStore.Set(0, rlm, StoreKeyDepositCounter.String(), counter)
123}
124
125func (s *launchpadStore) NextDepositID() string {
126 counter := s.GetDepositCounter()
127
128 return strconv.FormatInt(counter.Next(), 10)
129}
130
131// SetDepositCounter stores the deposit counter.
132// HasDepositsKey checks if the deposits key exists in the store.
133func (s *launchpadStore) HasDepositsKey() bool {
134 return s.kvStore.Has(StoreKeyDeposits.String())
135}
136
137// GetDeposits retrieves the deposits tree.
138func (s *launchpadStore) GetDeposits() *bptree.BPTree {
139 result, err := s.kvStore.Get(StoreKeyDeposits.String())
140 if err != nil {
141 panic(err)
142 }
143
144 deposits, ok := result.(*bptree.BPTree)
145 if !ok {
146 panic(ufmt.Sprintf("failed to cast result to *bptree.BPTree: %T", result))
147 }
148
149 return deposits
150}
151
152// SetDeposits stores the deposits tree.
153func (s *launchpadStore) SetDeposits(_ int, rlm realm, deposits *bptree.BPTree) error {
154 if !rlm.IsCurrent() {
155 return ErrSpoofedRealm
156 }
157
158 return s.kvStore.Set(0, rlm, StoreKeyDeposits.String(), deposits)
159}
160
161// NewLaunchpadStore creates a new launchpad store instance with the provided KV store.
162// This function is used by the upgrade system to create storage instances for each implementation.
163func NewLaunchpadStore(kvStore store.KVStore) ILaunchpadStore {
164 return &launchpadStore{
165 kvStore: kvStore,
166 }
167}