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

fix test

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