Skip to content

Commit

Permalink
change vote type
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Dec 26, 2024
1 parent 4af3dee commit 9deb288
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 45 deletions.
45 changes: 35 additions & 10 deletions gov/governance/config.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package governance

import (
"std"
"strconv"

"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"

"gno.land/r/gnoswap/v1/common"
Expand All @@ -11,7 +13,8 @@ import (

var (
config Config
configVersions = make(map[uint64]Config)
// configVersions = make(map[uint64]Config)
configVersions = avl.NewTree() // version -> Config
)

func init() {
Expand All @@ -28,7 +31,29 @@ func init() {

// config version 0 should return the current config
// therefore we set initial config version to 1
configVersions[uint64(len(configVersions)+1)] = config
setConfigVersion(1, config)
}

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

func getConfigByVersion(v uint64) (Config, bool) {
value, exists := configVersions.Get(strconv.Itoa(int(v)))
if !exists {
return Config{}, false
}
return value.(Config), true
}

func getLatestVersion() uint64 {
var maxVersion uint64
configVersions.ReverseIterate("", "", func(key string, value interface{}) bool {
version, _ := strconv.ParseUint(key, 10, 64)
maxVersion = version
return true
})
return maxVersion
}

// ReconfigureByAdmin updates the proposal realted configuration.
Expand All @@ -52,7 +77,7 @@ func ReconfigureByAdmin(
en.MintAndDistributeGns()
updateProposalsState()

newVersion := uint64(len(configVersions) + 1)
newVersion := getLatestVersion() + 1

config = Config{
VotingStartDelay: votingStartDelay,
Expand All @@ -63,7 +88,7 @@ func ReconfigureByAdmin(
ExecutionDelay: executionDelay,
ExecutionWindow: executionWindow,
}
configVersions[newVersion] = config
setConfigVersion(newVersion, config)

prevAddr, prevRealm := getPrev()
std.Emit(
Expand Down Expand Up @@ -100,7 +125,7 @@ func reconfigure(
en.MintAndDistributeGns()
updateProposalsState()

newVersion := uint64(len(configVersions) + 1)
newVersion := getLatestVersion() + 1

config = Config{
VotingStartDelay: votingStartDelay,
Expand All @@ -111,7 +136,7 @@ func reconfigure(
ExecutionDelay: executionDelay,
ExecutionWindow: executionWindow,
}
configVersions[newVersion] = config
setConfigVersion(newVersion, config)

prevAddr, prevRealm := getPrev()
std.Emit(
Expand Down Expand Up @@ -141,15 +166,15 @@ func GetConfigVersion(version uint64) Config {
return config
}

configValue, exist := configVersions[version]
if !exist {
cfg, exists := getConfigByVersion(version)
if !exists {
panic(addDetailToError(
errDataNotFound,
ufmt.Sprintf("config version(%d) does not exist", version),
))
}

return configValue
return cfg
}

// GetLatestConfig() returns the latest configuration.
Expand All @@ -159,7 +184,7 @@ func GetLatestConfig() Config {

// GetLatestConfigVersion() returns the latest configuration version.
func GetLatestConfigVersion() uint64 {
return uint64(len(configVersions))
return getLatestVersion()
}

// GetProposalCreationThreshold() returns the current proposal creation threshold.
Expand Down
16 changes: 11 additions & 5 deletions gov/governance/config_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"std"
"testing"

"gno.land/p/demo/avl"
"gno.land/p/demo/testutils"
"gno.land/p/demo/uassert"
"gno.land/p/demo/ufmt"
)

var (
Expand All @@ -27,13 +29,16 @@ func mockInit(t *testing.T) {
ExecutionWindow: uint64(2592000),
}

configVersions[uint64(len(configVersions)+1)] = config
// configVersions[uint64(len(configVersions)+1)] = config
configVersions = avl.NewTree()
setConfigVersion(1, config)
}

func resetGlobalConfig(t *testing.T) {
t.Helper()
config = Config{}
configVersions = make(map[uint64]Config)
// configVersions = make(map[uint64]Config)
configVersions = avl.NewTree()
}

func newConfigObj(t *testing.T) Config {
Expand Down Expand Up @@ -72,7 +77,7 @@ func TestInitialConfig(t *testing.T) {
})
}

uassert.Equal(t, len(configVersions), 1)
uassert.Equal(t, configVersions.Size(), 1)
}

func TestReconfigureByAdmin(t *testing.T) {
Expand Down Expand Up @@ -181,7 +186,8 @@ func TestGetConfigVersion(t *testing.T) {

expectedConfig := config
if tt.version != 0 {
expectedConfig = configVersions[tt.version]
// expectedConfig, _ = configVersions.Get(tt.version)
expectedConfig, _ = getConfigByVersion(tt.version)
}
uassert.Equal(t, result.VotingStartDelay, expectedConfig.VotingStartDelay)
})
Expand Down Expand Up @@ -209,6 +215,6 @@ func TestReconfigure(t *testing.T) {
uassert.Equal(t, version, initialVersion+1)
uassert.Equal(t, config.VotingStartDelay, newConfig.VotingStartDelay)

storedConfig := configVersions[version]
storedConfig, _ := getConfigByVersion(version)
uassert.Equal(t, storedConfig.VotingStartDelay, newConfig.VotingStartDelay)
}
18 changes: 5 additions & 13 deletions gov/governance/proposal.gno
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import (

var (
proposalId uint64
// proposals = make(map[uint64]ProposalInfo) // proposalId -> ProposalInfo
// latestProposalByProposer = make(map[std.Address]uint64) // proposer -> proposalId
proposals = avl.NewTree()
latestProposalByProposer = avl.NewTree()
proposals = avl.NewTree() // proposalId -> ProposalInfo
latestProposalByProposer = avl.NewTree() // proposer -> proposalId
)

const (
Expand Down Expand Up @@ -68,15 +66,13 @@ func ProposeText(
Nay: u256.Zero(),
MaxVotingWeight: u256.NewUint(votingMax),
PossibleAddressWithWeight: possibleAddressWithWeight,
ConfigVersion: uint64(len(configVersions)), // use latest config version
ConfigVersion: uint64(configVersions.Size()), // use latest config version
QuorumAmount: xgns.VotingSupply() * config.Quorum / 100,
Title: title,
Description: description,
}

proposalId++
// proposals[proposalId] = proposal
// latestProposalByProposer[proposer] = proposalId
proposals.Set(strconv.Itoa(int(proposalId)), proposal)
latestProposalByProposer.Set(proposer.String(), proposalId)

Expand Down Expand Up @@ -134,7 +130,7 @@ func ProposeCommunityPoolSpend(
Nay: u256.Zero(),
MaxVotingWeight: u256.NewUint(votingMax),
PossibleAddressWithWeight: possibleAddressWithWeight,
ConfigVersion: uint64(len(configVersions)),
ConfigVersion: uint64(configVersions.Size()),
QuorumAmount: xgns.VotingSupply() * config.Quorum / 100,
Title: title,
Description: description,
Expand All @@ -146,8 +142,6 @@ func ProposeCommunityPoolSpend(
}

proposalId++
// proposals[proposalId] = proposal
// latestProposalByProposer[proposer] = proposalId
proposals.Set(strconv.Itoa(int(proposalId)), proposal)
latestProposalByProposer.Set(proposer.String(), proposalId)

Expand Down Expand Up @@ -223,7 +217,7 @@ func ProposeParameterChange(
Nay: u256.Zero(),
MaxVotingWeight: u256.NewUint(votingMax),
PossibleAddressWithWeight: possibleAddressWithWeight,
ConfigVersion: uint64(len(configVersions)),
ConfigVersion: uint64(configVersions.Size()),
QuorumAmount: xgns.VotingSupply() * config.Quorum / 100,
Title: title,
Description: description,
Expand All @@ -234,8 +228,6 @@ func ProposeParameterChange(
}

proposalId++
// proposals[proposalId] = proposal
// latestProposalByProposer[proposer] = proposalId
proposals.Set(strconv.Itoa(int(proposalId)), proposal)
latestProposalByProposer.Set(proposer.String(), proposalId)

Expand Down
13 changes: 6 additions & 7 deletions gov/governance/proposal_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,13 @@ func TestProposeCommunityPoolSpend(t *testing.T) {

func TestUpdateProposalsState(t *testing.T) {
baseTime := uint64(1000)
configVersions = map[uint64]Config{
1: {
VotingStartDelay: 50,
VotingPeriod: 100,
ExecutionDelay: 50,
ExecutionWindow: 100,
},
newConfig := Config{
VotingStartDelay: 50,
VotingPeriod: 100,
ExecutionDelay: 50,
ExecutionWindow: 100,
}
setConfigVersion(1, newConfig)

testCases := []struct {
name string
Expand Down
24 changes: 14 additions & 10 deletions gov/governance/vote_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ func TestVote(t *testing.T) {
baseTime := uint64(time.Now().Unix())
voter := testutils.TestAddress("voter")

configVersions = map[uint64]Config{
1: {
VotingStartDelay: 50,
VotingPeriod: 100,
},
newConfig := Config{
VotingStartDelay: 50,
VotingPeriod: 100,
}
setConfigVersion(1, newConfig)

testCases := []struct {
name string
Expand Down Expand Up @@ -203,12 +202,17 @@ func TestNewVoteState(t *testing.T) {
voteKey := createVoteKey(1, voter.String())

// test config
configVersions = map[uint64]Config{
1: {
VotingStartDelay: 50,
VotingPeriod: 100,
},
// configVersions = map[uint64]Config{
// 1: {
// VotingStartDelay: 50,
// VotingPeriod: 100,
// },
// }
newConfig := Config{
VotingStartDelay: 50,
VotingPeriod: 100,
}
setConfigVersion(1, newConfig)

tests := []struct {
name string
Expand Down

0 comments on commit 9deb288

Please sign in to comment.