forked from TrueCloudLab/neoneo-go
core: extracted same logic to separate methods
This commit is contained in:
parent
c1f39d5c7b
commit
c93a8d2bc4
1 changed files with 47 additions and 73 deletions
|
@ -394,24 +394,9 @@ func (bc *Blockchain) storeBlock(block *Block) error {
|
|||
if err = cache.PutSpentCoinState(input.PrevHash, spentCoin); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(account.Votes) > 0 {
|
||||
for _, vote := range account.Votes {
|
||||
validator, err := cache.GetValidatorStateOrNew(vote)
|
||||
if err != nil {
|
||||
if err = processTXWithValidatorsSubtract(account, cache, prevTXOutput.Amount); err != nil {
|
||||
return err
|
||||
}
|
||||
validator.Votes -= prevTXOutput.Amount
|
||||
if !validator.RegisteredAndHasVotes() {
|
||||
if err = cache.DeleteValidatorState(validator); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = cache.PutValidatorState(validator); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
balancesLen := len(account.Balances[prevTXOutput.AssetID])
|
||||
|
@ -596,6 +581,14 @@ func processOutputs(tx *transaction.Transaction, dao *dao) error {
|
|||
if err = dao.PutAccountState(account); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = processTXWithValidatorsAdd(&output, account, dao); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func processTXWithValidatorsAdd(output *transaction.Output, account *state.Account, dao *dao) error {
|
||||
if output.AssetID.Equals(governingTokenTX().Hash()) && len(account.Votes) > 0 {
|
||||
for _, vote := range account.Votes {
|
||||
validatorState, err := dao.GetValidatorStateOrNew(vote)
|
||||
|
@ -608,6 +601,25 @@ func processOutputs(tx *transaction.Transaction, dao *dao) error {
|
|||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func processTXWithValidatorsSubtract(account *state.Account, dao *dao, toSubtract util.Fixed8) error {
|
||||
for _, vote := range account.Votes {
|
||||
validator, err := dao.GetValidatorStateOrNew(vote)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
validator.Votes -= toSubtract
|
||||
if !validator.RegisteredAndHasVotes() {
|
||||
if err := dao.DeleteValidatorState(validator); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := dao.PutValidatorState(validator); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -645,22 +657,9 @@ func processAccountStateDescriptor(descriptor *transaction.StateDescriptor, dao
|
|||
|
||||
if descriptor.Field == "Votes" {
|
||||
balance := account.GetBalanceValues()[governingTokenTX().Hash()]
|
||||
for _, vote := range account.Votes {
|
||||
validator, err := dao.GetValidatorStateOrNew(vote)
|
||||
if err != nil {
|
||||
if err = processTXWithValidatorsSubtract(account, dao, balance); err != nil {
|
||||
return err
|
||||
}
|
||||
validator.Votes -= balance
|
||||
if !validator.RegisteredAndHasVotes() {
|
||||
if err := dao.DeleteValidatorState(validator); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := dao.PutValidatorState(validator); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
votes := keys.PublicKeys{}
|
||||
err := votes.DecodeBytes(descriptor.Value)
|
||||
|
@ -1174,18 +1173,9 @@ func (bc *Blockchain) GetValidators(txes ...*transaction.Transaction) ([]*keys.P
|
|||
if err := cache.PutAccountState(accountState); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if output.AssetID.Equals(governingTokenTX().Hash()) && len(accountState.Votes) > 0 {
|
||||
for _, vote := range accountState.Votes {
|
||||
validatorState, err := cache.GetValidatorStateOrNew(vote)
|
||||
if err != nil {
|
||||
if err = processTXWithValidatorsAdd(&output, accountState, cache); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
validatorState.Votes += output.Amount
|
||||
if err = cache.PutValidatorState(validatorState); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// group inputs by the same previous hash and iterate through inputs
|
||||
|
@ -1209,25 +1199,9 @@ func (bc *Blockchain) GetValidators(txes ...*transaction.Transaction) ([]*keys.P
|
|||
}
|
||||
|
||||
// process account state votes: if there are any -> validators will be updated.
|
||||
if prevOutput.AssetID.Equals(governingTokenTX().Hash()) {
|
||||
if len(accountState.Votes) > 0 {
|
||||
for _, vote := range accountState.Votes {
|
||||
validatorState, err := cache.GetValidatorStateOrNew(vote)
|
||||
if err != nil {
|
||||
if err = processTXWithValidatorsSubtract(accountState, cache, prevOutput.Amount); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
validatorState.Votes -= prevOutput.Amount
|
||||
if err = cache.PutValidatorState(validatorState); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !validatorState.Registered && validatorState.Votes.Equal(util.Fixed8(0)) {
|
||||
if err = cache.DeleteValidatorState(validatorState); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
delete(accountState.Balances, prevOutput.AssetID)
|
||||
if err = cache.PutAccountState(accountState); err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in a new issue