-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added reward/penalties processing (#6819)
- Loading branch information
1 parent
69e8ac0
commit 64ddd9f
Showing
15 changed files
with
231 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
cmd/erigon-cl/core/transition/process_rewards_and_penalties.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package transition | ||
|
||
// ProcessRewardsAndPenalties applies rewards/penalties accumulated during previous epoch. | ||
func (s *StateTransistor) ProcessRewardsAndPenalties() error { | ||
// Get deltas for epoch transition. | ||
deltas, err := s.state.BalanceDeltas() | ||
if err != nil { | ||
return err | ||
} | ||
// Apply deltas | ||
for index, delta := range deltas { | ||
if delta > 0 { | ||
// Increment | ||
if err := s.state.IncreaseBalance(index, uint64(delta)); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
// Decrease balance | ||
if err := s.state.DecreaseBalance(index, uint64(-delta)); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
37 changes: 37 additions & 0 deletions
37
cmd/erigon-cl/core/transition/process_rewards_and_penalties_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package transition_test | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/ledgerwatch/erigon/cl/clparams" | ||
"github.com/ledgerwatch/erigon/cl/utils" | ||
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/state" | ||
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/transition" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
//go:embed test_data/rewards_finality_test_expected.ssz_snappy | ||
var expectedState []byte | ||
|
||
//go:embed test_data/rewards_finality_test_state.ssz_snappy | ||
var startingState []byte | ||
|
||
func TestProcessRewardsAndPenalties(t *testing.T) { | ||
// Load test states. | ||
testState := state.New(&clparams.MainnetBeaconConfig) | ||
require.NoError(t, utils.DecodeSSZSnappyWithVersion(testState, startingState, int(clparams.BellatrixVersion))) | ||
expected := state.New(&clparams.MainnetBeaconConfig) | ||
require.NoError(t, utils.DecodeSSZSnappyWithVersion(expected, expectedState, int(clparams.BellatrixVersion))) | ||
// Make up state transistor | ||
s := transition.New(testState, &clparams.MainnetBeaconConfig, nil, false) | ||
// Do processing | ||
require.NoError(t, s.ProcessRewardsAndPenalties()) | ||
// Now compare if the two states are the same by taking their root and comparing. | ||
haveRoot, err := testState.HashSSZ() | ||
require.NoError(t, err) | ||
expectedRoot, err := testState.HashSSZ() | ||
require.NoError(t, err) | ||
// Lastly compare | ||
require.Equal(t, expectedRoot, haveRoot) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.