Skip to content

Commit 33cb763

Browse files
committed
fix wired bug
1 parent 13ed00d commit 33cb763

16 files changed

+159
-176
lines changed

gov/governance/api.gno

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,19 @@ func getProposalStatus(id uint64) string {
198198
if !exist {
199199
return ""
200200
}
201-
202201
proposal := prop.(ProposalInfo)
202+
203203
config := GetConfigVersion(proposal.ConfigVersion)
204+
204205
votingStart := proposal.State.CreatedAt + config.VotingStartDelay
205206
votingEnd := votingStart + config.VotingPeriod
206207

207-
state := proposal.State
208-
proposalObj := json.Builder().
208+
node := createProposalStateNode(proposal.State, votingStart, votingEnd)
209+
return marshal(node)
210+
}
211+
212+
func createProposalStateNode(state ProposalState, votingStart, votingEnd uint64) *json.Node {
213+
return json.Builder().
209214
WriteString("createdAt", formatUint64(state.CreatedAt)).
210215
WriteString("upcoming", formatBool(state.Upcoming)).
211216
WriteString("active", formatBool(state.Active)).
@@ -222,8 +227,6 @@ func getProposalStatus(id uint64) string {
222227
WriteString("expired", formatBool(state.Expired)).
223228
WriteString("expiredAt", formatUint64(state.ExpiredAt)).
224229
Node()
225-
226-
return marshal(proposalObj)
227230
}
228231

229232
// getProposalVotes returns the votes of a proposal.

gov/governance/config.gno

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package governance
22

33
import (
44
"std"
5-
"strconv"
65

76
"gno.land/p/demo/avl"
87
"gno.land/p/demo/ufmt"
@@ -34,11 +33,11 @@ func init() {
3433
}
3534

3635
func setConfigVersion(v uint64, cfg Config) {
37-
configVersions.Set(strconv.Itoa(int(v)), cfg)
36+
configVersions.Set(formatUint64(v), cfg)
3837
}
3938

4039
func getConfigByVersion(v uint64) (Config, bool) {
41-
value, exists := configVersions.Get(strconv.Itoa(int(v)))
40+
value, exists := configVersions.Get(formatUint64(v))
4241
if !exists {
4342
return Config{}, false
4443
}

gov/governance/execute.gno

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ type ExecutionContext struct {
7474
WindowEnd uint64
7575
}
7676

77+
func (e *ExecutionContext) String() string {
78+
return ufmt.Sprintf(
79+
"ProposalId: %d, Now: %d, Config: %v, Proposal: %v, WindowStart: %d, WindowEnd: %d",
80+
e.ProposalId, e.Now, e.Config, e.Proposal, e.WindowStart, e.WindowEnd,
81+
)
82+
}
83+
7784
func Execute(proposalId uint64) error {
7885
ctx, err := prepareExecution(proposalId)
7986
if err != nil {
@@ -89,9 +96,8 @@ func Execute(proposalId uint64) error {
8996
}
9097

9198
registry := createParameterHandlers()
92-
// TODO: must fix this
9399
if err := executeProposal(ctx, registry); err != nil {
94-
panic(err)
100+
return err
95101
}
96102

97103
updateProposalState(ctx)
@@ -109,7 +115,7 @@ func Execute(proposalId uint64) error {
109115

110116
func executeProposal(ctx *ExecutionContext, registry *ParameterRegistry) error {
111117
switch ctx.Proposal.ProposalType {
112-
case ParameterChange:
118+
case ParameterChange, CommunityPoolSpend:
113119
return executeParameterChange(ctx.Proposal.Execution.Msgs, registry)
114120
default:
115121
return errUnsupportedProposalType

gov/governance/getter_vote.gno

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,32 @@ package governance
22

33
import (
44
"std"
5-
"strconv"
65

76
"gno.land/p/demo/ufmt"
8-
97
"gno.land/r/gnoswap/v1/common"
108
)
119

12-
// Helper function to validate and get vote information
13-
func getVoteInfoFromKey(voteKey string) (voteWithWeight, bool) {
14-
_, exists := votes.Get(voteKey)
15-
if !exists {
16-
panic(addDetailToError(
17-
errDataNotFound,
18-
ufmt.Sprintf("voteKey(%s) not found", voteKey),
19-
))
20-
}
21-
22-
pid, addr := divideVoteKeyToProposalIdAndUser(voteKey)
23-
24-
voteInfo, exists := getUserVote(addr, pid)
25-
if !exists {
26-
panic(addDetailToError(
27-
errDataNotFound,
28-
ufmt.Sprintf("voteKey(%s) not found", voteKey),
29-
))
30-
}
31-
32-
return voteInfo, true
33-
}
34-
3510
func GetVoteByVoteKey(voteKey string) bool {
36-
vote, exist := votes.Get(voteKey)
37-
if !exist {
38-
panic(addDetailToError(
39-
errDataNotFound,
40-
ufmt.Sprintf("voteKey(%s) not found", voteKey),
41-
))
42-
}
43-
44-
return vote.(bool)
11+
return mustGetVote(voteKey)
4512
}
4613

4714
func GetVoteYesByVoteKey(voteKey string) bool {
48-
voteInfo, _ := getVoteInfoFromKey(voteKey)
49-
return voteInfo.Yes
15+
return mustGetVoteInfo(voteKey).Yes
5016
}
5117

5218
func GetVoteWeightByVoteKey(voteKey string) uint64 {
53-
voteInfo, _ := getVoteInfoFromKey(voteKey)
54-
return voteInfo.Weight
19+
return mustGetVoteInfo(voteKey).Weight
5520
}
5621

5722
func GetVotedHeightByVoteKey(voteKey string) uint64 {
58-
voteInfo, _ := getVoteInfoFromKey(voteKey)
59-
return voteInfo.VotedHeight
23+
return mustGetVoteInfo(voteKey).VotedHeight
6024
}
6125

6226
func GetVotedAtByVoteKey(voteKey string) uint64 {
63-
voteInfo, _ := getVoteInfoFromKey(voteKey)
64-
return voteInfo.VotedAt
27+
return mustGetVoteInfo(voteKey).VotedAt
6528
}
6629

67-
func divideVoteKeyToProposalIdAndUser(voteKey string) (uint64, std.Address) {
30+
func divideVoteKeyToProposalIdAndUser(voteKey string) (proposalId uint64, user std.Address) {
6831
parts, err := common.Split(voteKey, ":", 2)
6932
if err != nil {
7033
panic(addDetailToError(
@@ -73,13 +36,5 @@ func divideVoteKeyToProposalIdAndUser(voteKey string) (uint64, std.Address) {
7336
))
7437
}
7538

76-
proposalId, err := strconv.ParseUint(parts[0], 10, 64)
77-
if err != nil {
78-
panic(addDetailToError(
79-
errInvalidInput,
80-
ufmt.Sprintf("proposalId(%s) is invalid", parts[0]),
81-
))
82-
}
83-
84-
return proposalId, std.Address(parts[1])
39+
return parseUint64(parts[0]), std.Address(parts[1])
8540
}

gov/governance/proposal.gno

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package governance
22

33
import (
44
"std"
5-
"strconv"
65
"strings"
76
"time"
87

@@ -73,7 +72,7 @@ func ProposeText(
7372
}
7473

7574
proposalId++
76-
proposals.Set(strconv.Itoa(int(proposalId)), proposal)
75+
proposals.Set(formatUint64(proposalId), proposal)
7776
latestProposalByProposer.Set(proposer.String(), proposalId)
7877

7978
prevAddr, prevPkgPath := getPrev()
@@ -145,7 +144,7 @@ func ProposeCommunityPoolSpend(
145144
}
146145

147146
proposalId++
148-
proposals.Set(strconv.Itoa(int(proposalId)), proposal)
147+
proposals.Set(formatUint64(proposalId), proposal)
149148
latestProposalByProposer.Set(proposer.String(), proposalId)
150149

151150
prevAddr, prevRealm := getPrev()
@@ -231,7 +230,7 @@ func ProposeParameterChange(
231230
}
232231

233232
proposalId++
234-
proposals.Set(strconv.Itoa(int(proposalId)), proposal)
233+
proposals.Set(formatUint64(proposalId), proposal)
235234
latestProposalByProposer.Set(proposer.String(), proposalId)
236235

237236
prevAddr, prevRealm := getPrev()

gov/governance/proposal_test.gno

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestProposeText(t *testing.T) {
7979

8080
uassert.NoError(t, err)
8181

82-
prop, exists := proposals.Get(strconv.Itoa(int(pid)))
82+
prop, exists := proposals.Get(formatUint64(pid))
8383
uassert.True(t, exists)
8484

8585
proposal := prop.(ProposalInfo)
@@ -146,7 +146,7 @@ func TestProposeCommunityPoolSpend(t *testing.T) {
146146

147147
uassert.NoError(t, err)
148148

149-
prop, exists := proposals.Get(strconv.Itoa(int(pid)))
149+
prop, exists := proposals.Get(formatUint64(pid))
150150
uassert.True(t, exists)
151151

152152
proposal := prop.(ProposalInfo)
@@ -168,7 +168,7 @@ func TestUpdateProposalsState(t *testing.T) {
168168
}
169169
setConfigVersion(1, newConfig)
170170

171-
testCases := []struct {
171+
tests := []struct {
172172
name string
173173
currentTime uint64
174174
setupProposal func(uint64) ProposalInfo
@@ -279,16 +279,16 @@ func TestUpdateProposalsState(t *testing.T) {
279279
},
280280
}
281281

282-
for _, tt := range testCases {
282+
for _, tt := range tests {
283283
t.Run(tt.name, func(t *testing.T) {
284284
proposals = avl.NewTree()
285285

286286
proposal := tt.setupProposal(tt.currentTime)
287-
proposals.Set(strconv.Itoa(int(1)), proposal)
287+
proposals.Set(formatUint64(1), proposal)
288288

289289
updateProposalsState()
290290

291-
updatedProposal, exists := proposals.Get(strconv.Itoa(int(1)))
291+
updatedProposal, exists := proposals.Get(formatUint64(1))
292292
uassert.True(t, exists)
293293
tt.validate(t, updatedProposal.(ProposalInfo))
294294
})
@@ -339,7 +339,7 @@ func TestProposeParameterChange(t *testing.T) {
339339

340340
uassert.NoError(t, err)
341341

342-
prop, exists := proposals.Get(strconv.Itoa(int(pid)))
342+
prop, exists := proposals.Get(formatUint64(pid))
343343
uassert.True(t, exists)
344344

345345
proposal := prop.(ProposalInfo)

0 commit comments

Comments
 (0)