Commit 94074959 authored by vipwzw's avatar vipwzw Committed by 33cn

fix test

parent c2cd9285
...@@ -122,7 +122,7 @@ func TestExecDrawLottery(t *testing.T) { ...@@ -122,7 +122,7 @@ func TestExecDrawLottery(t *testing.T) {
if !CompareLotteryExecResult(receipt, err, &targetReceipt, targetErr) { if !CompareLotteryExecResult(receipt, err, &targetReceipt, targetErr) {
t.Error(testNormErr) t.Error(testNormErr)
} }
lottery.SetEnv(100, 0, 0) lottery.SetEnv(100, 0, 0, nil, nil)
receipt, err = lottery.Exec(tx, 0) receipt, err = lottery.Exec(tx, 0)
targetErr = types.ErrActionNotSupport targetErr = types.ErrActionNotSupport
// ErrActionNotSupport case // ErrActionNotSupport case
......
...@@ -5,40 +5,17 @@ ...@@ -5,40 +5,17 @@
package executor package executor
import ( import (
"context"
"time"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
) )
const retryNum = 10 const retryNum = 10
// GetMainHeightByTxHash get Block height // GetMainHeightByTxHash get Block height
func (action *Action) GetMainHeightByTxHash(txHash []byte) int64 { func (action *Action) GetMainHeightByTxHash(txHash []byte) (int64, error) {
for i := 0; i < retryNum; i++ { param := &types.ReqHash{Hash: txHash}
req := &types.ReqHash{Hash: txHash} txDetail, err := action.lottery.GetExecutorAPI().QueryTx(param)
txDetail, err := action.grpcClient.QueryTransaction(context.Background(), req) if err != nil {
if err != nil { return -1, err
time.Sleep(time.Second)
} else {
return txDetail.GetHeight()
}
}
return -1
}
// GetMainBlockHashByHeight get Hash
func (action *Action) GetMainBlockHashByHeight(height int64) ([]byte, error) {
for i := 0; i < retryNum; i++ {
req := &types.ReqInt{Height: height}
replyHash, err := action.grpcClient.GetBlockHash(context.Background(), req)
if err != nil {
time.Sleep(time.Second)
} else {
return replyHash.Hash, nil
}
} }
return txDetail.GetHeight(), nil
return nil, types.ErrBlockNotFound
} }
...@@ -5,17 +5,14 @@ ...@@ -5,17 +5,14 @@
package executor package executor
import ( import (
"context"
"strconv" "strconv"
"github.com/33cn/chain33/account" "github.com/33cn/chain33/account"
"github.com/33cn/chain33/client"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db" dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/system/dapp" "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/lottery/types" pty "github.com/33cn/plugin/plugin/dapp/lottery/types"
"google.golang.org/grpc"
) )
const ( const (
...@@ -123,9 +120,6 @@ type Action struct { ...@@ -123,9 +120,6 @@ type Action struct {
height int64 height int64
execaddr string execaddr string
difficulty uint64 difficulty uint64
api client.QueueProtocolAPI
conn *grpc.ClientConn
grpcClient types.Chain33Client
index int index int
lottery *Lottery lottery *Lottery
} }
...@@ -134,19 +128,11 @@ type Action struct { ...@@ -134,19 +128,11 @@ type Action struct {
func NewLotteryAction(l *Lottery, tx *types.Transaction, index int) *Action { func NewLotteryAction(l *Lottery, tx *types.Transaction, index int) *Action {
hash := tx.Hash() hash := tx.Hash()
fromaddr := tx.From() fromaddr := tx.From()
return &Action{
msgRecvOp := grpc.WithMaxMsgSize(grpcRecSize) coinsAccount: l.GetCoinsAccount(), db: l.GetStateDB(),
paraRemoteGrpcClient := types.Conf("config.consensus").GStr("ParaRemoteGrpcClient") txhash: hash, fromaddr: fromaddr, blocktime: l.GetBlockTime(),
height: l.GetHeight(), execaddr: dapp.ExecAddress(string(tx.Execer)),
conn, err := grpc.Dial(paraRemoteGrpcClient, grpc.WithInsecure(), msgRecvOp) difficulty: l.GetDifficulty(), index: index, lottery: l}
if err != nil {
panic(err)
}
grpcClient := types.NewChain33Client(conn)
return &Action{l.GetCoinsAccount(), l.GetStateDB(), hash, fromaddr, l.GetBlockTime(),
l.GetHeight(), dapp.ExecAddress(string(tx.Execer)), l.GetDifficulty(), l.GetAPI(), conn, grpcClient, index, l}
} }
// GetLottCommonRecipt generate logs for lottery common action // GetLottCommonRecipt generate logs for lottery common action
...@@ -275,10 +261,10 @@ func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt, ...@@ -275,10 +261,10 @@ func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt,
lott.BuyAmount = 0 lott.BuyAmount = 0
llog.Debug("LotteryCreate", "OpRewardRatio", lott.OpRewardRatio, "DevRewardRatio", lott.DevRewardRatio) llog.Debug("LotteryCreate", "OpRewardRatio", lott.OpRewardRatio, "DevRewardRatio", lott.DevRewardRatio)
if types.IsPara() { if types.IsPara() {
mainHeight := action.GetMainHeightByTxHash(action.txhash) mainHeight, err := action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 { if mainHeight < 0 {
llog.Error("LotteryCreate", "mainHeight", mainHeight) llog.Error("LotteryCreate", "mainHeight", mainHeight)
return nil, pty.ErrLotteryStatus return nil, err
} }
lott.CreateOnMain = mainHeight lott.CreateOnMain = mainHeight
} }
...@@ -330,10 +316,10 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) { ...@@ -330,10 +316,10 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) {
lott.Status = pty.LotteryPurchase lott.Status = pty.LotteryPurchase
lott.Round++ lott.Round++
if types.IsPara() { if types.IsPara() {
mainHeight := action.GetMainHeightByTxHash(action.txhash) mainHeight, err := action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 { if mainHeight < 0 {
llog.Error("LotteryBuy", "mainHeight", mainHeight) llog.Error("LotteryBuy", "mainHeight", mainHeight)
return nil, pty.ErrLotteryStatus return nil, err
} }
lott.LastTransToPurStateOnMain = mainHeight lott.LastTransToPurStateOnMain = mainHeight
} }
...@@ -341,10 +327,10 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) { ...@@ -341,10 +327,10 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) {
if lott.Status == pty.LotteryPurchase { if lott.Status == pty.LotteryPurchase {
if types.IsPara() { if types.IsPara() {
mainHeight := action.GetMainHeightByTxHash(action.txhash) mainHeight, err := action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 { if mainHeight < 0 {
llog.Error("LotteryBuy", "mainHeight", mainHeight) llog.Error("LotteryBuy", "mainHeight", mainHeight)
return nil, pty.ErrLotteryStatus return nil, err
} }
if mainHeight-lott.LastTransToPurStateOnMain > lott.GetPurBlockNum() { if mainHeight-lott.LastTransToPurStateOnMain > lott.GetPurBlockNum() {
llog.Error("LotteryBuy", "action.height", action.height, "mainHeight", mainHeight, "LastTransToPurStateOnMain", lott.LastTransToPurStateOnMain) llog.Error("LotteryBuy", "action.height", action.height, "mainHeight", mainHeight, "LastTransToPurStateOnMain", lott.LastTransToPurStateOnMain)
...@@ -451,10 +437,10 @@ func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error) ...@@ -451,10 +437,10 @@ func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error)
} }
if types.IsPara() { if types.IsPara() {
mainHeight := action.GetMainHeightByTxHash(action.txhash) mainHeight, err := action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 { if mainHeight < 0 {
llog.Error("LotteryBuy", "mainHeight", mainHeight) llog.Error("LotteryBuy", "mainHeight", mainHeight)
return nil, pty.ErrLotteryStatus return nil, err
} }
if mainHeight-lott.GetLastTransToPurStateOnMain() < lott.GetDrawBlockNum() { if mainHeight-lott.GetLastTransToPurStateOnMain() < lott.GetDrawBlockNum() {
llog.Error("LotteryDraw", "action.height", action.height, "mainHeight", mainHeight, "GetLastTransToPurStateOnMain", lott.GetLastTransToPurState()) llog.Error("LotteryDraw", "action.height", action.height, "mainHeight", mainHeight, "GetLastTransToPurStateOnMain", lott.GetLastTransToPurState())
...@@ -566,10 +552,8 @@ func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, erro ...@@ -566,10 +552,8 @@ func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, erro
} }
//random used for verification in solo //random used for verification in solo
func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 { func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) (int64, error) {
var num int64 var num int64
var msg types.Message
var hash []byte
if isSolo { if isSolo {
//used for internal verification //used for internal verification
num = 12345 num = 12345
...@@ -577,51 +561,23 @@ func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 { ...@@ -577,51 +561,23 @@ func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 {
//发消息给randnum模块 //发消息给randnum模块
//在主链上,当前高度查询不到,如果要保证区块个数,高度传入action.height-1 //在主链上,当前高度查询不到,如果要保证区块个数,高度传入action.height-1
llog.Debug("findLuckyNum on randnum module") llog.Debug("findLuckyNum on randnum module")
if !types.IsPara() { param := &types.ReqRandHash{
blockHash, err := action.api.GetBlockHash(&types.ReqInt{Height: action.height - 1}) ExecName: "ticket",
if err != nil { BlockNum: blockNum,
return -1 Hash: action.lottery.GetLastHash(),
} }
hash, err := action.lottery.GetExecutorAPI().GetRandNum(param)
req := &types.ReqRandHash{ExecName: "ticket", Hash: blockHash.Hash, BlockNum: blockNum} if err != nil {
msg, err = action.api.Query("ticket", "RandNumHash", req) return -1, err
if err != nil {
return -1
}
reply := msg.(*types.ReplyHash)
hash = reply.Hash
} else {
txs := action.lottery.GetTxs()
if len(txs) < action.index+1 {
llog.Error("findLuckyNum", "len(txs)", len(txs), "index", action.index)
return -1
}
msg, err := action.api.Query("paracross", "GetMainBlockHash", txs[0])
if err != nil {
return -1
}
queryReply := msg.(*types.ReplyHash)
mainBlockHash := queryReply.Hash
req := &types.ReqRandHash{ExecName: "ticket", Hash: mainBlockHash, BlockNum: blockNum}
reply, err := action.grpcClient.QueryRandNum(context.Background(), req)
if err != nil {
return -1
}
hash = reply.Hash
} }
baseNum, err := strconv.ParseUint(common.ToHex(hash[0:4]), 0, 64) baseNum, err := strconv.ParseUint(common.ToHex(hash[0:4]), 0, 64)
llog.Debug("findLuckyNum", "baseNum", baseNum) llog.Debug("findLuckyNum", "baseNum", baseNum)
if err != nil { if err != nil {
llog.Error("findLuckyNum", "err", err) return -1, err
return -1
} }
num = int64(baseNum) % luckyNumMol num = int64(baseNum) % luckyNumMol
} }
return num return num, nil
} }
func checkFundAmount(luckynum int64, guessnum int64, way int64) (int64, int64) { func checkFundAmount(luckynum int64, guessnum int64, way int64) (int64, int64) {
...@@ -639,9 +595,9 @@ func checkFundAmount(luckynum int64, guessnum int64, way int64) (int64, int64) { ...@@ -639,9 +595,9 @@ func checkFundAmount(luckynum int64, guessnum int64, way int64) (int64, int64) {
} }
func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUpdateBuyInfo, *pty.LotteryGainInfos, error) { func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUpdateBuyInfo, *pty.LotteryGainInfos, error) {
luckynum := action.findLuckyNum(false, lott) luckynum, err := action.findLuckyNum(false, lott)
if luckynum < 0 || luckynum >= luckyNumMol { if luckynum < 0 || luckynum >= luckyNumMol {
return nil, nil, nil, pty.ErrLotteryErrLuckyNum return nil, nil, nil, err
} }
llog.Debug("checkDraw", "luckynum", luckynum) llog.Debug("checkDraw", "luckynum", luckynum)
...@@ -755,14 +711,13 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp ...@@ -755,14 +711,13 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
action.recordMissing(lott) action.recordMissing(lott)
if types.IsPara() { if types.IsPara() {
mainHeight := action.GetMainHeightByTxHash(action.txhash) mainHeight, err := action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 { if mainHeight < 0 {
llog.Error("LotteryDraw", "mainHeight", mainHeight) llog.Error("LotteryDraw", "mainHeight", mainHeight)
return nil, nil, nil, pty.ErrLotteryStatus return nil, nil, nil, err
} }
lott.LastTransToDrawStateOnMain = mainHeight lott.LastTransToDrawStateOnMain = mainHeight
} }
return &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}, &updateInfo, &gainInfos, nil return &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}, &updateInfo, &gainInfos, nil
} }
func (action *Action) recordMissing(lott *LotteryDB) { func (action *Action) recordMissing(lott *LotteryDB) {
......
...@@ -84,7 +84,7 @@ func TestTrade_Exec_SellLimit(t *testing.T) { ...@@ -84,7 +84,7 @@ func TestTrade_Exec_SellLimit(t *testing.T) {
accA.SaveExecAccount(address.ExecAddress("trade"), &accountA) accA.SaveExecAccount(address.ExecAddress("trade"), &accountA)
driver := newTrade() driver := newTrade()
driver.SetEnv(env.blockHeight, env.blockTime, env.difficulty) driver.SetEnv(env.blockHeight, env.blockTime, env.difficulty, nil, nil)
driver.SetStateDB(stateDB) driver.SetStateDB(stateDB)
sell := &pty.TradeSellTx{ sell := &pty.TradeSellTx{
...@@ -193,7 +193,7 @@ func TestTrade_Exec_BuyLimit(t *testing.T) { ...@@ -193,7 +193,7 @@ func TestTrade_Exec_BuyLimit(t *testing.T) {
accA.SaveExecAccount(address.ExecAddress("trade"), &accountA) accA.SaveExecAccount(address.ExecAddress("trade"), &accountA)
driver := newTrade() driver := newTrade()
driver.SetEnv(env.blockHeight, env.blockTime, env.difficulty) driver.SetEnv(env.blockHeight, env.blockTime, env.difficulty, nil, nil)
driver.SetStateDB(stateDB) driver.SetStateDB(stateDB)
buy := &pty.TradeBuyLimitTx{ buy := &pty.TradeBuyLimitTx{
......
...@@ -36,6 +36,7 @@ var ErrAPIEnv = errors.New("ErrAPIEnv") ...@@ -36,6 +36,7 @@ var ErrAPIEnv = errors.New("ErrAPIEnv")
type ExecutorAPI interface { type ExecutorAPI interface {
GetBlockByHashes(param *types.ReqHashes) (*types.BlockDetails, error) GetBlockByHashes(param *types.ReqHashes) (*types.BlockDetails, error)
GetRandNum(param *types.ReqRandHash) ([]byte, error) GetRandNum(param *types.ReqRandHash) ([]byte, error)
QueryTx(param *types.ReqHash) (*types.TransactionDetail, error)
IsErr() bool IsErr() bool
} }
...@@ -52,6 +53,11 @@ func New(api client.QueueProtocolAPI, grpcaddr string) ExecutorAPI { ...@@ -52,6 +53,11 @@ func New(api client.QueueProtocolAPI, grpcaddr string) ExecutorAPI {
return &mainChainAPI{api: api} return &mainChainAPI{api: api}
} }
func (api *mainChainAPI) QueryTx(param *types.ReqHash) (*types.TransactionDetail, error) {
data, err := api.api.QueryTx(param)
return data, seterr(err, &api.errflag)
}
func (api *mainChainAPI) IsErr() bool { func (api *mainChainAPI) IsErr() bool {
return atomic.LoadInt32(&api.errflag) == 1 return atomic.LoadInt32(&api.errflag) == 1
} }
...@@ -100,6 +106,11 @@ func (api *paraChainAPI) IsErr() bool { ...@@ -100,6 +106,11 @@ func (api *paraChainAPI) IsErr() bool {
return atomic.LoadInt32(&api.errflag) == 1 return atomic.LoadInt32(&api.errflag) == 1
} }
func (api *paraChainAPI) QueryTx(param *types.ReqHash) (*types.TransactionDetail, error) {
data, err := api.grpcClient.QueryTransaction(context.Background(), param)
return data, seterr(err, &api.errflag)
}
func (api *paraChainAPI) GetRandNum(param *types.ReqRandHash) ([]byte, error) { func (api *paraChainAPI) GetRandNum(param *types.ReqRandHash) ([]byte, error) {
reply, err := api.grpcClient.QueryRandNum(context.Background(), param) reply, err := api.grpcClient.QueryRandNum(context.Background(), param)
if err != nil { if err != nil {
......
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