Commit 408ddd34 authored by mdj33's avatar mdj33 Committed by vipwzw

proc filter tx

parent 93acdf5c
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
//"github.com/stretchr/testify/mock" //"github.com/stretchr/testify/mock"
"encoding/hex" "encoding/hex"
"errors"
"math/rand" "math/rand"
"testing" "testing"
"time" "time"
...@@ -116,20 +115,6 @@ func createTxsGroup(txs []*types.Transaction) ([]*types.Transaction, error) { ...@@ -116,20 +115,6 @@ func createTxsGroup(txs []*types.Transaction) ([]*types.Transaction, error) {
return group.Txs, nil return group.Txs, nil
} }
func TestGetBlockHashForkHeightOnMainChain(t *testing.T) {
para := new(client)
grpcClient := &typesmocks.Chain33Client{}
grpcClient.On("GetFork", mock.Anything, &types.ReqKey{Key: []byte("ForkBlockHash")}).Return(&types.Int64{Data: 1}, errors.New("err")).Once()
para.grpcClient = grpcClient
_, err := para.GetForkHeightOnMainChain("ForkBlockHash")
assert.NotNil(t, err)
grpcClient.On("GetFork", mock.Anything, &types.ReqKey{Key: []byte("ForkBlockHash")}).Return(&types.Int64{Data: 1}, nil).Once()
ret, err := para.GetForkHeightOnMainChain("ForkBlockHash")
assert.Nil(t, err)
assert.Equal(t, int64(1), ret)
}
func createTestTxs(t *testing.T) (*types.BlockDetail, []*types.Transaction, []*types.Transaction) { func createTestTxs(t *testing.T) (*types.BlockDetail, []*types.Transaction, []*types.Transaction) {
//all para tx group //all para tx group
tx5, err := createCrossParaTx("toB", 5) tx5, err := createCrossParaTx("toB", 5)
......
This diff is collapsed.
...@@ -8,19 +8,53 @@ import ( ...@@ -8,19 +8,53 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/hex" "encoding/hex"
"errors"
"sync/atomic"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
) )
func (client *client) GetBlockedSeq(hash []byte) (int64, error) { func (client *client) setLocalDb(set *types.LocalDBSet) error {
//from blockchain db //如果追赶上主链了,则落盘
blockedSeq, err := client.GetAPI().GetMainSequenceByHash(&types.ReqHash{Hash: hash}) if atomic.LoadInt32(&client.isCaughtUp) == 1 {
set.Txid = 1
}
msg := client.GetQueueClient().NewMessage("blockchain", types.EventSetValueByKey, set)
err := client.GetQueueClient().Send(msg, true)
if err != nil {
return err
}
resp, err := client.GetQueueClient().Wait(msg)
if err != nil {
return err
}
if resp.GetData().(*types.Reply).IsOk {
return nil
}
return errors.New(string(resp.GetData().(*types.Reply).GetMsg()))
}
func (client *client) getLocalDb(set *types.LocalDBGet, count int) ([][]byte, error) {
msg := client.GetQueueClient().NewMessage("blockchain", types.EventGetValueByKey, set)
err := client.GetQueueClient().Send(msg, true)
if err != nil {
return nil, err
}
resp, err := client.GetQueueClient().Wait(msg)
if err != nil { if err != nil {
return -2, err return nil, err
} }
return blockedSeq.Data, nil
reply := resp.GetData().(*types.LocalReplyValue)
if len(reply.Values) != count {
plog.Error("Parachain getLocalDb count not match", "expert", count, "real", len(reply.Values))
return nil, types.ErrInvalidParam
}
return reply.Values, nil
} }
func (client *client) GetBlockByHeight(height int64) (*types.Block, error) { func (client *client) GetBlockByHeight(height int64) (*types.Block, error) {
...@@ -62,16 +96,6 @@ func (client *client) getLastBlockInfo() (*types.Block, error) { ...@@ -62,16 +96,6 @@ func (client *client) getLastBlockInfo() (*types.Block, error) {
return lastBlock, nil return lastBlock, nil
} }
func (client *client) GetForkHeightOnMainChain(key string) (int64, error) {
ret, err := client.grpcClient.GetFork(context.Background(), &types.ReqKey{Key: []byte(key)})
if err != nil {
plog.Error("para get rpc ForkHeight fail", "key", key, "err", err.Error())
return types.MaxHeight, err
}
return ret.Data, nil
}
func (client *client) GetLastHeightOnMainChain() (int64, error) { func (client *client) GetLastHeightOnMainChain() (int64, error) {
header, err := client.grpcClient.GetLastHeader(context.Background(), &types.ReqNil{}) header, err := client.grpcClient.GetLastHeader(context.Background(), &types.ReqNil{})
if err != nil { if err != nil {
...@@ -91,15 +115,6 @@ func (client *client) GetLastSeqOnMainChain() (int64, error) { ...@@ -91,15 +115,6 @@ func (client *client) GetLastSeqOnMainChain() (int64, error) {
return seq.Data, nil return seq.Data, nil
} }
func (client *client) GetSeqByHeightOnMainChain(height int64) (int64, []byte, error) {
hash, err := client.GetHashByHeightOnMainChain(height)
if err != nil {
return -1, nil, err
}
seq, err := client.GetSeqByHashOnMainChain(hash)
return seq, hash, err
}
func (client *client) GetHashByHeightOnMainChain(height int64) ([]byte, error) { func (client *client) GetHashByHeightOnMainChain(height int64) ([]byte, error) {
reply, err := client.grpcClient.GetBlockHash(context.Background(), &types.ReqInt{Height: height}) reply, err := client.grpcClient.GetBlockHash(context.Background(), &types.ReqInt{Height: height})
if err != nil { if err != nil {
...@@ -136,15 +151,15 @@ func (client *client) GetBlockOnMainBySeq(seq int64) (*types.BlockSeq, error) { ...@@ -136,15 +151,15 @@ func (client *client) GetBlockOnMainBySeq(seq int64) (*types.BlockSeq, error) {
return blockSeq, nil return blockSeq, nil
} }
func (client *client) GetBlockOnMainByHash(hash []byte) (*types.Block, error) { //func (client *client) GetParaTxByTitle(req *pt.ReqParaTxByTitle) (*pt.ParaTxDetails, error) {
blocks, err := client.grpcClient.GetBlockByHashes(context.Background(), &types.ReqHashes{Hashes: [][]byte{hash}}) // txDetails, err := client.grpcClient.GetParaTxByTitle(context.Background(), req)
if err != nil || blocks.Items[0] == nil { // if err != nil {
plog.Error("GetBlockOnMainByHash Not found", "blockhash", common.ToHex(hash)) // plog.Error("GetParaTxByTitle wrong", "err", err.Error(),"start",req.Start,"end",req.End)
return nil, err // return nil, err
} // }
//
return blocks.Items[0].Block, nil // return txDetails, nil
} //}
func (client *client) QueryTxOnMainByHash(hash []byte) (*types.TransactionDetail, error) { func (client *client) QueryTxOnMainByHash(hash []byte) (*types.TransactionDetail, error) {
detail, err := client.grpcClient.QueryTransaction(context.Background(), &types.ReqHash{Hash: hash}) detail, err := client.grpcClient.QueryTransaction(context.Background(), &types.ReqHash{Hash: hash})
......
...@@ -759,7 +759,7 @@ func getCrossTxHashsByRst(api client.QueueProtocolAPI, status *pt.ParacrossNodeS ...@@ -759,7 +759,7 @@ func getCrossTxHashsByRst(api client.QueueProtocolAPI, status *pt.ParacrossNodeS
} }
//抽取平行链交易和跨链交易 //抽取平行链交易和跨链交易
paraAllTxs := FilterTxsForPara(status.Title, blockDetail) paraAllTxs := FilterTxsForParaByBlock(status.Title, blockDetail)
var baseHashs [][]byte var baseHashs [][]byte
for _, tx := range paraAllTxs { for _, tx := range paraAllTxs {
baseHashs = append(baseHashs, tx.Hash()) baseHashs = append(baseHashs, tx.Hash())
...@@ -790,7 +790,7 @@ func getCrossTxHashs(api client.QueueProtocolAPI, status *pt.ParacrossNodeStatus ...@@ -790,7 +790,7 @@ func getCrossTxHashs(api client.QueueProtocolAPI, status *pt.ParacrossNodeStatus
return nil, nil, err return nil, nil, err
} }
//校验 //校验
paraBaseTxs := FilterTxsForPara(status.Title, blockDetail) paraBaseTxs := FilterTxsForParaByBlock(status.Title, blockDetail)
paraCrossHashs := FilterParaCrossTxHashes(status.Title, paraBaseTxs) paraCrossHashs := FilterParaCrossTxHashes(status.Title, paraBaseTxs)
var baseHashs [][]byte var baseHashs [][]byte
for _, tx := range paraBaseTxs { for _, tx := range paraBaseTxs {
......
...@@ -46,33 +46,7 @@ func checkReceiptExecOk(receipt *types.ReceiptData) bool { ...@@ -46,33 +46,7 @@ func checkReceiptExecOk(receipt *types.ReceiptData) bool {
// 1, 主链+平行链 user.p.xx.paracross 交易组 混合跨链资产转移 paracross主链执行成功 // 1, 主链+平行链 user.p.xx.paracross 交易组 混合跨链资产转移 paracross主链执行成功
// 2, 平行链 user.p.xx.paracross + user.p.xx.other 混合平行链组合 paracross主链执行成功 // 2, 平行链 user.p.xx.paracross + user.p.xx.other 混合平行链组合 paracross主链执行成功
// 3, 平行链 user.p.xx.other 交易组 混合平行链组合 other主链pack // 3, 平行链 user.p.xx.other 交易组 混合平行链组合 other主链pack
func filterParaTxGroup(title string, tx *types.Transaction, main *types.BlockDetail, index int, forkHeight int64) ([]*types.Transaction, int) { func filterParaTxGroup(title string, tx *types.Transaction, allTxs []*pt.TxDetail, index int, blockHeight, forkHeight int64) ([]*types.Transaction, int) {
var headIdx int
for i := index; i >= 0; i-- {
if bytes.Equal(tx.Header, main.Block.Txs[i].Hash()) {
headIdx = i
break
}
}
endIdx := headIdx + int(tx.GroupCount)
for i := headIdx; i < endIdx; i++ {
if types.IsPara() && main.Block.Height < forkHeight {
if types.IsSpecificParaExecName(title, string(main.Block.Txs[i].Execer)) {
continue
}
}
if !checkReceiptExecOk(main.Receipts[i]) {
return nil, endIdx
}
}
//全部是平行链交易 或平行链在主链执行成功的tx
return main.Block.Txs[headIdx:endIdx], endIdx
}
func filterParaTxGroupPlus(title string, tx *types.Transaction, allTxs []*pt.TxDetail, index int, blockHeight, forkHeight int64) ([]*types.Transaction, int) {
var headIdx int var headIdx int
for i := index; i >= 0; i-- { for i := index; i >= 0; i-- {
...@@ -103,38 +77,14 @@ func filterParaTxGroupPlus(title string, tx *types.Transaction, allTxs []*pt.TxD ...@@ -103,38 +77,14 @@ func filterParaTxGroupPlus(title string, tx *types.Transaction, allTxs []*pt.TxD
} }
//FilterTxsForPara include some main tx in tx group before ForkParacrossCommitTx //FilterTxsForPara include some main tx in tx group before ForkParacrossCommitTx
func FilterTxsForPara(title string, main *types.BlockDetail) []*types.Transaction { func FilterTxsForPara(title string, main *pt.ParaTxDetail) []*types.Transaction {
var txs []*types.Transaction
forkHeight := pt.GetDappForkHeight(pt.ForkCommitTx)
for i := 0; i < len(main.Block.Txs); i++ {
tx := main.Block.Txs[i]
if types.IsSpecificParaExecName(title, string(tx.Execer)) {
if tx.GroupCount >= 2 {
mainTxs, endIdx := filterParaTxGroup(title, tx, main, i, forkHeight)
txs = append(txs, mainTxs...)
i = endIdx - 1
continue
}
//单独的paracross tx 如果主链执行失败也要排除, 6.2fork原因 没有排除 非user.p.xx.paracross的平行链交易
if main.Block.Height >= forkHeight && bytes.HasSuffix(tx.Execer, []byte(pt.ParaX)) && !checkReceiptExecOk(main.Receipts[i]) {
continue
}
txs = append(txs, tx)
}
}
return txs
}
//FilterTxsForParaPlus include some main tx in tx group before ForkParacrossCommitTx
func FilterTxsForParaPlus(title string, main *pt.ParaTxDetail) []*types.Transaction {
var txs []*types.Transaction var txs []*types.Transaction
forkHeight := pt.GetDappForkHeight(pt.ForkCommitTx) forkHeight := pt.GetDappForkHeight(pt.ForkCommitTx)
for i := 0; i < len(main.TxDetails); i++ { for i := 0; i < len(main.TxDetails); i++ {
tx := main.TxDetails[i].Tx tx := main.TxDetails[i].Tx
if types.IsSpecificParaExecName(title, string(tx.Execer)) { if types.IsSpecificParaExecName(title, string(tx.Execer)) {
if tx.GroupCount >= 2 { if tx.GroupCount >= 2 {
mainTxs, endIdx := filterParaTxGroupPlus(title, tx, main.TxDetails, i, main.Header.Height, forkHeight) mainTxs, endIdx := filterParaTxGroup(title, tx, main.TxDetails, i, main.Header.Height, forkHeight)
txs = append(txs, mainTxs...) txs = append(txs, mainTxs...)
i = endIdx - 1 i = endIdx - 1
continue continue
...@@ -150,6 +100,12 @@ func FilterTxsForParaPlus(title string, main *pt.ParaTxDetail) []*types.Transact ...@@ -150,6 +100,12 @@ func FilterTxsForParaPlus(title string, main *pt.ParaTxDetail) []*types.Transact
return txs return txs
} }
//FilterTxsForParaByBlock include some main tx in tx group before ForkParacrossCommitTx
func FilterTxsForParaByBlock(title string, main *types.BlockDetail) []*types.Transaction {
txDetail := BlockDetail2ParaTxs(0, main.Block.Hash(), main)
return FilterTxsForPara(title, txDetail)
}
// FilterParaCrossTxHashes only all para chain cross txs like xx.paracross exec // FilterParaCrossTxHashes only all para chain cross txs like xx.paracross exec
func FilterParaCrossTxHashes(title string, txs []*types.Transaction) [][]byte { func FilterParaCrossTxHashes(title string, txs []*types.Transaction) [][]byte {
var txHashs [][]byte var txHashs [][]byte
......
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