Skip to content

Commit

Permalink
update dapp fee split (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
siovanus authored and laizy committed Mar 21, 2019
1 parent 902dd12 commit 0c8b68e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 24 deletions.
15 changes: 13 additions & 2 deletions cmd/abi/native_abi_script/governance.json
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@
"type":"Int"
},
{
"name":"Field1",
"type":"ByteArray"
"name":"DappFee",
"type":"Int"
},
{
"name":"Field2",
Expand Down Expand Up @@ -552,6 +552,17 @@
}
],
"returnType":"Bool"
},
{
"name":"setGasAddress",
"parameters":
[
{
"name":"Address",
"type":"Address"
}
],
"returnType":"Bool"
}
],
"events":
Expand Down
35 changes: 31 additions & 4 deletions smartcontract/service/native/governance/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const (
ADD_INIT_POS = "addInitPos"
REDUCE_INIT_POS = "reduceInitPos"
SET_PROMISE_POS = "setPromisePos"
SET_GAS_ADDRESS = "setGasAddress"

//key prefix
GLOBAL_PARAM = "globalParam"
Expand All @@ -93,6 +94,7 @@ const (
SPLIT_FEE_ADDRESS = "splitFeeAddress"
PROMISE_POS = "promisePos"
PRE_CONFIG = "preConfig"
GAS_ADDRESS = "gasAddress"

//global
PRECISE = 1000000
Expand Down Expand Up @@ -148,6 +150,7 @@ func RegisterGovernanceContract(native *native.NativeService) {
native.Register(UPDATE_SPLIT_CURVE, UpdateSplitCurve)
native.Register(TRANSFER_PENALTY, TransferPenalty)
native.Register(SET_PROMISE_POS, SetPromisePos)
native.Register(SET_GAS_ADDRESS, SetGasAddress)
}

//Init governance contract, include vbft config, global param and ontid admin.
Expand Down Expand Up @@ -1163,10 +1166,6 @@ func UpdateGlobalParam2(native *native.NativeService) ([]byte, error) {
return utils.BYTE_FALSE, fmt.Errorf("deserialize, deserialize globalParam2 error: %v", err)
}

//check the globalParam
if globalParam2.MinAuthorizePos == 0 {
return utils.BYTE_FALSE, fmt.Errorf("globalParam2.MinAuthorizePos can not be 0")
}
// get config
config, err := getConfig(native, contract)
if err != nil {
Expand Down Expand Up @@ -1627,3 +1626,31 @@ func SetPromisePos(native *native.NativeService) ([]byte, error) {

return utils.BYTE_TRUE, nil
}

//set gas address to receive 50% of gas fee
func SetGasAddress(native *native.NativeService) ([]byte, error) {
// get operator from database
operatorAddress, err := global_params.GetStorageRole(native,
global_params.GenerateOperatorKey(utils.ParamContractAddress))
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("SetGasAddress, get operator error: %v", err)
}

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

param := new(GasAddress)
if err := param.Deserialization(common.NewZeroCopySource(native.Input)); err != nil {
return utils.BYTE_FALSE, fmt.Errorf("deserialize, contract params deserialize error: %v", err)
}
err = putGasAddress(native, contract, param)
if err != nil {
return utils.BYTE_FALSE, fmt.Errorf("put gasAddress error: %v", err)
}

return utils.BYTE_TRUE, nil
}
39 changes: 32 additions & 7 deletions smartcontract/service/native/governance/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,12 @@ func executeSplit2(native *native.NativeService, contract common.Address, view u
return splitSum, fmt.Errorf("getConfig, get config error: %v", err)
}

//get globalParam2
globalParam2, err := getGlobalParam2(native, contract)
if err != nil {
return splitSum, fmt.Errorf("getGlobalParam2, getGlobalParam2 error: %v", err)
}

//get peerPoolMap
peerPoolMap, err := GetPeerPoolMap(native, contract, view-1)
if err != nil {
Expand All @@ -828,7 +834,31 @@ func executeSplit2(native *native.NativeService, contract common.Address, view u
if err != nil {
return splitSum, fmt.Errorf("getSplitFee, getSplitFee error: %v", err)
}
if balance < splitFee {
panic("balance less than splitFee to withdraw!")
}
income := balance - splitFee

//fee split to dapp address
dappIncome := income * uint64(globalParam2.DappFee) / 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
} else {
err := appCallTransferOng(native, utils.GovernanceContractAddress, gasAddress.Address, dappIncome)
if err != nil {
return splitSum, fmt.Errorf("appCallTransferOng, appCallTransferOng error: %v", err)
}
}

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

//fee split of consensus peer
for i := 0; i < int(config.K); i++ {
nodeAmount := income * uint64(globalParam.A) / 100 * peersCandidate[i].S / sumS
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)
if err != nil {
Expand All @@ -894,11 +924,6 @@ func executeSplit2(native *native.NativeService, contract common.Address, view u

//fee split of candidate peer
//cal s of each candidate node
//get globalParam2
globalParam2, err := getGlobalParam2(native, contract)
if err != nil {
return splitSum, fmt.Errorf("getGlobalParam2, getGlobalParam2 error: %v", err)
}
var length int
if int(globalParam2.CandidateFeeSplitNum) >= len(peersCandidate) {
length = len(peersCandidate)
Expand All @@ -913,7 +938,7 @@ func executeSplit2(native *native.NativeService, contract common.Address, view u
return splitSum, nil
}
for i := int(config.K); i < length; i++ {
nodeAmount := income * uint64(globalParam.B) / 100 * peersCandidate[i].Stake / sum
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)
if err != nil {
Expand Down
50 changes: 39 additions & 11 deletions smartcontract/service/native/governance/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ func (this *GlobalParam) Deserialize(r io.Reader) error {
type GlobalParam2 struct {
MinAuthorizePos uint32 //min ONT of each authorization, 500 default
CandidateFeeSplitNum uint32 //num of peer can receive motivation(include consensus and candidate)
Field1 []byte //reserved field
DappFee uint32 //fee split to dapp bonus
Field2 []byte //reserved field
Field3 []byte //reserved field
Field4 []byte //reserved field
Expand All @@ -655,14 +655,20 @@ type GlobalParam2 struct {
}

func (this *GlobalParam2) Serialize(w io.Writer) error {
if this.MinAuthorizePos == 0 {
return fmt.Errorf("globalParam2.MinAuthorizePos can not be 0")
}
if this.DappFee > 100 {
return fmt.Errorf("globalParam2.DappFee must <= 100")
}
if err := utils.WriteVarUint(w, uint64(this.MinAuthorizePos)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize minAuthorizePos error: %v", err)
}
if err := utils.WriteVarUint(w, uint64(this.CandidateFeeSplitNum)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize candidateFeeSplitNum error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Field1); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize field1 error: %v", err)
if err := utils.WriteVarUint(w, uint64(this.DappFee)); err != nil {
return fmt.Errorf("utils.WriteVarUint, serialize dappFee error: %v", err)
}
if err := serialization.WriteVarBytes(w, this.Field2); err != nil {
return fmt.Errorf("serialization.WriteVarBytes, serialize field2 error: %v", err)
Expand Down Expand Up @@ -691,25 +697,25 @@ func (this *GlobalParam2) Deserialize(r io.Reader) error {
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize candidateFeeSplitNum error: %v", err)
}
field1, err := serialization.ReadVarBytes(r)
dappFee, err := utils.ReadVarUint(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize field1 error: %v", err)
return fmt.Errorf("utils.ReadVarUint, deserialize dappFee error: %v", err)
}
field2, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize field2 error: %v", err)
return fmt.Errorf("serialization.ReadVarBytes, deserialize field2 error: %v", err)
}
field3, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize field3 error: %v", err)
return fmt.Errorf("serialization.ReadVarBytes, deserialize field3 error: %v", err)
}
field4, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize field4 error: %v", err)
return fmt.Errorf("serialization.ReadVarBytes, deserialize field4 error: %v", err)
}
field5, err := serialization.ReadVarBytes(r)
if err != nil {
return fmt.Errorf("utils.ReadVarUint, deserialize field5 error: %v", err)
return fmt.Errorf("serialization.ReadVarBytes, deserialize field5 error: %v", err)
}
field6, err := serialization.ReadVarBytes(r)
if err != nil {
Expand All @@ -721,10 +727,15 @@ func (this *GlobalParam2) Deserialize(r io.Reader) error {
if candidateFeeSplitNum > math.MaxUint32 {
return fmt.Errorf("candidateFeeSplitNum larger than max of uint32")
}

if minAuthorizePos == 0 {
return fmt.Errorf("globalParam2.MinAuthorizePos can not be 0")
}
if dappFee > 100 {
return fmt.Errorf("globalParam2.DappFee must <= 100")
}
this.MinAuthorizePos = uint32(minAuthorizePos)
this.CandidateFeeSplitNum = uint32(candidateFeeSplitNum)
this.Field1 = field1
this.DappFee = uint32(dappFee)
this.Field2 = field2
this.Field3 = field3
this.Field4 = field4
Expand Down Expand Up @@ -992,3 +1003,20 @@ func (this *ChangeInitPosParam) Deserialize(r io.Reader) error {
this.Pos = uint32(pos)
return nil
}

type GasAddress struct {
Address common.Address
}

func (this *GasAddress) Serialization(sink *common.ZeroCopySink) {
utils.EncodeAddress(sink, this.Address)
}

func (this *GasAddress) Deserialization(source *common.ZeroCopySource) error {
address, err := utils.DecodeAddress(source)
if err != nil {
return fmt.Errorf("utils.DecodeAddress, deserialize address error: %v", err)
}
this.Address = address
return nil
}
26 changes: 26 additions & 0 deletions smartcontract/service/native/governance/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,29 @@ func putPromisePos(native *native.NativeService, contract common.Address, promis
cstates.GenRawStorageItem(bf.Bytes()))
return nil
}

func getGasAddress(native *native.NativeService, contract common.Address) (*GasAddress, error) {
gasAddressBytes, err := native.CacheDB.Get(utils.ConcatKey(contract, []byte(GAS_ADDRESS)))
if err != nil {
return nil, fmt.Errorf("get gasAddressBytes error: %v", err)
}
gasAddress := new(GasAddress)
if gasAddressBytes != nil {
gasAddressStore, err := cstates.GetValueFromRawStorageItem(gasAddressBytes)
if err != nil {
return nil, fmt.Errorf("get value from gasAddressBytes err:%v", err)
}
if err := gasAddress.Deserialization(common.NewZeroCopySource(gasAddressStore)); err != nil {
return nil, fmt.Errorf("deserialize, deserialize gasAddress error: %v", err)
}
}
return gasAddress, nil
}

func putGasAddress(native *native.NativeService, contract common.Address, gasAddress *GasAddress) error {
sink := common.NewZeroCopySink(nil)
gasAddress.Serialization(sink)
native.CacheDB.Put(utils.ConcatKey(contract, []byte(GAS_ADDRESS)),
cstates.GenRawStorageItem(sink.Bytes()))
return nil
}

0 comments on commit 0c8b68e

Please sign in to comment.