Commit e355c325 authored by mdj33's avatar mdj33 Committed by vipwzw

add ticket list cmd

parent ab5a7d60
...@@ -29,6 +29,7 @@ func TicketCmd() *cobra.Command { ...@@ -29,6 +29,7 @@ func TicketCmd() *cobra.Command {
CountTicketCmd(), CountTicketCmd(),
CloseTicketCmd(), CloseTicketCmd(),
GetColdAddrByMinerCmd(), GetColdAddrByMinerCmd(),
listTicketCmd(),
) )
return cmd return cmd
...@@ -97,6 +98,41 @@ func countTicket(cmd *cobra.Command, args []string) { ...@@ -97,6 +98,41 @@ func countTicket(cmd *cobra.Command, args []string) {
ctx.Run() ctx.Run()
} }
// listTicketCmd get ticket count
func listTicketCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Get ticket id list",
Run: listTicket,
}
cmd.Flags().StringP("miner_acct", "m", "", "miner address (optional)")
cmd.Flags().Int32P("status", "s", 1, "ticket status (default 1:opened tickets)")
return cmd
}
func listTicket(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
minerAddr, _ := cmd.Flags().GetString("miner_acct")
status, _ := cmd.Flags().GetInt32("status")
if minerAddr != "" {
var params rpctypes.Query4Jrpc
params.Execer = ty.TicketX
params.FuncName = "TicketList"
req := ty.TicketList{Addr: minerAddr, Status: status}
params.Payload = types.MustPBToJSON(&req)
var res ty.ReplyTicketList
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
return
}
var res []ty.Ticket
ctx := jsonclient.NewRPCCtx(rpcLaddr, "ticket.GetTicketList", nil, &res)
ctx.Run()
}
// CloseTicketCmd close all accessible tickets // CloseTicketCmd close all accessible tickets
func CloseTicketCmd() *cobra.Command { func CloseTicketCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
......
...@@ -51,7 +51,7 @@ func NewDB(cfg *types.Chain33Config, id, minerAddress, returnWallet string, bloc ...@@ -51,7 +51,7 @@ func NewDB(cfg *types.Chain33Config, id, minerAddress, returnWallet string, bloc
t.MinerAddress = minerAddress t.MinerAddress = minerAddress
t.ReturnAddress = returnWallet t.ReturnAddress = returnWallet
t.CreateTime = blocktime t.CreateTime = blocktime
t.Status = 1 t.Status = ty.TicketOpened
t.IsGenesis = isGenesis t.IsGenesis = isGenesis
t.prevstatus = 0 t.prevstatus = 0
//height == 0 的情况下,不去改变 genesis block //height == 0 的情况下,不去改变 genesis block
...@@ -73,11 +73,11 @@ func NewDB(cfg *types.Chain33Config, id, minerAddress, returnWallet string, bloc ...@@ -73,11 +73,11 @@ func NewDB(cfg *types.Chain33Config, id, minerAddress, returnWallet string, bloc
// GetReceiptLog get receipt // GetReceiptLog get receipt
func (t *DB) GetReceiptLog() *types.ReceiptLog { func (t *DB) GetReceiptLog() *types.ReceiptLog {
log := &types.ReceiptLog{} log := &types.ReceiptLog{}
if t.Status == 1 { if t.Status == ty.TicketOpened {
log.Ty = ty.TyLogNewTicket log.Ty = ty.TyLogNewTicket
} else if t.Status == 2 { } else if t.Status == ty.TicketMined {
log.Ty = ty.TyLogMinerTicket log.Ty = ty.TyLogMinerTicket
} else if t.Status == 3 { } else if t.Status == ty.TicketClosed {
log.Ty = ty.TyLogCloseTicket log.Ty = ty.TyLogCloseTicket
} }
r := &ty.ReceiptTicket{} r := &ty.ReceiptTicket{}
...@@ -305,7 +305,7 @@ func (action *Action) TicketMiner(miner *ty.TicketMiner, index int) (*types.Rece ...@@ -305,7 +305,7 @@ func (action *Action) TicketMiner(miner *ty.TicketMiner, index int) (*types.Rece
if err != nil { if err != nil {
return nil, err return nil, err
} }
if ticket.Status != 1 { if ticket.Status != ty.TicketOpened {
return nil, types.ErrCoinBaseTicketStatus return nil, types.ErrCoinBaseTicketStatus
} }
cfg := ty.GetTicketMinerParam(chain33Cfg, action.height) cfg := ty.GetTicketMinerParam(chain33Cfg, action.height)
...@@ -330,7 +330,7 @@ func (action *Action) TicketMiner(miner *ty.TicketMiner, index int) (*types.Rece ...@@ -330,7 +330,7 @@ func (action *Action) TicketMiner(miner *ty.TicketMiner, index int) (*types.Rece
} }
} }
prevstatus := ticket.Status prevstatus := ticket.Status
ticket.Status = 2 ticket.Status = ty.TicketMined
ticket.MinerValue = miner.Reward ticket.MinerValue = miner.Reward
if chain33Cfg.IsFork(action.height, "ForkMinerTime") { if chain33Cfg.IsFork(action.height, "ForkMinerTime") {
ticket.MinerTime = action.blocktime ticket.MinerTime = action.blocktime
...@@ -383,20 +383,20 @@ func (action *Action) TicketClose(tclose *ty.TicketClose) (*types.Receipt, error ...@@ -383,20 +383,20 @@ func (action *Action) TicketClose(tclose *ty.TicketClose) (*types.Receipt, error
return nil, err return nil, err
} }
//ticket 的生成时间超过 2天,可提款 //ticket 的生成时间超过 2天,可提款
if ticket.Status != 2 && ticket.Status != 1 { if ticket.Status != ty.TicketMined && ticket.Status != ty.TicketOpened {
tlog.Error("ticket", "id", ticket.GetTicketId(), "status", ticket.GetStatus()) tlog.Error("ticket", "id", ticket.GetTicketId(), "status", ticket.GetStatus())
return nil, ty.ErrTicketClosed return nil, ty.ErrTicketClosed
} }
if !ticket.IsGenesis { if !ticket.IsGenesis {
//分成两种情况 //分成两种情况
if ticket.Status == 1 && action.blocktime-ticket.GetCreateTime() < cfg.TicketWithdrawTime { if ticket.Status == ty.TicketOpened && action.blocktime-ticket.GetCreateTime() < cfg.TicketWithdrawTime {
return nil, ty.ErrTime return nil, ty.ErrTime
} }
//已经挖矿成功了 //已经挖矿成功了
if ticket.Status == 2 && action.blocktime-ticket.GetCreateTime() < cfg.TicketWithdrawTime { if ticket.Status == ty.TicketMined && action.blocktime-ticket.GetCreateTime() < cfg.TicketWithdrawTime {
return nil, ty.ErrTime return nil, ty.ErrTime
} }
if ticket.Status == 2 && action.blocktime-ticket.GetMinerTime() < cfg.TicketMinerWaitTime { if ticket.Status == ty.TicketMined && action.blocktime-ticket.GetMinerTime() < cfg.TicketMinerWaitTime {
return nil, ty.ErrTime return nil, ty.ErrTime
} }
} }
...@@ -405,7 +405,7 @@ func (action *Action) TicketClose(tclose *ty.TicketClose) (*types.Receipt, error ...@@ -405,7 +405,7 @@ func (action *Action) TicketClose(tclose *ty.TicketClose) (*types.Receipt, error
return nil, types.ErrFromAddr return nil, types.ErrFromAddr
} }
prevstatus := ticket.Status prevstatus := ticket.Status
ticket.Status = 3 ticket.Status = ty.TicketClosed
tickets[i] = &DB{*ticket, prevstatus} tickets[i] = &DB{*ticket, prevstatus}
} }
var logs []*types.ReceiptLog var logs []*types.ReceiptLog
......
...@@ -143,3 +143,25 @@ func (c *Jrpc) SetAutoMining(in *ty.MinerFlag, result *rpctypes.Reply) error { ...@@ -143,3 +143,25 @@ func (c *Jrpc) SetAutoMining(in *ty.MinerFlag, result *rpctypes.Reply) error {
*result = reply *result = reply
return nil return nil
} }
// GetTicketList get ticket list info
func (g *channelClient) GetTicketList(ctx context.Context, in *types.ReqNil) ([]*ty.Ticket, error) {
inn := *in
data, err := g.ExecWalletFunc(ty.TicketX, "WalletGetTickets", &inn)
if err != nil {
return nil, err
}
return data.(*ty.ReplyWalletTickets).Tickets, nil
}
// GetTicketList get ticket list info
func (c *Jrpc) GetTicketList(in *types.ReqNil, result *interface{}) error {
resp, err := c.cli.GetTicketList(context.Background(), &types.ReqNil{})
if err != nil {
return err
}
*result = resp
return nil
}
...@@ -13,6 +13,18 @@ import ( ...@@ -13,6 +13,18 @@ import (
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
) )
// 0 -> 未成熟 1 -> 可挖矿 2 -> 已挖成功 3-> 已关闭
const (
//TicketInit ticket init status
TicketInit = iota
//TicketOpened ticket opened status
TicketOpened
//TicketMined ticket mined status
TicketMined
//TicketClosed ticket closed status
TicketClosed
)
const ( const (
//log for ticket //log for ticket
......
...@@ -406,13 +406,13 @@ func (policy *ticketPolicy) forceCloseTicketList(height int64, priv crypto.PrivK ...@@ -406,13 +406,13 @@ func (policy *ticketPolicy) forceCloseTicketList(height int64, priv crypto.PrivK
cfg := ty.GetTicketMinerParam(chain33Cfg, height) cfg := ty.GetTicketMinerParam(chain33Cfg, height)
for _, t := range tlist { for _, t := range tlist {
if !t.IsGenesis { if !t.IsGenesis {
if t.Status == 1 && now-t.GetCreateTime() < cfg.TicketWithdrawTime { if t.Status == ty.TicketOpened && now-t.GetCreateTime() < cfg.TicketWithdrawTime {
continue continue
} }
if t.Status == 2 && now-t.GetCreateTime() < cfg.TicketWithdrawTime { if t.Status == ty.TicketMined && now-t.GetCreateTime() < cfg.TicketWithdrawTime {
continue continue
} }
if t.Status == 2 && now-t.GetMinerTime() < cfg.TicketMinerWaitTime { if t.Status == ty.TicketMined && now-t.GetMinerTime() < cfg.TicketMinerWaitTime {
continue continue
} }
} }
......
...@@ -40,9 +40,9 @@ func TestForceCloseTicketList(t *testing.T) { ...@@ -40,9 +40,9 @@ func TestForceCloseTicketList(t *testing.T) {
wallet.api = qapi wallet.api = qapi
ticket.walletOperate = wallet ticket.walletOperate = wallet
t1 := &ty.Ticket{Status: 1, IsGenesis: false} t1 := &ty.Ticket{Status: ty.TicketOpened, IsGenesis: false}
t2 := &ty.Ticket{Status: 2, IsGenesis: false} t2 := &ty.Ticket{Status: ty.TicketMined, IsGenesis: false}
t3 := &ty.Ticket{Status: 3, IsGenesis: false} t3 := &ty.Ticket{Status: ty.TicketClosed, IsGenesis: false}
tlist := []*ty.Ticket{t1, t2, t3} tlist := []*ty.Ticket{t1, t2, t3}
r1, r2 := ticket.forceCloseTicketList(0, nil, tlist) r1, r2 := ticket.forceCloseTicketList(0, nil, tlist)
...@@ -69,9 +69,9 @@ func TestCloseTicketsByAddr(t *testing.T) { ...@@ -69,9 +69,9 @@ func TestCloseTicketsByAddr(t *testing.T) {
wallet.api = qapi wallet.api = qapi
ticket.walletOperate = wallet ticket.walletOperate = wallet
t1 := &ty.Ticket{Status: 1, IsGenesis: false} t1 := &ty.Ticket{Status: ty.TicketOpened, IsGenesis: false}
t2 := &ty.Ticket{Status: 2, IsGenesis: false} t2 := &ty.Ticket{Status: ty.TicketMined, IsGenesis: false}
t3 := &ty.Ticket{Status: 3, IsGenesis: false} t3 := &ty.Ticket{Status: ty.TicketClosed, IsGenesis: false}
tlist := &ty.ReplyTicketList{Tickets: []*ty.Ticket{t1, t2, t3}} tlist := &ty.ReplyTicketList{Tickets: []*ty.Ticket{t1, t2, t3}}
qapi.On("Query", ty.TicketX, "TicketList", mock.Anything).Return(tlist, nil) qapi.On("Query", ty.TicketX, "TicketList", mock.Anything).Return(tlist, 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