Skip to content
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

Fix delegation for suspended and zombie #784

Merged
merged 4 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type ConsensusConf struct {
EnableValidationSharding bool
ChangeKillTxValidation bool
IncreaseGodInvitesLimit bool
FixDelegation bool
}

type ConsensusVerson uint16
Expand Down Expand Up @@ -166,6 +167,7 @@ func ApplyConsensusVersion(ver ConsensusVerson, cfg *ConsensusConf) {
cfg.EnableValidationSharding = true
cfg.ChangeKillTxValidation = true
cfg.IncreaseGodInvitesLimit = true
cfg.FixDelegation = true
cfg.Version = ConsensusV6
cfg.MigrationTimeout = 0
cfg.GenerateGenesisAfterUpgrade = true
Expand Down
17 changes: 13 additions & 4 deletions core/ceremony/ceremony.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,14 +863,23 @@ func (vc *ValidationCeremony) sendTx(txType uint16, payload []byte) (common.Hash
return signedTx.Hash(), err
}

func applyOnState(appState *appstate.AppState, statsCollector collector.StatsCollector, addr common.Address, value cacheValue) (identitiesCount int) {
func applyOnState(cfg *config.ConsensusConf, appState *appstate.AppState, statsCollector collector.StatsCollector, addr common.Address, value cacheValue) (identitiesCount int) {
collector.BeginFailedValidationBalanceUpdate(statsCollector, addr, appState)
appState.State.SetState(addr, value.state)
collector.CompleteBalanceUpdate(statsCollector, appState)
if !value.missed {
appState.State.AddNewScore(addr, common.EncodeScore(value.shortFlipPoint, value.shortQualifiedFlipsCount))
}
appState.State.SetBirthday(addr, value.birthday)

if cfg.FixDelegation && value.state.NewbieOrBetter() && (value.prevState == state.Suspended || value.prevState == state.Zombie) && value.delegatee != nil {
transitiveDelegatee := appState.State.Delegatee(*value.delegatee)
if transitiveDelegatee != nil {
value.delegatee = nil
appState.State.RemoveDelegatee(addr)
}
}

if value.state == state.Verified && value.prevState == state.Newbie {
addToBalance := math.ToInt(decimal.NewFromBigInt(appState.State.GetStakeBalance(addr), 0).Mul(decimal.NewFromFloat(common.StakeToBalanceCoef)))
addTo := addr
Expand Down Expand Up @@ -908,7 +917,7 @@ func (vc *ValidationCeremony) ApplyNewEpoch(height uint64, appState *appstate.Ap

if len(applyingCache.epochApplyingResult) > 0 {
for addr, value := range applyingCache.epochApplyingResult {
identitiesCount += applyOnState(appState, statsCollector, addr, value)
identitiesCount += applyOnState(vc.config.Consensus, appState, statsCollector, addr, value)
}
return identitiesCount, applyingCache.validationResults, false
}
Expand Down Expand Up @@ -1037,7 +1046,7 @@ func (vc *ValidationCeremony) ApplyNewEpoch(height uint64, appState *appstate.Ap
}

for addr, value := range epochApplyingValues {
identitiesCount += applyOnState(appState, statsCollector, addr, value)
identitiesCount += applyOnState(vc.config.Consensus, appState, statsCollector, addr, value)
}
for shardId, shard := range vc.shardCandidates {
for _, addr := range shard.nonCandidates {
Expand All @@ -1059,7 +1068,7 @@ func (vc *ValidationCeremony) ApplyNewEpoch(height uint64, appState *appstate.Ap
delegatee: identity.Delegatee,
}
epochApplyingValues[addr] = value
identitiesCount += applyOnState(appState, statsCollector, addr, value)
identitiesCount += applyOnState(vc.config.Consensus, appState, statsCollector, addr, value)
}
}

Expand Down
18 changes: 16 additions & 2 deletions core/ceremony/ceremony_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,9 @@ func Test_applyOnState(t *testing.T) {
appstate.State.AddStake(addr1, big.NewInt(100))
appstate.State.AddBalance(addr1, big.NewInt(10))
appstate.State.AddNewScore(addr1, common.EncodeScore(5, 6))
appstate.State.SetDelegatee(addr1, delegatee)

identities := applyOnState(appstate, collector.NewStatsCollector(), addr1, cacheValue{
identities := applyOnState(config.ConsensusVersions[config.ConsensusV6], appstate, collector.NewStatsCollector(), addr1, cacheValue{
prevState: state.Newbie,
birthday: 3,
shortFlipPoint: 1,
Expand All @@ -682,13 +683,26 @@ func Test_applyOnState(t *testing.T) {
require.True(t, appstate.State.GetBalance(addr1).Cmp(big.NewInt(10)) == 0)
require.True(t, appstate.State.GetStakeBalance(addr1).Cmp(big.NewInt(25)) == 0)

applyOnState(appstate, collector.NewStatsCollector(), addr1, cacheValue{
applyOnState(config.ConsensusVersions[config.ConsensusV6], appstate, collector.NewStatsCollector(), addr1, cacheValue{
shortFlipPoint: 0,
shortQualifiedFlipsCount: 0,
missed: true,
})
identity = appstate.State.GetIdentity(addr1)
require.Equal(t, []byte{common.EncodeScore(5, 6), common.EncodeScore(1, 2)}, identity.Scores)


appstate.State.SetDelegatee(delegatee, common.Address{0x3})

applyOnState(config.ConsensusVersions[config.ConsensusV6], appstate, collector.NewStatsCollector(), addr1, cacheValue{
prevState: state.Zombie,
shortFlipPoint: 1,
shortQualifiedFlipsCount: 2,
state: state.Verified,
delegatee: &delegatee,
})
identity = appstate.State.GetIdentity(addr1)
require.Nil(t, identity.Delegatee)
}

func Test_calculateNewTotalScore(t *testing.T) {
Expand Down