-
Notifications
You must be signed in to change notification settings - Fork 215
Integration test rewards Andromeda #6875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b8c614a
new integration test
miiu96 0c39446
fix import
miiu96 c8fd097
Merge branch 'feat/andromeda-fixes' into integration-test-rewards
miiu96 853973c
add proofs for genesis
miiu96 e01e07a
Merge remote-tracking branch 'origin/integration-test-rewards' into i…
miiu96 79732a6
import
miiu96 e02814e
fixes after review
miiu96 6264b00
fixes after re review
miiu96 9d8c039
refactoring
miiu96 fcbd839
check also values from rewards transactions
miiu96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
210 changes: 210 additions & 0 deletions
210
integrationTests/chainSimulator/rewards/rewards_test.go
This file contains hidden or 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,210 @@ | ||
| package rewards | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "math/big" | ||
| "os" | ||
| "path" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/multiversx/mx-chain-core-go/core" | ||
| apiCore "github.com/multiversx/mx-chain-core-go/data/api" | ||
| "github.com/multiversx/mx-chain-core-go/data/block" | ||
| "github.com/multiversx/mx-chain-go/node/chainSimulator" | ||
| "github.com/multiversx/mx-chain-go/node/chainSimulator/components/api" | ||
| "github.com/multiversx/mx-chain-go/sharding/nodesCoordinator" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultPathToInitialConfig = "../../../cmd/node/config/" | ||
| ) | ||
|
|
||
| func TestRewardsTxsAfterEquivalentMessages(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("this is not a short test") | ||
| } | ||
|
|
||
| startTime := time.Now().Unix() | ||
| roundDurationInMillis := uint64(6000) | ||
| roundsPerEpoch := core.OptionalUint64{ | ||
| HasValue: true, | ||
| Value: 200, | ||
| } | ||
|
|
||
| numOfShards := uint32(3) | ||
|
|
||
| tempDir := t.TempDir() | ||
| cs, err := chainSimulator.NewChainSimulator(chainSimulator.ArgsChainSimulator{ | ||
| BypassTxSignatureCheck: true, | ||
| TempDir: tempDir, | ||
| PathToInitialConfig: defaultPathToInitialConfig, | ||
| NumOfShards: numOfShards, | ||
| GenesisTimestamp: startTime, | ||
| RoundDurationInMillis: roundDurationInMillis, | ||
| RoundsPerEpoch: roundsPerEpoch, | ||
| ApiInterface: api.NewNoApiInterface(), | ||
| MinNodesPerShard: 3, | ||
| MetaChainMinNodes: 3, | ||
| }) | ||
| require.Nil(t, err) | ||
| require.NotNil(t, cs) | ||
| defer cs.Close() | ||
|
|
||
| targetEpoch := 9 | ||
| for i := 0; i < targetEpoch; i++ { | ||
| err = cs.ForceChangeOfEpoch() | ||
| require.Nil(t, err) | ||
| } | ||
|
|
||
| err = cs.GenerateBlocks(210) | ||
| require.Nil(t, err) | ||
|
|
||
| metaFacadeHandler := cs.GetNodeHandler(core.MetachainShardId).GetFacadeHandler() | ||
|
|
||
| nodesSetupFile := path.Join(tempDir, "config", "nodesSetup.json") | ||
| validators, err := readValidatorsAndOwners(nodesSetupFile) | ||
| require.Nil(t, err) | ||
|
|
||
| // find block with rewards transactions, in this range we should find the epoch start block | ||
| var metaBlock *apiCore.Block | ||
| found := false | ||
| for nonce := uint64(210); nonce < 235; nonce++ { | ||
| metaBlock, err = metaFacadeHandler.GetBlockByNonce(nonce, apiCore.BlockQueryOptions{ | ||
| WithTransactions: true, | ||
| }) | ||
| require.Nil(t, err) | ||
|
|
||
| isEpochStart := metaBlock.EpochStartInfo != nil | ||
| if !isEpochStart { | ||
| continue | ||
| } | ||
|
|
||
| found = true | ||
| break | ||
| } | ||
| require.True(t, found) | ||
| require.NotNil(t, metaBlock) | ||
|
|
||
| coordinator := cs.GetNodeHandler(0).GetProcessComponents().NodesCoordinator() | ||
|
|
||
| rewardsPerShard, err := computeRewardsForShards(metaBlock, coordinator, validators) | ||
| require.Nil(t, err) | ||
|
|
||
| for shardID, reward := range rewardsPerShard { | ||
| fmt.Printf("rewards on shard %d: %s\n", shardID, reward.String()) | ||
| } | ||
|
|
||
| require.True(t, allValuesEqual(rewardsPerShard)) | ||
AdoAdoAdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func computeRewardsForShards( | ||
| metaBlock *apiCore.Block, | ||
| coordinator nodesCoordinator.NodesCoordinator, | ||
| validators map[string]string, | ||
| ) (map[uint32]*big.Int, error) { | ||
| shards := []uint32{0, 1, 2, core.MetachainShardId} | ||
| rewardsPerShard := make(map[uint32]*big.Int) | ||
|
|
||
| for _, shardID := range shards { | ||
AdoAdoAdo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| rewardsPerShard[shardID] = big.NewInt(0) // Initialize reward entry | ||
| err := computeRewardsForShard(metaBlock, coordinator, validators, shardID, rewardsPerShard) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| return rewardsPerShard, nil | ||
| } | ||
|
|
||
| func computeRewardsForShard(metaBlock *apiCore.Block, | ||
| coordinator nodesCoordinator.NodesCoordinator, | ||
| validators map[string]string, | ||
| shardID uint32, | ||
| rewardsPerShard map[uint32]*big.Int, | ||
| ) error { | ||
| validatorsPerShard, _ := coordinator.GetAllEligibleValidatorsPublicKeysForShard(8, shardID) | ||
|
|
||
| for _, validator := range validatorsPerShard { | ||
| owner, exists := validators[hex.EncodeToString([]byte(validator))] | ||
| if !exists { | ||
| continue | ||
| } | ||
| err := accumulateShardRewards(metaBlock, shardID, owner, rewardsPerShard) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func accumulateShardRewards(metaBlock *apiCore.Block, shardID uint32, owner string, rewardsPerShard map[uint32]*big.Int) error { | ||
| var firstValue *big.Int | ||
| for _, mb := range metaBlock.MiniBlocks { | ||
| if mb.Type != block.RewardsBlock.String() { | ||
| continue | ||
| } | ||
|
|
||
| for _, tx := range mb.Transactions { | ||
| if tx.Receiver != owner { | ||
| continue | ||
| } | ||
|
|
||
| valueBig, _ := new(big.Int).SetString(tx.Value, 10) | ||
| if firstValue == nil { | ||
| firstValue = valueBig | ||
| } | ||
| if valueBig.Cmp(firstValue) != 0 { | ||
| return errors.New("different values in rewards transactions") | ||
| } | ||
|
|
||
| rewardsPerShard[shardID].Add(rewardsPerShard[shardID], valueBig) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func readValidatorsAndOwners(filePath string) (map[string]string, error) { | ||
| file, err := os.ReadFile(filePath) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var nodesSetup struct { | ||
| InitialNodes []struct { | ||
| PubKey string `json:"pubkey"` | ||
| Address string `json:"address"` | ||
| } `json:"initialNodes"` | ||
| } | ||
|
|
||
| err = json.Unmarshal(file, &nodesSetup) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| validators := make(map[string]string) | ||
| for _, node := range nodesSetup.InitialNodes { | ||
| validators[node.PubKey] = node.Address | ||
| } | ||
|
|
||
| return validators, nil | ||
| } | ||
|
|
||
| func allValuesEqual(m map[uint32]*big.Int) bool { | ||
| if len(m) == 0 { | ||
| return true | ||
| } | ||
| expectedValue := m[0] | ||
| for _, v := range m { | ||
| if expectedValue.Cmp(v) != 0 { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we consider this a long test as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, changed