Skip to content

Commit

Permalink
fix some governance bugs (#836)
Browse files Browse the repository at this point in the history
* fix some overflow bugs

* add change view block num constraint

* fix if consensus bug

* add more constrain

* fix comment
  • Loading branch information
siovanus authored and laizy committed Apr 4, 2019
1 parent 0a4b493 commit 2ce7f73
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
23 changes: 23 additions & 0 deletions smartcontract/service/native/governance/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,9 @@ func UnAuthorizeForPeer(native *native.NativeService) ([]byte, error) {
}

//check pos
if pos < 1 {
return utils.BYTE_FALSE, fmt.Errorf("unAuthorizeForPeer, pos must >= 1")
}
if authorizeInfo.ConsensusPos+authorizeInfo.CandidatePos+authorizeInfo.NewPos < uint64(globalParam2.MinAuthorizePos) {
pos = authorizeInfo.ConsensusPos + authorizeInfo.CandidatePos + authorizeInfo.NewPos
} else if pos < uint64(globalParam2.MinAuthorizePos) || pos%uint64(globalParam2.MinAuthorizePos) != 0 {
Expand Down Expand Up @@ -923,6 +926,10 @@ func Withdraw(native *native.NativeService) ([]byte, error) {
for i := 0; i < len(params.PeerPubkeyList); i++ {
peerPubkey := params.PeerPubkeyList[i]
pos := params.WithdrawList[i]

if pos < 1 {
return utils.BYTE_FALSE, fmt.Errorf("withdraw, amount of withdraw must >= 1")
}
peerPubkeyPrefix, err := hex.DecodeString(peerPubkey)
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("hex.DecodeString, peerPubkey format error: %v", err)
Expand Down Expand Up @@ -1075,6 +1082,9 @@ func UpdateConfig(native *native.NativeService) ([]byte, error) {
if configuration.PeerHandshakeTimeout < 10 {
return utils.BYTE_FALSE, fmt.Errorf("updateConfig. PeerHandshakeTimeout must >= 10")
}
if configuration.MaxBlockChangeView < 10000 {
return utils.BYTE_FALSE, fmt.Errorf("updateConfig. MaxBlockChangeView must >= 10000")
}

preConfig := &PreConfig{
Configuration: configuration,
Expand Down Expand Up @@ -1134,6 +1144,9 @@ func UpdateGlobalParam(native *native.NativeService) ([]byte, error) {
if globalParam.CandidateFee != 0 && globalParam.CandidateFee < MIN_CANDIDATE_FEE {
return utils.BYTE_FALSE, fmt.Errorf("updateGlobalParam. CandidateFee must >= %d", MIN_CANDIDATE_FEE)
}
if globalParam.MinInitStake < 1 {
return utils.BYTE_FALSE, fmt.Errorf("updateGlobalParam. MinInitStake must >= 1")
}
err = putGlobalParam(native, contract, globalParam)
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("putGlobalParam, put globalParam error: %v", err)
Expand Down Expand Up @@ -1465,6 +1478,11 @@ func AddInitPos(native *native.NativeService) ([]byte, error) {
}
contract := native.ContextRef.CurrentContext().ContractAddress

//check pos
if params.Pos < 1 {
return utils.BYTE_FALSE, fmt.Errorf("addInitPos, pos must >= 1")
}

//check if is peer owner
//get current view
view, err := GetView(native, contract)
Expand Down Expand Up @@ -1526,6 +1544,11 @@ func ReduceInitPos(native *native.NativeService) ([]byte, error) {
}
contract := native.ContextRef.CurrentContext().ContractAddress

//check pos
if params.Pos < 1 {
return utils.BYTE_FALSE, fmt.Errorf("reduceInitPos, pos must >= 1")
}

//check if is peer owner
//get current view
view, err := GetView(native, contract)
Expand Down
39 changes: 26 additions & 13 deletions smartcontract/service/native/governance/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"sort"

"github.com/ontio/ontology/common"
Expand Down Expand Up @@ -185,6 +186,9 @@ func authorizeForPeer(native *native.NativeService, flag string) error {
pos := params.PosList[i]

//check pos
if pos < 1 {
return fmt.Errorf("authorizeForPeer, pos must >= 1")
}
if pos < globalParam2.MinAuthorizePos || pos%globalParam2.MinAuthorizePos != 0 {
return fmt.Errorf("authorizeForPeer, pos must be times of %d", globalParam2.MinAuthorizePos)
}
Expand Down Expand Up @@ -840,25 +844,26 @@ func executeSplit2(native *native.NativeService, contract common.Address, view u
income := balance - splitFee

//fee split to dapp address
dappIncome := income * uint64(globalParam2.DappFee) / 100
dappIncome := new(big.Int).Div(new(big.Int).Mul(new(big.Int).SetUint64(income),
new(big.Int).SetUint64(uint64(globalParam2.DappFee))), new(big.Int).SetUint64(100))
gasAddress, err := getGasAddress(native, contract)
if err != nil {
return splitSum, fmt.Errorf("getGasAddress, getGasAddress error: %v", err)
}
if gasAddress.Address == common.ADDRESS_EMPTY {
dappIncome = 0
dappIncome = new(big.Int).SetUint64(0)
} else {
err := appCallTransferOng(native, utils.GovernanceContractAddress, gasAddress.Address, dappIncome)
err := appCallTransferOng(native, utils.GovernanceContractAddress, gasAddress.Address, dappIncome.Uint64())
if err != nil {
return splitSum, fmt.Errorf("appCallTransferOng, appCallTransferOng error: %v", err)
}
}

//fee split to node
if income < dappIncome {
if income < dappIncome.Uint64() {
panic("income less than dappIncome!")
}
nodeIncome := income - dappIncome
nodeIncome := new(big.Int).Sub(new(big.Int).SetUint64(income), dappIncome)
//get globalParam
globalParam, err := getGlobalParam(native, contract)
if err != nil {
Expand Down Expand Up @@ -913,13 +918,17 @@ func executeSplit2(native *native.NativeService, contract common.Address, view u

//fee split of consensus peer
for i := 0; i < int(config.K); i++ {
nodeAmount := nodeIncome * uint64(globalParam.A) / 100 * peersCandidate[i].S / sumS
err = splitNodeFee(native, contract, peersCandidate[i].PeerPubkey, peersCandidate[i].Address, true,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].TotalPos, nodeAmount)
//nodeAmount := nodeIncome * uint64(globalParam.A) / 100 * peersCandidate[i].S / sumS
nodeAmount := new(big.Int).Div(new(big.Int).Mul(new(big.Int).Div(new(big.Int).Mul(nodeIncome,
new(big.Int).SetUint64(uint64(globalParam.A))), new(big.Int).SetUint64(100)),
new(big.Int).SetUint64(peersCandidate[i].S)), new(big.Int).SetUint64(sumS))
err = splitNodeFee(native, contract, peersCandidate[i].PeerPubkey, peersCandidate[i].Address,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].Status == ConsensusStatus,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].TotalPos, nodeAmount.Uint64())
if err != nil {
return splitSum, fmt.Errorf("executeSplit2, splitNodeFee error: %v", err)
}
splitSum += nodeAmount
splitSum += nodeAmount.Uint64()
}

//fee split of candidate peer
Expand All @@ -938,13 +947,17 @@ func executeSplit2(native *native.NativeService, contract common.Address, view u
return splitSum, nil
}
for i := int(config.K); i < length; i++ {
nodeAmount := nodeIncome * uint64(globalParam.B) / 100 * peersCandidate[i].Stake / sum
err = splitNodeFee(native, contract, peersCandidate[i].PeerPubkey, peersCandidate[i].Address, false,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].TotalPos, nodeAmount)
//nodeAmount := nodeIncome * uint64(globalParam.B) / 100 * peersCandidate[i].Stake / sum
nodeAmount := new(big.Int).Div(new(big.Int).Mul(new(big.Int).Div(new(big.Int).Mul(nodeIncome,
new(big.Int).SetUint64(uint64(globalParam.A))), new(big.Int).SetUint64(100)),
new(big.Int).SetUint64(peersCandidate[i].Stake)), new(big.Int).SetUint64(sum))
err = splitNodeFee(native, contract, peersCandidate[i].PeerPubkey, peersCandidate[i].Address,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].Status == ConsensusStatus,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].TotalPos, nodeAmount.Uint64())
if err != nil {
return splitSum, fmt.Errorf("executeSplit2, splitNodeFee error: %v", err)
}
splitSum += nodeAmount
splitSum += nodeAmount.Uint64()
}

return splitSum, nil
Expand Down
3 changes: 2 additions & 1 deletion smartcontract/service/native/governance/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ func splitCurve(native *native.NativeService, contract common.Address, pos uint6
index := xi / (PRECISE / 10)
if index > uint64(len(Xi)-2) {
index = uint64(len(Xi) - 2)
xi = uint64(Xi[len(Xi)-1])
}
splitCurve, err := getSplitCurve(native, contract)
if err != nil {
return 0, fmt.Errorf("getSplitCurve, get splitCurve error: %v", err)
}
Yi := splitCurve.Yi
s := ((uint64(Yi[index+1])-uint64(Yi[index]))*xi + uint64(Yi[index])*uint64(Xi[index+1]) - uint64(Yi[index+1])*uint64(Xi[index])) / (uint64(Xi[index+1]) - uint64(Xi[index]))
s := (uint64(Yi[index+1])*xi + uint64(Yi[index])*uint64(Xi[index+1]) - uint64(Yi[index])*xi - uint64(Yi[index+1])*uint64(Xi[index])) / (uint64(Xi[index+1]) - uint64(Xi[index]))
return s, nil
}

Expand Down

0 comments on commit 2ce7f73

Please sign in to comment.