Skip to content

Commit 963ced5

Browse files
committed
fmt
1 parent d9d9106 commit 963ced5

9 files changed

+193
-181
lines changed

gov/governance/api.gno

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
func createProposalJsonNode(id uint64, proposal ProposalInfo) *json.Node {
1515
return json.Builder().
16-
WriteString("id", strconv.FormatUint(id, 10)).
17-
WriteString("configVersion", strconv.FormatUint(proposal.ConfigVersion, 10)).
16+
WriteString("id", formatUint64(id)).
17+
WriteString("configVersion", formatUint64(proposal.ConfigVersion)).
1818
WriteString("proposer", proposal.Proposer.String()).
1919
WriteString("status", b64Encode(getProposalStatus(id))).
2020
WriteString("type", proposal.ProposalType.String()).
@@ -53,7 +53,7 @@ func GetProposalById(id uint64) string {
5353
en.MintAndDistributeGns()
5454
updateProposalsState()
5555

56-
_, exists := proposals.Get(strconv.FormatUint(id, 10))
56+
_, exists := proposals.Get(formatUint64(id))
5757
if !exists {
5858
return ""
5959
}
@@ -78,13 +78,13 @@ func GetVoteStatusFromProposalById(id uint64) string {
7878
en.MintAndDistributeGns()
7979
updateProposalsState()
8080

81-
_, exists := proposals.Get(strconv.FormatUint(id, 10))
81+
_, exists := proposals.Get(formatUint64(id))
8282
if !exists {
8383
return ""
8484
}
8585

8686
votesObj := metaNode()
87-
votesObj.AppendObject("proposalId", json.StringNode("proposalId", strconv.FormatUint(id, 10)))
87+
votesObj.AppendObject("proposalId", json.StringNode("proposalId", formatUint64(id)))
8888
votesObj.AppendObject("votes", json.StringNode("votes", b64Encode(getProposalVotes(id)))) // max, yes, no
8989

9090
return marshal(votesObj)
@@ -139,17 +139,17 @@ func GetVoteByAddressFromProposalById(addr std.Address, id uint64) string {
139139

140140
func createVoteJsonNode(addr std.Address, id uint64, vote voteWithWeight) *json.Node {
141141
return json.Builder().
142-
WriteString("proposalId", strconv.FormatUint(id, 10)).
143-
WriteString("voteYes", strconv.FormatBool(vote.Yes)).
144-
WriteString("voteWeight", strconv.FormatUint(vote.Weight, 10)).
145-
WriteString("voteHeight", strconv.FormatUint(vote.VotedHeight, 10)).
146-
WriteString("voteTimestamp", strconv.FormatUint(vote.VotedAt, 10)).
142+
WriteString("proposalId", formatUint64(id)).
143+
WriteString("voteYes", formatBool(vote.Yes)).
144+
WriteString("voteWeight", formatUint64(vote.Weight)).
145+
WriteString("voteHeight", formatUint64(vote.VotedHeight)).
146+
WriteString("voteTimestamp", formatUint64(vote.VotedAt)).
147147
Node()
148148
}
149149

150150
// getProposalExtraData returns the extra data of a proposal based on its type.
151151
func getProposalExtraData(proposalId uint64) string {
152-
proposal, exist := proposals.Get(strconv.FormatUint(proposalId, 10))
152+
proposal, exist := proposals.Get(formatUint64(proposalId))
153153
if !exist {
154154
return ""
155155
}
@@ -176,7 +176,7 @@ func getCommunityPoolSpendProposalData(proposalId uint64) string {
176176
proposalObj := json.Builder().
177177
WriteString("to", proposal.CommunityPoolSpend.To.String()).
178178
WriteString("tokenPath", proposal.CommunityPoolSpend.TokenPath).
179-
WriteString("amount", strconv.FormatUint(proposal.CommunityPoolSpend.Amount, 10)).
179+
WriteString("amount", formatUint64(proposal.CommunityPoolSpend.Amount)).
180180
Node()
181181

182182
return marshal(proposalObj)
@@ -194,7 +194,7 @@ func getParameterChangeProposalData(proposalId uint64) string {
194194

195195
// getProposalStatus returns the status of a proposal.
196196
func getProposalStatus(id uint64) string {
197-
prop, exist := proposals.Get(strconv.Itoa(int(id)))
197+
prop, exist := proposals.Get(formatUint64(id))
198198
if !exist {
199199
return ""
200200
}
@@ -204,30 +204,31 @@ func getProposalStatus(id uint64) string {
204204
votingStart := proposal.State.CreatedAt + config.VotingStartDelay
205205
votingEnd := votingStart + config.VotingPeriod
206206

207+
state := proposal.State
207208
proposalObj := json.Builder().
208-
WriteString("CreatedAt", strconv.FormatUint(proposal.State.CreatedAt, 10)).
209-
WriteString("Upcoming", strconv.FormatBool(proposal.State.Upcoming)).
210-
WriteString("Active", strconv.FormatBool(proposal.State.Active)).
211-
WriteString("VotingStart", strconv.FormatUint(votingStart, 10)).
212-
WriteString("VotingEnd", strconv.FormatUint(votingEnd, 10)).
213-
WriteString("Passed", strconv.FormatBool(proposal.State.Passed)).
214-
WriteString("PassedAt", strconv.FormatUint(proposal.State.PassedAt, 10)).
215-
WriteString("Rejected", strconv.FormatBool(proposal.State.Rejected)).
216-
WriteString("RejectedAt", strconv.FormatUint(proposal.State.RejectedAt, 10)).
217-
WriteString("Canceled", strconv.FormatBool(proposal.State.Canceled)).
218-
WriteString("CanceledAt", strconv.FormatUint(proposal.State.CanceledAt, 10)).
219-
WriteString("Executed", strconv.FormatBool(proposal.State.Executed)).
220-
WriteString("ExecutedAt", strconv.FormatUint(proposal.State.ExecutedAt, 10)).
221-
WriteString("Expired", strconv.FormatBool(proposal.State.Expired)).
222-
WriteString("ExpiredAt", strconv.FormatUint(proposal.State.ExpiredAt, 10)).
209+
WriteString("CreatedAt", formatUint64(state.CreatedAt)).
210+
WriteString("Upcoming", formatBool(state.Upcoming)).
211+
WriteString("Active", formatBool(state.Active)).
212+
WriteString("VotingStart", formatUint64(votingStart)).
213+
WriteString("VotingEnd", formatUint64(votingEnd)).
214+
WriteString("Passed", formatBool(state.Passed)).
215+
WriteString("PassedAt", formatUint64(state.PassedAt)).
216+
WriteString("Rejected", formatBool(state.Rejected)).
217+
WriteString("RejectedAt", formatUint64(state.RejectedAt)).
218+
WriteString("Canceled", formatBool(state.Canceled)).
219+
WriteString("CanceledAt", formatUint64(state.CanceledAt)).
220+
WriteString("Executed", formatBool(state.Executed)).
221+
WriteString("ExecutedAt", formatUint64(state.ExecutedAt)).
222+
WriteString("Expired", formatBool(state.Expired)).
223+
WriteString("ExpiredAt", formatUint64(state.ExpiredAt)).
223224
Node()
224225

225226
return marshal(proposalObj)
226227
}
227228

228229
// getProposalVotes returns the votes of a proposal.
229230
func getProposalVotes(id uint64) string {
230-
prop, exist := proposals.Get(strconv.FormatUint(id, 10))
231+
prop, exist := proposals.Get(formatUint64(id))
231232
if !exist {
232233
return ""
233234
}
@@ -236,7 +237,7 @@ func getProposalVotes(id uint64) string {
236237
maxVoting := proposal.MaxVotingWeight.ToString()
237238

238239
proposalObj := json.Builder().
239-
WriteString("quorum", strconv.FormatUint(proposal.QuorumAmount, 10)).
240+
WriteString("quorum", formatUint64(proposal.QuorumAmount)).
240241
WriteString("max", maxVoting).
241242
WriteString("yes", proposal.Yea.ToString()).
242243
WriteString("no", proposal.Nay.ToString()).

gov/governance/config.gno

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ func getConfigByVersion(v uint64) (Config, bool) {
4848
func getLatestVersion() uint64 {
4949
var maxVersion uint64
5050
configVersions.ReverseIterate("", "", func(key string, value interface{}) bool {
51-
version, _ := strconv.ParseUint(key, 10, 64)
52-
maxVersion = version
51+
maxVersion = parseUint64(key)
5352
return true
5453
})
5554
return maxVersion
@@ -117,14 +116,14 @@ func reconfigure(
117116
"Reconfigure",
118117
"prevAddr", prevAddr,
119118
"prevRealm", prevPkgPath,
120-
"votingStartDelay", ufmt.Sprintf("%d", config.VotingStartDelay),
121-
"votingPeriod", ufmt.Sprintf("%d", config.VotingPeriod),
122-
"votingWeightSmoothingDuration", ufmt.Sprintf("%d", config.VotingWeightSmoothingDuration),
123-
"quorum", ufmt.Sprintf("%d", config.Quorum),
124-
"proposalCreationThreshold", ufmt.Sprintf("%d", config.ProposalCreationThreshold),
125-
"executionDelay", ufmt.Sprintf("%d", config.ExecutionDelay),
126-
"executionWindow", ufmt.Sprintf("%d", config.ExecutionWindow),
127-
"newConfigVersion", ufmt.Sprintf("%d", newVersion),
119+
"votingStartDelay", formatUint64(config.VotingStartDelay),
120+
"votingPeriod", formatUint64(config.VotingPeriod),
121+
"votingWeightSmoothingDuration", formatUint64(config.VotingWeightSmoothingDuration),
122+
"quorum", formatUint64(config.Quorum),
123+
"proposalCreationThreshold", formatUint64(config.ProposalCreationThreshold),
124+
"executionDelay", formatUint64(config.ExecutionDelay),
125+
"executionWindow", formatUint64(config.ExecutionWindow),
126+
"newConfigVersion", formatUint64(newVersion),
128127
)
129128

130129
return newVersion
@@ -134,7 +133,6 @@ func reconfigure(
134133
// If version is 0, it returns the current configuration.
135134
func GetConfigVersion(version uint64) Config {
136135
en.MintAndDistributeGns()
137-
// updateProposalsState() // do not call this function here, it will cause a init loop in updateProposal()
138136

139137
if version == 0 {
140138
return config

gov/governance/config_test.gno

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ func TestGetConfigVersion(t *testing.T) {
186186

187187
expectedConfig := config
188188
if tt.version != 0 {
189-
// expectedConfig, _ = configVersions.Get(tt.version)
190189
expectedConfig, _ = getConfigByVersion(tt.version)
191190
}
192191
uassert.Equal(t, result.VotingStartDelay, expectedConfig.VotingStartDelay)

gov/governance/execute_test.gno

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,61 @@ package governance
33
import (
44
"errors"
55
"testing"
6+
"time"
67

8+
"gno.land/p/demo/avl"
79
"gno.land/p/demo/uassert"
810
"gno.land/r/gnoswap/v1/consts"
11+
12+
u256 "gno.land/p/gnoswap/uint256"
913
)
1014

15+
func TestParameterRegistry_Register(t *testing.T) {
16+
registry := NewParameterRegistry()
17+
18+
testHandler := func(params []string) error {
19+
return nil
20+
}
21+
22+
tests := []struct {
23+
name string
24+
pkgPath string
25+
function string
26+
handler ParameterHandler
27+
}{
28+
{
29+
name: "pass",
30+
pkgPath: "test/pkg",
31+
function: "testFunc",
32+
handler: testHandler,
33+
},
34+
{
35+
name: "empty pass",
36+
pkgPath: "",
37+
function: "testFunc",
38+
handler: testHandler,
39+
},
40+
}
41+
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
registry.Register(tt.pkgPath, tt.function, tt.handler)
45+
46+
handler, err := registry.Handler(tt.pkgPath, tt.function)
47+
uassert.NoError(t, err)
48+
49+
if handler == nil {
50+
t.Errorf("handler is nil")
51+
}
52+
53+
expectedKey := makeHandlerKey(tt.pkgPath, tt.function)
54+
if _, exists := registry.handlers.Get(expectedKey); !exists {
55+
t.Errorf("expected key %s not found", expectedKey)
56+
}
57+
})
58+
}
59+
}
60+
1161
func TestParameterRegistry(t *testing.T) {
1262
tests := []struct {
1363
name string
@@ -110,8 +160,10 @@ func TestExecuteParameterChange(t *testing.T) {
110160
for _, tt := range tests {
111161
t.Run(tt.name, func(t *testing.T) {
112162
err := executeParameterChange(tt.msgs, registry)
113-
if (err != nil) != tt.wantErr {
114-
t.Errorf("executeParameterChange2() error = %v, wantErr %v", err, tt.wantErr)
163+
if !tt.wantErr {
164+
uassert.NoError(t, err)
165+
} else {
166+
uassert.Error(t, err)
115167
}
116168
})
117169
}

0 commit comments

Comments
 (0)