Skip to content

Commit

Permalink
update new peer cost (#1266)
Browse files Browse the repository at this point in the history
* update new peer cost

* set new node cost height and update setPeerCost
  • Loading branch information
siovanus authored Aug 3, 2020
1 parent 9569d9a commit 5aebc29
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 36 deletions.
11 changes: 11 additions & 0 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ func GetOntHolderUnboundDeadline() uint32 {
}
}

func GetNewPeerCostHeight() uint32 {
switch DefConfig.P2PNode.NetworkId {
case NETWORK_ID_MAIN_NET:
return constants.BLOCKHEIGHT_NEW_PEER_COST_MAINNET
case NETWORK_ID_POLARIS_NET:
return constants.BLOCKHEIGHT_NEW_PEER_COST_POLARIS
default:
return 0
}
}

// the end of unbound timestamp offset from genesis block's timestamp
func GetGovUnboundDeadline() (uint32, uint64) {
count := uint64(0)
Expand Down
4 changes: 4 additions & 0 deletions common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ const BLOCKHEIGHT_ONTFS_MAINNET = 8550000
const BLOCKHEIGHT_ONTFS_POLARIS = 12250000

const BLOCKHEIGHT_CC_POLARIS = 13130000

//new node cost height
const BLOCKHEIGHT_NEW_PEER_COST_MAINNET = 9400000
const BLOCKHEIGHT_NEW_PEER_COST_POLARIS = 13360000
65 changes: 65 additions & 0 deletions smartcontract/service/native/governance/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const (
TRANSFER_PENALTY = "transferPenalty"
CHANGE_MAX_AUTHORIZATION = "changeMaxAuthorization"
SET_PEER_COST = "setPeerCost"
SET_FEE_PERCENTAGE = "setFeePercentage"
ADD_INIT_POS = "addInitPos"
REDUCE_INIT_POS = "reduceInitPos"
SET_PROMISE_POS = "setPromisePos"
Expand Down Expand Up @@ -139,6 +140,7 @@ func RegisterGovernanceContract(native *native.NativeService) {
native.Register(WITHDRAW_ONG, WithdrawOng)
native.Register(CHANGE_MAX_AUTHORIZATION, ChangeMaxAuthorization)
native.Register(SET_PEER_COST, SetPeerCost)
native.Register(SET_FEE_PERCENTAGE, SetFeePercentage)
native.Register(WITHDRAW_FEE, WithdrawFee)
native.Register(ADD_INIT_POS, AddInitPos)
native.Register(REDUCE_INIT_POS, ReduceInitPos)
Expand Down Expand Up @@ -1411,6 +1413,69 @@ func SetPeerCost(native *native.NativeService) ([]byte, error) {
return utils.BYTE_FALSE, fmt.Errorf("getPeerAttributes error: %v", err)
}
peerAttributes.T2PeerCost = uint64(params.PeerCost)
peerAttributes.T2StakeCost = 0

err = putPeerAttributes(native, contract, peerAttributes)
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("putPeerAttributes error: %v", err)
}

return utils.BYTE_TRUE, nil
}

//Set node cost, node can take some percentage of fee before split
func SetFeePercentage(native *native.NativeService) ([]byte, error) {
if native.Height < config.GetNewPeerCostHeight() {
return utils.BYTE_FALSE, fmt.Errorf("block num is not reached for this func")
}
params := new(SetFeePercentageParam)
if err := params.Deserialization(common.NewZeroCopySource(native.Input)); err != nil {
return utils.BYTE_FALSE, fmt.Errorf("deserialize, deserialize setPeerCostParam error: %v", err)
}
if params.PeerCost > 100 {
return utils.BYTE_FALSE, fmt.Errorf("peerCost must >= 0 and <= 100")
}
if params.StakeCost > 100 {
return utils.BYTE_FALSE, fmt.Errorf("stakeCost must >= 0 and <= 100")
}
stakeCost := uint64(params.StakeCost)
if params.StakeCost == 0 {
stakeCost = 101 // in storage, 101 means 0, 0 means null
}

//check witness
err := utils.ValidateOwner(native, params.Address)
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("validateOwner, checkWitness error: %v", err)
}
contract := native.ContextRef.CurrentContext().ContractAddress

//check if is peer owner
//get current view
view, err := GetView(native, contract)
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("getView, get view error: %v", err)
}

//get peerPoolMap
peerPoolMap, err := GetPeerPoolMap(native, contract, view)
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("getPeerPoolMap, get peerPoolMap error: %v", err)
}
peerPoolItem, ok := peerPoolMap.PeerPoolMap[params.PeerPubkey]
if !ok {
return utils.BYTE_FALSE, fmt.Errorf("setPeerCost, peerPubkey is not in peerPoolMap")
}
if peerPoolItem.Address != params.Address {
return utils.BYTE_FALSE, fmt.Errorf("address is not peer owner")
}

peerAttributes, err := getPeerAttributes(native, contract, params.PeerPubkey)
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("getPeerAttributes error: %v", err)
}
peerAttributes.T2PeerCost = uint64(params.PeerCost)
peerAttributes.T2StakeCost = stakeCost

err = putPeerAttributes(native, contract, peerAttributes)
if err != nil {
Expand Down
34 changes: 25 additions & 9 deletions smartcontract/service/native/governance/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ func executeSplit2(native *native.NativeService, contract common.Address, view u
err = splitNodeFee(native, contract, peersCandidate[i].PeerPubkey, peersCandidate[i].Address,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].Status == ConsensusStatus,
currentPeerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].Status == ConsensusStatus,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].TotalPos, nodeAmount.Uint64())
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].InitPos, peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].TotalPos, nodeAmount.Uint64())
if err != nil {
return splitSum, fmt.Errorf("executeSplit2, splitNodeFee error: %v", err)
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ func executeSplit2(native *native.NativeService, contract common.Address, view u
err = splitNodeFee(native, contract, peersCandidate[i].PeerPubkey, peersCandidate[i].Address,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].Status == ConsensusStatus,
currentPeerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].Status == ConsensusStatus,
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].TotalPos, nodeAmount.Uint64())
peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].InitPos, peerPoolMap.PeerPoolMap[peersCandidate[i].PeerPubkey].TotalPos, nodeAmount.Uint64())
if err != nil {
return splitSum, fmt.Errorf("executeSplit2, splitNodeFee error: %v", err)
}
Expand Down Expand Up @@ -1275,10 +1275,10 @@ func executeCommitDpos2(native *native.NativeService, contract common.Address) e
if err != nil {
return fmt.Errorf("getPeerAttributes error: %v", err)
}
t2PeerCost := peerAttributes.T2PeerCost
t1PeerCost := peerAttributes.T1PeerCost
peerAttributes.TPeerCost = t1PeerCost
peerAttributes.T1PeerCost = t2PeerCost
peerAttributes.TPeerCost = peerAttributes.T1PeerCost
peerAttributes.T1PeerCost = peerAttributes.T2PeerCost
peerAttributes.TStakeCost = peerAttributes.T1StakeCost
peerAttributes.T1StakeCost = peerAttributes.T2StakeCost
err = putPeerAttributes(native, contract, peerAttributes)
if err != nil {
return fmt.Errorf("putPeerAttributes error: %v", err)
Expand Down Expand Up @@ -1396,18 +1396,34 @@ func executeCommitDpos2(native *native.NativeService, contract common.Address) e
return nil
}

func splitNodeFee(native *native.NativeService, contract common.Address, peerPubkey string, peerAddress common.Address, preIfConsensus, ifConsensus bool, totalPos uint64, nodeAmount uint64) error {
func splitNodeFee(native *native.NativeService, contract common.Address, peerPubkey string, peerAddress common.Address,
preIfConsensus, ifConsensus bool, initPos, totalPos uint64, nodeAmount uint64) error {
peerPubkeyPrefix, err := hex.DecodeString(peerPubkey)
if err != nil {
return fmt.Errorf("hex.DecodeString, peerPubkey format error: %v", err)
}
//fee split of address
//get peerCost
peerCost, err := getPeerCost(native, contract, peerPubkey)
peerCost, stakeCost, err := getPeerCost(native, contract, peerPubkey)
if err != nil {
return fmt.Errorf("getPeerCost, getPeerCost error: %v", err)
}
amount := nodeAmount * (100 - peerCost) / 100
if stakeCost == 0 {
stakeCost = peerCost
}
if stakeCost == 101 { // in storage, 101 means 0, 0 means null
stakeCost = 0
}
var amount uint64
if native.Height > config.GetNewPeerCostHeight() {
stakeFee := new(big.Int).Sub(
new(big.Int).Mul(new(big.Int).SetUint64(nodeAmount), new(big.Int).SetUint64(totalPos)),
new(big.Int).Add(new(big.Int).SetUint64(initPos), new(big.Int).SetUint64(totalPos))).Uint64()
nodeFee := nodeAmount - stakeFee
amount = stakeFee*(100-stakeCost)/100 + nodeFee*(100-peerCost)/100
} else {
amount = nodeAmount * (100 - peerCost) / 100
}
var sumAmount uint64 = 0
iter := native.CacheDB.NewIterator(utils.ConcatKey(contract, AUTHORIZE_INFO_POOL, peerPubkeyPrefix))
defer iter.Release()
Expand Down
45 changes: 45 additions & 0 deletions smartcontract/service/native/governance/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,51 @@ func (this *SetPeerCostParam) Deserialization(source *common.ZeroCopySource) err
return nil
}

type SetFeePercentageParam struct {
PeerPubkey string
Address common.Address
PeerCost uint32
StakeCost uint32
}

func (this *SetFeePercentageParam) Serialization(sink *common.ZeroCopySink) error {
sink.WriteString(this.PeerPubkey)
sink.WriteVarBytes(this.Address[:])
utils.EncodeVarUint(sink, uint64(this.PeerCost))
utils.EncodeVarUint(sink, uint64(this.StakeCost))
return nil
}

func (this *SetFeePercentageParam) Deserialization(source *common.ZeroCopySource) error {
peerPubkey, err := utils.DecodeString(source)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
}
address, err := utils.DecodeAddress(source)
if err != nil {
return fmt.Errorf("utils.ReadAddress, deserialize address error: %v", err)
}
peerCost, err := utils.DecodeVarUint(source)
if err != nil {
return fmt.Errorf("serialization.ReadBool, deserialize peerCost error: %v", err)
}
stakeCost, err := utils.DecodeVarUint(source)
if err != nil {
return fmt.Errorf("serialization.ReadBool, deserialize stakeCost error: %v", err)
}
if peerCost > math.MaxUint32 {
return fmt.Errorf("peerCost larger than max of uint32")
}
if stakeCost > math.MaxUint32 {
return fmt.Errorf("stakeCost larger than max of uint32")
}
this.PeerPubkey = peerPubkey
this.Address = address
this.PeerCost = uint32(peerCost)
this.StakeCost = uint32(stakeCost)
return nil
}

type WithdrawFeeParam struct {
Address common.Address
}
Expand Down
48 changes: 24 additions & 24 deletions smartcontract/service/native/governance/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,12 @@ type CandidateSplitInfo struct {
type PeerAttributes struct {
PeerPubkey string
MaxAuthorize uint64 //max authorzie pos this peer can receive(number of ont), set by peer owner
T2PeerCost uint64 //candidate or consensus node doesn't share income percent with authorize users, 100 means node will take all incomes, it will take effect in view T + 2
T1PeerCost uint64 //candidate or consensus node doesn't share income percent with authorize users, 100 means node will take all incomes, it will take effect in view T + 1
TPeerCost uint64 //candidate or consensus node doesn't share income percent with authorize users, 100 means node will take all incomes, it will take effect in view T
Field1 []byte //reserved field
Field2 []byte //reserved field
Field3 []byte //reserved field
T2PeerCost uint64 //candidate or consensus node doesn't share initpos income percent with authorize users, 100 means node will take all incomes, it will take effect in view T + 2
T1PeerCost uint64 //candidate or consensus node doesn't share initpos income percent with authorize users, 100 means node will take all incomes, it will take effect in view T + 1
TPeerCost uint64 //candidate or consensus node doesn't share initpos income percent with authorize users, 100 means node will take all incomes, it will take effect in view T
T2StakeCost uint64 //candidate or consensus node doesn't share stake income percent with authorize users, it will take effect in view T + 2, 101 means 0, 0 means null
T1StakeCost uint64 //candidate or consensus node doesn't share stake income percent with authorize users, it will take effect in view T + 1, 101 means 0, 0 means null
TStakeCost uint64 //candidate or consensus node doesn't share stake income percent with authorize users, it will take effect in view T, 101 means 0, 0 means null
Field4 []byte //reserved field
}

Expand All @@ -415,57 +415,57 @@ func (this *PeerAttributes) Serialization(sink *common.ZeroCopySink) {
sink.WriteUint64(this.T2PeerCost)
sink.WriteUint64(this.T1PeerCost)
sink.WriteUint64(this.TPeerCost)
sink.WriteVarBytes(this.Field1)
sink.WriteVarBytes(this.Field2)
sink.WriteVarBytes(this.Field3)
utils.EncodeVarUint(sink, this.T2StakeCost)
utils.EncodeVarUint(sink, this.T1StakeCost)
utils.EncodeVarUint(sink, this.TStakeCost)
sink.WriteVarBytes(this.Field4)
}

func (this *PeerAttributes) Deserialization(source *common.ZeroCopySource) error {
peerPubkey, err := utils.DecodeString(source)
if err != nil {
return fmt.Errorf("serialization.ReadString, deserialize peerPubkey error: %v", err)
return fmt.Errorf("utils.DecodeString, deserialize peerPubkey error: %v", err)
}
maxAuthorize, eof := source.NextUint64()
if eof {
return fmt.Errorf("serialization.ReadBool, deserialize maxAuthorize error: %v", io.ErrUnexpectedEOF)
return fmt.Errorf("source.NextUint64, deserialize maxAuthorize error: %v", io.ErrUnexpectedEOF)
}
t2PeerCost, eof := source.NextUint64()
if eof {
return fmt.Errorf("serialization.ReadUint64, deserialize t2PeerCost error: %v", io.ErrUnexpectedEOF)
return fmt.Errorf("source.NextUint64, deserialize t2PeerCost error: %v", io.ErrUnexpectedEOF)
}
t1PeerCost, eof := source.NextUint64()
if eof {
return fmt.Errorf("serialization.ReadUint64, deserialize t1PeerCost error: %v", io.ErrUnexpectedEOF)
return fmt.Errorf("source.NextUint64, deserialize t1PeerCost error: %v", io.ErrUnexpectedEOF)
}
tPeerCost, eof := source.NextUint64()
if eof {
return fmt.Errorf("serialization.ReadUint64, deserialize tPeerCost error: %v", io.ErrUnexpectedEOF)
return fmt.Errorf("source.NextUint64, deserialize tPeerCost error: %v", io.ErrUnexpectedEOF)
}
field1, err := utils.DecodeVarBytes(source)
t2StakeCost, err := utils.DecodeVarUint(source)
if err != nil {
return fmt.Errorf("serialization.ReadVarBytes. deserialize field1 error: %v", err)
return fmt.Errorf("utils.DecodeVarUint, deserialize t2StakeCost error: %v", err)
}
field2, err := utils.DecodeVarBytes(source)
t1StakeCost, err := utils.DecodeVarUint(source)
if err != nil {
return fmt.Errorf("serialization.ReadVarBytes. deserialize field2 error: %v", err)
return fmt.Errorf("utils.DecodeVarUint, deserialize t1StakeCost error: %v", err)
}
field3, err := utils.DecodeVarBytes(source)
tStakeCost, err := utils.DecodeVarUint(source)
if err != nil {
return fmt.Errorf("serialization.ReadVarBytes, deserialize field3 error: %v", err)
return fmt.Errorf("utils.DecodeVarUint, deserialize tStakeCost error: %v", err)
}
field4, err := utils.DecodeVarBytes(source)
if err != nil {
return fmt.Errorf("serialization.ReadVarBytes. deserialize field4 error: %v", err)
return fmt.Errorf("utils.DecodeVarBytes, deserialize field4 error: %v", err)
}
this.PeerPubkey = peerPubkey
this.MaxAuthorize = maxAuthorize
this.T2PeerCost = t2PeerCost
this.T1PeerCost = t1PeerCost
this.TPeerCost = tPeerCost
this.Field1 = field1
this.Field2 = field2
this.Field3 = field3
this.T2StakeCost = t2StakeCost
this.T1StakeCost = t1StakeCost
this.TStakeCost = tStakeCost
this.Field4 = field4
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions smartcontract/service/native/governance/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,14 @@ func getPeerAttributes(native *native.NativeService, contract common.Address, pe
return peerAttributes, nil
}

func getPeerCost(native *native.NativeService, contract common.Address, peerPubkey string) (uint64, error) {
func getPeerCost(native *native.NativeService, contract common.Address, peerPubkey string) (uint64, uint64, error) {
//get peerAttributes
peerAttributes, err := getPeerAttributes(native, contract, peerPubkey)
if err != nil {
return 0, fmt.Errorf("getPeerAttributes error: %v", err)
return 0, 0, fmt.Errorf("getPeerAttributes error: %v", err)
}

return peerAttributes.TPeerCost, nil
return peerAttributes.TPeerCost, peerAttributes.TStakeCost, nil
}

func putPeerAttributes(native *native.NativeService, contract common.Address, peerAttributes *PeerAttributes) error {
Expand Down

0 comments on commit 5aebc29

Please sign in to comment.