Skip to content

Commit

Permalink
fix wired bug
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Jan 8, 2025
1 parent 13ed00d commit 33cb763
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 176 deletions.
13 changes: 8 additions & 5 deletions gov/governance/api.gno
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,19 @@ func getProposalStatus(id uint64) string {
if !exist {
return ""
}

proposal := prop.(ProposalInfo)

config := GetConfigVersion(proposal.ConfigVersion)

votingStart := proposal.State.CreatedAt + config.VotingStartDelay
votingEnd := votingStart + config.VotingPeriod

state := proposal.State
proposalObj := json.Builder().
node := createProposalStateNode(proposal.State, votingStart, votingEnd)
return marshal(node)
}

func createProposalStateNode(state ProposalState, votingStart, votingEnd uint64) *json.Node {
return json.Builder().
WriteString("createdAt", formatUint64(state.CreatedAt)).
WriteString("upcoming", formatBool(state.Upcoming)).
WriteString("active", formatBool(state.Active)).
Expand All @@ -222,8 +227,6 @@ func getProposalStatus(id uint64) string {
WriteString("expired", formatBool(state.Expired)).
WriteString("expiredAt", formatUint64(state.ExpiredAt)).
Node()

return marshal(proposalObj)
}

// getProposalVotes returns the votes of a proposal.
Expand Down
5 changes: 2 additions & 3 deletions gov/governance/config.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package governance

import (
"std"
"strconv"

"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
Expand Down Expand Up @@ -34,11 +33,11 @@ func init() {
}

func setConfigVersion(v uint64, cfg Config) {
configVersions.Set(strconv.Itoa(int(v)), cfg)
configVersions.Set(formatUint64(v), cfg)
}

func getConfigByVersion(v uint64) (Config, bool) {
value, exists := configVersions.Get(strconv.Itoa(int(v)))
value, exists := configVersions.Get(formatUint64(v))
if !exists {
return Config{}, false
}
Expand Down
12 changes: 9 additions & 3 deletions gov/governance/execute.gno
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ type ExecutionContext struct {
WindowEnd uint64
}

func (e *ExecutionContext) String() string {
return ufmt.Sprintf(
"ProposalId: %d, Now: %d, Config: %v, Proposal: %v, WindowStart: %d, WindowEnd: %d",
e.ProposalId, e.Now, e.Config, e.Proposal, e.WindowStart, e.WindowEnd,
)
}

func Execute(proposalId uint64) error {
ctx, err := prepareExecution(proposalId)
if err != nil {
Expand All @@ -89,9 +96,8 @@ func Execute(proposalId uint64) error {
}

registry := createParameterHandlers()
// TODO: must fix this
if err := executeProposal(ctx, registry); err != nil {
panic(err)
return err
}

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

func executeProposal(ctx *ExecutionContext, registry *ParameterRegistry) error {
switch ctx.Proposal.ProposalType {
case ParameterChange:
case ParameterChange, CommunityPoolSpend:
return executeParameterChange(ctx.Proposal.Execution.Msgs, registry)
default:
return errUnsupportedProposalType
Expand Down
59 changes: 7 additions & 52 deletions gov/governance/getter_vote.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,32 @@ package governance

import (
"std"
"strconv"

"gno.land/p/demo/ufmt"

"gno.land/r/gnoswap/v1/common"
)

// Helper function to validate and get vote information
func getVoteInfoFromKey(voteKey string) (voteWithWeight, bool) {
_, exists := votes.Get(voteKey)
if !exists {
panic(addDetailToError(
errDataNotFound,
ufmt.Sprintf("voteKey(%s) not found", voteKey),
))
}

pid, addr := divideVoteKeyToProposalIdAndUser(voteKey)

voteInfo, exists := getUserVote(addr, pid)
if !exists {
panic(addDetailToError(
errDataNotFound,
ufmt.Sprintf("voteKey(%s) not found", voteKey),
))
}

return voteInfo, true
}

func GetVoteByVoteKey(voteKey string) bool {
vote, exist := votes.Get(voteKey)
if !exist {
panic(addDetailToError(
errDataNotFound,
ufmt.Sprintf("voteKey(%s) not found", voteKey),
))
}

return vote.(bool)
return mustGetVote(voteKey)
}

func GetVoteYesByVoteKey(voteKey string) bool {
voteInfo, _ := getVoteInfoFromKey(voteKey)
return voteInfo.Yes
return mustGetVoteInfo(voteKey).Yes
}

func GetVoteWeightByVoteKey(voteKey string) uint64 {
voteInfo, _ := getVoteInfoFromKey(voteKey)
return voteInfo.Weight
return mustGetVoteInfo(voteKey).Weight
}

func GetVotedHeightByVoteKey(voteKey string) uint64 {
voteInfo, _ := getVoteInfoFromKey(voteKey)
return voteInfo.VotedHeight
return mustGetVoteInfo(voteKey).VotedHeight
}

func GetVotedAtByVoteKey(voteKey string) uint64 {
voteInfo, _ := getVoteInfoFromKey(voteKey)
return voteInfo.VotedAt
return mustGetVoteInfo(voteKey).VotedAt
}

func divideVoteKeyToProposalIdAndUser(voteKey string) (uint64, std.Address) {
func divideVoteKeyToProposalIdAndUser(voteKey string) (proposalId uint64, user std.Address) {
parts, err := common.Split(voteKey, ":", 2)
if err != nil {
panic(addDetailToError(
Expand All @@ -73,13 +36,5 @@ func divideVoteKeyToProposalIdAndUser(voteKey string) (uint64, std.Address) {
))
}

proposalId, err := strconv.ParseUint(parts[0], 10, 64)
if err != nil {
panic(addDetailToError(
errInvalidInput,
ufmt.Sprintf("proposalId(%s) is invalid", parts[0]),
))
}

return proposalId, std.Address(parts[1])
return parseUint64(parts[0]), std.Address(parts[1])
}
7 changes: 3 additions & 4 deletions gov/governance/proposal.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package governance

import (
"std"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -73,7 +72,7 @@ func ProposeText(
}

proposalId++
proposals.Set(strconv.Itoa(int(proposalId)), proposal)
proposals.Set(formatUint64(proposalId), proposal)
latestProposalByProposer.Set(proposer.String(), proposalId)

prevAddr, prevPkgPath := getPrev()
Expand Down Expand Up @@ -145,7 +144,7 @@ func ProposeCommunityPoolSpend(
}

proposalId++
proposals.Set(strconv.Itoa(int(proposalId)), proposal)
proposals.Set(formatUint64(proposalId), proposal)
latestProposalByProposer.Set(proposer.String(), proposalId)

prevAddr, prevRealm := getPrev()
Expand Down Expand Up @@ -231,7 +230,7 @@ func ProposeParameterChange(
}

proposalId++
proposals.Set(strconv.Itoa(int(proposalId)), proposal)
proposals.Set(formatUint64(proposalId), proposal)
latestProposalByProposer.Set(proposer.String(), proposalId)

prevAddr, prevRealm := getPrev()
Expand Down
14 changes: 7 additions & 7 deletions gov/governance/proposal_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestProposeText(t *testing.T) {

uassert.NoError(t, err)

prop, exists := proposals.Get(strconv.Itoa(int(pid)))
prop, exists := proposals.Get(formatUint64(pid))
uassert.True(t, exists)

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

uassert.NoError(t, err)

prop, exists := proposals.Get(strconv.Itoa(int(pid)))
prop, exists := proposals.Get(formatUint64(pid))
uassert.True(t, exists)

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

testCases := []struct {
tests := []struct {
name string
currentTime uint64
setupProposal func(uint64) ProposalInfo
Expand Down Expand Up @@ -279,16 +279,16 @@ func TestUpdateProposalsState(t *testing.T) {
},
}

for _, tt := range testCases {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
proposals = avl.NewTree()

proposal := tt.setupProposal(tt.currentTime)
proposals.Set(strconv.Itoa(int(1)), proposal)
proposals.Set(formatUint64(1), proposal)

updateProposalsState()

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

uassert.NoError(t, err)

prop, exists := proposals.Get(strconv.Itoa(int(pid)))
prop, exists := proposals.Get(formatUint64(pid))
uassert.True(t, exists)

proposal := prop.(ProposalInfo)
Expand Down
Loading

0 comments on commit 33cb763

Please sign in to comment.