Unverified Commit 870bb008 authored by vipwzw's avatar vipwzw Committed by GitHub

Merge pull request #35 from jixingwei/fix_lot_lint

Fix lottery code warning
parents 5bb83618 cee0ed2c
...@@ -9,21 +9,25 @@ import ( ...@@ -9,21 +9,25 @@ import (
pty "github.com/33cn/plugin/plugin/dapp/lottery/types" pty "github.com/33cn/plugin/plugin/dapp/lottery/types"
) )
// Exec_Create Action
func (l *Lottery) Exec_Create(payload *pty.LotteryCreate, tx *types.Transaction, index int) (*types.Receipt, error) { func (l *Lottery) Exec_Create(payload *pty.LotteryCreate, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewLotteryAction(l, tx, index) actiondb := NewLotteryAction(l, tx, index)
return actiondb.LotteryCreate(payload) return actiondb.LotteryCreate(payload)
} }
// Exec_Buy Action
func (l *Lottery) Exec_Buy(payload *pty.LotteryBuy, tx *types.Transaction, index int) (*types.Receipt, error) { func (l *Lottery) Exec_Buy(payload *pty.LotteryBuy, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewLotteryAction(l, tx, index) actiondb := NewLotteryAction(l, tx, index)
return actiondb.LotteryBuy(payload) return actiondb.LotteryBuy(payload)
} }
// Exec_Draw Action
func (l *Lottery) Exec_Draw(payload *pty.LotteryDraw, tx *types.Transaction, index int) (*types.Receipt, error) { func (l *Lottery) Exec_Draw(payload *pty.LotteryDraw, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewLotteryAction(l, tx, index) actiondb := NewLotteryAction(l, tx, index)
return actiondb.LotteryDraw(payload) return actiondb.LotteryDraw(payload)
} }
// Exec_Close Action
func (l *Lottery) Exec_Close(payload *pty.LotteryClose, tx *types.Transaction, index int) (*types.Receipt, error) { func (l *Lottery) Exec_Close(payload *pty.LotteryClose, tx *types.Transaction, index int) (*types.Receipt, error) {
actiondb := NewLotteryAction(l, tx, index) actiondb := NewLotteryAction(l, tx, index)
return actiondb.LotteryClose(payload) return actiondb.LotteryClose(payload)
......
...@@ -41,18 +41,22 @@ func (l *Lottery) execDelLocal(tx *types.Transaction, receiptData *types.Receipt ...@@ -41,18 +41,22 @@ func (l *Lottery) execDelLocal(tx *types.Transaction, receiptData *types.Receipt
} }
// ExecDelLocal_Create Action
func (l *Lottery) ExecDelLocal_Create(payload *pty.LotteryCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) { func (l *Lottery) ExecDelLocal_Create(payload *pty.LotteryCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return nil, nil return nil, nil
} }
// ExecDelLocal_Buy Action
func (l *Lottery) ExecDelLocal_Buy(payload *pty.LotteryBuy, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) { func (l *Lottery) ExecDelLocal_Buy(payload *pty.LotteryBuy, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return nil, nil return nil, nil
} }
// ExecDelLocal_Draw Action
func (l *Lottery) ExecDelLocal_Draw(payload *pty.LotteryDraw, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) { func (l *Lottery) ExecDelLocal_Draw(payload *pty.LotteryDraw, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return nil, nil return nil, nil
} }
// ExecDelLocal_Close Action
func (l *Lottery) ExecDelLocal_Close(payload *pty.LotteryClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) { func (l *Lottery) ExecDelLocal_Close(payload *pty.LotteryClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return nil, nil return nil, nil
} }
...@@ -40,18 +40,22 @@ func (l *Lottery) execLocal(tx *types.Transaction, receipt *types.ReceiptData) ( ...@@ -40,18 +40,22 @@ func (l *Lottery) execLocal(tx *types.Transaction, receipt *types.ReceiptData) (
return set, nil return set, nil
} }
// ExecLocal_Create Action
func (l *Lottery) ExecLocal_Create(payload *pty.LotteryCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) { func (l *Lottery) ExecLocal_Create(payload *pty.LotteryCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return l.execLocal(tx, receiptData) return l.execLocal(tx, receiptData)
} }
// ExecLocal_Buy Action
func (l *Lottery) ExecLocal_Buy(payload *pty.LotteryBuy, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) { func (l *Lottery) ExecLocal_Buy(payload *pty.LotteryBuy, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return l.execLocal(tx, receiptData) return l.execLocal(tx, receiptData)
} }
// ExecLocal_Draw Action
func (l *Lottery) ExecLocal_Draw(payload *pty.LotteryDraw, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) { func (l *Lottery) ExecLocal_Draw(payload *pty.LotteryDraw, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return l.execLocal(tx, receiptData) return l.execLocal(tx, receiptData)
} }
// ExecLocal_Close Action
func (l *Lottery) ExecLocal_Close(payload *pty.LotteryClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) { func (l *Lottery) ExecLocal_Close(payload *pty.LotteryClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return l.execLocal(tx, receiptData) return l.execLocal(tx, receiptData)
} }
...@@ -6,32 +6,32 @@ package executor ...@@ -6,32 +6,32 @@ package executor
import "fmt" import "fmt"
func calcLotteryBuyPrefix(lotteryId string, addr string) []byte { func calcLotteryBuyPrefix(lotteryID string, addr string) []byte {
key := fmt.Sprintf("LODB-lottery-buy:%s:%s", lotteryId, addr) key := fmt.Sprintf("LODB-lottery-buy:%s:%s", lotteryID, addr)
return []byte(key) return []byte(key)
} }
func calcLotteryBuyRoundPrefix(lotteryId string, addr string, round int64) []byte { func calcLotteryBuyRoundPrefix(lotteryID string, addr string, round int64) []byte {
key := fmt.Sprintf("LODB-lottery-buy:%s:%s:%10d", lotteryId, addr, round) key := fmt.Sprintf("LODB-lottery-buy:%s:%s:%10d", lotteryID, addr, round)
return []byte(key) return []byte(key)
} }
func calcLotteryBuyKey(lotteryId string, addr string, round int64, index int64) []byte { func calcLotteryBuyKey(lotteryID string, addr string, round int64, index int64) []byte {
key := fmt.Sprintf("LODB-lottery-buy:%s:%s:%10d:%18d", lotteryId, addr, round, index) key := fmt.Sprintf("LODB-lottery-buy:%s:%s:%10d:%18d", lotteryID, addr, round, index)
return []byte(key) return []byte(key)
} }
func calcLotteryDrawPrefix(lotteryId string) []byte { func calcLotteryDrawPrefix(lotteryID string) []byte {
key := fmt.Sprintf("LODB-lottery-draw:%s", lotteryId) key := fmt.Sprintf("LODB-lottery-draw:%s", lotteryID)
return []byte(key) return []byte(key)
} }
func calcLotteryDrawKey(lotteryId string, round int64) []byte { func calcLotteryDrawKey(lotteryID string, round int64) []byte {
key := fmt.Sprintf("LODB-lottery-draw:%s:%10d", lotteryId, round) key := fmt.Sprintf("LODB-lottery-draw:%s:%10d", lotteryID, round)
return []byte(key) return []byte(key)
} }
func calcLotteryKey(lotteryId string, status int32) []byte { func calcLotteryKey(lotteryID string, status int32) []byte {
key := fmt.Sprintf("LODB-lottery-:%d:%s", status, lotteryId) key := fmt.Sprintf("LODB-lottery-:%d:%s", status, lotteryID)
return []byte(key) return []byte(key)
} }
...@@ -27,6 +27,7 @@ type subConfig struct { ...@@ -27,6 +27,7 @@ type subConfig struct {
var cfg subConfig var cfg subConfig
// Init lottery
func Init(name string, sub []byte) { func Init(name string, sub []byte) {
driverName := GetName() driverName := GetName()
if name != driverName { if name != driverName {
...@@ -38,10 +39,12 @@ func Init(name string, sub []byte) { ...@@ -38,10 +39,12 @@ func Init(name string, sub []byte) {
drivers.Register(driverName, newLottery, types.GetDappFork(driverName, "Enable")) drivers.Register(driverName, newLottery, types.GetDappFork(driverName, "Enable"))
} }
// GetName for lottery
func GetName() string { func GetName() string {
return newLottery().GetName() return newLottery().GetName()
} }
// Lottery driver
type Lottery struct { type Lottery struct {
drivers.DriverBase drivers.DriverBase
} }
...@@ -53,7 +56,8 @@ func newLottery() drivers.Driver { ...@@ -53,7 +56,8 @@ func newLottery() drivers.Driver {
return l return l
} }
func (l *Lottery) GetDriverName() string { // GetDriverName for lottery
func (lott *Lottery) GetDriverName() string {
return pty.LotteryX return pty.LotteryX
} }
...@@ -120,9 +124,8 @@ func (lott *Lottery) findLotteryDrawRecord(key []byte) (*pty.LotteryDrawRecord, ...@@ -120,9 +124,8 @@ func (lott *Lottery) findLotteryDrawRecord(key []byte) (*pty.LotteryDrawRecord,
func (lott *Lottery) saveLotteryBuy(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) { func (lott *Lottery) saveLotteryBuy(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) {
key := calcLotteryBuyKey(lotterylog.LotteryId, lotterylog.Addr, lotterylog.Round, lotterylog.Index) key := calcLotteryBuyKey(lotterylog.LotteryId, lotterylog.Addr, lotterylog.Round, lotterylog.Index)
kv := &types.KeyValue{} record := &pty.LotteryBuyRecord{Number: lotterylog.Number, Amount: lotterylog.Amount, Round: lotterylog.Round, Type: 0, Way: lotterylog.Way, Index: lotterylog.Index, Time: lotterylog.Time, TxHash: lotterylog.TxHash}
record := &pty.LotteryBuyRecord{lotterylog.Number, lotterylog.Amount, lotterylog.Round, 0, lotterylog.Way, lotterylog.Index, lotterylog.Time, lotterylog.TxHash} kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kv = &types.KeyValue{key, types.Encode(record)}
kvs = append(kvs, kv) kvs = append(kvs, kv)
return kvs return kvs
...@@ -131,7 +134,7 @@ func (lott *Lottery) saveLotteryBuy(lotterylog *pty.ReceiptLottery) (kvs []*type ...@@ -131,7 +134,7 @@ func (lott *Lottery) saveLotteryBuy(lotterylog *pty.ReceiptLottery) (kvs []*type
func (lott *Lottery) deleteLotteryBuy(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) { func (lott *Lottery) deleteLotteryBuy(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) {
key := calcLotteryBuyKey(lotterylog.LotteryId, lotterylog.Addr, lotterylog.Round, lotterylog.Index) key := calcLotteryBuyKey(lotterylog.LotteryId, lotterylog.Addr, lotterylog.Round, lotterylog.Index)
kv := &types.KeyValue{key, nil} kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv) kvs = append(kvs, kv)
return kvs return kvs
} }
...@@ -158,7 +161,6 @@ func (lott *Lottery) updateLotteryBuy(lotterylog *pty.ReceiptLottery, isAdd bool ...@@ -158,7 +161,6 @@ func (lott *Lottery) updateLotteryBuy(lotterylog *pty.ReceiptLottery, isAdd bool
if err != nil || record == nil { if err != nil || record == nil {
return kvs return kvs
} }
kv := &types.KeyValue{}
if isAdd { if isAdd {
llog.Debug("updateLotteryBuy update key") llog.Debug("updateLotteryBuy update key")
...@@ -167,7 +169,7 @@ func (lott *Lottery) updateLotteryBuy(lotterylog *pty.ReceiptLottery, isAdd bool ...@@ -167,7 +169,7 @@ func (lott *Lottery) updateLotteryBuy(lotterylog *pty.ReceiptLottery, isAdd bool
record.Type = 0 record.Type = 0
} }
kv = &types.KeyValue{key, types.Encode(record)} kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv) kvs = append(kvs, kv)
} }
} }
...@@ -178,16 +180,15 @@ func (lott *Lottery) updateLotteryBuy(lotterylog *pty.ReceiptLottery, isAdd bool ...@@ -178,16 +180,15 @@ func (lott *Lottery) updateLotteryBuy(lotterylog *pty.ReceiptLottery, isAdd bool
func (lott *Lottery) saveLotteryDraw(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) { func (lott *Lottery) saveLotteryDraw(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) {
key := calcLotteryDrawKey(lotterylog.LotteryId, lotterylog.Round) key := calcLotteryDrawKey(lotterylog.LotteryId, lotterylog.Round)
kv := &types.KeyValue{} record := &pty.LotteryDrawRecord{Number: lotterylog.LuckyNumber, Round: lotterylog.Round, Time: lotterylog.Time, TxHash: lotterylog.TxHash}
record := &pty.LotteryDrawRecord{lotterylog.LuckyNumber, lotterylog.Round, lotterylog.Time, lotterylog.TxHash} kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kv = &types.KeyValue{key, types.Encode(record)}
kvs = append(kvs, kv) kvs = append(kvs, kv)
return kvs return kvs
} }
func (lott *Lottery) deleteLotteryDraw(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) { func (lott *Lottery) deleteLotteryDraw(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) {
key := calcLotteryDrawKey(lotterylog.LotteryId, lotterylog.Round) key := calcLotteryDrawKey(lotterylog.LotteryId, lotterylog.Round)
kv := &types.KeyValue{key, nil} kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv) kvs = append(kvs, kv)
return kvs return kvs
} }
...@@ -210,20 +211,21 @@ func (lott *Lottery) deleteLottery(lotterylog *pty.ReceiptLottery) (kvs []*types ...@@ -210,20 +211,21 @@ func (lott *Lottery) deleteLottery(lotterylog *pty.ReceiptLottery) (kvs []*types
return kvs return kvs
} }
func addlottery(lotteryId string, status int32) *types.KeyValue { func addlottery(lotteryID string, status int32) *types.KeyValue {
kv := &types.KeyValue{} kv := &types.KeyValue{}
kv.Key = calcLotteryKey(lotteryId, status) kv.Key = calcLotteryKey(lotteryID, status)
kv.Value = []byte(lotteryId) kv.Value = []byte(lotteryID)
return kv return kv
} }
func dellottery(lotteryId string, status int32) *types.KeyValue { func dellottery(lotteryID string, status int32) *types.KeyValue {
kv := &types.KeyValue{} kv := &types.KeyValue{}
kv.Key = calcLotteryKey(lotteryId, status) kv.Key = calcLotteryKey(lotteryID, status)
kv.Value = nil kv.Value = nil
return kv return kv
} }
// GetPayloadValue lotteryAction
func (lott *Lottery) GetPayloadValue() types.Message { func (lott *Lottery) GetPayloadValue() types.Message {
return &pty.LotteryAction{} return &pty.LotteryAction{}
} }
...@@ -20,7 +20,7 @@ func (action *Action) getTxActions(height int64, blockNum int64) ([]*tickettypes ...@@ -20,7 +20,7 @@ func (action *Action) getTxActions(height int64, blockNum int64) ([]*tickettypes
var txActions []*tickettypes.TicketAction var txActions []*tickettypes.TicketAction
llog.Error("getTxActions", "height", height, "blockNum", blockNum) llog.Error("getTxActions", "height", height, "blockNum", blockNum)
if !types.IsPara() { if !types.IsPara() {
req := &types.ReqBlocks{height - blockNum + 1, height, false, []string{""}} req := &types.ReqBlocks{Start: height - blockNum + 1, End: height, IsDetail: false, Pid: []string{""}}
blockDetails, err := action.api.GetBlocks(req) blockDetails, err := action.api.GetBlocks(req)
if err != nil { if err != nil {
...@@ -36,7 +36,8 @@ func (action *Action) getTxActions(height int64, blockNum int64) ([]*tickettypes ...@@ -36,7 +36,8 @@ func (action *Action) getTxActions(height int64, blockNum int64) ([]*tickettypes
txActions = append(txActions, ticketAction) txActions = append(txActions, ticketAction)
} }
return txActions, nil return txActions, nil
} else { }
//block height on main //block height on main
mainHeight := action.GetMainHeightByTxHash(action.txhash) mainHeight := action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 { if mainHeight < 0 {
...@@ -58,13 +59,13 @@ func (action *Action) getTxActions(height int64, blockNum int64) ([]*tickettypes ...@@ -58,13 +59,13 @@ func (action *Action) getTxActions(height int64, blockNum int64) ([]*tickettypes
txActions = append(txActions, ticketAction) txActions = append(txActions, ticketAction)
} }
return txActions, nil return txActions, nil
}
} }
//TransactionDetail // GetMainHeightByTxHash get Block height
func (action *Action) GetMainHeightByTxHash(txHash []byte) int64 { func (action *Action) GetMainHeightByTxHash(txHash []byte) int64 {
for i := 0; i < retryNum; i++ { for i := 0; i < retryNum; i++ {
req := &types.ReqHash{txHash} req := &types.ReqHash{Hash: txHash}
txDetail, err := action.grpcClient.QueryTransaction(context.Background(), req) txDetail, err := action.grpcClient.QueryTransaction(context.Background(), req)
if err != nil { if err != nil {
time.Sleep(time.Second) time.Sleep(time.Second)
...@@ -76,8 +77,9 @@ func (action *Action) GetMainHeightByTxHash(txHash []byte) int64 { ...@@ -76,8 +77,9 @@ func (action *Action) GetMainHeightByTxHash(txHash []byte) int64 {
return -1 return -1
} }
// GetBlocksOnMain get Block from main chain
func (action *Action) GetBlocksOnMain(start int64, end int64) (*types.BlockDetails, error) { func (action *Action) GetBlocksOnMain(start int64, end int64) (*types.BlockDetails, error) {
req := &types.ReqBlocks{start, end, false, []string{""}} req := &types.ReqBlocks{Start: start, End: end, IsDetail: false, Pid: []string{""}}
getBlockSucc := false getBlockSucc := false
var reply *types.Reply var reply *types.Reply
var err error var err error
......
...@@ -35,6 +35,7 @@ const ( ...@@ -35,6 +35,7 @@ const (
creatorKey = "lottery-creator" creatorKey = "lottery-creator"
) )
// List control
const ( const (
ListDESC = int32(0) ListDESC = int32(0)
ListASC = int32(1) ListASC = int32(1)
...@@ -42,6 +43,7 @@ const ( ...@@ -42,6 +43,7 @@ const (
MaxCount = int32(100) //最多取100条 MaxCount = int32(100) //最多取100条
) )
// Star level
const ( const (
FiveStar = 5 FiveStar = 5
ThreeStar = 3 ThreeStar = 3
...@@ -56,14 +58,16 @@ const randMolNum = 5 ...@@ -56,14 +58,16 @@ const randMolNum = 5
const grpcRecSize int = 5 * 30 * 1024 * 1024 const grpcRecSize int = 5 * 30 * 1024 * 1024
const blockNum = 5 const blockNum = 5
// LotteryDB struct
type LotteryDB struct { type LotteryDB struct {
pty.Lottery pty.Lottery
} }
func NewLotteryDB(lotteryId string, purBlock int64, drawBlock int64, // NewLotteryDB New management instance
func NewLotteryDB(lotteryID string, purBlock int64, drawBlock int64,
blockHeight int64, addr string) *LotteryDB { blockHeight int64, addr string) *LotteryDB {
lott := &LotteryDB{} lott := &LotteryDB{}
lott.LotteryId = lotteryId lott.LotteryId = lotteryID
lott.PurBlockNum = purBlock lott.PurBlockNum = purBlock
lott.DrawBlockNum = drawBlock lott.DrawBlockNum = drawBlock
lott.CreateHeight = blockHeight lott.CreateHeight = blockHeight
...@@ -75,17 +79,19 @@ func NewLotteryDB(lotteryId string, purBlock int64, drawBlock int64, ...@@ -75,17 +79,19 @@ func NewLotteryDB(lotteryId string, purBlock int64, drawBlock int64,
lott.MissingRecords = make([]*pty.MissingRecord, 5) lott.MissingRecords = make([]*pty.MissingRecord, 5)
for index := range lott.MissingRecords { for index := range lott.MissingRecords {
tempTimes := make([]int32, 10) tempTimes := make([]int32, 10)
lott.MissingRecords[index] = &pty.MissingRecord{tempTimes} lott.MissingRecords[index] = &pty.MissingRecord{Times: tempTimes}
} }
return lott return lott
} }
// GetKVSet for LotteryDB
func (lott *LotteryDB) GetKVSet() (kvset []*types.KeyValue) { func (lott *LotteryDB) GetKVSet() (kvset []*types.KeyValue) {
value := types.Encode(&lott.Lottery) value := types.Encode(&lott.Lottery)
kvset = append(kvset, &types.KeyValue{Key(lott.LotteryId), value}) kvset = append(kvset, &types.KeyValue{Key: Key(lott.LotteryId), Value: value})
return kvset return kvset
} }
// Save for LotteryDB
func (lott *LotteryDB) Save(db dbm.KV) { func (lott *LotteryDB) Save(db dbm.KV) {
set := lott.GetKVSet() set := lott.GetKVSet()
for i := 0; i < len(set); i++ { for i := 0; i < len(set); i++ {
...@@ -93,12 +99,14 @@ func (lott *LotteryDB) Save(db dbm.KV) { ...@@ -93,12 +99,14 @@ func (lott *LotteryDB) Save(db dbm.KV) {
} }
} }
// Key for lottery
func Key(id string) (key []byte) { func Key(id string) (key []byte) {
key = append(key, []byte("mavl-"+pty.LotteryX+"-")...) key = append(key, []byte("mavl-"+pty.LotteryX+"-")...)
key = append(key, []byte(id)...) key = append(key, []byte(id)...)
return key return key
} }
// Action struct
type Action struct { type Action struct {
coinsAccount *account.DB coinsAccount *account.DB
db dbm.KV db dbm.KV
...@@ -114,6 +122,7 @@ type Action struct { ...@@ -114,6 +122,7 @@ type Action struct {
index int index int
} }
// NewLotteryAction generate New Action
func NewLotteryAction(l *Lottery, tx *types.Transaction, index int) *Action { func NewLotteryAction(l *Lottery, tx *types.Transaction, index int) *Action {
hash := tx.Hash() hash := tx.Hash()
fromaddr := tx.From() fromaddr := tx.From()
...@@ -130,6 +139,7 @@ func NewLotteryAction(l *Lottery, tx *types.Transaction, index int) *Action { ...@@ -130,6 +139,7 @@ func NewLotteryAction(l *Lottery, tx *types.Transaction, index int) *Action {
l.GetHeight(), dapp.ExecAddress(string(tx.Execer)), l.GetDifficulty(), l.GetApi(), conn, grpcClient, index} l.GetHeight(), dapp.ExecAddress(string(tx.Execer)), l.GetDifficulty(), l.GetApi(), conn, grpcClient, index}
} }
// GetReceiptLog generate logs for all lottery action
func (action *Action) GetReceiptLog(lottery *pty.Lottery, preStatus int32, logTy int32, func (action *Action) GetReceiptLog(lottery *pty.Lottery, preStatus int32, logTy int32,
round int64, buyNumber int64, amount int64, way int64, luckyNum int64, updateInfo *pty.LotteryUpdateBuyInfo) *types.ReceiptLog { round int64, buyNumber int64, amount int64, way int64, luckyNum int64, updateInfo *pty.LotteryUpdateBuyInfo) *types.ReceiptLog {
log := &types.ReceiptLog{} log := &types.ReceiptLog{}
...@@ -164,17 +174,19 @@ func (action *Action) GetReceiptLog(lottery *pty.Lottery, preStatus int32, logTy ...@@ -164,17 +174,19 @@ func (action *Action) GetReceiptLog(lottery *pty.Lottery, preStatus int32, logTy
return log return log
} }
//fmt.Sprintf("%018d", action.height*types.MaxTxsPerBlock+int64(action.index)) // GetIndex returns index in block
func (action *Action) GetIndex() int64 { func (action *Action) GetIndex() int64 {
return action.height*types.MaxTxsPerBlock + int64(action.index) return action.height*types.MaxTxsPerBlock + int64(action.index)
} }
// LotteryCreate Action
// creator should be valid
func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt, error) { func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt, error) {
var logs []*types.ReceiptLog var logs []*types.ReceiptLog
var kv []*types.KeyValue var kv []*types.KeyValue
var receipt *types.Receipt var receipt *types.Receipt
lotteryId := common.ToHex(action.txhash) lotteryID := common.ToHex(action.txhash)
if !isRightCreator(action.fromaddr, action.db, false) { if !isRightCreator(action.fromaddr, action.db, false) {
return nil, pty.ErrNoPrivilege return nil, pty.ErrNoPrivilege
...@@ -192,13 +204,13 @@ func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt, ...@@ -192,13 +204,13 @@ func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt,
return nil, pty.ErrLotteryDrawBlockLimit return nil, pty.ErrLotteryDrawBlockLimit
} }
_, err := findLottery(action.db, lotteryId) _, err := findLottery(action.db, lotteryID)
if err != types.ErrNotFound { if err != types.ErrNotFound {
llog.Error("LotteryCreate", "LotteryCreate repeated", lotteryId) llog.Error("LotteryCreate", "LotteryCreate repeated", lotteryID)
return nil, pty.ErrLotteryRepeatHash return nil, pty.ErrLotteryRepeatHash
} }
lott := NewLotteryDB(lotteryId, create.GetPurBlockNum(), lott := NewLotteryDB(lotteryID, create.GetPurBlockNum(),
create.GetDrawBlockNum(), action.height, action.fromaddr) create.GetDrawBlockNum(), action.height, action.fromaddr)
if types.IsPara() { if types.IsPara() {
...@@ -210,7 +222,7 @@ func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt, ...@@ -210,7 +222,7 @@ func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt,
lott.CreateOnMain = mainHeight lott.CreateOnMain = mainHeight
} }
llog.Debug("LotteryCreate created", "lotteryId", lotteryId) llog.Debug("LotteryCreate created", "lotteryID", lotteryID)
lott.Save(action.db) lott.Save(action.db)
kv = append(kv, lott.GetKVSet()...) kv = append(kv, lott.GetKVSet()...)
...@@ -218,11 +230,12 @@ func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt, ...@@ -218,11 +230,12 @@ func (action *Action) LotteryCreate(create *pty.LotteryCreate) (*types.Receipt,
receiptLog := action.GetReceiptLog(&lott.Lottery, 0, pty.TyLogLotteryCreate, 0, 0, 0, 0, 0, nil) receiptLog := action.GetReceiptLog(&lott.Lottery, 0, pty.TyLogLotteryCreate, 0, 0, 0, 0, 0, nil)
logs = append(logs, receiptLog) logs = append(logs, receiptLog)
receipt = &types.Receipt{types.ExecOk, kv, logs} receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil return receipt, nil
} }
//one bty for one ticket // LotteryBuy Action
// One coin for one ticket
func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) { func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) {
var logs []*types.ReceiptLog var logs []*types.ReceiptLog
var kv []*types.KeyValue var kv []*types.KeyValue
...@@ -254,7 +267,7 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) { ...@@ -254,7 +267,7 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) {
llog.Debug("LotteryBuy switch to purchasestate") llog.Debug("LotteryBuy switch to purchasestate")
lott.LastTransToPurState = action.height lott.LastTransToPurState = action.height
lott.Status = pty.LotteryPurchase lott.Status = pty.LotteryPurchase
lott.Round += 1 lott.Round++
if types.IsPara() { if types.IsPara() {
mainHeight := action.GetMainHeightByTxHash(action.txhash) mainHeight := action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 { if mainHeight < 0 {
...@@ -303,7 +316,7 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) { ...@@ -303,7 +316,7 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) {
lott.Records = make(map[string]*pty.PurchaseRecords) lott.Records = make(map[string]*pty.PurchaseRecords)
} }
newRecord := &pty.PurchaseRecord{buy.GetAmount(), buy.GetNumber(), action.GetIndex(), buy.GetWay()} newRecord := &pty.PurchaseRecord{Amount: buy.GetAmount(), Number: buy.GetNumber(), Index: action.GetIndex(), Way: buy.GetWay()}
llog.Debug("LotteryBuy", "amount", buy.GetAmount(), "number", buy.GetNumber()) llog.Debug("LotteryBuy", "amount", buy.GetAmount(), "number", buy.GetNumber())
/********** /**********
...@@ -347,12 +360,13 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) { ...@@ -347,12 +360,13 @@ func (action *Action) LotteryBuy(buy *pty.LotteryBuy) (*types.Receipt, error) {
receiptLog := action.GetReceiptLog(&lott.Lottery, preStatus, pty.TyLogLotteryBuy, lott.Round, buy.GetNumber(), buy.GetAmount(), buy.GetWay(), 0, nil) receiptLog := action.GetReceiptLog(&lott.Lottery, preStatus, pty.TyLogLotteryBuy, lott.Round, buy.GetNumber(), buy.GetAmount(), buy.GetWay(), 0, nil)
logs = append(logs, receiptLog) logs = append(logs, receiptLog)
receipt = &types.Receipt{types.ExecOk, kv, logs} receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil return receipt, nil
} }
//1.Anyone who buy a ticket // LotteryDraw Action
//2.Creator // 1.Anyone who buy a ticket
// 2.Creator
func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error) { func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error) {
var logs []*types.ReceiptLog var logs []*types.ReceiptLog
var kv []*types.KeyValue var kv []*types.KeyValue
...@@ -410,10 +424,11 @@ func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error) ...@@ -410,10 +424,11 @@ func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error)
receiptLog := action.GetReceiptLog(&lott.Lottery, preStatus, pty.TyLogLotteryDraw, lott.Round, 0, 0, 0, lott.LuckyNumber, updateInfo) receiptLog := action.GetReceiptLog(&lott.Lottery, preStatus, pty.TyLogLotteryDraw, lott.Round, 0, 0, 0, lott.LuckyNumber, updateInfo)
logs = append(logs, receiptLog) logs = append(logs, receiptLog)
receipt = &types.Receipt{types.ExecOk, kv, logs} receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
return receipt, nil return receipt, nil
} }
// LotteryClose Action
func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, error) { func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, error) {
var logs []*types.ReceiptLog var logs []*types.ReceiptLog
var kv []*types.KeyValue var kv []*types.KeyValue
...@@ -442,7 +457,7 @@ func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, erro ...@@ -442,7 +457,7 @@ func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, erro
addrkeys := make([]string, len(lott.Records)) addrkeys := make([]string, len(lott.Records))
i := 0 i := 0
var totalReturn int64 = 0 var totalReturn int64
for addr := range lott.Records { for addr := range lott.Records {
totalReturn += lott.Records[addr].AmountOneRound totalReturn += lott.Records[addr].AmountOneRound
addrkeys[i] = addr addrkeys[i] = addr
...@@ -487,17 +502,18 @@ func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, erro ...@@ -487,17 +502,18 @@ func (action *Action) LotteryClose(draw *pty.LotteryClose) (*types.Receipt, erro
receiptLog := action.GetReceiptLog(&lott.Lottery, preStatus, pty.TyLogLotteryClose, 0, 0, 0, 0, 0, nil) receiptLog := action.GetReceiptLog(&lott.Lottery, preStatus, pty.TyLogLotteryClose, 0, 0, 0, 0, 0, nil)
logs = append(logs, receiptLog) logs = append(logs, receiptLog)
return &types.Receipt{types.ExecOk, kv, logs}, nil return &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}, nil
} }
func (action *Action) GetModify(beg, end int64, randMolNum int64) ([]byte, error) { // GetCalculableHash return hash for calculation
func (action *Action) GetCalculableHash(beg, end int64, randMolNum int64) ([]byte, error) {
//通过某个区间计算modify //通过某个区间计算modify
timeSource := int64(0) timeSource := int64(0)
total := int64(0) total := int64(0)
//last := []byte("last") //last := []byte("last")
newmodify := "" newmodify := ""
for i := beg; i < end; i += randMolNum { for i := beg; i < end; i += randMolNum {
req := &types.ReqBlocks{i, i, false, []string{""}} req := &types.ReqBlocks{Start: i, End: i, IsDetail: false, Pid: []string{""}}
blocks, err := action.api.GetBlocks(req) blocks, err := action.api.GetBlocks(req)
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
...@@ -534,14 +550,14 @@ func (action *Action) GetModify(beg, end int64, randMolNum int64) ([]byte, error ...@@ -534,14 +550,14 @@ func (action *Action) GetModify(beg, end int64, randMolNum int64) ([]byte, error
//random used for verfication in solo //random used for verfication in solo
func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 { func (action *Action) findLuckyNum(isSolo bool, lott *LotteryDB) int64 {
var num int64 = 0 var num int64
if isSolo { if isSolo {
//used for internal verfication //used for internal verfication
num = 12345 num = 12345
} else { } else {
randMolNum := (lott.TotalPurchasedTxNum+action.height-lott.LastTransToPurState)%3 + 2 //3~5 randMolNum := (lott.TotalPurchasedTxNum+action.height-lott.LastTransToPurState)%3 + 2 //3~5
modify, err := action.GetModify(lott.LastTransToPurState, action.height-1, randMolNum) modify, err := action.GetCalculableHash(lott.LastTransToPurState, action.height-1, randMolNum)
llog.Error("findLuckyNum", "begin", lott.LastTransToPurState, "end", action.height-1, "randMolNum", randMolNum) llog.Error("findLuckyNum", "begin", lott.LastTransToPurState, "end", action.height-1, "randMolNum", randMolNum)
if err != nil { if err != nil {
...@@ -591,8 +607,8 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp ...@@ -591,8 +607,8 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
//calculate fund for all participant showed their number //calculate fund for all participant showed their number
var updateInfo pty.LotteryUpdateBuyInfo var updateInfo pty.LotteryUpdateBuyInfo
updateInfo.BuyInfo = make(map[string]*pty.LotteryUpdateRecs) updateInfo.BuyInfo = make(map[string]*pty.LotteryUpdateRecs)
var tempFund int64 = 0 var tempFund int64
var totalFund int64 = 0 var totalFund int64
addrkeys := make([]string, len(lott.Records)) addrkeys := make([]string, len(lott.Records))
i := 0 i := 0
for addr := range lott.Records { for addr := range lott.Records {
...@@ -601,7 +617,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp ...@@ -601,7 +617,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
for _, rec := range lott.Records[addr].Record { for _, rec := range lott.Records[addr].Record {
fund, fundType := checkFundAmount(luckynum, rec.Number, rec.Way) fund, fundType := checkFundAmount(luckynum, rec.Number, rec.Way)
if fund != 0 { if fund != 0 {
newUpdateRec := &pty.LotteryUpdateRec{rec.Index, fundType} newUpdateRec := &pty.LotteryUpdateRec{Index: rec.Index, Type: fundType}
if update, ok := updateInfo.BuyInfo[addr]; ok { if update, ok := updateInfo.BuyInfo[addr]; ok {
update.Records = append(update.Records, newUpdateRec) update.Records = append(update.Records, newUpdateRec)
} else { } else {
...@@ -617,7 +633,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp ...@@ -617,7 +633,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
} }
llog.Debug("checkDraw", "lenofupdate", len(updateInfo.BuyInfo)) llog.Debug("checkDraw", "lenofupdate", len(updateInfo.BuyInfo))
llog.Debug("checkDraw", "update", updateInfo.BuyInfo) llog.Debug("checkDraw", "update", updateInfo.BuyInfo)
var factor float64 = 0 var factor float64
if totalFund > lott.GetFund()/2 { if totalFund > lott.GetFund()/2 {
llog.Debug("checkDraw ajust fund", "lott.Fund", lott.Fund, "totalFund", totalFund) llog.Debug("checkDraw ajust fund", "lott.Fund", lott.Fund, "totalFund", totalFund)
factor = (float64)(lott.GetFund()) / 2 / (float64)(totalFund) factor = (float64)(lott.GetFund()) / 2 / (float64)(totalFund)
...@@ -677,7 +693,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp ...@@ -677,7 +693,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
lott.LastTransToDrawStateOnMain = mainHeight lott.LastTransToDrawStateOnMain = mainHeight
} }
return &types.Receipt{types.ExecOk, kv, logs}, &updateInfo, nil return &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}, &updateInfo, nil
} }
func (action *Action) recordMissing(lott *LotteryDB) { func (action *Action) recordMissing(lott *LotteryDB) {
temp := int32(lott.LuckyNumber) temp := int32(lott.LuckyNumber)
...@@ -692,7 +708,7 @@ func (action *Action) recordMissing(lott *LotteryDB) { ...@@ -692,7 +708,7 @@ func (action *Action) recordMissing(lott *LotteryDB) {
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
for j := 0; j < 10; j++ { for j := 0; j < 10; j++ {
if eachNum[i] != sample[j] { if eachNum[i] != sample[j] {
lott.MissingRecords[i].Times[j] += 1 lott.MissingRecords[i].Times[j]++
} }
} }
} }
...@@ -710,7 +726,7 @@ func getManageKey(key string, db dbm.KV) ([]byte, error) { ...@@ -710,7 +726,7 @@ func getManageKey(key string, db dbm.KV) ([]byte, error) {
func isRightCreator(addr string, db dbm.KV, isSolo bool) bool { func isRightCreator(addr string, db dbm.KV, isSolo bool) bool {
if isSolo { if isSolo {
return true return true
} else { }
value, err := getManageKey(creatorKey, db) value, err := getManageKey(creatorKey, db)
if err != nil { if err != nil {
llog.Error("LotteryCreate", "creatorKey", creatorKey) llog.Error("LotteryCreate", "creatorKey", creatorKey)
...@@ -734,15 +750,15 @@ func isRightCreator(addr string, db dbm.KV, isSolo bool) bool { ...@@ -734,15 +750,15 @@ func isRightCreator(addr string, db dbm.KV, isSolo bool) bool {
} }
} }
return false return false
}
} }
func isEableToClose() bool { func isEableToClose() bool {
return true return true
} }
func findLottery(db dbm.KV, lotteryId string) (*pty.Lottery, error) { func findLottery(db dbm.KV, lotteryID string) (*pty.Lottery, error) {
data, err := db.Get(Key(lotteryId)) data, err := db.Get(Key(lotteryID))
if err != nil { if err != nil {
llog.Debug("findLottery", "get", err) llog.Debug("findLottery", "get", err)
return nil, err return nil, err
...@@ -757,6 +773,7 @@ func findLottery(db dbm.KV, lotteryId string) (*pty.Lottery, error) { ...@@ -757,6 +773,7 @@ func findLottery(db dbm.KV, lotteryId string) (*pty.Lottery, error) {
return &lott, nil return &lott, nil
} }
// CheckExecAccount check the account avoiding rollback
func (action *Action) CheckExecAccount(addr string, amount int64, isFrozen bool) bool { func (action *Action) CheckExecAccount(addr string, amount int64, isFrozen bool) bool {
acc := action.coinsAccount.LoadExecAccount(addr, action.execaddr) acc := action.coinsAccount.LoadExecAccount(addr, action.execaddr)
if isFrozen { if isFrozen {
...@@ -772,6 +789,7 @@ func (action *Action) CheckExecAccount(addr string, amount int64, isFrozen bool) ...@@ -772,6 +789,7 @@ func (action *Action) CheckExecAccount(addr string, amount int64, isFrozen bool)
return false return false
} }
// ListLotteryLuckyHistory returns all the luckynum in history
func ListLotteryLuckyHistory(db dbm.Lister, stateDB dbm.KV, param *pty.ReqLotteryLuckyHistory) (types.Message, error) { func ListLotteryLuckyHistory(db dbm.Lister, stateDB dbm.KV, param *pty.ReqLotteryLuckyHistory) (types.Message, error) {
direction := ListDESC direction := ListDESC
if param.GetDirection() == ListASC { if param.GetDirection() == ListASC {
...@@ -811,6 +829,7 @@ func ListLotteryLuckyHistory(db dbm.Lister, stateDB dbm.KV, param *pty.ReqLotter ...@@ -811,6 +829,7 @@ func ListLotteryLuckyHistory(db dbm.Lister, stateDB dbm.KV, param *pty.ReqLotter
return &records, nil return &records, nil
} }
// ListLotteryBuyRecords for addr
func ListLotteryBuyRecords(db dbm.Lister, stateDB dbm.KV, param *pty.ReqLotteryBuyHistory) (types.Message, error) { func ListLotteryBuyRecords(db dbm.Lister, stateDB dbm.KV, param *pty.ReqLotteryBuyHistory) (types.Message, error) {
direction := ListDESC direction := ListDESC
if param.GetDirection() == ListASC { if param.GetDirection() == ListASC {
......
...@@ -9,17 +9,19 @@ import ( ...@@ -9,17 +9,19 @@ import (
pty "github.com/33cn/plugin/plugin/dapp/lottery/types" pty "github.com/33cn/plugin/plugin/dapp/lottery/types"
) )
// Query_GetLotteryNormalInfo not changed info
func (l *Lottery) Query_GetLotteryNormalInfo(param *pty.ReqLotteryInfo) (types.Message, error) { func (l *Lottery) Query_GetLotteryNormalInfo(param *pty.ReqLotteryInfo) (types.Message, error) {
lottery, err := findLottery(l.GetStateDB(), param.GetLotteryId()) lottery, err := findLottery(l.GetStateDB(), param.GetLotteryId())
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &pty.ReplyLotteryNormalInfo{lottery.CreateHeight, return &pty.ReplyLotteryNormalInfo{CreateHeight: lottery.CreateHeight,
lottery.PurBlockNum, PurBlockNum: lottery.PurBlockNum,
lottery.DrawBlockNum, DrawBlockNum: lottery.DrawBlockNum,
lottery.CreateAddr}, nil CreateAddr: lottery.CreateAddr}, nil
} }
// Query_GetLotteryPurchaseAddr for current round
func (l *Lottery) Query_GetLotteryPurchaseAddr(param *pty.ReqLotteryInfo) (types.Message, error) { func (l *Lottery) Query_GetLotteryPurchaseAddr(param *pty.ReqLotteryInfo) (types.Message, error) {
lottery, err := findLottery(l.GetStateDB(), param.GetLotteryId()) lottery, err := findLottery(l.GetStateDB(), param.GetLotteryId())
if err != nil { if err != nil {
...@@ -33,6 +35,7 @@ func (l *Lottery) Query_GetLotteryPurchaseAddr(param *pty.ReqLotteryInfo) (types ...@@ -33,6 +35,7 @@ func (l *Lottery) Query_GetLotteryPurchaseAddr(param *pty.ReqLotteryInfo) (types
return reply, nil return reply, nil
} }
// Query_GetLotteryCurrentInfo state
func (l *Lottery) Query_GetLotteryCurrentInfo(param *pty.ReqLotteryInfo) (types.Message, error) { func (l *Lottery) Query_GetLotteryCurrentInfo(param *pty.ReqLotteryInfo) (types.Message, error) {
lottery, err := findLottery(l.GetStateDB(), param.GetLotteryId()) lottery, err := findLottery(l.GetStateDB(), param.GetLotteryId())
if err != nil { if err != nil {
...@@ -54,10 +57,12 @@ func (l *Lottery) Query_GetLotteryCurrentInfo(param *pty.ReqLotteryInfo) (types. ...@@ -54,10 +57,12 @@ func (l *Lottery) Query_GetLotteryCurrentInfo(param *pty.ReqLotteryInfo) (types.
return reply, nil return reply, nil
} }
// Query_GetLotteryHistoryLuckyNumber for all history
func (l *Lottery) Query_GetLotteryHistoryLuckyNumber(param *pty.ReqLotteryLuckyHistory) (types.Message, error) { func (l *Lottery) Query_GetLotteryHistoryLuckyNumber(param *pty.ReqLotteryLuckyHistory) (types.Message, error) {
return ListLotteryLuckyHistory(l.GetLocalDB(), l.GetStateDB(), param) return ListLotteryLuckyHistory(l.GetLocalDB(), l.GetStateDB(), param)
} }
// Query_GetLotteryRoundLuckyNumber for each round
func (l *Lottery) Query_GetLotteryRoundLuckyNumber(param *pty.ReqLotteryLuckyInfo) (types.Message, error) { func (l *Lottery) Query_GetLotteryRoundLuckyNumber(param *pty.ReqLotteryLuckyInfo) (types.Message, error) {
// var req pty.ReqLotteryLuckyInfo // var req pty.ReqLotteryLuckyInfo
var records []*pty.LotteryDrawRecord var records []*pty.LotteryDrawRecord
...@@ -77,10 +82,12 @@ func (l *Lottery) Query_GetLotteryRoundLuckyNumber(param *pty.ReqLotteryLuckyInf ...@@ -77,10 +82,12 @@ func (l *Lottery) Query_GetLotteryRoundLuckyNumber(param *pty.ReqLotteryLuckyInf
return &pty.LotteryDrawRecords{Records: records}, nil return &pty.LotteryDrawRecords{Records: records}, nil
} }
// Query_GetLotteryHistoryBuyInfo for all history
func (l *Lottery) Query_GetLotteryHistoryBuyInfo(param *pty.ReqLotteryBuyHistory) (types.Message, error) { func (l *Lottery) Query_GetLotteryHistoryBuyInfo(param *pty.ReqLotteryBuyHistory) (types.Message, error) {
return ListLotteryBuyRecords(l.GetLocalDB(), l.GetStateDB(), param) return ListLotteryBuyRecords(l.GetLocalDB(), l.GetStateDB(), param)
} }
// Query_GetLotteryBuyRoundInfo for each round
func (l *Lottery) Query_GetLotteryBuyRoundInfo(param *pty.ReqLotteryBuyInfo) (types.Message, error) { func (l *Lottery) Query_GetLotteryBuyRoundInfo(param *pty.ReqLotteryBuyInfo) (types.Message, error) {
key := calcLotteryBuyRoundPrefix(param.LotteryId, param.Addr, param.Round) key := calcLotteryBuyRoundPrefix(param.LotteryId, param.Addr, param.Round)
record, err := l.findLotteryBuyRecords(key) record, err := l.findLotteryBuyRecords(key)
......
...@@ -6,6 +6,7 @@ package types ...@@ -6,6 +6,7 @@ package types
import "errors" import "errors"
// Errors for lottery
var ( var (
ErrNoPrivilege = errors.New("ErrNoPrivilege") ErrNoPrivilege = errors.New("ErrNoPrivilege")
ErrLotteryStatus = errors.New("ErrLotteryStatus") ErrLotteryStatus = errors.New("ErrLotteryStatus")
......
...@@ -23,17 +23,20 @@ func init() { ...@@ -23,17 +23,20 @@ func init() {
types.RegisterDappFork(LotteryX, "Enable", 0) types.RegisterDappFork(LotteryX, "Enable", 0)
} }
// LotteryType def
type LotteryType struct { type LotteryType struct {
types.ExecTypeBase types.ExecTypeBase
} }
// NewType method
func NewType() *LotteryType { func NewType() *LotteryType {
c := &LotteryType{} c := &LotteryType{}
c.SetChild(c) c.SetChild(c)
return c return c
} }
func (at *LotteryType) GetLogMap() map[int64]*types.LogInfo { // GetLogMap method
func (lottery *LotteryType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{ return map[int64]*types.LogInfo{
TyLogLotteryCreate: {reflect.TypeOf(ReceiptLottery{}), "LogLotteryCreate"}, TyLogLotteryCreate: {reflect.TypeOf(ReceiptLottery{}), "LogLotteryCreate"},
TyLogLotteryBuy: {reflect.TypeOf(ReceiptLottery{}), "LogLotteryBuy"}, TyLogLotteryBuy: {reflect.TypeOf(ReceiptLottery{}), "LogLotteryBuy"},
...@@ -42,13 +45,15 @@ func (at *LotteryType) GetLogMap() map[int64]*types.LogInfo { ...@@ -42,13 +45,15 @@ func (at *LotteryType) GetLogMap() map[int64]*types.LogInfo {
} }
} }
func (at *LotteryType) GetPayload() types.Message { // GetPayload method
func (lottery *LotteryType) GetPayload() types.Message {
return &LotteryAction{} return &LotteryAction{}
} }
// CreateTx method
func (lottery LotteryType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) { func (lottery LotteryType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
llog.Debug("lottery.CreateTx", "action", action) llog.Debug("lottery.CreateTx", "action", action)
var tx *types.Transaction
if action == "LotteryCreate" { if action == "LotteryCreate" {
var param LotteryCreateTx var param LotteryCreateTx
err := json.Unmarshal(message, &param) err := json.Unmarshal(message, &param)
...@@ -84,11 +89,10 @@ func (lottery LotteryType) CreateTx(action string, message json.RawMessage) (*ty ...@@ -84,11 +89,10 @@ func (lottery LotteryType) CreateTx(action string, message json.RawMessage) (*ty
} else { } else {
return nil, types.ErrNotSupport return nil, types.ErrNotSupport
} }
return tx, nil
} }
func (lott LotteryType) GetTypeMap() map[string]int32 { // GetTypeMap method
func (lottery LotteryType) GetTypeMap() map[string]int32 {
return map[string]int32{ return map[string]int32{
"Create": LotteryActionCreate, "Create": LotteryActionCreate,
"Buy": LotteryActionBuy, "Buy": LotteryActionBuy,
...@@ -97,6 +101,7 @@ func (lott LotteryType) GetTypeMap() map[string]int32 { ...@@ -97,6 +101,7 @@ func (lott LotteryType) GetTypeMap() map[string]int32 {
} }
} }
// CreateRawLotteryCreateTx method
func CreateRawLotteryCreateTx(parm *LotteryCreateTx) (*types.Transaction, error) { func CreateRawLotteryCreateTx(parm *LotteryCreateTx) (*types.Transaction, error) {
if parm == nil { if parm == nil {
llog.Error("CreateRawLotteryCreateTx", "parm", parm) llog.Error("CreateRawLotteryCreateTx", "parm", parm)
...@@ -125,6 +130,7 @@ func CreateRawLotteryCreateTx(parm *LotteryCreateTx) (*types.Transaction, error) ...@@ -125,6 +130,7 @@ func CreateRawLotteryCreateTx(parm *LotteryCreateTx) (*types.Transaction, error)
return tx, nil return tx, nil
} }
// CreateRawLotteryBuyTx method
func CreateRawLotteryBuyTx(parm *LotteryBuyTx) (*types.Transaction, error) { func CreateRawLotteryBuyTx(parm *LotteryBuyTx) (*types.Transaction, error) {
if parm == nil { if parm == nil {
llog.Error("CreateRawLotteryBuyTx", "parm", parm) llog.Error("CreateRawLotteryBuyTx", "parm", parm)
...@@ -132,7 +138,7 @@ func CreateRawLotteryBuyTx(parm *LotteryBuyTx) (*types.Transaction, error) { ...@@ -132,7 +138,7 @@ func CreateRawLotteryBuyTx(parm *LotteryBuyTx) (*types.Transaction, error) {
} }
v := &LotteryBuy{ v := &LotteryBuy{
LotteryId: parm.LotteryId, LotteryId: parm.LotteryID,
Amount: parm.Amount, Amount: parm.Amount,
Number: parm.Number, Number: parm.Number,
Way: parm.Way, Way: parm.Way,
...@@ -155,6 +161,7 @@ func CreateRawLotteryBuyTx(parm *LotteryBuyTx) (*types.Transaction, error) { ...@@ -155,6 +161,7 @@ func CreateRawLotteryBuyTx(parm *LotteryBuyTx) (*types.Transaction, error) {
return tx, nil return tx, nil
} }
// CreateRawLotteryDrawTx method
func CreateRawLotteryDrawTx(parm *LotteryDrawTx) (*types.Transaction, error) { func CreateRawLotteryDrawTx(parm *LotteryDrawTx) (*types.Transaction, error) {
if parm == nil { if parm == nil {
llog.Error("CreateRawLotteryDrawTx", "parm", parm) llog.Error("CreateRawLotteryDrawTx", "parm", parm)
...@@ -162,7 +169,7 @@ func CreateRawLotteryDrawTx(parm *LotteryDrawTx) (*types.Transaction, error) { ...@@ -162,7 +169,7 @@ func CreateRawLotteryDrawTx(parm *LotteryDrawTx) (*types.Transaction, error) {
} }
v := &LotteryDraw{ v := &LotteryDraw{
LotteryId: parm.LotteryId, LotteryId: parm.LotteryID,
} }
draw := &LotteryAction{ draw := &LotteryAction{
Ty: LotteryActionDraw, Ty: LotteryActionDraw,
...@@ -182,6 +189,7 @@ func CreateRawLotteryDrawTx(parm *LotteryDrawTx) (*types.Transaction, error) { ...@@ -182,6 +189,7 @@ func CreateRawLotteryDrawTx(parm *LotteryDrawTx) (*types.Transaction, error) {
return tx, nil return tx, nil
} }
// CreateRawLotteryCloseTx method
func CreateRawLotteryCloseTx(parm *LotteryCloseTx) (*types.Transaction, error) { func CreateRawLotteryCloseTx(parm *LotteryCloseTx) (*types.Transaction, error) {
if parm == nil { if parm == nil {
llog.Error("CreateRawLotteryCloseTx", "parm", parm) llog.Error("CreateRawLotteryCloseTx", "parm", parm)
...@@ -189,7 +197,7 @@ func CreateRawLotteryCloseTx(parm *LotteryCloseTx) (*types.Transaction, error) { ...@@ -189,7 +197,7 @@ func CreateRawLotteryCloseTx(parm *LotteryCloseTx) (*types.Transaction, error) {
} }
v := &LotteryClose{ v := &LotteryClose{
LotteryId: parm.LotteryId, LotteryId: parm.LotteryID,
} }
close := &LotteryAction{ close := &LotteryAction{
Ty: LotteryActionClose, Ty: LotteryActionClose,
......
...@@ -4,26 +4,30 @@ ...@@ -4,26 +4,30 @@
package types package types
// LotteryCreateTx for construction
type LotteryCreateTx struct { type LotteryCreateTx struct {
PurBlockNum int64 `json:"purBlockNum"` PurBlockNum int64 `json:"purBlockNum"`
DrawBlockNum int64 `json:"drawBlockNum"` DrawBlockNum int64 `json:"drawBlockNum"`
Fee int64 `json:"fee"` Fee int64 `json:"fee"`
} }
// LotteryBuyTx for construction
type LotteryBuyTx struct { type LotteryBuyTx struct {
LotteryId string `json:"lotteryId"` LotteryID string `json:"lotteryId"`
Amount int64 `json:"amount"` Amount int64 `json:"amount"`
Number int64 `json:"number"` Number int64 `json:"number"`
Way int64 `json:"way"` Way int64 `json:"way"`
Fee int64 `json:"fee"` Fee int64 `json:"fee"`
} }
// LotteryDrawTx for construction
type LotteryDrawTx struct { type LotteryDrawTx struct {
LotteryId string `json:"lotteryId"` LotteryID string `json:"lotteryId"`
Fee int64 `json:"fee"` Fee int64 `json:"fee"`
} }
// LotteryCloseTx for construction
type LotteryCloseTx struct { type LotteryCloseTx struct {
LotteryId string `json:"lotteryId"` LotteryID string `json:"lotteryId"`
Fee int64 `json:"fee"` Fee int64 `json:"fee"`
} }
...@@ -19,6 +19,7 @@ const ( ...@@ -19,6 +19,7 @@ const (
TyLogLotteryClose = 804 TyLogLotteryClose = 804
) )
// Lottery name
const ( const (
LotteryX = "lottery" LotteryX = "lottery"
) )
......
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