Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
3c2cadc7
Commit
3c2cadc7
authored
Dec 13, 2018
by
wjx@disanbo.com
Committed by
vipwzw
Dec 14, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add query interface
parent
c19d422d
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
176 additions
and
13 deletions
+176
-13
exec_del_local.go
plugin/dapp/lottery/executor/exec_del_local.go
+2
-0
exec_local.go
plugin/dapp/lottery/executor/exec_local.go
+2
-0
keys.go
plugin/dapp/lottery/executor/keys.go
+10
-0
lottery.go
plugin/dapp/lottery/executor/lottery.go
+43
-0
lotterydb.go
plugin/dapp/lottery/executor/lotterydb.go
+67
-13
query.go
plugin/dapp/lottery/executor/query.go
+15
-0
lottery.proto
plugin/dapp/lottery/proto/lottery.proto
+37
-0
lottery.pb.go
plugin/dapp/lottery/types/lottery.pb.go
+0
-0
No files found.
plugin/dapp/lottery/executor/exec_del_local.go
View file @
3c2cadc7
...
...
@@ -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
...
)
}
}
}
...
...
plugin/dapp/lottery/executor/exec_local.go
View file @
3c2cadc7
...
...
@@ -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
...
)
}
}
}
...
...
plugin/dapp/lottery/executor/keys.go
View file @
3c2cadc7
...
...
@@ -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
)
}
plugin/dapp/lottery/executor/lottery.go
View file @
3c2cadc7
...
...
@@ -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
}
plugin/dapp/lottery/executor/lotterydb.go
View file @
3c2cadc7
...
...
@@ -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
}
plugin/dapp/lottery/executor/query.go
View file @
3c2cadc7
...
...
@@ -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
}
plugin/dapp/lottery/proto/lottery.proto
View file @
3c2cadc7
...
...
@@ -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
;
}
plugin/dapp/lottery/types/lottery.pb.go
View file @
3c2cadc7
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment