Repository: 33cn/plugin
Pull request #1051 (2021-09-14 07:15:39)

增加支持平行链配置无跨链资产交易高度列表
#fixes 1049

codecov[bot]: # [Codecov](https://codecov.io/gh/33cn/plugin/pull/1051?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#1051](https://codecov.io/gh/33cn/plugin/pull/1051?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (6f12591) into [master](https://codecov.io/gh/33cn/plugin/commit/a55e97874abdee1351746b162a2ed3cd7fbc84f0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (a55e978) will **increase** coverage by `0.03%`.
> The diff coverage is `n/a`.

> :exclamation: Current head 6f12591 differs from pull request most recent head 6b40e27. Consider uploading reports for the commit 6b40e27 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/33cn/plugin/pull/1051/graphs/tree.svg?width=650&height=150&src=pr&token=EwYhDbvpC8&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)](https://codecov.io/gh/33cn/plugin/pull/1051?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

```diff
@@            Coverage Diff             @@
##           master    #1051      +/-   ##
==========================================
+ Coverage   58.48%   58.52%   +0.03%     
==========================================
  Files         205      205              
  Lines       33129    33129              
==========================================
+ Hits        19376    19389      +13     
+ Misses      12075    12061      -14     
- Partials     1678     1679       +1     
```


| [Impacted Files](https://codecov.io/gh/33cn/plugin/pull/1051?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [plugin/consensus/raft/block.go](https://codecov.io/gh/33cn/plugin/pull/1051/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-cGx1Z2luL2NvbnNlbnN1cy9yYWZ0L2Jsb2NrLmdv) | `68.83% <0.00%> (-0.65%)` | :arrow_down: |
| [plugin/consensus/qbft/consensus\_state.go](https://codecov.io/gh/33cn/plugin/pull/1051/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-cGx1Z2luL2NvbnNlbnN1cy9xYmZ0L2NvbnNlbnN1c19zdGF0ZS5nbw==) | `60.91% <0.00%> (-0.30%)` | :arrow_down: |
| [plugin/consensus/qbft/peer\_set.go](https://codecov.io/gh/33cn/plugin/pull/1051/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-cGx1Z2luL2NvbnNlbnN1cy9xYmZ0L3BlZXJfc2V0Lmdv) | `63.89% <0.00%> (-0.13%)` | :arrow_down: |
| [plugin/consensus/raft/httpapi.go](https://codecov.io/gh/33cn/plugin/pull/1051/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-cGx1Z2luL2NvbnNlbnN1cy9yYWZ0L2h0dHBhcGkuZ28=) | `23.52% <0.00%> (ø)` | |
| [plugin/p2p/gossip/p2pserver.go](https://codecov.io/gh/33cn/plugin/pull/1051/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-cGx1Z2luL3AycC9nb3NzaXAvcDJwc2VydmVyLmdv) | `49.53% <0.00%> (+0.46%)` | :arrow_up: |
| [plugin/store/mpt/db2/trie.go](https://codecov.io/gh/33cn/plugin/pull/1051/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-cGx1Z2luL3N0b3JlL21wdC9kYjIvdHJpZS5nbw==) | `56.34% <0.00%> (+1.17%)` | :arrow_up: |
| [plugin/consensus/qbft/node.go](https://codecov.io/gh/33cn/plugin/pull/1051/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-cGx1Z2luL2NvbnNlbnN1cy9xYmZ0L25vZGUuZ28=) | `69.91% <0.00%> (+3.25%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/33cn/plugin/pull/1051?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/33cn/plugin/pull/1051?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Last update [a55e978...6b40e27](https://codecov.io/gh/33cn/plugin/pull/1051?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).


Patch:
diff --git a/chain33.toml b/chain33.toml
index 989f08b85..1d8ae9b0c 100644
--- a/chain33.toml
+++ b/chain33.toml
@@ -273,6 +273,10 @@ superManager=[
 nodeGroupFrozenCoins=0
 #平行链共识停止后主链等待的高度
 paraConsensusStopBlocks=30000
+#配置平行链资产跨链交易的高度列表，title省略user.p,不同title使用,分割，不同hit高度使用"."分割，
+#不同ignore范围高度使用"-"分割，hit高度在ignore范围内，为平行链自身的高度，不是主链高度
+#para.hit.10.50.250, para.ignore.1-100.200-300
+paraCrossAssetTxHeightList=[]
 
 [exec.sub.autonomy]
 total="16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp"
diff --git a/plugin/dapp/coinsx/commands/utils.go b/plugin/dapp/coinsx/commands/utils.go
index c5a3c9d25..72e6e76b6 100644
--- a/plugin/dapp/coinsx/commands/utils.go
+++ b/plugin/dapp/coinsx/commands/utils.go
@@ -7,6 +7,8 @@ package commands
 import (
 	"strings"
 
+	pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
+
 	"github.com/33cn/chain33/common/address"
 	"github.com/33cn/chain33/types"
 )
@@ -23,7 +25,7 @@ func GetExecAddr(exec string) (string, error) {
 }
 
 func getRealExecName(paraName string, name string) string {
-	if strings.HasPrefix(name, "user.p.") {
+	if strings.HasPrefix(name, pt.ParaPrefix) {
 		return name
 	}
 	return paraName + name
diff --git a/plugin/dapp/paracross/commands/utils.go b/plugin/dapp/paracross/commands/utils.go
index c5a3c9d25..72e6e76b6 100644
--- a/plugin/dapp/paracross/commands/utils.go
+++ b/plugin/dapp/paracross/commands/utils.go
@@ -7,6 +7,8 @@ package commands
 import (
 	"strings"
 
+	pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
+
 	"github.com/33cn/chain33/common/address"
 	"github.com/33cn/chain33/types"
 )
@@ -23,7 +25,7 @@ func GetExecAddr(exec string) (string, error) {
 }
 
 func getRealExecName(paraName string, name string) string {
-	if strings.HasPrefix(name, "user.p.") {
+	if strings.HasPrefix(name, pt.ParaPrefix) {
 		return name
 	}
 	return paraName + name
diff --git a/plugin/dapp/paracross/executor/action.go b/plugin/dapp/paracross/executor/action.go
index 5f1087760..b797c440c 100644
--- a/plugin/dapp/paracross/executor/action.go
+++ b/plugin/dapp/paracross/executor/action.go
@@ -7,6 +7,7 @@ package executor
 import (
 	"bytes"
 	"encoding/hex"
+	"strconv"
 	"strings"
 
 	"github.com/33cn/chain33/account"
@@ -981,6 +982,123 @@ func rollbackCrossTx(a *action, cross *types.TransactionDetail, crossTxHash []by
 
 }
 
+//无跨链交易高度列表是人为配置的，是确认的历史高度，是一种特殊处理，不会影响区块状态hash
+//para.ignore.10-100.200-300
+func isInIgnoreHeightList(str string, status *pt.ParacrossNodeStatus) (bool, error) {
+	if len(str) <= 0 {
+		return false, nil
+	}
+	e := strings.Split(str, ".")
+	if len(e) <= 2 {
+		return false, errors.Wrapf(types.ErrInvalidParam, "wrong config str=%s,title=%s", str, status.Title)
+	}
+	if strings.ToLower(pt.ParaPrefix+e[0]+".") != strings.ToLower(status.Title) {
+		return false, errors.Wrapf(types.ErrInvalidParam, "wrong title str=%s,title=%s", str, status.Title)
+	}
+
+	if e[1] != pt.ParaCrossAssetTxIgnoreKey {
+		return false, errors.Wrapf(types.ErrInvalidParam, "wrong ignore str=%s,title=%s", str, status.Title)
+	}
+
+	for _, h := range e[2:] {
+		p := strings.Split(h, "-")
+		if len(p) != 2 {
+			return false, errors.Wrapf(types.ErrInvalidParam, "check NoHeightCrossAsseList title=%s,height=%s", status.Title, h)
+		}
+		s, err := strconv.Atoi(p[0])
+		if err != nil {
+			return false, errors.Wrapf(err, "check NoHeightCrossAsseList title=%s,height=%s", status.Title, h)
+		}
+		e, err := strconv.Atoi(p[1])
+		if err != nil {
+			return false, errors.Wrapf(err, "check NoHeightCrossAsseList title=%s,height=%s", status.Title, h)
+		}
+		if s > e {
+			return false, errors.Wrapf(types.ErrInvalidParam, "check NoHeightCrossAsseList title=%s,height=%s", status.Title, h)
+		}
+
+		//共识的平行链高度(不是主链高度）落在范围内，说明此高度没有跨链资产交易，可以忽略
+		if status.Height >= int64(s) && status.Height <= int64(e) {
+			return true, nil
+		}
+	}
+	return false, nil
+}
+
+// "para.hit.10.100.200"
+func isInHitHeightList(str string, status *pt.ParacrossNodeStatus) (bool, error) {
+	if len(str) <= 0 {
+		return false, nil
+	}
+
+	e := strings.Split(str, ".")
+	if len(e) <= 2 {
+		return false, errors.Wrapf(types.ErrInvalidParam, "wrong config str=%s,title=%s", str, status.Title)
+	}
+	if strings.ToLower(pt.ParaPrefix+e[0]+".") != strings.ToLower(status.Title) {
+		return false, errors.Wrapf(types.ErrInvalidParam, "wrong title str=%s,title=%s", str, status.Title)
+	}
+	if e[1] != pt.ParaCrossAssetTxHitKey {
+		return false, errors.Wrapf(types.ErrInvalidParam, "wrong hit str=%s,title=%s", str, status.Title)
+	}
+
+	for _, hStr := range e[2:] {
+		h, err := strconv.Atoi(hStr)
+		if err != nil {
+			return false, errors.Wrapf(types.ErrInvalidParam, "wrong config str=%s in %s,title=%s", str, hStr, status.Title)
+		}
+		//高度命中
+		if status.Height == int64(h) {
+			return true, nil
+		}
+	}
+	return false, nil
+}
+
+//命中高度
+//s: para.hit.10.100, title=user.p.para.
+func checkIsIgnoreHeight(heightList []string, status *pt.ParacrossNodeStatus) (bool, error) {
+	if len(heightList) <= 0 {
+		return false, nil
+	}
+
+	var hitStr, ignoreStr string
+	hitPrefix := strings.ToLower(status.Title + pt.ParaCrossAssetTxHitKey)[len(pt.ParaPrefix):]
+	ignorePrefix := strings.ToLower(status.Title + pt.ParaCrossAssetTxIgnoreKey)[len(pt.ParaPrefix):]
+
+	for _, s := range heightList {
+		desStr := strings.ToLower(s)
+		if strings.HasPrefix(desStr, hitPrefix) {
+			if len(hitStr) > 0 {
+				return false, errors.Wrapf(types.ErrInvalidParam, "checkIsInIgnoreHeightList repeate=%s", hitPrefix)
+			}
+			hitStr = s
+		}
+		if strings.HasPrefix(desStr, ignorePrefix) {
+			if len(ignoreStr) > 0 {
+				return false, errors.Wrapf(types.ErrInvalidParam, "checkIsInIgnoreHeightList repeate=%s", ignorePrefix)
+			}
+			ignoreStr = s
+		}
+		if len(hitStr) > 0 && len(ignoreStr) > 0 {
+			break
+		}
+
+	}
+
+	in, err := isInHitHeightList(hitStr, status)
+	if err != nil {
+		return false, err
+	}
+	//如果在hit 列表中，不忽略
+	if in {
+		return false, nil
+	}
+
+	return isInIgnoreHeightList(ignoreStr, status)
+
+}
+
 func getCrossTxHashsByRst(api client.QueueProtocolAPI, status *pt.ParacrossNodeStatus) ([][]byte, []byte, error) {
 	//支持带版本号的跨链交易bitmap
 	//1.如果等于0，是老版本的平行链，按老的方式处理. 2. 如果大于0等于ver，新版本且没有跨链交易，不需要处理. 3. 大于ver，说明有跨链交易按老的方式处理
@@ -1001,6 +1119,19 @@ func getCrossTxHashsByRst(api client.QueueProtocolAPI, status *pt.ParacrossNodeS
 		}
 	}
 
+	//para.hit.6.8, para.ignore.1-10, 比如高度7， 如果命中则继续处理，如果没命中，检查是否在ignore列表，如果在直接退出，否则继续处理
+	//零散的命中列表可以减少忽略高度列表的范围
+	//此平行链高度在忽略检查跨链交易列表中,则直接退出
+	conf := types.ConfSub(api.GetConfig(), pt.ParaX)
+	heightList := conf.GStrList("paraCrossAssetTxHeightList")
+	ignore, err := checkIsIgnoreHeight(heightList, status)
+	if err != nil {
+		return nil, nil, err
+	}
+	if ignore {
+		return nil, nil, nil
+	}
+
 	blockDetail, err := GetBlock(api, status.MainBlockHash)
 	if err != nil {
 		return nil, nil, err
@@ -1033,6 +1164,19 @@ func getCrossTxHashs(api client.QueueProtocolAPI, status *pt.ParacrossNodeStatus
 		return nil, nil, types.ErrCheckTxHash
 	}
 
+	//para.hit.6.8, para.ignore.1-10, 比如高度7， 如果命中则继续处理，如果没命中，检查是否在ignore列表，如果在直接退出，否则继续处理
+	//零散的命中列表可以减少忽略高度列表的范围
+	//比如高度6，命中，则继续处理，高度7，未命中，但是在ignore scope,退出，高度11，未命中，也不在ignore scope,继续处理
+	conf := types.ConfSub(api.GetConfig(), pt.ParaX)
+	heightList := conf.GStrList("paraCrossAssetTxHeightList")
+	ignore, err := checkIsIgnoreHeight(heightList, status)
+	if err != nil {
+		return nil, nil, err
+	}
+	if ignore {
+		return nil, nil, nil
+	}
+
 	blockDetail, err := GetBlock(api, status.MainBlockHash)
 	if err != nil {
 		return nil, nil, err
@@ -1111,6 +1255,7 @@ func (a *action) execCrossTxs(status *pt.ParacrossNodeStatus) (*types.Receipt, e
 			if receiptCross == nil {
 				continue
 			}
+			clog.Debug("paracross.Commit commitDone.title ok ", "title", status.Title, "height", status.Height, "main", a.height, "i", i, "hash", common.ToHex(crossTxHashs[i]))
 			receipt.KV = append(receipt.KV, receiptCross.KV...)
 			receipt.Logs = append(receipt.Logs, receiptCross.Logs...)
 		} else {
@@ -1127,6 +1272,7 @@ func (a *action) execCrossTxs(status *pt.ParacrossNodeStatus) (*types.Receipt, e
 				if receiptCross == nil {
 					continue
 				}
+				clog.Debug("paracross.Commit commitDone.title rbk", "title", status.Title, "height", status.Height, "main", a.height, "i", i, "hash", common.ToHex(crossTxHashs[i]))
 				receipt.KV = append(receipt.KV, receiptCross.KV...)
 				receipt.Logs = append(receipt.Logs, receiptCross.Logs...)
 			}
diff --git a/plugin/dapp/paracross/types/paracross.go b/plugin/dapp/paracross/types/paracross.go
index 83fe9e21a..90b44bfa9 100644
--- a/plugin/dapp/paracross/types/paracross.go
+++ b/plugin/dapp/paracross/types/paracross.go
@@ -99,6 +99,14 @@ const (
 	ParaCrossStatusBitMapVer1   = "0001"
 )
 
+const ParaPrefix = "user.p."
+
+//配置跨链交易高度列表的prefix 比如hit.para.100.200,ignore.para.100-300
+const (
+	ParaCrossAssetTxHitKey    = "hit"
+	ParaCrossAssetTxIgnoreKey = "ignore"
+)
+
 //paracross asset porcess
 const (
 	ParacrossNoneTransfer = iota
diff --git a/plugin/dapp/token/commands/utils.go b/plugin/dapp/token/commands/utils.go
index c5a3c9d25..72e6e76b6 100644
--- a/plugin/dapp/token/commands/utils.go
+++ b/plugin/dapp/token/commands/utils.go
@@ -7,6 +7,8 @@ package commands
 import (
 	"strings"
 
+	pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
+
 	"github.com/33cn/chain33/common/address"
 	"github.com/33cn/chain33/types"
 )
@@ -23,7 +25,7 @@ func GetExecAddr(exec string) (string, error) {
 }
 
 func getRealExecName(paraName string, name string) string {
-	if strings.HasPrefix(name, "user.p.") {
+	if strings.HasPrefix(name, pt.ParaPrefix) {
 		return name
 	}
 	return paraName + name
diff --git a/plugin/dapp/unfreeze/commands/unfreeze.go b/plugin/dapp/unfreeze/commands/unfreeze.go
index 7295814bc..7f214d9df 100644
--- a/plugin/dapp/unfreeze/commands/unfreeze.go
+++ b/plugin/dapp/unfreeze/commands/unfreeze.go
@@ -11,6 +11,8 @@ import (
 	"os"
 	"strings"
 
+	pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
+
 	cmdtypes "github.com/33cn/chain33/system/dapp/commands/types"
 	"github.com/pkg/errors"
 
@@ -355,7 +357,7 @@ func show(cmd *cobra.Command, args []string) {
 }
 
 func getRealExecName(paraName string, name string) string {
-	if strings.HasPrefix(name, "user.p.") {
+	if strings.HasPrefix(name, pt.ParaPrefix) {
 		return name
 	}
 	return paraName + name


Test patch:
diff --git a/plugin/dapp/paracross/executor/paracross_test.go b/plugin/dapp/paracross/executor/paracross_test.go
index cdf1fce55..bc6896951 100644
--- a/plugin/dapp/paracross/executor/paracross_test.go
+++ b/plugin/dapp/paracross/executor/paracross_test.go
@@ -877,3 +877,45 @@ func TestVerifyBlsSign(t *testing.T) {
 	assert.Equal(t, nil, err)
 
 }
+
+func TestCheckIsIgnoreHeight(t *testing.T) {
+	status1 := &pt.ParacrossNodeStatus{
+		Title:  "user.p.MC.",
+		Height: 1000,
+	}
+	strList := []string{"mcc.hit.260", "mc.hit.7.9.250", "mc.ignore.1-100.200-300"}
+	isIn, err := checkIsIgnoreHeight(strList, status1)
+	assert.Nil(t, err)
+	assert.Equal(t, false, isIn)
+
+	status1.Height = 9
+	isIn, err = checkIsIgnoreHeight(strList, status1)
+	assert.Nil(t, err)
+	assert.Equal(t, false, isIn)
+	status1.Height = 250
+	isIn, err = checkIsIgnoreHeight(strList, status1)
+	assert.Nil(t, err)
+	assert.Equal(t, false, isIn)
+
+	status1.Height = 1
+	isIn, err = checkIsIgnoreHeight(strList, status1)
+	assert.Nil(t, err)
+	assert.Equal(t, true, isIn)
+
+	status1.Height = 10
+	isIn, err = checkIsIgnoreHeight(strList, status1)
+	assert.Nil(t, err)
+	assert.Equal(t, true, isIn)
+
+	status1.Height = 251
+	isIn, err = checkIsIgnoreHeight(strList, status1)
+	assert.Nil(t, err)
+	assert.Equal(t, true, isIn)
+
+	//mc和mcc不能混淆
+	status1.Height = 260
+	isIn, err = checkIsIgnoreHeight(strList, status1)
+	assert.Nil(t, err)
+	assert.Equal(t, true, isIn)
+
+}