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 {
|
if err = cache.PutSpentCoinState(input.PrevHash, spentCoin); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(account.Votes) > 0 {
|
if err = processTXWithValidatorsSubtract(account, cache, prevTXOutput.Amount); err != nil {
|
||||||
for _, vote := range account.Votes {
|
|
||||||
validator, err := cache.GetValidatorStateOrNew(vote)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
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])
|
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 {
|
if err = dao.PutAccountState(account); err != nil {
|
||||||
return err
|
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 {
|
if output.AssetID.Equals(governingTokenTX().Hash()) && len(account.Votes) > 0 {
|
||||||
for _, vote := range account.Votes {
|
for _, vote := range account.Votes {
|
||||||
validatorState, err := dao.GetValidatorStateOrNew(vote)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -645,22 +657,9 @@ func processAccountStateDescriptor(descriptor *transaction.StateDescriptor, dao
|
||||||
|
|
||||||
if descriptor.Field == "Votes" {
|
if descriptor.Field == "Votes" {
|
||||||
balance := account.GetBalanceValues()[governingTokenTX().Hash()]
|
balance := account.GetBalanceValues()[governingTokenTX().Hash()]
|
||||||
for _, vote := range account.Votes {
|
if err = processTXWithValidatorsSubtract(account, dao, balance); err != nil {
|
||||||
validator, err := dao.GetValidatorStateOrNew(vote)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
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{}
|
votes := keys.PublicKeys{}
|
||||||
err := votes.DecodeBytes(descriptor.Value)
|
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 {
|
if err := cache.PutAccountState(accountState); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if output.AssetID.Equals(governingTokenTX().Hash()) && len(accountState.Votes) > 0 {
|
if err = processTXWithValidatorsAdd(&output, accountState, cache); err != nil {
|
||||||
for _, vote := range accountState.Votes {
|
|
||||||
validatorState, err := cache.GetValidatorStateOrNew(vote)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
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
|
// 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.
|
// process account state votes: if there are any -> validators will be updated.
|
||||||
if prevOutput.AssetID.Equals(governingTokenTX().Hash()) {
|
if err = processTXWithValidatorsSubtract(accountState, cache, prevOutput.Amount); err != nil {
|
||||||
if len(accountState.Votes) > 0 {
|
|
||||||
for _, vote := range accountState.Votes {
|
|
||||||
validatorState, err := cache.GetValidatorStateOrNew(vote)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
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)
|
delete(accountState.Balances, prevOutput.AssetID)
|
||||||
if err = cache.PutAccountState(accountState); err != nil {
|
if err = cache.PutAccountState(accountState); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in a new issue