|
4 | 4 | "crypto/rand" |
5 | 5 | "encoding/binary" |
6 | 6 | eventsdb "github.com/MinterTeam/events-db" |
| 7 | + "github.com/MinterTeam/minter-go-node/core/state/candidates" |
7 | 8 | "github.com/MinterTeam/minter-go-node/core/types" |
| 9 | + "github.com/tendermint/tendermint/crypto/ed25519" |
8 | 10 | db "github.com/tendermint/tm-db" |
9 | 11 | "math/big" |
10 | 12 | "testing" |
@@ -214,6 +216,119 @@ func TestStakeSufficiency(t *testing.T) { |
214 | 216 | } |
215 | 217 | } |
216 | 218 |
|
| 219 | +func TestDoubleSignPenalty(t *testing.T) { |
| 220 | + st := getState() |
| 221 | + |
| 222 | + pubkey := createTestCandidate(st) |
| 223 | + |
| 224 | + coin := types.GetBaseCoin() |
| 225 | + amount := big.NewInt(100) |
| 226 | + var addr types.Address |
| 227 | + binary.BigEndian.PutUint64(addr[:], 1) |
| 228 | + st.Candidates.Delegate(addr, pubkey, coin, amount, big.NewInt(0)) |
| 229 | + |
| 230 | + st.Candidates.RecalculateStakes(1) |
| 231 | + |
| 232 | + var pk ed25519.PubKeyEd25519 |
| 233 | + copy(pk[:], pubkey[:]) |
| 234 | + |
| 235 | + var tmAddr types.TmAddress |
| 236 | + copy(tmAddr[:], pk.Address().Bytes()) |
| 237 | + |
| 238 | + st.Candidates.PunishByzantineCandidate(1, tmAddr) |
| 239 | + |
| 240 | + stake := st.Candidates.GetStakeValueOfAddress(pubkey, addr, coin) |
| 241 | + if stake.Cmp(big.NewInt(0)) != 0 { |
| 242 | + t.Fatalf("Stake is not correct. Expected 0, got %s", stake.String()) |
| 243 | + } |
| 244 | + |
| 245 | + ffs := st.FrozenFunds.GetFrozenFunds(1 + candidates.UnbondPeriod) |
| 246 | + exists := false |
| 247 | + for _, ff := range ffs.List { |
| 248 | + if ff.Address == addr { |
| 249 | + exists = true |
| 250 | + |
| 251 | + newValue := big.NewInt(0).Set(amount) |
| 252 | + newValue.Mul(newValue, big.NewInt(95)) |
| 253 | + newValue.Div(newValue, big.NewInt(100)) |
| 254 | + newValue.Sub(newValue, ff.Value) |
| 255 | + if newValue.Cmp(big.NewInt(0)) != 0 { |
| 256 | + t.Fatalf("Wrong frozen fund value. Expected %s, got %s", newValue.String(), ff.Value.String()) |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + if !exists { |
| 262 | + t.Fatalf("Frozen fund not found") |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +func TestAbsentPenalty(t *testing.T) { |
| 267 | + st := getState() |
| 268 | + |
| 269 | + pubkey := createTestCandidate(st) |
| 270 | + |
| 271 | + coin := types.GetBaseCoin() |
| 272 | + amount := big.NewInt(100) |
| 273 | + var addr types.Address |
| 274 | + binary.BigEndian.PutUint64(addr[:], 1) |
| 275 | + st.Candidates.Delegate(addr, pubkey, coin, amount, big.NewInt(0)) |
| 276 | + |
| 277 | + st.Candidates.RecalculateStakes(1) |
| 278 | + |
| 279 | + var pk ed25519.PubKeyEd25519 |
| 280 | + copy(pk[:], pubkey[:]) |
| 281 | + |
| 282 | + var tmAddr types.TmAddress |
| 283 | + copy(tmAddr[:], pk.Address().Bytes()) |
| 284 | + |
| 285 | + st.Candidates.Punish(1, tmAddr) |
| 286 | + |
| 287 | + stake := st.Candidates.GetStakeValueOfAddress(pubkey, addr, coin) |
| 288 | + newValue := big.NewInt(0).Set(amount) |
| 289 | + newValue.Mul(newValue, big.NewInt(99)) |
| 290 | + newValue.Div(newValue, big.NewInt(100)) |
| 291 | + if stake.Cmp(newValue) != 0 { |
| 292 | + t.Fatalf("Stake is not correct. Expected %s, got %s", newValue, stake.String()) |
| 293 | + } |
| 294 | +} |
| 295 | + |
| 296 | +func TestDoubleAbsentPenalty(t *testing.T) { |
| 297 | + st := getState() |
| 298 | + |
| 299 | + pubkey := createTestCandidate(st) |
| 300 | + |
| 301 | + coin := types.GetBaseCoin() |
| 302 | + amount := big.NewInt(100) |
| 303 | + var addr types.Address |
| 304 | + binary.BigEndian.PutUint64(addr[:], 1) |
| 305 | + st.Candidates.Delegate(addr, pubkey, coin, amount, big.NewInt(0)) |
| 306 | + st.Candidates.SetOnline(pubkey) |
| 307 | + |
| 308 | + st.Candidates.RecalculateStakes(1) |
| 309 | + |
| 310 | + var pk ed25519.PubKeyEd25519 |
| 311 | + copy(pk[:], pubkey[:]) |
| 312 | + |
| 313 | + var tmAddr types.TmAddress |
| 314 | + copy(tmAddr[:], pk.Address().Bytes()) |
| 315 | + |
| 316 | + st.Validators.SetNewValidators(st.Candidates.GetNewCandidates(1)) |
| 317 | + |
| 318 | + for i := 1000; i < 1050; i++ { |
| 319 | + st.Validators.SetValidatorAbsent(uint64(i), tmAddr) |
| 320 | + st.Validators.SetNewValidators(st.Candidates.GetNewCandidates(1)) |
| 321 | + } |
| 322 | + |
| 323 | + stake := st.Candidates.GetStakeValueOfAddress(pubkey, addr, coin) |
| 324 | + newValue := big.NewInt(0).Set(amount) |
| 325 | + newValue.Mul(newValue, big.NewInt(99)) |
| 326 | + newValue.Div(newValue, big.NewInt(100)) |
| 327 | + if stake.Cmp(newValue) != 0 { |
| 328 | + t.Fatalf("Stake is not correct. Expected %s, got %s", newValue, stake.String()) |
| 329 | + } |
| 330 | +} |
| 331 | + |
217 | 332 | func getState() *State { |
218 | 333 | s, err := NewState(0, db.NewMemDB(), emptyEvents{}, 1, 1) |
219 | 334 |
|
|
0 commit comments