Return early if found delegation

pull/1846/head
Rongjian Lan 5 years ago
parent c16ededa3e
commit 07cc574f34
  1. 32
      core/blockchain.go

@ -2541,29 +2541,31 @@ func (bc *BlockChain) UpdateStakingMetaData(tx *staking.StakingTransaction, root
}
func (bc *BlockChain) addDelegationIndex(delegatorAddress, validatorAddress common.Address, root common.Hash) error {
// Get existing delegations
delegations, err := bc.ReadDelegationsByDelegator(delegatorAddress)
if err != nil {
return err
}
found := false
// If there is an existing delegation, just return
for _, delegation := range delegations {
if bytes.Compare(delegation.ValidatorAddress.Bytes(), validatorAddress.Bytes()) == 0 {
found = true
break
return nil
}
}
if !found {
wrapper, err := bc.ReadValidatorDataAt(validatorAddress, root) // Note this is already reading from the state of current block in concern
if err != nil {
return err
}
for i := range wrapper.Delegations {
if bytes.Compare(wrapper.Delegations[i].DelegatorAddress.Bytes(), delegatorAddress.Bytes()) == 0 {
delegations = append(delegations, staking.DelegationIndex{
validatorAddress,
uint64(i),
})
}
// Found the delegation from state and add the delegation index
// Note this should read from the state of current block in concern
wrapper, err := bc.ReadValidatorDataAt(validatorAddress, root)
if err != nil {
return err
}
for i := range wrapper.Delegations {
if bytes.Compare(wrapper.Delegations[i].DelegatorAddress.Bytes(), delegatorAddress.Bytes()) == 0 {
delegations = append(delegations, staking.DelegationIndex{
validatorAddress,
uint64(i),
})
}
}
return bc.WriteDelegationsByDelegator(delegatorAddress, delegations)

Loading…
Cancel
Save