-
Notifications
You must be signed in to change notification settings - Fork 197
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
Staking V4: Refactor stakingDataProvider to pre-fill auction data #4155
Merged
mariusmihaic
merged 12 commits into
feat/liquid-staking
from
refactor-stakingv4-auction-data-preparation
Jun 16, 2022
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3dd4804
FEAT: First version, failing tests
mariusmihaic 0e54a39
FIX: Broken tests
mariusmihaic 2aa03c8
FIX: Refactor 1
mariusmihaic cc06ceb
FIX: Refactor 2
mariusmihaic 50ade61
FEAT: Unit tests
mariusmihaic 0e74cb5
FIX: Small fixes
mariusmihaic e8ad3d6
Merge branch 'EN-12196-implement-soft-auction' into refactor-stakingv…
mariusmihaic 9d99f23
FIX: Merge conflict
mariusmihaic 5965872
FIX: Auction list init
mariusmihaic f462c41
Merge branch 'feat/liquid-staking' into refactor-stakingv4-auction-da…
mariusmihaic 83df95f
Merge branch 'feat/liquid-staking' into refactor-stakingv4-auction-da…
mariusmihaic 1f0d05e
FIX: Review findings
mariusmihaic File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package epochStart | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ElrondNetwork/elrond-go/state" | ||
) | ||
|
||
// OwnerData is a struct containing relevant information about owner's nodes data | ||
type OwnerData struct { | ||
NumStakedNodes int64 | ||
NumActiveNodes int64 | ||
NumAuctionNodes int64 | ||
TotalTopUp *big.Int | ||
TopUpPerNode *big.Int | ||
AuctionList []state.ValidatorInfoHandler | ||
Qualified bool | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package metachain | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"math" | ||
"math/big" | ||
|
@@ -16,11 +15,11 @@ import ( | |
"github.com/ElrondNetwork/elrond-go/state" | ||
) | ||
|
||
type ownerData struct { | ||
type ownerAuctionData struct { | ||
numStakedNodes int64 | ||
numActiveNodes int64 | ||
numAuctionNodes int64 | ||
numQualifiedAuctionNodes int64 | ||
numStakedNodes int64 | ||
totalTopUp *big.Int | ||
topUpPerNode *big.Int | ||
qualifiedTopUpPerNode *big.Int | ||
|
@@ -137,23 +136,20 @@ func checkNilArgs(args AuctionListSelectorArgs) error { | |
// to common.SelectNodesFromAuctionList | ||
func (als *auctionListSelector) SelectNodesFromAuctionList( | ||
validatorsInfoMap state.ShardValidatorsInfoMapHandler, | ||
unqualifiedOwners map[string]struct{}, | ||
randomness []byte, | ||
) error { | ||
if len(randomness) == 0 { | ||
return process.ErrNilRandSeed | ||
} | ||
|
||
ownersData, auctionListSize, currNumOfValidators, err := als.getAuctionDataAndNumOfValidators(validatorsInfoMap, unqualifiedOwners) | ||
if err != nil { | ||
return err | ||
} | ||
ownersData, auctionListSize := als.getAuctionData() | ||
if auctionListSize == 0 { | ||
log.Info("auctionListSelector.SelectNodesFromAuctionList: empty auction list; skip selection") | ||
return nil | ||
} | ||
|
||
currNodesConfig := als.nodesConfigProvider.GetCurrentNodesConfig() | ||
currNumOfValidators := als.stakingDataProvider.GetNumOfValidatorsInCurrentEpoch() | ||
numOfShuffledNodes := currNodesConfig.NodesToShufflePerShard * (als.shardCoordinator.NumberOfShards() + 1) | ||
numOfValidatorsAfterShuffling, err := safeSub(currNumOfValidators, numOfShuffledNodes) | ||
if err != nil { | ||
|
@@ -198,107 +194,34 @@ func (als *auctionListSelector) SelectNodesFromAuctionList( | |
return als.sortAuctionList(ownersData, numOfAvailableNodeSlots, validatorsInfoMap, randomness) | ||
} | ||
|
||
func (als *auctionListSelector) getAuctionDataAndNumOfValidators( | ||
validatorsInfoMap state.ShardValidatorsInfoMapHandler, | ||
unqualifiedOwners map[string]struct{}, | ||
) (map[string]*ownerData, uint32, uint32, error) { | ||
ownersData := make(map[string]*ownerData) | ||
numOfValidators := uint32(0) | ||
func (als *auctionListSelector) getAuctionData() (map[string]*ownerAuctionData, uint32) { | ||
ownersData := make(map[string]*ownerAuctionData) | ||
numOfNodesInAuction := uint32(0) | ||
|
||
for _, validator := range validatorsInfoMap.GetAllValidatorsInfo() { | ||
blsKey := validator.GetPublicKey() | ||
owner, err := als.stakingDataProvider.GetBlsKeyOwner(blsKey) | ||
if err != nil { | ||
return nil, 0, 0, err | ||
} | ||
|
||
if isInAuction(validator) { | ||
_, isUnqualified := unqualifiedOwners[owner] | ||
if isUnqualified { | ||
log.Debug("auctionListSelector: found node in auction with unqualified owner, do not add it to selection", | ||
"owner", hex.EncodeToString([]byte(owner)), | ||
"bls key", hex.EncodeToString(blsKey), | ||
) | ||
continue | ||
for owner, ownerData := range als.stakingDataProvider.GetOwnersData() { | ||
if ownerData.Qualified && ownerData.NumAuctionNodes > 0 { | ||
ownersData[owner] = &ownerAuctionData{ | ||
numActiveNodes: ownerData.NumActiveNodes, | ||
numAuctionNodes: ownerData.NumAuctionNodes, | ||
numQualifiedAuctionNodes: ownerData.NumAuctionNodes, | ||
numStakedNodes: ownerData.NumStakedNodes, | ||
totalTopUp: ownerData.TotalTopUp, | ||
topUpPerNode: ownerData.TopUpPerNode, | ||
qualifiedTopUpPerNode: ownerData.TopUpPerNode, | ||
auctionList: make([]state.ValidatorInfoHandler, len(ownerData.AuctionList)), | ||
} | ||
|
||
err = als.addOwnerData(owner, validator, ownersData) | ||
if err != nil { | ||
return nil, 0, 0, err | ||
} | ||
|
||
numOfNodesInAuction++ | ||
continue | ||
} | ||
if isValidator(validator) { | ||
numOfValidators++ | ||
copy(ownersData[owner].auctionList, ownerData.AuctionList) | ||
numOfNodesInAuction += uint32(ownerData.NumAuctionNodes) | ||
} | ||
} | ||
|
||
return ownersData, numOfNodesInAuction, numOfValidators, nil | ||
return ownersData, numOfNodesInAuction | ||
} | ||
|
||
func isInAuction(validator state.ValidatorInfoHandler) bool { | ||
return validator.GetList() == string(common.AuctionList) | ||
} | ||
|
||
func (als *auctionListSelector) addOwnerData( | ||
owner string, | ||
validator state.ValidatorInfoHandler, | ||
ownersData map[string]*ownerData, | ||
) error { | ||
ownerPubKey := []byte(owner) | ||
validatorPubKey := validator.GetPublicKey() | ||
stakedNodes, err := als.stakingDataProvider.GetNumStakedNodes(ownerPubKey) | ||
if err != nil { | ||
return fmt.Errorf("auctionListSelector.addOwnerData: error getting num staked nodes: %w, owner: %s, node: %s", | ||
err, | ||
hex.EncodeToString(ownerPubKey), | ||
hex.EncodeToString(validatorPubKey), | ||
) | ||
} | ||
if stakedNodes == 0 { | ||
return fmt.Errorf("auctionListSelector.addOwnerData error: %w, owner: %s, node: %s", | ||
epochStart.ErrOwnerHasNoStakedNode, | ||
hex.EncodeToString(ownerPubKey), | ||
hex.EncodeToString(validatorPubKey), | ||
) | ||
} | ||
|
||
totalTopUp, err := als.stakingDataProvider.GetTotalTopUp(ownerPubKey) | ||
if err != nil { | ||
return fmt.Errorf("auctionListSelector.addOwnerData: error getting total top up: %w, owner: %s, node: %s", | ||
err, | ||
hex.EncodeToString(ownerPubKey), | ||
hex.EncodeToString(validatorPubKey), | ||
) | ||
} | ||
|
||
data, exists := ownersData[owner] | ||
if exists { | ||
data.numAuctionNodes++ | ||
data.numQualifiedAuctionNodes++ | ||
data.numActiveNodes-- | ||
data.auctionList = append(data.auctionList, validator) | ||
} else { | ||
stakedNodesBigInt := big.NewInt(stakedNodes) | ||
topUpPerNode := big.NewInt(0).Div(totalTopUp, stakedNodesBigInt) | ||
ownersData[owner] = &ownerData{ | ||
numAuctionNodes: 1, | ||
numQualifiedAuctionNodes: 1, | ||
numActiveNodes: stakedNodes - 1, | ||
numStakedNodes: stakedNodes, | ||
totalTopUp: big.NewInt(0).SetBytes(totalTopUp.Bytes()), | ||
topUpPerNode: topUpPerNode, | ||
qualifiedTopUpPerNode: topUpPerNode, | ||
auctionList: []state.ValidatorInfoHandler{validator}, | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// TODO: Move this in elrond-go-core | ||
func safeSub(a, b uint32) (uint32, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have safeSub for uint64 already in go-core |
||
if a < b { | ||
|
@@ -308,7 +231,7 @@ func safeSub(a, b uint32) (uint32, error) { | |
} | ||
|
||
func (als *auctionListSelector) sortAuctionList( | ||
ownersData map[string]*ownerData, | ||
ownersData map[string]*ownerAuctionData, | ||
numOfAvailableNodeSlots uint32, | ||
validatorsInfoMap state.ShardValidatorsInfoMapHandler, | ||
randomness []byte, | ||
|
@@ -319,9 +242,9 @@ func (als *auctionListSelector) sortAuctionList( | |
} | ||
|
||
func (als *auctionListSelector) calcSoftAuctionNodesConfig( | ||
data map[string]*ownerData, | ||
data map[string]*ownerAuctionData, | ||
numAvailableSlots uint32, | ||
) map[string]*ownerData { | ||
) map[string]*ownerAuctionData { | ||
ownersData := copyOwnersData(data) | ||
minTopUp, maxTopUp := als.getMinMaxPossibleTopUp(ownersData) | ||
log.Debug("auctionListSelector: calc min and max possible top up", | ||
|
@@ -344,7 +267,7 @@ func (als *auctionListSelector) calcSoftAuctionNodesConfig( | |
return previousConfig | ||
} | ||
|
||
func (als *auctionListSelector) getMinMaxPossibleTopUp(ownersData map[string]*ownerData) (*big.Int, *big.Int) { | ||
func (als *auctionListSelector) getMinMaxPossibleTopUp(ownersData map[string]*ownerAuctionData) (*big.Int, *big.Int) { | ||
min := big.NewInt(0).SetBytes(als.softAuctionConfig.maxTopUp.Bytes()) | ||
max := big.NewInt(0).SetBytes(als.softAuctionConfig.minTopUp.Bytes()) | ||
|
||
|
@@ -367,10 +290,10 @@ func (als *auctionListSelector) getMinMaxPossibleTopUp(ownersData map[string]*ow | |
return min, max | ||
} | ||
|
||
func copyOwnersData(ownersData map[string]*ownerData) map[string]*ownerData { | ||
ret := make(map[string]*ownerData) | ||
func copyOwnersData(ownersData map[string]*ownerAuctionData) map[string]*ownerAuctionData { | ||
ret := make(map[string]*ownerAuctionData) | ||
for owner, data := range ownersData { | ||
ret[owner] = &ownerData{ | ||
ret[owner] = &ownerAuctionData{ | ||
numActiveNodes: data.numActiveNodes, | ||
numAuctionNodes: data.numAuctionNodes, | ||
numQualifiedAuctionNodes: data.numQualifiedAuctionNodes, | ||
|
@@ -386,7 +309,7 @@ func copyOwnersData(ownersData map[string]*ownerData) map[string]*ownerData { | |
return ret | ||
} | ||
|
||
func calcNodesConfig(ownersData map[string]*ownerData, topUp *big.Int) int64 { | ||
func calcNodesConfig(ownersData map[string]*ownerAuctionData, topUp *big.Int) int64 { | ||
numNodesQualifyingForTopUp := int64(0) | ||
|
||
for ownerPubKey, owner := range ownersData { | ||
|
Oops, something went wrong.
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.
I think
NumAuctionNodes
can be removed because are duplicated information with thelen(AuctionList)
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.
Right, good suggestion; removed it from here and from
stakingDataProvider
.However, I've kept it in
auctionListSelector
, since there we do a lot of iterations and I don't want to re-calculatelen
every time.