Commit ce76e9bc authored by madengji's avatar madengji Committed by 33cn

para bind miner fix kvmvcc issue

parent 6d6d870f
......@@ -556,7 +556,7 @@ func createNodeBindTx(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stderr, "coins should bigger than 0")
}
payload := &pt.ParaBindMinerInfo{BindAction: int32(action), BindCount: int64(coins), TargetNode: node}
payload := &pt.ParaBindMinerCmd{BindAction: int32(action), BindCoins: int64(coins), TargetNode: node}
params := &rpctypes.CreateTxIn{
Execer: getRealExecName(paraName, pt.ParaX),
ActionName: "ParaBindMiner",
......
......@@ -300,13 +300,16 @@ func (a *action) getNodesGroup(title string) (map[string]struct{}, []string, err
}
func (a *action) isValidSuperNode(addr string) (bool, error) {
func (a *action) isValidSuperNode(addr string) error {
cfg := a.api.GetConfig()
nodes, _, err := getParacrossNodes(a.db, cfg.GetTitle())
if err != nil {
return false, errors.Wrapf(err, "getNodes for title:%s", cfg.GetTitle())
return errors.Wrapf(err, "getNodes for title:%s", cfg.GetTitle())
}
return validNode(addr, nodes), nil
if !validNode(addr, nodes) {
return errors.Wrapf(pt.ErrParaNodeAddrNotExisted, "invalid node=%s", addr)
}
return nil
}
//相同的BlockHash,只保留一份数据
......
......@@ -121,7 +121,7 @@ func (e *Paracross) Exec_SelfStageConfig(payload *pt.ParaStageConfig, tx *types.
}
//Exec_ParaBindMiner node group config process
func (e *Paracross) Exec_ParaBindMiner(payload *pt.ParaBindMinerInfo, tx *types.Transaction, index int) (*types.Receipt, error) {
func (e *Paracross) Exec_ParaBindMiner(payload *pt.ParaBindMinerCmd, tx *types.Transaction, index int) (*types.Receipt, error) {
a := newAction(e, tx)
return a.bindMiner(payload)
}
......@@ -558,12 +558,5 @@ func (p *Paracross) Query_GetNodeBindMinerList(in *types.ReqString) (types.Messa
resp.Details = append(resp.Details, info)
}
info, err := getBindAddrInfo(p.GetStateDB(), in.Data, "1E5saiXVb9mW8wcWUUZjsHJPZs5GmdzuSY")
if err != nil {
clog.Error("Query_GetNodeBindMinerList get addr", "err", err, "node", in.Data, "addr", "1E5saiXVb9mW8wcWUUZjsHJPZs5GmdzuSY")
return nil, errors.Cause(err)
}
resp.Details = append(resp.Details, info)
return &resp, nil
}
......@@ -89,7 +89,7 @@ func (a *action) rewardBindAddr(coinReward int64, bindList []*pt.ParaNodeBindLis
var totalCoins int64
for _, addr := range bindAddrList {
totalCoins += addr.BindCount
totalCoins += addr.BindCoins
}
//分配给矿工的单位奖励
......@@ -100,10 +100,10 @@ func (a *action) rewardBindAddr(coinReward int64, bindList []*pt.ParaNodeBindLis
//如果不等分转到发展基金
change = coinReward % minerUnit
for _, miner := range bindAddrList {
rep, err := a.coinsAccount.ExecDeposit(miner.Addr, a.execaddr, minerUnit*miner.BindCount)
rep, err := a.coinsAccount.ExecDeposit(miner.Addr, a.execaddr, minerUnit*miner.BindCoins)
if err != nil {
clog.Error("paracross bind miner reward deposit err", "height", statusHeight,
"execAddr", a.execaddr, "minerAddr", miner.Addr, "amount", minerUnit*miner.BindCount, "err", err)
"execAddr", a.execaddr, "minerAddr", miner.Addr, "amount", minerUnit*miner.BindCoins, "err", err)
return nil, 0, err
}
receipt = mergeReceipt(receipt, rep)
......@@ -195,15 +195,11 @@ func makeAddrBindReceipt(node, addr string, prev, current *pt.ParaBindMinerInfo)
Prev: prev,
Current: current,
}
var val []byte
if current != nil {
val = types.Encode(current)
}
return &types.Receipt{
Ty: types.ExecOk,
KV: []*types.KeyValue{
{Key: key, Value: val},
{Key: key, Value: types.Encode(current)},
},
Logs: []*types.ReceiptLog{
{
......@@ -220,15 +216,11 @@ func makeNodeBindReceipt(addr string, prev, current *pt.ParaNodeBindList) *types
Prev: prev,
Current: current,
}
var val []byte
if current != nil {
val = types.Encode(current)
}
return &types.Receipt{
Ty: types.ExecOk,
KV: []*types.KeyValue{
{Key: key, Value: val},
{Key: key, Value: types.Encode(current)},
},
Logs: []*types.ReceiptLog{
{
......@@ -305,23 +297,15 @@ func getBindAddrInfo(db dbm.KV, node, addr string) (*pt.ParaBindMinerInfo, error
return &info, nil
}
func (a *action) bindOp(cmd *pt.ParaBindMinerInfo) (*types.Receipt, error) {
if len(cmd.Addr) > 0 && cmd.Addr != a.fromaddr {
return nil, errors.Wrapf(types.ErrInvalidParam, "bindMiner addr=%s not from addr %s", cmd.Addr, a.fromaddr)
func (a *action) bindOp(cmd *pt.ParaBindMinerCmd) (*types.Receipt, error) {
if cmd.BindCoins <= 0 {
return nil, errors.Wrapf(types.ErrInvalidParam, "bindMiner BindCoins nil from addr %s", a.fromaddr)
}
if cmd.BindCount <= 0 {
return nil, errors.Wrapf(types.ErrInvalidParam, "bindMiner bindCount nil from addr %s", a.fromaddr)
}
ok, err := a.isValidSuperNode(cmd.TargetNode)
err := a.isValidSuperNode(cmd.TargetNode)
if err != nil {
return nil, err
}
//非有效超级节点
if !ok {
return nil, errors.Wrapf(types.ErrInvalidParam, "invalid target node=%s", cmd.TargetNode)
}
current, err := getBindAddrInfo(a.db, cmd.TargetNode, a.fromaddr)
if err != nil && !isNotFound(errors.Cause(err)) {
......@@ -329,49 +313,49 @@ func (a *action) bindOp(cmd *pt.ParaBindMinerInfo) (*types.Receipt, error) {
}
//found, 修改当前的绑定
if current != nil {
if current != nil && current.BindStatus == opBind {
var receipt *types.Receipt
if cmd.BindCount == current.BindCount {
return nil, errors.Wrapf(types.ErrInvalidParam, "bind coins same current=%d, cmd=%d", current.BindCount, cmd.BindCount)
if cmd.BindCoins == current.BindCoins {
return nil, errors.Wrapf(types.ErrInvalidParam, "bind coins same current=%d, cmd=%d", current.BindCoins, cmd.BindCoins)
}
//释放一部分coins
if cmd.BindCount < current.BindCount {
receipt, err = a.coinsAccount.ExecActive(a.fromaddr, a.execaddr, (current.BindCount-cmd.BindCount)*types.Coin)
if cmd.BindCoins < current.BindCoins {
receipt, err = a.coinsAccount.ExecActive(a.fromaddr, a.execaddr, (current.BindCoins-cmd.BindCoins)*types.Coin)
if err != nil {
return nil, errors.Wrapf(err, "bindOp Active addr=%s,execaddr=%s,coins=%d", a.fromaddr, a.execaddr, current.BindCount-cmd.BindCount)
return nil, errors.Wrapf(err, "bindOp Active addr=%s,execaddr=%s,coins=%d", a.fromaddr, a.execaddr, current.BindCoins-cmd.BindCoins)
}
} else {
//冻结更多
receipt, err = a.coinsAccount.ExecFrozen(a.fromaddr, a.execaddr, (cmd.BindCount-current.BindCount)*types.Coin)
receipt, err = a.coinsAccount.ExecFrozen(a.fromaddr, a.execaddr, (cmd.BindCoins-current.BindCoins)*types.Coin)
if err != nil {
return nil, errors.Wrapf(err, "bindOp frozen more addr=%s,execaddr=%s,coins=%d", a.fromaddr, a.execaddr, cmd.BindCount-current.BindCount)
return nil, errors.Wrapf(err, "bindOp frozen more addr=%s,execaddr=%s,coins=%d", a.fromaddr, a.execaddr, cmd.BindCoins-current.BindCoins)
}
}
acctCopy := *current
current.BindCount = cmd.BindCount
current.BindCoins = cmd.BindCoins
r := makeAddrBindReceipt(cmd.TargetNode, a.fromaddr, &acctCopy, current)
return mergeReceipt(receipt, r), nil
}
//not found, 增加新绑定
receipt, err := a.coinsAccount.ExecFrozen(a.fromaddr, a.execaddr, cmd.BindCount*types.Coin)
//not bind, 增加新绑定
receipt, err := a.coinsAccount.ExecFrozen(a.fromaddr, a.execaddr, cmd.BindCoins*types.Coin)
if err != nil {
return nil, errors.Wrapf(err, "bindOp frozen addr=%s,execaddr=%s,count=%d", a.fromaddr, a.execaddr, cmd.BindCount)
return nil, errors.Wrapf(err, "bindOp frozen addr=%s,execaddr=%s,count=%d", a.fromaddr, a.execaddr, cmd.BindCoins)
}
//bind addr
acct := &pt.ParaBindMinerInfo{
newer := &pt.ParaBindMinerInfo{
Addr: a.fromaddr,
BindAction: opBind,
BindCount: cmd.BindCount,
BindStatus: opBind,
BindCoins: cmd.BindCoins,
BlockTime: a.blocktime,
BlockHeight: a.height,
TargetNode: cmd.TargetNode,
}
rBind := makeAddrBindReceipt(cmd.TargetNode, a.fromaddr, nil, acct)
rBind := makeAddrBindReceipt(cmd.TargetNode, a.fromaddr, current, newer)
mergeReceipt(receipt, rBind)
//增加到列表中
......@@ -384,7 +368,7 @@ func (a *action) bindOp(cmd *pt.ParaBindMinerInfo) (*types.Receipt, error) {
}
func (a *action) unBindOp(cmd *pt.ParaBindMinerInfo) (*types.Receipt, error) {
func (a *action) unBindOp(cmd *pt.ParaBindMinerCmd) (*types.Receipt, error) {
acct, err := getBindAddrInfo(a.db, cmd.TargetNode, a.fromaddr)
if err != nil {
return nil, err
......@@ -397,13 +381,18 @@ func (a *action) unBindOp(cmd *pt.ParaBindMinerInfo) (*types.Receipt, error) {
}
//unfrozen
receipt, err := a.coinsAccount.ExecActive(a.fromaddr, a.execaddr, acct.BindCount*types.Coin)
receipt, err := a.coinsAccount.ExecActive(a.fromaddr, a.execaddr, acct.BindCoins*types.Coin)
if err != nil {
return nil, errors.Wrapf(err, "unBindOp addr=%s,execaddr=%s,count=%d", a.fromaddr, a.execaddr, acct.BindCount)
return nil, errors.Wrapf(err, "unBindOp addr=%s,execaddr=%s,count=%d", a.fromaddr, a.execaddr, acct.BindCoins)
}
//删除 bind addr
rUnBind := makeAddrBindReceipt(cmd.TargetNode, a.fromaddr, acct, nil)
//由于kvmvcc的原因,不能通过把一个key值=nil的方式删除,kvmvcc这样是删除了当前版本,就会查询更早的版本,&struct{}也不行,len=0 也被认为是删除了的
acctCopy := *acct
acct.BindStatus = opUnBind
acct.BlockHeight = a.height
acct.BlockTime = a.blocktime
rUnBind := makeAddrBindReceipt(cmd.TargetNode, a.fromaddr, &acctCopy, acct)
mergeReceipt(receipt, rUnBind)
//从列表删除
......@@ -416,7 +405,7 @@ func (a *action) unBindOp(cmd *pt.ParaBindMinerInfo) (*types.Receipt, error) {
return receipt, nil
}
func (a *action) bindMiner(info *pt.ParaBindMinerInfo) (*types.Receipt, error) {
func (a *action) bindMiner(info *pt.ParaBindMinerCmd) (*types.Receipt, error) {
if len(info.TargetNode) <= 0 {
return nil, errors.Wrapf(types.ErrInvalidParam, "bindMiner TargetNode should not be nil to addr %s", a.fromaddr)
}
......
......@@ -159,10 +159,16 @@ message RespParacrossNodeGroups {
}
//para bind miner
message ParaBindMinerCmd{
int32 bindAction = 1; // 1: bind, 2:unbind
int64 bindCoins = 2; // bind coins count
string targetNode = 3; // super node addr
}
message ParaBindMinerInfo{
string addr = 1; // miner addr
int32 bindAction = 2; // 1: bind, 2:unbind
int64 bindCount = 3; // bind coins count
int32 bindStatus = 2; // 1: bind, 2:unbind
int64 bindCoins = 3; // bind coins count
string targetNode = 4; // super node addr
int64 blockTime = 5; // action bind block time
int64 blockHeight = 6; // action bind block height
......@@ -334,7 +340,7 @@ message ParacrossAction {
ParaNodeGroupConfig nodeGroupConfig = 10;
ParaStageConfig selfStageConfig = 11;
CrossAssetTransfer crossAssetTransfer = 12;
ParaBindMinerInfo paraBindMiner = 13;
ParaBindMinerCmd paraBindMiner = 13;
}
int32 ty = 2;
}
......
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