Commit 3c2cadc7 authored by wjx@disanbo.com's avatar wjx@disanbo.com Committed by vipwzw

add query interface

parent c19d422d
......@@ -31,6 +31,8 @@ func (l *Lottery) execDelLocal(tx *types.Transaction, receiptData *types.Receipt
set.KV = append(set.KV, kv...)
kv = l.updateLotteryBuy(&lotterylog, false)
set.KV = append(set.KV, kv...)
kv = l.deleteLotteryGain(&lotterylog)
set.KV = append(set.KV, kv...)
}
}
}
......
......@@ -31,6 +31,8 @@ func (l *Lottery) execLocal(tx *types.Transaction, receipt *types.ReceiptData) (
set.KV = append(set.KV, kv...)
kv = l.updateLotteryBuy(&lotterylog, true)
set.KV = append(set.KV, kv...)
kv = l.saveLotteryGain(&lotterylog)
set.KV = append(set.KV, kv...)
}
}
}
......
......@@ -35,3 +35,13 @@ func calcLotteryKey(lotteryID string, status int32) []byte {
key := fmt.Sprintf("LODB-lottery-:%d:%s", status, lotteryID)
return []byte(key)
}
func calcLotteryGainPrefix(lotteryID string, addr string) []byte {
key := fmt.Sprintf("LODB-lottery-gain:%s:%s", lotteryID, addr)
return []byte(key)
}
func calcLotteryGainKey(lotteryID string, addr string, round int64) []byte {
key := fmt.Sprintf("LODB-lottery-gain:%s:%s:%10d", lotteryID, addr, round)
return []byte(key)
}
......@@ -122,6 +122,25 @@ func (lott *Lottery) findLotteryDrawRecord(key []byte) (*pty.LotteryDrawRecord,
return &record, nil
}
func (lott *Lottery) findLotteryGainRecord(key []byte) (*pty.LotteryGainRecord, error) {
value, err := lott.GetLocalDB().Get(key)
if err != nil && err != types.ErrNotFound {
llog.Error("findLotteryGainRecord", "err", err)
return nil, err
}
if err == types.ErrNotFound {
return nil, nil
}
var record pty.LotteryGainRecord
err = types.Decode(value, &record)
if err != nil {
llog.Error("findLotteryGainRecord", "err", err)
return nil, err
}
return &record, nil
}
func (lott *Lottery) saveLotteryBuy(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) {
key := calcLotteryBuyKey(lotterylog.LotteryId, lotterylog.Addr, lotterylog.Round, lotterylog.Index)
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}
......@@ -234,3 +253,27 @@ func (lott *Lottery) GetPayloadValue() types.Message {
func (lott *Lottery) CheckReceiptExecOk() bool {
return true
}
func (lott *Lottery) saveLotteryGain(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) {
for _, gain := range lotterylog.GainInfos.Gains {
key := calcLotteryGainKey(lotterylog.LotteryId, gain.Addr, lotterylog.Round)
record := &pty.LotteryGainRecord{Addr: gain.Addr, BuyAmount: gain.BuyAmount, FundAmount: gain.FundAmount, Round: lotterylog.Round}
kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv)
}
return kvs
}
func (lott *Lottery) deleteLotteryGain(lotterylog *pty.ReceiptLottery) (kvs []*types.KeyValue) {
for _, gain := range lotterylog.GainInfos.Gains {
kv := &types.KeyValue{}
kv.Key = calcLotteryGainKey(lotterylog.LotteryId, gain.Addr, lotterylog.Round)
kv.Value = nil
kvs = append(kvs, kv)
}
return kvs
}
......@@ -192,7 +192,7 @@ func (action *Action) GetBuyReceiptLog(lottery *pty.Lottery, preStatus int32, ro
}
// GetDrawReceiptLog generate logs for lottery draw action
func (action *Action) GetDrawReceiptLog(lottery *pty.Lottery, preStatus int32, round int64, luckyNum int64, updateInfo *pty.LotteryUpdateBuyInfo, addrNumThisRound int64, buyAmountThisRound int64) *types.ReceiptLog {
func (action *Action) GetDrawReceiptLog(lottery *pty.Lottery, preStatus int32, round int64, luckyNum int64, updateInfo *pty.LotteryUpdateBuyInfo, addrNumThisRound int64, buyAmountThisRound int64, gainInfos *pty.LotteryGainInfos) *types.ReceiptLog {
log := &types.ReceiptLog{}
log.Ty = pty.TyLogLotteryDraw
......@@ -208,6 +208,8 @@ func (action *Action) GetDrawReceiptLog(lottery *pty.Lottery, preStatus int32, r
l.UpdateInfo = updateInfo
}
l.GainInfos = gainInfos
log.Log = types.Encode(l)
return log
......@@ -476,7 +478,7 @@ func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error)
addrNumThisRound := lott.TotalAddrNum
buyAmountThisRound := lott.BuyAmount
rec, updateInfo, err := action.checkDraw(lott)
rec, updateInfo, gainInfos, err := action.checkDraw(lott)
if err != nil {
return nil, err
}
......@@ -486,7 +488,7 @@ func (action *Action) LotteryDraw(draw *pty.LotteryDraw) (*types.Receipt, error)
lott.Save(action.db)
kv = append(kv, lott.GetKVSet()...)
receiptLog := action.GetDrawReceiptLog(&lott.Lottery, preStatus, lott.Round, lott.LuckyNumber, updateInfo, addrNumThisRound, buyAmountThisRound)
receiptLog := action.GetDrawReceiptLog(&lott.Lottery, preStatus, lott.Round, lott.LuckyNumber, updateInfo, addrNumThisRound, buyAmountThisRound, gainInfos)
logs = append(logs, receiptLog)
receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}
......@@ -624,10 +626,10 @@ func checkFundAmount(luckynum int64, guessnum int64, way int64) (int64, int64) {
}
}
func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUpdateBuyInfo, error) {
func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUpdateBuyInfo, *pty.LotteryGainInfos, error) {
luckynum := action.findLuckyNum(false, lott)
if luckynum < 0 || luckynum >= luckyNumMol {
return nil, nil, pty.ErrLotteryErrLuckyNum
return nil, nil, nil, pty.ErrLotteryErrLuckyNum
}
llog.Debug("checkDraw", "luckynum", luckynum)
......@@ -635,6 +637,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
var logs []*types.ReceiptLog
var kv []*types.KeyValue
var updateInfo pty.LotteryUpdateBuyInfo
var gainInfos pty.LotteryGainInfos
var tempFund int64
var totalFund int64
......@@ -676,11 +679,11 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
//protection for rollback
if factor == 1.0 {
if !action.CheckExecAccount(lott.CreateAddr, totalFund, true) {
return nil, nil, pty.ErrLotteryFundNotEnough
return nil, nil, nil, pty.ErrLotteryFundNotEnough
}
} else {
if !action.CheckExecAccount(lott.CreateAddr, decimal*lott.Fund/2+1, true) {
return nil, nil, pty.ErrLotteryFundNotEnough
return nil, nil, nil, pty.ErrLotteryFundNotEnough
}
}
......@@ -688,13 +691,17 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
if recs.FundWin > 0 {
fund := (recs.FundWin * int64(factor*exciting)) * decimal * (rewardBase - lott.OpRewardRatio - lott.DevRewardRatio) / (exciting * rewardBase) //any problem when too little?
llog.Debug("checkDraw", "fund", fund)
gain := &pty.LotteryGainInfo{Addr: recs.Addr, BuyAmount: recs.AmountOneRound, FundAmount: float32(fund) / float32(decimal)}
gainInfos.Gains = append(gainInfos.Gains, gain)
receipt, err := action.coinsAccount.ExecTransferFrozen(lott.CreateAddr, recs.Addr, action.execaddr, fund)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
kv = append(kv, receipt.KV...)
logs = append(logs, receipt.Logs...)
} else {
gain := &pty.LotteryGainInfo{Addr: recs.Addr, BuyAmount: recs.AmountOneRound, FundAmount: 0}
gainInfos.Gains = append(gainInfos.Gains, gain)
}
}
......@@ -702,7 +709,7 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
fundOp := int64(factor*decimal) * totalFund * lott.OpRewardRatio / rewardBase
receipt, err := action.coinsAccount.ExecTransferFrozen(lott.CreateAddr, opRewardAddr, action.execaddr, fundOp)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
kv = append(kv, receipt.KV...)
logs = append(logs, receipt.Logs...)
......@@ -710,10 +717,15 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
fundDev := int64(factor*decimal) * totalFund * lott.DevRewardRatio / rewardBase
receipt, err = action.coinsAccount.ExecTransferFrozen(lott.CreateAddr, devRewardAddr, action.execaddr, fundDev)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
kv = append(kv, receipt.KV...)
logs = append(logs, receipt.Logs...)
} else {
for _, recs := range lott.PurRecords {
gain := &pty.LotteryGainInfo{Addr: recs.Addr, BuyAmount: recs.AmountOneRound, FundAmount: 0}
gainInfos.Gains = append(gainInfos.Gains, gain)
}
}
for i := range lott.PurRecords {
......@@ -734,12 +746,12 @@ func (action *Action) checkDraw(lott *LotteryDB) (*types.Receipt, *pty.LotteryUp
mainHeight := action.GetMainHeightByTxHash(action.txhash)
if mainHeight < 0 {
llog.Error("LotteryDraw", "mainHeight", mainHeight)
return nil, nil, pty.ErrLotteryStatus
return nil, nil, nil, pty.ErrLotteryStatus
}
lott.LastTransToDrawStateOnMain = mainHeight
}
return &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}, &updateInfo, nil
return &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: logs}, &updateInfo, &gainInfos, nil
}
func (action *Action) recordMissing(lott *LotteryDB) {
temp := int32(lott.LuckyNumber)
......@@ -916,3 +928,45 @@ func ListLotteryBuyRecords(db dbm.Lister, stateDB dbm.KV, param *pty.ReqLotteryB
return &records, nil
}
// ListLotteryGainRecords for addr
func ListLotteryGainRecords(db dbm.Lister, stateDB dbm.KV, param *pty.ReqLotteryGainHistory) (types.Message, error) {
direction := ListDESC
if param.GetDirection() == ListASC {
direction = ListASC
}
count := DefultCount
if 0 < param.GetCount() && param.GetCount() <= MaxCount {
count = param.GetCount()
}
var prefix []byte
var key []byte
var values [][]byte
var err error
prefix = calcLotteryGainPrefix(param.LotteryId, param.Addr)
key = calcLotteryGainKey(param.LotteryId, param.Addr, param.GetRound())
if param.GetRound() == 0 { //第一次查询
values, err = db.List(prefix, nil, count, direction)
} else {
values, err = db.List(prefix, key, count, direction)
}
if err != nil {
return nil, err
}
var records pty.LotteryGainRecords
for _, value := range values {
var record pty.LotteryGainRecord
err := types.Decode(value, &record)
if err != nil {
continue
}
records.Records = append(records.Records, &record)
}
return &records, nil
}
......@@ -99,3 +99,18 @@ func (l *Lottery) Query_GetLotteryBuyRoundInfo(param *pty.ReqLotteryBuyInfo) (ty
}
return record, nil
}
// Query_GetLotteryHistoryGainInfo for all history
func (l *Lottery) Query_GetLotteryHistoryGainInfo(param *pty.ReqLotteryGainHistory) (types.Message, error) {
return ListLotteryGainRecords(l.GetLocalDB(), l.GetStateDB(), param)
}
// Query_GetLotteryRoundGainInfo for each round
func (l *Lottery) Query_GetLotteryRoundGainInfo(param *pty.ReqLotteryGainInfo) (types.Message, error) {
key := calcLotteryGainKey(param.LotteryId, param.Addr, param.Round)
record, err := l.findLotteryGainRecord(key)
if err != nil {
return nil, err
}
return record, nil
}
......@@ -93,6 +93,7 @@ message ReceiptLottery {
int64 index = 13;
int64 totalAddrNum = 14;
int64 buyAmount = 15;
LotteryGainInfos gainInfos = 16;
}
message ReqLotteryInfo {
......@@ -210,3 +211,39 @@ message LotteryUpdateBuyInfo {
message ReplyLotteryPurchaseAddr {
repeated string address = 1;
}
message LotteryGainInfos{
repeated LotteryGainInfo gains = 1;
}
message LotteryGainInfo{
string addr = 1;
int64 buyAmount = 2;
float fundAmount = 3;
}
message LotteryGainRecord{
string addr = 1;
int64 buyAmount = 2;
float fundAmount = 3;
int64 round = 4;
}
message LotteryGainRecords {
repeated LotteryGainRecord records = 1;
}
message ReqLotteryGainHistory {
string lotteryId = 1;
string addr = 2;
int64 round = 3;
int32 count = 4;
int32 direction = 5;
}
message ReqLotteryGainInfo {
string lotteryId = 1;
string addr = 2;
int64 round = 3;
}
This diff is collapsed.
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