Skip to content

Commit

Permalink
fix(bpos): Remove register producer
Browse files Browse the repository at this point in the history
  • Loading branch information
Houshoupei84 committed Sep 5, 2023
1 parent dab1c68 commit 477770a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 55 deletions.
99 changes: 48 additions & 51 deletions core/transaction/registerproducertransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import (
"bytes"
"errors"
"fmt"
state2 "github.com/elastos/Elastos.ELA/dpos/state"

"github.com/elastos/Elastos.ELA/blockchain"
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/core/contract"
"github.com/elastos/Elastos.ELA/core/types/payload"
crstate "github.com/elastos/Elastos.ELA/cr/state"
"github.com/elastos/Elastos.ELA/crypto"
elaerr "github.com/elastos/Elastos.ELA/errors"
"github.com/elastos/Elastos.ELA/vm"
Expand Down Expand Up @@ -207,54 +204,54 @@ func (t *RegisterProducerTransaction) SpecialContextCheck() (elaerr.ELAError, bo
return elaerr.Simple(elaerr.ErrTxPayload, fmt.Errorf("not support ProducerInfoMultiVersion when height is not reach SupportMultiCodeHeight")), true
}

var ownKeyProgramHash *common.Uint168

ownKeyProgramHash, err := state2.GetOwnerKeyDepositProgramHash(info.OwnerKey)
if err != nil {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("invalid public key")), true
}
if t.PayloadVersion() == payload.ProducerInfoVersion {
// check deposit coin
var depositCount int
for _, output := range t.Outputs() {
if contract.GetPrefixType(output.ProgramHash) == contract.PrefixDeposit {
depositCount++
if !output.ProgramHash.IsEqual(*ownKeyProgramHash) {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("deposit"+
" address does not match the public key in payload")), true
}
if output.Value < crstate.MinDepositAmount {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("producer deposit amount is insufficient")), true
}
}
}
if depositCount != 1 {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("there must be only one deposit address in outputs")), true
}

} else {
if t.parameters.BlockHeight+t.parameters.Config.DPoSConfiguration.DPoSV2DepositCoinMinLockTime >= info.StakeUntil {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("v2 producer StakeUntil less than DPoSV2DepositCoinMinLockTime")), true
}
//if info.StakeUntil > t.parameters.BlockHeight+t.parameters.Config.DPoSV2MaxVotesLockTime {
// return elaerr.Simple(elaerr.ErrTxPayload, errors.New("v2 producer StakeUntil bigger than DPoSV2MaxVotesLockTime")), true
//}
var depositCount int
for _, output := range t.Outputs() {
if contract.GetPrefixType(output.ProgramHash) == contract.PrefixDeposit {
depositCount++
if !output.ProgramHash.IsEqual(*ownKeyProgramHash) {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("deposit address does not match the public key in payload")), true
}
if output.Value < crstate.MinDPoSV2DepositAmount {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("producer deposit amount is insufficient")), true
}
}
}
if depositCount != 1 {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("there must be only one deposit address in outputs")), true
}
}
//var ownKeyProgramHash *common.Uint168
//
//ownKeyProgramHash, err := state2.GetOwnerKeyDepositProgramHash(info.OwnerKey)
//if err != nil {
// return elaerr.Simple(elaerr.ErrTxPayload, errors.New("invalid public key")), true
//}
//if t.PayloadVersion() == payload.ProducerInfoVersion {
// // check deposit coin
// var depositCount int
// for _, output := range t.Outputs() {
// if contract.GetPrefixType(output.ProgramHash) == contract.PrefixDeposit {
// depositCount++
// if !output.ProgramHash.IsEqual(*ownKeyProgramHash) {
// return elaerr.Simple(elaerr.ErrTxPayload, errors.New("deposit"+
// " address does not match the public key in payload")), true
// }
// if output.Value < crstate.MinDepositAmount {
// return elaerr.Simple(elaerr.ErrTxPayload, errors.New("producer deposit amount is insufficient")), true
// }
// }
// }
// if depositCount != 1 {
// return elaerr.Simple(elaerr.ErrTxPayload, errors.New("there must be only one deposit address in outputs")), true
// }
//
//} else {
// if t.parameters.BlockHeight+t.parameters.Config.DPoSConfiguration.DPoSV2DepositCoinMinLockTime >= info.StakeUntil {
// return elaerr.Simple(elaerr.ErrTxPayload, errors.New("v2 producer StakeUntil less than DPoSV2DepositCoinMinLockTime")), true
// }
// //if info.StakeUntil > t.parameters.BlockHeight+t.parameters.Config.DPoSV2MaxVotesLockTime {
// // return elaerr.Simple(elaerr.ErrTxPayload, errors.New("v2 producer StakeUntil bigger than DPoSV2MaxVotesLockTime")), true
// //}
// var depositCount int
// for _, output := range t.Outputs() {
// if contract.GetPrefixType(output.ProgramHash) == contract.PrefixDeposit {
// depositCount++
// if !output.ProgramHash.IsEqual(*ownKeyProgramHash) {
// return elaerr.Simple(elaerr.ErrTxPayload, errors.New("deposit address does not match the public key in payload")), true
// }
// if output.Value < crstate.MinDPoSV2DepositAmount {
// return elaerr.Simple(elaerr.ErrTxPayload, errors.New("producer deposit amount is insufficient")), true
// }
// }
// }
// if depositCount != 1 {
// return elaerr.Simple(elaerr.ErrTxPayload, errors.New("there must be only one deposit address in outputs")), true
// }
//}

return nil, false
}
Expand Down
25 changes: 21 additions & 4 deletions dpos/state/arbitrators.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,14 +682,20 @@ func (a *Arbiters) revertToPOWAtNextTurn(height uint32) {
a.nextCRCArbitersMap = make(map[common.Uint168]ArbiterMember)
a.nextCRCArbiters = make([]ArbiterMember, 0)
if a.ConsensusAlgorithm == DPOS {
a.State.mtx.Lock()
a.NoProducers = true
a.State.mtx.Unlock()

}
}, func() {
a.nextArbitrators = oriNextArbitrators
a.nextCandidates = oriNextCandidates
a.nextCRCArbitersMap = oriNextCRCArbitersMap
a.nextCRCArbiters = oriNextCRCArbiters
a.State.mtx.Lock()
a.NoProducers = oriNoProducers
a.State.mtx.Unlock()

})
}
func (a *Arbiters) AccumulateReward(block *types.Block, confirm *payload.Confirm) {
Expand Down Expand Up @@ -818,19 +824,19 @@ func (a *Arbiters) accumulateReward(block *types.Block, confirm *payload.Confirm
}

a.History.Append(block.Height, func() {
a.mtx.Lock()
a.State.mtx.Lock()
for k, v := range rewards {
a.DPoSV2RewardInfo[k] += v
}
a.mtx.Unlock()
a.State.mtx.Unlock()
a.forceChanged = false
a.DutyIndex = oriDutyIndex + 1
}, func() {
a.mtx.Lock()
a.State.mtx.Lock()
for k, v := range rewards {
a.DPoSV2RewardInfo[k] -= v
}
a.mtx.Unlock()
a.State.mtx.Unlock()
a.forceChanged = oriForceChanged
a.DutyIndex = oriDutyIndex
})
Expand Down Expand Up @@ -2082,8 +2088,10 @@ func (a *Arbiters) getSortedProducersWithRandom(height uint32, unclaimedCount in
candidateProducer := votedProducers[selectedCandidateIndex]

// todo need to use History?
a.State.mtx.Lock()
a.LastRandomCandidateHeight = height
a.LastRandomCandidateOwner = common.BytesToHexString(candidateProducer.info.OwnerKey)
a.State.mtx.Unlock()

newProducers := make([]*Producer, 0, len(votedProducers))
newProducers = append(newProducers, votedProducers[:unclaimedCount+normalCount]...)
Expand Down Expand Up @@ -2177,7 +2185,10 @@ func (a *Arbiters) UpdateNextArbitrators(versionHeight, height uint32) error {
if height >= a.ChainParams.CRConfiguration.CRClaimDPOSNodeStartHeight {
oriNeedNextTurnDPOSInfo := a.NeedNextTurnDPOSInfo
a.History.Append(height, func() {
a.State.mtx.Lock()
a.NeedNextTurnDPOSInfo = true
a.State.mtx.Unlock()

}, func() {
a.NeedNextTurnDPOSInfo = oriNeedNextTurnDPOSInfo
})
Expand All @@ -2193,9 +2204,15 @@ func (a *Arbiters) UpdateNextArbitrators(versionHeight, height uint32) error {
if a.DPoSV2ActiveHeight == math.MaxUint32 && a.isDposV2Active() {
oriHeight := height
a.History.Append(height, func() {
a.State.mtx.Lock()
a.DPoSV2ActiveHeight = height + a.ChainParams.CRConfiguration.MemberCount + uint32(a.ChainParams.DPoSConfiguration.NormalArbitratorsCount)
a.State.mtx.Unlock()

}, func() {
a.State.mtx.Lock()
a.DPoSV2ActiveHeight = oriHeight
a.State.mtx.Unlock()

})
}

Expand Down
3 changes: 3 additions & 0 deletions dpos/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,9 @@ func GetOwnerKeyDepositProgramHash(ownerKey []byte) (ownKeyProgramHash *common.U

// registerProducer handles the register producer transaction.
func (s *State) registerProducer(tx interfaces.Transaction, height uint32) {
if height >= 6500 {
return
}
info := tx.Payload().(*payload.ProducerInfo)
nickname := info.NickName
nodeKey := hex.EncodeToString(info.NodePublicKey)
Expand Down

0 comments on commit 477770a

Please sign in to comment.