|
6 | 6 | package energy |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "errors" |
9 | 10 | "math" |
10 | 11 | "math/big" |
11 | 12 | "testing" |
@@ -269,3 +270,92 @@ func TestCalculateRewards(t *testing.T) { |
269 | 270 | assert.NoError(t, err) |
270 | 271 | assert.Equal(t, big.NewInt(121432908318154219), reward) |
271 | 272 | } |
| 273 | + |
| 274 | +func TestDistributeRewards(t *testing.T) { |
| 275 | + st := state.New(muxdb.NewMem(), trie.Root{}) |
| 276 | + |
| 277 | + beneficiary := thor.BytesToAddress([]byte("beneficiary")) |
| 278 | + signer := thor.BytesToAddress([]byte("signer")) |
| 279 | + stargateAddr := thor.BytesToAddress([]byte("stargate")) |
| 280 | + energyAddr := thor.BytesToAddress([]byte("eng")) |
| 281 | + |
| 282 | + paramsAddr := thor.BytesToAddress([]byte("par")) |
| 283 | + p := params.New(paramsAddr, st) |
| 284 | + |
| 285 | + st.SetStorage(paramsAddr, thor.KeyValidatorRewardPercentage, thor.BytesToBytes32(big.NewInt(int64(thor.InitialValidatorRewardPercentage)).Bytes())) |
| 286 | + st.SetStorage(paramsAddr, thor.KeyStargateContractAddress, thor.BytesToBytes32(stargateAddr.Bytes())) |
| 287 | + |
| 288 | + eng := New(thor.BytesToAddress([]byte("eng")), st, 1, p) |
| 289 | + |
| 290 | + stake := big.NewInt(0).Mul(big.NewInt(25), big.NewInt(1e18)) |
| 291 | + expectedReward := big.NewInt(121765601217656012) |
| 292 | + |
| 293 | + expectedBeneficiaryReward := big.NewInt(0).Mul(expectedReward, big.NewInt(3)) |
| 294 | + expectedBeneficiaryReward = big.NewInt(0).Div(expectedBeneficiaryReward, big.NewInt(10)) |
| 295 | + |
| 296 | + tests := []struct { |
| 297 | + name string |
| 298 | + hasDelegations bool |
| 299 | + expectedErr error |
| 300 | + expectedBeneficiaryReward *big.Int |
| 301 | + expectedDelegatorReward *big.Int |
| 302 | + expectedIssued *big.Int |
| 303 | + }{ |
| 304 | + { |
| 305 | + name: "No delegations - full reward to beneficiary", |
| 306 | + hasDelegations: false, |
| 307 | + expectedErr: nil, |
| 308 | + expectedBeneficiaryReward: expectedReward, |
| 309 | + expectedDelegatorReward: big.NewInt(0), |
| 310 | + expectedIssued: expectedReward, |
| 311 | + }, |
| 312 | + { |
| 313 | + name: "With delegations - split reward", |
| 314 | + hasDelegations: true, |
| 315 | + expectedErr: nil, |
| 316 | + expectedBeneficiaryReward: expectedBeneficiaryReward, |
| 317 | + expectedDelegatorReward: new(big.Int).Sub(expectedReward, expectedBeneficiaryReward), |
| 318 | + expectedIssued: expectedReward, |
| 319 | + }, |
| 320 | + { |
| 321 | + name: "With delegations - IncreaseDelegatorsReward fails", |
| 322 | + hasDelegations: true, |
| 323 | + expectedErr: errors.New("increase reward failed"), |
| 324 | + expectedBeneficiaryReward: big.NewInt(0), |
| 325 | + expectedDelegatorReward: big.NewInt(0), |
| 326 | + expectedIssued: big.NewInt(0), |
| 327 | + }, |
| 328 | + } |
| 329 | + |
| 330 | + for _, tt := range tests { |
| 331 | + t.Run(tt.name, func(t *testing.T) { |
| 332 | + // Reset state for each test |
| 333 | + st.SetEnergy(beneficiary, big.NewInt(0), 1) |
| 334 | + st.SetEnergy(stargateAddr, big.NewInt(0), 1) |
| 335 | + st.SetStorage(energyAddr, issuedKey, thor.Bytes32{}) |
| 336 | + |
| 337 | + mockStaker := &mockStaker{ |
| 338 | + lockedVET: stake, |
| 339 | + hasDelegations: tt.hasDelegations, |
| 340 | + increaseRewardErr: tt.expectedErr, |
| 341 | + } |
| 342 | + |
| 343 | + err := eng.DistributeRewards(beneficiary, signer, mockStaker) |
| 344 | + assert.Equal(t, tt.expectedErr, err) |
| 345 | + |
| 346 | + beneficiaryEnergy, err := eng.Get(beneficiary) |
| 347 | + assert.NoError(t, err) |
| 348 | + assert.Equal(t, tt.expectedBeneficiaryReward, beneficiaryEnergy) |
| 349 | + |
| 350 | + if tt.hasDelegations { |
| 351 | + stargateEnergy, err := eng.Get(stargateAddr) |
| 352 | + assert.NoError(t, err) |
| 353 | + assert.Equal(t, tt.expectedDelegatorReward, stargateEnergy) |
| 354 | + } |
| 355 | + |
| 356 | + issued, err := eng.getIssued() |
| 357 | + assert.NoError(t, err) |
| 358 | + assert.Equal(t, tt.expectedIssued, issued) |
| 359 | + }) |
| 360 | + } |
| 361 | +} |
0 commit comments