Unverified Commit c5730591 authored by vipwzw's avatar vipwzw Committed by GitHub

Merge pull request #229 from zzh33cn/guess

Guess
parents fe35ee74 8caed19d
...@@ -20,21 +20,13 @@ func init() { ...@@ -20,21 +20,13 @@ func init() {
ety.InitFuncList(types.ListMethod(&Guess{})) ety.InitFuncList(types.ListMethod(&Guess{}))
} }
type subConfig struct {
ParaRemoteGrpcClient string `json:"paraRemoteGrpcClient"`
}
var cfg subConfig
// Init Guess // Init Guess
func Init(name string, sub []byte) { func Init(name string, sub []byte) {
driverName := GetName() driverName := GetName()
if name != driverName { if name != driverName {
panic("system dapp can't be rename") panic("system dapp can't be rename")
} }
if sub != nil {
types.MustDecode(sub, &cfg)
}
drivers.Register(driverName, newGuessGame, types.GetDappFork(driverName, "Enable")) drivers.Register(driverName, newGuessGame, types.GetDappFork(driverName, "Enable"))
} }
......
...@@ -5,17 +5,12 @@ ...@@ -5,17 +5,12 @@
package executor package executor
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/33cn/chain33/common/db" "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/common/db/table" "github.com/33cn/chain33/common/db/table"
"github.com/33cn/chain33/client"
"google.golang.org/grpc"
"github.com/33cn/chain33/account" "github.com/33cn/chain33/account"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db" dbm "github.com/33cn/chain33/common/db"
...@@ -48,12 +43,6 @@ const ( ...@@ -48,12 +43,6 @@ const (
//MaxExpireHeight 距离游戏创建区块的最大过期高度差 //MaxExpireHeight 距离游戏创建区块的最大过期高度差
MaxExpireHeight = 1000000 MaxExpireHeight = 1000000
//grpcRecSize 接收缓冲大小
grpcRecSize int = 30 * 1024 * 1024
//retryNum 失败时的重试次数
retryNum = 10
) )
//Action 具体动作执行 //Action 具体动作执行
...@@ -67,9 +56,7 @@ type Action struct { ...@@ -67,9 +56,7 @@ type Action struct {
execaddr string execaddr string
localDB dbm.KVDB localDB dbm.KVDB
index int index int
api client.QueueProtocolAPI mainHeight int64
conn *grpc.ClientConn
grpcClient types.Chain33Client
} }
//NewAction 生成Action对象 //NewAction 生成Action对象
...@@ -77,19 +64,6 @@ func NewAction(guess *Guess, tx *types.Transaction, index int) *Action { ...@@ -77,19 +64,6 @@ func NewAction(guess *Guess, tx *types.Transaction, index int) *Action {
hash := tx.Hash() hash := tx.Hash()
fromAddr := tx.From() fromAddr := tx.From()
msgRecvOp := grpc.WithMaxMsgSize(grpcRecSize)
paraRemoteGrpcClient := types.Conf("config.consensus").GStr("ParaRemoteGrpcClient")
if types.IsPara() && paraRemoteGrpcClient == "" {
panic("ParaRemoteGrpcClient error")
}
conn, err := grpc.Dial(paraRemoteGrpcClient, grpc.WithInsecure(), msgRecvOp)
if err != nil {
panic(err)
}
grpcClient := types.NewChain33Client(conn)
return &Action{ return &Action{
coinsAccount: guess.GetCoinsAccount(), coinsAccount: guess.GetCoinsAccount(),
db: guess.GetStateDB(), db: guess.GetStateDB(),
...@@ -100,9 +74,7 @@ func NewAction(guess *Guess, tx *types.Transaction, index int) *Action { ...@@ -100,9 +74,7 @@ func NewAction(guess *Guess, tx *types.Transaction, index int) *Action {
execaddr: dapp.ExecAddress(string(tx.Execer)), execaddr: dapp.ExecAddress(string(tx.Execer)),
localDB: guess.GetLocalDB(), localDB: guess.GetLocalDB(),
index: index, index: index,
api: guess.GetAPI(), mainHeight: guess.GetMainHeight(),
conn: conn,
grpcClient: grpcClient,
} }
} }
...@@ -299,7 +271,7 @@ func (action *Action) readGame(id string) (*gty.GuessGame, error) { ...@@ -299,7 +271,7 @@ func (action *Action) readGame(id string) (*gty.GuessGame, error) {
func (action *Action) newGame(gameID string, start *gty.GuessGameStart) (*gty.GuessGame, error) { func (action *Action) newGame(gameID string, start *gty.GuessGameStart) (*gty.GuessGame, error) {
game := &gty.GuessGame{ game := &gty.GuessGame{
GameID: gameID, GameID: gameID,
Status: gty.GuessGameActionStart, Status: gty.GuessGameStatusStart,
//StartTime: action.blocktime, //StartTime: action.blocktime,
StartTxHash: gameID, StartTxHash: gameID,
Topic: start.Topic, Topic: start.Topic,
...@@ -375,16 +347,7 @@ func (action *Action) GameStart(start *gty.GuessGameStart) (*types.Receipt, erro ...@@ -375,16 +347,7 @@ func (action *Action) GameStart(start *gty.GuessGameStart) (*types.Receipt, erro
gameID := common.ToHex(action.txhash) gameID := common.ToHex(action.txhash)
game, _ := action.newGame(gameID, start) game, _ := action.newGame(gameID, start)
game.StartTime = action.blocktime game.StartTime = action.blocktime
if types.IsPara() { game.StartHeight = action.mainHeight
mainHeight := action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 {
logger.Error("GameStart", "mainHeight", mainHeight)
return nil, gty.ErrGuessStatus
}
game.StartHeight = mainHeight
} else {
game.StartHeight = action.height
}
game.AdminAddr = action.fromaddr game.AdminAddr = action.fromaddr
game.PreIndex = 0 game.PreIndex = 0
game.Index = action.getIndex() game.Index = action.getIndex()
...@@ -831,18 +794,7 @@ func (action *Action) changeAllAddrIndex(game *gty.GuessGame) { ...@@ -831,18 +794,7 @@ func (action *Action) changeAllAddrIndex(game *gty.GuessGame) {
//refreshStatusByTime 检测游戏是否过期,是否可以下注 //refreshStatusByTime 检测游戏是否过期,是否可以下注
func (action *Action) refreshStatusByTime(game *gty.GuessGame) (canBet bool) { func (action *Action) refreshStatusByTime(game *gty.GuessGame) (canBet bool) {
mainHeight := action.mainHeight
var mainHeight int64
if types.IsPara() {
mainHeight = action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 {
logger.Error("RefreshStatusByTime", "mainHeight err", mainHeight)
return true
}
} else {
mainHeight = action.height
}
//如果完全由管理员驱动状态变化,则除了保护性过期判断外,不需要做其他判断。 //如果完全由管理员驱动状态变化,则除了保护性过期判断外,不需要做其他判断。
if game.DrivenByAdmin { if game.DrivenByAdmin {
...@@ -899,18 +851,3 @@ func (action *Action) checkTime(start *gty.GuessGameStart) bool { ...@@ -899,18 +851,3 @@ func (action *Action) checkTime(start *gty.GuessGameStart) bool {
return false return false
} }
// 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
}
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