Commit 063535fc authored by jixing wei's avatar jixing wei Committed by vipwzw

randnum

parent ed0f98f0
...@@ -27,3 +27,18 @@ func (action *Action) GetMainHeightByTxHash(txHash []byte) int64 { ...@@ -27,3 +27,18 @@ func (action *Action) GetMainHeightByTxHash(txHash []byte) int64 {
return -1 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 nil, types.ErrBlockNotFound
}
...@@ -135,10 +135,9 @@ func NewLotteryAction(l *Lottery, tx *types.Transaction, index int) *Action { ...@@ -135,10 +135,9 @@ func NewLotteryAction(l *Lottery, tx *types.Transaction, index int) *Action {
fromaddr := tx.From() fromaddr := tx.From()
msgRecvOp := grpc.WithMaxMsgSize(grpcRecSize) msgRecvOp := grpc.WithMaxMsgSize(grpcRecSize)
if types.IsPara() && cfg.ParaRemoteGrpcClient == "" { paraRemoteGrpcClient := types.Conf("config.consensus").GStr("ParaRemoteGrpcClient")
panic("ParaRemoteGrpcClient error")
} conn, err := grpc.Dial(paraRemoteGrpcClient, grpc.WithInsecure(), msgRecvOp)
conn, err := grpc.Dial(cfg.ParaRemoteGrpcClient, grpc.WithInsecure(), msgRecvOp)
if err != nil { if err != nil {
panic(err) panic(err)
...@@ -569,7 +568,6 @@ func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, erro ...@@ -569,7 +568,6 @@ func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, erro
func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 { func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 {
var num int64 var num int64
var msg types.Message var msg types.Message
var err error
var hash []byte var hash []byte
if isSolo { if isSolo {
//used for internal verification //used for internal verification
...@@ -579,7 +577,12 @@ func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 { ...@@ -579,7 +577,12 @@ func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 {
//在主链上,当前高度查询不到,如果要保证区块个数,高度传入action.height-1 //在主链上,当前高度查询不到,如果要保证区块个数,高度传入action.height-1
llog.Debug("findLuckyNum on randnum module") llog.Debug("findLuckyNum on randnum module")
if !types.IsPara() { if !types.IsPara() {
req := &types.ReqRandHash{ExecName: "ticket", Height: action.height - 1, BlockNum: blockNum} 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) msg, err = action.api.Query("ticket", "RandNumHash", req)
if err != nil { if err != nil {
return -1 return -1
...@@ -592,7 +595,14 @@ func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 { ...@@ -592,7 +595,14 @@ func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 {
llog.Error("findLuckyNum", "mainHeight", mainHeight) llog.Error("findLuckyNum", "mainHeight", mainHeight)
return -1 return -1
} }
req := &types.ReqRandHash{ExecName: "ticket", Height: mainHeight, BlockNum: blockNum}
mainBlockHash, err := action.GetMainBlockHashByHeight(mainHeight)
if err != nil {
llog.Error("findLuckyNum", "err", err)
return -1
}
req := &types.ReqRandHash{ExecName: "ticket", Hash: mainBlockHash, BlockNum: blockNum}
reply, err := action.grpcClient.QueryRandNum(context.Background(), req) reply, err := action.grpcClient.QueryRandNum(context.Background(), req)
if err != nil { if err != nil {
return -1 return -1
......
...@@ -47,5 +47,5 @@ func (ticket *Ticket) Query_MinerSourceList(param *types.ReqString) (types.Messa ...@@ -47,5 +47,5 @@ func (ticket *Ticket) Query_MinerSourceList(param *types.ReqString) (types.Messa
// Query_RandNumHash query randnumhash // Query_RandNumHash query randnumhash
func (ticket *Ticket) Query_RandNumHash(param *types.ReqRandHash) (types.Message, error) { func (ticket *Ticket) Query_RandNumHash(param *types.ReqRandHash) (types.Message, error) {
return ticket.GetRandNum(param.Height, param.BlockNum) return ticket.GetRandNum(param.Hash, param.BlockNum)
} }
...@@ -18,19 +18,19 @@ const ( ...@@ -18,19 +18,19 @@ const (
) )
// GetRandNum for ticket executor // GetRandNum for ticket executor
func (ticket *Ticket) GetRandNum(height int64, blockNum int64) (types.Message, error) { func (ticket *Ticket) GetRandNum(blockHash []byte, blockNum int64) (types.Message, error) {
tlog.Debug("GetRandNum", "height", height, "blockNum", blockNum) tlog.Debug("GetRandNum", "blockHash", blockHash, "blockNum", blockNum)
if blockNum < minBlockNum { if blockNum < minBlockNum {
blockNum = minBlockNum blockNum = minBlockNum
} else if blockNum > maxBlockNum { } else if blockNum > maxBlockNum {
blockNum = maxBlockNum blockNum = maxBlockNum
} }
if blockNum >= height { if len(blockHash) == 0 {
return nil, types.ErrNotFound return nil, types.ErrBlockNotFound
} }
txActions, err := ticket.getTxActions(height, blockNum) txActions, err := ticket.getTxActions(blockHash, blockNum)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -54,20 +54,36 @@ func (ticket *Ticket) GetRandNum(height int64, blockNum int64) (types.Message, e ...@@ -54,20 +54,36 @@ func (ticket *Ticket) GetRandNum(height int64, blockNum int64) (types.Message, e
return &types.ReplyHash{Hash: modify}, nil return &types.ReplyHash{Hash: modify}, nil
} }
func (ticket *Ticket) getTxActions(height int64, blockNum int64) ([]*tickettypes.TicketAction, error) { func (ticket *Ticket) getTxActions(blockHash []byte, blockNum int64) ([]*tickettypes.TicketAction, error) {
var txActions []*tickettypes.TicketAction var txActions []*tickettypes.TicketAction
var reqHashes types.ReqHashes
currHash := blockHash
tlog.Debug("getTxActions", "blockHash", blockHash, "blockNum", blockNum)
tlog.Debug("getTxActions", "height", height, "blockNum", blockNum) //根据blockHash,查询block,循环blockNum
for blockNum > 0 {
req := types.ReqHash{Hash: currHash}
req := &types.ReqBlocks{Start: height - blockNum + 1, End: height, IsDetail: false, Pid: []string{""}} tempBlock, err := ticket.GetAPI().GetBlockOverview(&req)
if err != nil {
return txActions, err
}
reqHashes.Hashes = append(reqHashes.Hashes, currHash)
currHash = tempBlock.Head.ParentHash
if tempBlock.Head.Height < 0 && blockNum > 1 {
return txActions, types.ErrBlockNotFound
}
blockNum--
}
blockDetails, err := ticket.GetAPI().GetBlocks(req) blockDetails, err := ticket.GetAPI().GetBlockByHashes(&reqHashes)
if err != nil { if err != nil {
tlog.Error("getTxActions", "height", height, "blockNum", blockNum, "err", err) tlog.Error("getTxActions", "blockHash", blockHash, "blockNum", blockNum, "err", err)
return txActions, err return txActions, err
} }
for _, block := range blockDetails.Items { for _, block := range blockDetails.Items {
//tlog.Debug("getTxActions", "blockHeight", block.Block.Height, "blockhash", block.Block.Hash()) tlog.Debug("getTxActions", "blockHeight", block.Block.Height, "blockhash", block.Block.Hash())
ticketAction, err := ticket.getMinerTx(block.Block) ticketAction, err := ticket.getMinerTx(block.Block)
if err != nil { if err != nil {
return txActions, err return txActions, err
......
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