diff --git a/main.go b/main.go index 1c27dc0..16c45b9 100644 --- a/main.go +++ b/main.go @@ -312,14 +312,6 @@ func mainLoop(oracleInstance *oracle.Oracle, onchain *oracle.Onchain, cfg *oracl // Every X slots we update the onchain validators and cleanup any stranded oracle validators if oracleInstance.State().LatestProcessedSlot%UpdateValidatorsIntervalSlots == 0 { onchain.RefreshBeaconValidators() - - // Do the validator cleanup: redisitribute the pending rewards of validators subscribed to the pool - // that are not in the beacon chain anymore (e.g. slashed) - err := oracleInstance.ValidatorCleanup(oracleInstance.State().LatestProcessedSlot) - // As precaution, we stop the oracle if anything happened during the cleanup - if err != nil { - log.Fatal("Could not cleanup validators: ", err) - } } // Every CheckPointSizeInSlots we commit the state given some conditions, starting from diff --git a/oracle/oracle.go b/oracle/oracle.go index 9e93819..1b3e5e2 100644 --- a/oracle/oracle.go +++ b/oracle/oracle.go @@ -206,6 +206,16 @@ func (or *Oracle) AdvanceStateToNextSlot(fullBlock *FullBlock) (uint64, error) { // Handle the donations from this block or.handleDonations(blockDonations) + // Handle validator cleanup: redisitribute the pending rewards of validators subscribed to the pool + // that are not in the beacon chain anymore (exited/slashed). We dont run this on every slot because + // its expensive. Runs every 4 hours. + if or.state.NextSlotToProcess%uint64(1200) == 0 { + err = or.ValidatorCleanup(or.state.NextSlotToProcess) + if err != nil { + return 0, errors.Wrap(err, "could not cleanup validators") + } + } + processedSlot := or.state.NextSlotToProcess or.state.LatestProcessedSlot = processedSlot or.state.NextSlotToProcess++ @@ -217,8 +227,6 @@ func (or *Oracle) AdvanceStateToNextSlot(fullBlock *FullBlock) (uint64, error) { // Unsubscribes validators that are not active. Shares their pending rewards to the pool func (or *Oracle) ValidatorCleanup(slot uint64) error { - or.mutex.Lock() - defer or.mutex.Unlock() // Only cleanup if we're past the cleanup slot fork if slot >= SlotFork1[or.cfg.Network] { diff --git a/oracle/oracle_test.go b/oracle/oracle_test.go index c1e3590..9567496 100644 --- a/oracle/oracle_test.go +++ b/oracle/oracle_test.go @@ -2534,7 +2534,6 @@ func Test_ValidatorCleanup_1(t *testing.T) { require.Equal(t, big.NewInt(99), oracle.state.Validators[22].AccumulatedRewardsWei) require.Equal(t, Active, oracle.state.Validators[22].ValidatorStatus) require.Equal(t, big.NewInt(76548235), oracle.state.PoolAccumulatedFees) - require.Equal(t, big.NewInt(76548235), oracle.state.PoolAccumulatedFees) // Test5: log.Info("Test5: Exited validator rewards are reset and go to the rest (including yellow)") @@ -2581,7 +2580,6 @@ func Test_ValidatorCleanup_1(t *testing.T) { require.Equal(t, big.NewInt(99), oracle.state.Validators[22].AccumulatedRewardsWei) require.Equal(t, Active, oracle.state.Validators[22].ValidatorStatus) require.Equal(t, big.NewInt(76548235), oracle.state.PoolAccumulatedFees) - require.Equal(t, big.NewInt(76548235), oracle.state.PoolAccumulatedFees) // Test6: log.Info("Test6: Slashed validator in the beacon chain. Pending goes to the rest") @@ -2628,7 +2626,6 @@ func Test_ValidatorCleanup_1(t *testing.T) { require.Equal(t, big.NewInt(99), oracle.state.Validators[22].AccumulatedRewardsWei) require.Equal(t, Active, oracle.state.Validators[22].ValidatorStatus) require.Equal(t, big.NewInt(76548235), oracle.state.PoolAccumulatedFees) - require.Equal(t, big.NewInt(76548235), oracle.state.PoolAccumulatedFees) // Test7: log.Info("Test7: Exited validator rewards are reset and go to the rest (not red)")