Commit 913fc189 authored by madengji's avatar madengji Committed by vipwzw

miner register adjust

parent cf01c685
......@@ -6,6 +6,7 @@ package executor
import (
"github.com/33cn/chain33/types"
"github.com/33cn/plugin/plugin/dapp/paracross/executor/minerrewards"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/pkg/errors"
)
......@@ -71,31 +72,16 @@ func (a *action) isSelfConsensOn(miner *pt.ParacrossMinerAction) (bool, error) {
return isSelfConsensOn, nil
}
const (
normalMiner = iota
halveMiner
customMiner
)
type rewardValFn func(cfg *types.Chain33Config, height int64) (int64, int64, int64)
var getConfigRewards = make(map[int]rewardValFn)
func init() {
getConfigRewards[normalMiner] = getNormalReward
}
func (a *action) issueCoins(miner *pt.ParacrossMinerAction) (*types.Receipt, error) {
cfg := a.api.GetConfig()
mode := int(cfg.MGInt("mver.consensus.paracross.minerMode", a.height))
if getConfigRewards[mode] == nil {
if minerrewards.MinerRewards[mode] == nil {
panic("getTotalReward not be set depend on consensus.paracross.minerMode")
}
coinReward, coinFundReward, _ := getConfigRewards[mode](cfg, a.height)
coinReward, coinFundReward, _ := minerrewards.MinerRewards[mode].GetConfigReward(cfg, a.height)
totalReward := coinReward + coinFundReward
if totalReward > 0 {
issueReceipt, err := a.coinsAccount.ExecIssueCoins(a.execaddr, totalReward)
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
package minerrewards
import (
"fmt"
......@@ -29,6 +29,8 @@ const (
var addrs = []string{addrStaffA, addrStaffB, addrBoss}
var addrsMap = make(map[string]int64)
type custom struct{}
func checkQuota() {
var sum int64
for _, a := range addrs {
......@@ -46,13 +48,11 @@ func checkQuota() {
}
func init() {
getConfigRewards[customMiner] = getCustomReward
rewardMiner[customMiner] = customRewardMiner
register(customMiner, &custom{})
checkQuota()
}
func getCustomReward(cfg *types.Chain33Config, height int64) (int64, int64, int64) {
func (c *custom) GetConfigReward(cfg *types.Chain33Config, height int64) (int64, int64, int64) {
n := getCurrentN(height)
return calcCoins(n), 0, 0
......@@ -94,12 +94,12 @@ func calcCoins(n uint32) int64 {
return int64(math.Trunc(float64(vf)))
}
func customRewardMiner(coinReward int64, miners []string, height int64) ([]*pt.ParaMinerReward, int64) {
func (c *custom) RewardMiners(coinReward int64, miners []string, height int64) ([]*pt.ParaMinerReward, int64) {
//找零
var change int64
var rewards []*pt.ParaMinerReward
coins, _, _ := getCustomReward(nil, height)
coins, _, _ := c.GetConfigReward(nil, height)
var sum int64
//get quto to miner
for _, m := range miners {
......
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
package minerrewards
import (
"fmt"
......
package minerrewards
import (
"github.com/33cn/chain33/types"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
)
type normal struct {
}
func init() {
register(normalMiner, &normal{})
}
func (n *normal) RewardMiners(coinReward int64, miners []string, height int64) ([]*pt.ParaMinerReward, int64) {
//找零
var change int64
var rewards []*pt.ParaMinerReward
//分配给矿工的平均奖励
minerUnit := coinReward / int64(len(miners))
if minerUnit > 0 {
for _, m := range miners {
r := &pt.ParaMinerReward{Addr: m, Amount: minerUnit}
rewards = append(rewards, r)
}
//如果不等分转到发展基金
change = coinReward % minerUnit
}
return rewards, change
}
func (n *normal) GetConfigReward(cfg *types.Chain33Config, height int64) (int64, int64, int64) {
coinReward := cfg.MGInt("mver.consensus.paracross.coinReward", height)
fundReward := cfg.MGInt("mver.consensus.paracross.coinDevFund", height)
coinBaseReward := cfg.MGInt("mver.consensus.paracross.coinBaseReward", height)
if coinReward < 0 || fundReward < 0 || coinBaseReward < 0 {
panic("para config consensus.paracross.coinReward should bigger than 0")
}
//decimalMode=false,意味着精简模式,需要乘1e8
decimalMode := cfg.MIsEnable("mver.consensus.paracross.decimalMode", height)
if !decimalMode {
coinReward *= types.Coin
fundReward *= types.Coin
coinBaseReward *= types.Coin
}
//防止coinBaseReward 设置出错场景, coinBaseReward 一定要比coinReward小
if coinBaseReward >= coinReward {
coinBaseReward = coinReward / 10
}
return coinReward, fundReward, coinBaseReward
}
package minerrewards
import (
"fmt"
"github.com/33cn/chain33/types"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
)
type RewardPolicy interface {
GetConfigReward(cfg *types.Chain33Config, height int64) (int64, int64, int64)
RewardMiners(coinReward int64, miners []string, height int64) ([]*pt.ParaMinerReward, int64)
}
const (
normalMiner = iota
halveMiner
customMiner
)
var MinerRewards = make(map[int]RewardPolicy)
func register(ty int, policy RewardPolicy) {
if _, ok := MinerRewards[ty]; ok {
panic(fmt.Sprintf("paracross minerreward ty=%d registered", ty))
}
MinerRewards[ty] = policy
}
......@@ -3,6 +3,8 @@ package executor
import (
"bytes"
"github.com/33cn/plugin/plugin/dapp/paracross/executor/minerrewards"
"github.com/pkg/errors"
dbm "github.com/33cn/chain33/common/db"
......@@ -40,14 +42,6 @@ func (a *action) getBindAddrs(nodes []string, statusHeight int64) (*pt.ParaNodeB
}
type rewardFn func(coinReward int64, miners []string, height int64) ([]*pt.ParaMinerReward, int64)
var rewardMiner = make(map[int]rewardFn)
func init() {
rewardMiner[normalMiner] = rewardEven
}
func rewardEven(coinReward int64, miners []string, height int64) ([]*pt.ParaMinerReward, int64) {
//找零
var change int64
......@@ -71,10 +65,10 @@ func (a *action) rewardSuperNode(coinReward int64, miners []string, statusHeight
receipt := &types.Receipt{Ty: types.ExecOk}
mode := int(cfg.MGInt("mver.consensus.paracross.minerMode", a.height))
if rewardMiner[mode] == nil {
if minerrewards.MinerRewards[mode] == nil {
panic("getReward not be set depend on consensus.paracross.minerMode")
}
rewards, change := rewardMiner[mode](coinReward, miners, statusHeight)
rewards, change := minerrewards.MinerRewards[mode].RewardMiners(coinReward, miners, statusHeight)
resp, err := a.rewardDeposit(rewards, statusHeight)
if err != nil {
return nil, 0, err
......@@ -139,29 +133,6 @@ func (a *action) rewardBindAddr(coinReward int64, bindList *pt.ParaNodeBindList,
return receipt, change, nil
}
func getNormalReward(cfg *types.Chain33Config, height int64) (int64, int64, int64) {
coinReward := cfg.MGInt("mver.consensus.paracross.coinReward", height)
fundReward := cfg.MGInt("mver.consensus.paracross.coinDevFund", height)
coinBaseReward := cfg.MGInt("mver.consensus.paracross.coinBaseReward", height)
if coinReward < 0 || fundReward < 0 || coinBaseReward < 0 {
panic("para config consensus.paracross.coinReward should bigger than 0")
}
//decimalMode=false,意味着精简模式,需要乘1e8
decimalMode := cfg.MIsEnable("mver.consensus.paracross.decimalMode", height)
if !decimalMode {
coinReward *= types.Coin
fundReward *= types.Coin
coinBaseReward *= types.Coin
}
//防止coinBaseReward 设置出错场景, coinBaseReward 一定要比coinReward小
if coinBaseReward >= coinReward {
coinBaseReward = coinReward / 10
}
return coinReward, fundReward, coinBaseReward
}
// reward 挖矿奖励, 主要处理挖矿分配逻辑,先实现基本策略,后面根据需求进行重构
func (a *action) reward(nodeStatus *pt.ParacrossNodeStatus, stat *pt.ParacrossHeightStatus) (*types.Receipt, error) {
//获取挖矿相关配置,这里需注意是共识的高度,而不是交易的高度
......@@ -172,7 +143,7 @@ func (a *action) reward(nodeStatus *pt.ParacrossNodeStatus, stat *pt.ParacrossHe
}
mode := int(cfg.MGInt("mver.consensus.paracross.minerMode", a.height))
coinReward, fundReward, coinBaseReward := getConfigRewards[mode](cfg, nodeStatus.Height)
coinReward, fundReward, coinBaseReward := minerrewards.MinerRewards[mode].GetConfigReward(cfg, nodeStatus.Height)
fundAddr := cfg.MGStr("mver.consensus.fundKeyAddr", nodeStatus.Height)
//超级节点地址
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment