|
| 1 | +package governance |
| 2 | + |
| 3 | +import ( |
| 4 | + "std" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "gno.land/p/demo/testutils" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + adminAddr = testutils.TestAddress("admin") |
| 12 | + notAdminAddr = testutils.TestAddress("notadmin") |
| 13 | +) |
| 14 | + |
| 15 | +// we cannot call init() in test, so we mock it here. |
| 16 | +// it's totally same as init() |
| 17 | +func mockInit(t *testing.T) { |
| 18 | + t.Helper() |
| 19 | + config = Config{ |
| 20 | + VotingStartDelay: uint64(86400), |
| 21 | + VotingPeriod: uint64(604800), |
| 22 | + VotingWeightSmoothingDuration: uint64(86400), |
| 23 | + Quorum: uint64(50), |
| 24 | + ProposalCreationThreshold: uint64(1_000_000_000), |
| 25 | + ExecutionDelay: uint64(86400), |
| 26 | + ExecutionWindow: uint64(2592000), |
| 27 | + } |
| 28 | + |
| 29 | + configVersions[uint64(len(configVersions)+1)] = config |
| 30 | +} |
| 31 | + |
| 32 | +func resetGlobalConfig(t *testing.T) { |
| 33 | + t.Helper() |
| 34 | + config = Config{} |
| 35 | + configVersions = make(map[uint64]Config) |
| 36 | +} |
| 37 | + |
| 38 | +func TestInitialConfig(t *testing.T) { |
| 39 | + resetGlobalConfig(t) |
| 40 | + mockInit(t) |
| 41 | + |
| 42 | + tests := []struct { |
| 43 | + name string |
| 44 | + got uint64 |
| 45 | + expected uint64 |
| 46 | + }{ |
| 47 | + {"VotingStartDelay", config.VotingStartDelay, 86400}, |
| 48 | + {"VotingPeriod", config.VotingPeriod, 604800}, |
| 49 | + {"VotingWeightSmoothingDuration", config.VotingWeightSmoothingDuration, 86400}, |
| 50 | + {"Quorum", config.Quorum, 50}, |
| 51 | + {"ProposalCreationThreshold", config.ProposalCreationThreshold, 1_000_000_000}, |
| 52 | + {"ExecutionDelay", config.ExecutionDelay, 86400}, |
| 53 | + {"ExecutionWindow", config.ExecutionWindow, 2592000}, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + if tt.got != tt.expected { |
| 59 | + t.Errorf("Incorrect initial %s: got %d, want %d", |
| 60 | + tt.name, tt.got, tt.expected) |
| 61 | + } |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + if len(configVersions) != 1 { |
| 66 | + t.Errorf("Expected 1 config version, got %d", len(configVersions)) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestReconfigureByAdmin(t *testing.T) { |
| 71 | + resetGlobalConfig(t) |
| 72 | + mockInit(t) |
| 73 | + |
| 74 | + tests := []struct { |
| 75 | + name string |
| 76 | + caller std.Address |
| 77 | + newConfig Config |
| 78 | + expectError bool |
| 79 | + errorContains string |
| 80 | + }{ |
| 81 | + { |
| 82 | + name: "Valid admin reconfiguration", |
| 83 | + caller: adminAddr, |
| 84 | + newConfig: Config{ |
| 85 | + VotingStartDelay: 86400, |
| 86 | + VotingPeriod: 700000, |
| 87 | + VotingWeightSmoothingDuration: 90000, |
| 88 | + Quorum: 60, |
| 89 | + ProposalCreationThreshold: 2_000_000_000, |
| 90 | + ExecutionDelay: 90000, |
| 91 | + ExecutionWindow: 3000000, |
| 92 | + }, |
| 93 | + expectError: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "Non-admin caller", |
| 97 | + caller: testutils.TestAddress("notadmin"), |
| 98 | + newConfig: Config{ |
| 99 | + VotingStartDelay: 86400, |
| 100 | + }, |
| 101 | + expectError: true, |
| 102 | + errorContains: "admin", |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + for _, tt := range tests { |
| 107 | + t.Run(tt.name, func(t *testing.T) { |
| 108 | + var err error |
| 109 | + func() { |
| 110 | + defer func() { |
| 111 | + if r := recover(); r != nil { |
| 112 | + err = r.(error) |
| 113 | + } |
| 114 | + }() |
| 115 | + |
| 116 | + ReconfigureByAdmin( |
| 117 | + tt.newConfig.VotingStartDelay, |
| 118 | + tt.newConfig.VotingPeriod, |
| 119 | + tt.newConfig.VotingWeightSmoothingDuration, |
| 120 | + tt.newConfig.Quorum, |
| 121 | + tt.newConfig.ProposalCreationThreshold, |
| 122 | + tt.newConfig.ExecutionDelay, |
| 123 | + tt.newConfig.ExecutionWindow, |
| 124 | + ) |
| 125 | + }() |
| 126 | + |
| 127 | + if tt.expectError { |
| 128 | + if err == nil { |
| 129 | + t.Errorf("Expected error containing '%s', got none", tt.errorContains) |
| 130 | + } |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + if config.VotingStartDelay != tt.newConfig.VotingStartDelay { |
| 135 | + t.Errorf("VotingStartDelay not updated correctly: got %d, want %d", |
| 136 | + config.VotingStartDelay, tt.newConfig.VotingStartDelay) |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestGetConfigVersion(t *testing.T) { |
| 143 | + // don't call mockInit here. |
| 144 | + resetGlobalConfig(t) |
| 145 | + |
| 146 | + tests := []struct { |
| 147 | + name string |
| 148 | + version uint64 |
| 149 | + expectError bool |
| 150 | + }{ |
| 151 | + { |
| 152 | + name: "Get current config (version 0)", |
| 153 | + version: 0, |
| 154 | + expectError: false, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "Get existing version", |
| 158 | + version: 1, |
| 159 | + expectError: false, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "Get non-existent version", |
| 163 | + version: 999, |
| 164 | + expectError: true, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + for _, tt := range tests { |
| 169 | + t.Run(tt.name, func(t *testing.T) { |
| 170 | + var result Config |
| 171 | + |
| 172 | + if tt.expectError { |
| 173 | + defer func() { |
| 174 | + if r := recover(); r == nil { |
| 175 | + t.Errorf("expected panic, got none") |
| 176 | + } |
| 177 | + }() |
| 178 | + result = GetConfigVersion(tt.version) |
| 179 | + } |
| 180 | + |
| 181 | + expectedConfig := config |
| 182 | + if tt.version != 0 { |
| 183 | + expectedConfig = configVersions[tt.version] |
| 184 | + } |
| 185 | + |
| 186 | + if result.VotingStartDelay != expectedConfig.VotingStartDelay { |
| 187 | + t.Errorf("Config mismatch: got %+v, want %+v", |
| 188 | + result, expectedConfig) |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestReconfigure(t *testing.T) { |
| 195 | + resetGlobalConfig(t) |
| 196 | + mockInit(t) |
| 197 | + |
| 198 | + newConfig := Config{ |
| 199 | + VotingStartDelay: 100000, |
| 200 | + VotingPeriod: 700000, |
| 201 | + VotingWeightSmoothingDuration: 90000, |
| 202 | + Quorum: 60, |
| 203 | + ProposalCreationThreshold: 2_000_000_000, |
| 204 | + ExecutionDelay: 90000, |
| 205 | + ExecutionWindow: 3000000, |
| 206 | + } |
| 207 | + |
| 208 | + initialVersion := GetLatestConfigVersion() |
| 209 | + |
| 210 | + version := reconfigure( |
| 211 | + newConfig.VotingStartDelay, |
| 212 | + newConfig.VotingPeriod, |
| 213 | + newConfig.VotingWeightSmoothingDuration, |
| 214 | + newConfig.Quorum, |
| 215 | + newConfig.ProposalCreationThreshold, |
| 216 | + newConfig.ExecutionDelay, |
| 217 | + newConfig.ExecutionWindow, |
| 218 | + ) |
| 219 | + |
| 220 | + if version != initialVersion+1 { |
| 221 | + t.Errorf("Expected version %d, got %d", initialVersion+1, version) |
| 222 | + } |
| 223 | + |
| 224 | + if config.VotingStartDelay != newConfig.VotingStartDelay { |
| 225 | + t.Errorf("Config not updated correctly") |
| 226 | + } |
| 227 | + |
| 228 | + storedConfig := configVersions[version] |
| 229 | + if storedConfig.VotingStartDelay != newConfig.VotingStartDelay { |
| 230 | + t.Errorf("Config version not stored correctly") |
| 231 | + } |
| 232 | +} |
0 commit comments