Commit 556c6d21 authored by vipwzw's avatar vipwzw Committed by 33cn

fix test

parent 9fad87f0
...@@ -26,7 +26,7 @@ func init() { ...@@ -26,7 +26,7 @@ func init() {
func initExec(ldb db.DB, kvdb db.KVDB, code string, t assert.TestingT) *js { func initExec(ldb db.DB, kvdb db.KVDB, code string, t assert.TestingT) *js {
e := newjs().(*js) e := newjs().(*js)
e.SetEnv(1, time.Now().Unix(), 1, nil, nil) e.SetEnv(1, time.Now().Unix(), 1)
mockapi := &mocks.QueueProtocolAPI{} mockapi := &mocks.QueueProtocolAPI{}
mockapi.On("Query", "ticket", "RandNumHash", mock.Anything).Return(&types.ReplyHash{Hash: []byte("hello")}, nil) mockapi.On("Query", "ticket", "RandNumHash", mock.Anything).Return(&types.ReplyHash{Hash: []byte("hello")}, nil)
e.SetAPI(mockapi) e.SetAPI(mockapi)
......
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"github.com/33cn/chain33/types"
)
const retryNum = 10
// GetMainHeightByTxHash get Block height
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 txDetail.GetHeight(), nil
}
...@@ -261,12 +261,7 @@ func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt, ...@@ -261,12 +261,7 @@ 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, err := action.GetMainHeightByTxHash(action.txhash) lott.CreateOnMain = action.lottery.GetMainHeight()
if mainHeight < 0 {
llog.Error("LotteryCreate", "mainHeight", mainHeight)
return nil, err
}
lott.CreateOnMain = mainHeight
} }
llog.Debug("LotteryCreate created", "lotteryID", lotteryID) llog.Debug("LotteryCreate created", "lotteryID", lotteryID)
...@@ -316,22 +311,13 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) { ...@@ -316,22 +311,13 @@ 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, err := action.GetMainHeightByTxHash(action.txhash) lott.LastTransToPurStateOnMain = action.lottery.GetMainHeight()
if mainHeight < 0 {
llog.Error("LotteryBuy", "mainHeight", mainHeight)
return nil, err
}
lott.LastTransToPurStateOnMain = mainHeight
} }
} }
if lott.Status == pty.LotteryPurchase { if lott.Status == pty.LotteryPurchase {
if types.IsPara() { if types.IsPara() {
mainHeight, err := action.GetMainHeightByTxHash(action.txhash) mainHeight := action.lottery.GetMainHeight()
if mainHeight < 0 {
llog.Error("LotteryBuy", "mainHeight", mainHeight)
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)
return nil, pty.ErrLotteryStatus return nil, pty.ErrLotteryStatus
...@@ -437,11 +423,7 @@ func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error) ...@@ -437,11 +423,7 @@ func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error)
} }
if types.IsPara() { if types.IsPara() {
mainHeight, err := action.GetMainHeightByTxHash(action.txhash) mainHeight := action.lottery.GetMainHeight()
if mainHeight < 0 {
llog.Error("LotteryBuy", "mainHeight", mainHeight)
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())
return nil, pty.ErrLotteryStatus return nil, pty.ErrLotteryStatus
...@@ -711,12 +693,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp ...@@ -711,12 +693,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
action.recordMissing(lott) action.recordMissing(lott)
if types.IsPara() { if types.IsPara() {
mainHeight, err := action.GetMainHeightByTxHash(action.txhash) lott.LastTransToDrawStateOnMain = action.lottery.GetHeight()
if mainHeight < 0 {
llog.Error("LotteryDraw", "mainHeight", mainHeight)
return nil, nil, nil, err
}
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
} }
......
...@@ -90,7 +90,7 @@ func TestUnfreeze(t *testing.T) { ...@@ -90,7 +90,7 @@ func TestUnfreeze(t *testing.T) {
} }
exec := newUnfreeze() exec := newUnfreeze()
exec.SetStateDB(stateDB) exec.SetStateDB(stateDB)
exec.SetEnv(env.blockHeight, env.blockTime, env.difficulty, nil, nil) exec.SetEnv(env.blockHeight, env.blockTime, env.difficulty)
receipt, err := exec.Exec(createTx, int(1)) receipt, err := exec.Exec(createTx, int(1))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, receipt) assert.NotNil(t, receipt)
...@@ -117,7 +117,7 @@ func TestUnfreeze(t *testing.T) { ...@@ -117,7 +117,7 @@ func TestUnfreeze(t *testing.T) {
t.Error("RPC_UnfreezeWithdrawTx sign", "err", err) t.Error("RPC_UnfreezeWithdrawTx sign", "err", err)
} }
blockTime := int64(10) blockTime := int64(10)
exec.SetEnv(env.blockHeight+1, env.blockTime+blockTime, env.difficulty, nil, nil) exec.SetEnv(env.blockHeight+1, env.blockTime+blockTime, env.difficulty)
receipt, err = exec.Exec(withdrawTx, 1) receipt, err = exec.Exec(withdrawTx, 1)
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, receipt) assert.NotNil(t, receipt)
...@@ -151,7 +151,7 @@ func TestUnfreeze(t *testing.T) { ...@@ -151,7 +151,7 @@ func TestUnfreeze(t *testing.T) {
t.Error("RPC_UnfreezeWithdrawTx sign", "err", err) t.Error("RPC_UnfreezeWithdrawTx sign", "err", err)
} }
blockTime := int64(10) blockTime := int64(10)
exec.SetEnv(env.blockHeight+1, env.blockTime+blockTime, env.difficulty, nil, nil) exec.SetEnv(env.blockHeight+1, env.blockTime+blockTime, env.difficulty)
receipt, err = exec.Exec(withdrawTx, 1) receipt, err = exec.Exec(withdrawTx, 1)
assert.Equal(t, pty.ErrNoPrivilege, err) assert.Equal(t, pty.ErrNoPrivilege, err)
assert.Nil(t, receipt) assert.Nil(t, receipt)
...@@ -214,7 +214,7 @@ func TestUnfreeze(t *testing.T) { ...@@ -214,7 +214,7 @@ func TestUnfreeze(t *testing.T) {
t.Error("RPC_UnfreezeWithdrawTx sign", "err", err) t.Error("RPC_UnfreezeWithdrawTx sign", "err", err)
} }
blockTime := int64(10) blockTime := int64(10)
exec.SetEnv(env.blockHeight+1, env.blockTime+blockTime+blockTime, env.difficulty, nil, nil) exec.SetEnv(env.blockHeight+1, env.blockTime+blockTime+blockTime, env.difficulty)
receipt, err = exec.Exec(withdrawTx, 1) receipt, err = exec.Exec(withdrawTx, 1)
assert.Equal(t, pty.ErrUnfreezeEmptied, err) assert.Equal(t, pty.ErrUnfreezeEmptied, err)
assert.Nil(t, receipt) assert.Nil(t, receipt)
......
...@@ -398,6 +398,11 @@ func (d *DriverBase) GetHeight() int64 { ...@@ -398,6 +398,11 @@ func (d *DriverBase) GetHeight() int64 {
return d.height return d.height
} }
// GetMainHeight return height
func (d *DriverBase) GetMainHeight() int64 {
return d.mainHeight
}
// GetBlockTime return block time // GetBlockTime return block time
func (d *DriverBase) GetBlockTime() int64 { func (d *DriverBase) GetBlockTime() int64 {
return d.blocktime return d.blocktime
......
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