mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 19:42:23 +00:00
core: refactor voting processing, fix pending tx subtractions
Deduplicate code and add missing token check for subtractions that are being done in GetValidators() for pending transactions.
This commit is contained in:
parent
c8a248596e
commit
26af9200b0
1 changed files with 15 additions and 25 deletions
|
@ -436,7 +436,7 @@ func (bc *Blockchain) storeBlock(block *block.Block) error {
|
||||||
if err = cache.PutSpentCoinState(input.PrevHash, spentCoin); err != nil {
|
if err = cache.PutSpentCoinState(input.PrevHash, spentCoin); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = processTXWithValidatorsSubtract(account, cache, prevTXOutput.Amount); err != nil {
|
if err = processTXWithValidatorsSubtract(&prevTXOutput, account, cache); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -638,36 +638,26 @@ func processOutputs(tx *transaction.Transaction, dao *cachedDao) error {
|
||||||
|
|
||||||
func processTXWithValidatorsAdd(output *transaction.Output, account *state.Account, dao *cachedDao) error {
|
func processTXWithValidatorsAdd(output *transaction.Output, account *state.Account, dao *cachedDao) error {
|
||||||
if output.AssetID.Equals(governingTokenTX().Hash()) && len(account.Votes) > 0 {
|
if output.AssetID.Equals(governingTokenTX().Hash()) && len(account.Votes) > 0 {
|
||||||
for _, vote := range account.Votes {
|
return modAccountVotes(account, dao, output.Amount)
|
||||||
validatorState, err := dao.GetValidatorStateOrNew(vote)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
validatorState.Votes += output.Amount
|
|
||||||
if err = dao.PutValidatorState(validatorState); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
vc, err := dao.GetValidatorsCount()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
vc[len(account.Votes)-1] += output.Amount
|
|
||||||
err = dao.PutValidatorsCount(vc)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processTXWithValidatorsSubtract(account *state.Account, dao *cachedDao, toSubtract util.Fixed8) error {
|
func processTXWithValidatorsSubtract(output *transaction.Output, account *state.Account, dao *cachedDao) error {
|
||||||
|
if output.AssetID.Equals(governingTokenTX().Hash()) && len(account.Votes) > 0 {
|
||||||
|
return modAccountVotes(account, dao, -output.Amount)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// modAccountVotes adds given value to given account voted validators.
|
||||||
|
func modAccountVotes(account *state.Account, dao *cachedDao, value util.Fixed8) error {
|
||||||
for _, vote := range account.Votes {
|
for _, vote := range account.Votes {
|
||||||
validator, err := dao.GetValidatorStateOrNew(vote)
|
validator, err := dao.GetValidatorStateOrNew(vote)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
validator.Votes -= toSubtract
|
validator.Votes += value
|
||||||
if validator.UnregisteredAndHasNoVotes() {
|
if validator.UnregisteredAndHasNoVotes() {
|
||||||
if err := dao.DeleteValidatorState(validator); err != nil {
|
if err := dao.DeleteValidatorState(validator); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -683,7 +673,7 @@ func processTXWithValidatorsSubtract(account *state.Account, dao *cachedDao, toS
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
vc[len(account.Votes)-1] -= toSubtract
|
vc[len(account.Votes)-1] += value
|
||||||
err = dao.PutValidatorsCount(vc)
|
err = dao.PutValidatorsCount(vc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -724,7 +714,7 @@ func processAccountStateDescriptor(descriptor *transaction.StateDescriptor, dao
|
||||||
|
|
||||||
if descriptor.Field == "Votes" {
|
if descriptor.Field == "Votes" {
|
||||||
balance := account.GetBalanceValues()[governingTokenTX().Hash()]
|
balance := account.GetBalanceValues()[governingTokenTX().Hash()]
|
||||||
if err = processTXWithValidatorsSubtract(account, dao, balance); err != nil {
|
if err = modAccountVotes(account, dao, -balance); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1356,7 +1346,7 @@ func (bc *Blockchain) GetValidators(txes ...*transaction.Transaction) ([]*keys.P
|
||||||
}
|
}
|
||||||
|
|
||||||
// process account state votes: if there are any -> validators will be updated.
|
// process account state votes: if there are any -> validators will be updated.
|
||||||
if err = processTXWithValidatorsSubtract(accountState, cache, prevOutput.Amount); err != nil {
|
if err = processTXWithValidatorsSubtract(&prevOutput, accountState, cache); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
delete(accountState.Balances, prevOutput.AssetID)
|
delete(accountState.Balances, prevOutput.AssetID)
|
||||||
|
|
Loading…
Reference in a new issue