Commit 3fcb74f9 authored by jiangpeng's avatar jiangpeng

dapp/vote:classify vote with status

parent 3dc1c260
......@@ -13,6 +13,7 @@ import (
func createGroupCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "createGroup",
Aliases: []string{"cg"},
Short: "create tx(create vote group)",
Run: createGroup,
Example: "createGroup -n=group1 -a=admin1 -m=member1 -m=member2",
......@@ -64,6 +65,7 @@ func createGroup(cmd *cobra.Command, args []string) {
func updateGroupCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "updateGroup",
Aliases: []string{"ug"},
Short: "create tx(update group members or admin)",
Run: updateGroup,
Example: "updateGroup -g=id -a=addMember1 -a=addMember2 -r=removeMember1 ...",
......@@ -74,11 +76,11 @@ func updateGroupCMD() *cobra.Command {
func updateGroupFlags(cmd *cobra.Command) {
cmd.Flags().StringP("groupID", "g", "", "group id")
cmd.Flags().StringArrayP("addMembers", "a", nil, "group member address array for adding")
cmd.Flags().StringArrayP("addMembers", "m", nil, "group member address array for adding")
cmd.Flags().UintSliceP("weights", "w", nil, "member vote weight array for adding")
cmd.Flags().StringArrayP("removeMembers", "r", nil, "group member address array for removing")
cmd.Flags().StringArrayP("addAdmins", "d", nil, "group admin address array for adding")
cmd.Flags().StringArrayP("removeAdmins", "m", nil, "group admin address array for removing")
cmd.Flags().StringArrayP("removeMembers", "v", nil, "group member address array for removing")
cmd.Flags().StringArrayP("addAdmins", "a", nil, "group admin address array for adding")
cmd.Flags().StringArrayP("removeAdmins", "r", nil, "group admin address array for removing")
markRequired(cmd, "groupID")
}
......@@ -120,9 +122,10 @@ func updateGroup(cmd *cobra.Command, args []string) {
func createVoteCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "createVote",
Short: "create tx(create vote)",
Run: createVote,
Use: "createVote",
Aliases: []string{"ctv"},
Short: "create tx(create vote)",
Run: createVote,
}
createVoteFlags(cmd)
return cmd
......@@ -180,9 +183,10 @@ func createVote(cmd *cobra.Command, args []string) {
func commitVoteCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "commitVote",
Short: "create tx(commit vote)",
Run: commitVote,
Use: "commitVote",
Aliases: []string{"cmv"},
Short: "create tx(commit vote)",
Run: commitVote,
}
commitVoteFlags(cmd)
return cmd
......@@ -211,9 +215,10 @@ func commitVote(cmd *cobra.Command, args []string) {
func closeVoteCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "closeVote",
Short: "create tx(close vote)",
Run: closeVote,
Use: "closeVote",
Aliases: []string{"csv"},
Short: "create tx(close vote)",
Run: closeVote,
}
closeVoteFlags(cmd)
return cmd
......@@ -239,9 +244,10 @@ func closeVote(cmd *cobra.Command, args []string) {
func updateMemberCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "updateMember",
Short: "create tx(update member name)",
Run: updateMember,
Use: "updateMember",
Aliases: []string{"um"},
Short: "create tx(update member name)",
Run: updateMember,
}
updateMemberFlags(cmd)
return cmd
......
......@@ -12,6 +12,7 @@ import (
func groupInfoCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "groupInfo",
Aliases: []string{"gf"},
Short: "get group infos",
Run: groupInfo,
Example: "groupInfo -g=id1 -g=id2...",
......@@ -41,9 +42,10 @@ func groupInfo(cmd *cobra.Command, args []string) {
func voteInfoCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "voteInfo",
Short: "get vote info",
Run: voteInfo,
Use: "voteInfo",
Aliases: []string{"vf"},
Short: "get vote info",
Run: voteInfo,
}
voteInfoFlags(cmd)
return cmd
......@@ -70,9 +72,10 @@ func voteInfo(cmd *cobra.Command, args []string) {
func memberInfoCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "memberInfo",
Short: "get member info",
Run: memberInfo,
Use: "memberInfo",
Aliases: []string{"mf"},
Short: "get member info",
Run: memberInfo,
}
memberInfoFlags(cmd)
return cmd
......@@ -99,9 +102,10 @@ func memberInfo(cmd *cobra.Command, args []string) {
func listGroupCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "listGroup",
Short: "show group list",
Run: listGroup,
Use: "listGroup",
Aliases: []string{"lg"},
Short: "show group list",
Run: listGroup,
}
listCmdFlags(cmd)
return cmd
......@@ -113,33 +117,38 @@ func listGroup(cmd *cobra.Command, args []string) {
func listVoteCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "listVote",
Short: "show vote list",
Run: listVote,
Use: "listVote",
Aliases: []string{"lv"},
Short: "show vote list",
Run: listVote,
}
listVoteFlags(cmd)
return cmd
}
func listVoteFlags(cmd *cobra.Command) {
cmd.Flags().StringP("groupID", "g", "", "list vote belongs to specified group, list all if not set")
cmd.Flags().Uint32P("status", "t", 0, "vote status")
listCmdFlags(cmd)
}
func listVote(cmd *cobra.Command, args []string) {
groupID, _ := cmd.Flags().GetString("groupID")
status, _ := cmd.Flags().GetUint32("status")
listReq := getListReq(cmd)
req := &vty.ReqListVote{
GroupID: groupID,
ListReq: listReq,
Status: status,
}
sendQueryRPC(cmd, "ListVote", req, &vty.ReplyVoteList{})
}
func listMemberCMD() *cobra.Command {
cmd := &cobra.Command{
Use: "listMember",
Short: "show member list",
Run: listMember,
Use: "listMember",
Aliases: []string{"lm"},
Short: "show member list",
Run: listMember,
}
listCmdFlags(cmd)
return cmd
......@@ -151,7 +160,7 @@ func listMember(cmd *cobra.Command, args []string) {
func listCmdFlags(cmd *cobra.Command) {
cmd.Flags().StringP("startItem", "s", "", "list start item id, default nil value")
cmd.Flags().Uint32P("count", "c", 10, "list count, default 10")
cmd.Flags().Uint32P("count", "c", 5, "list count, default 5")
cmd.Flags().Uint32P("direction", "d", 1, "list direction, default 1 (Ascending order)")
}
......
......@@ -147,6 +147,7 @@ func (a *action) createVote(create *vty.CreateVote) (*types.Receipt, error) {
vote.Name = create.Name
vote.GroupID = create.GroupID
vote.Description = create.Description
vote.Creator = a.fromAddr
vote.VoteOptions = make([]*vty.VoteOption, 0)
for _, option := range create.VoteOptions {
vote.VoteOptions = append(vote.VoteOptions, &vty.VoteOption{Option: option})
......
......@@ -96,7 +96,7 @@ func (v *vote) ExecLocal_CommitVote(payload *vty.CommitVote, tx *types.Transacti
//implement code, add customize kv to dbSet...
commitInfo := decodeCommitInfo(receiptData.Logs[0].Log)
table := newVoteTable(v.GetLocalDB())
row, err := table.GetData([]byte(formatVoteID(payload.GetVoteID())))
row, err := table.GetData([]byte(payload.GetVoteID()))
if err != nil {
elog.Error("execLocal commitVote", "txHash", hex.EncodeToString(tx.Hash()), "voteTable get", err)
return nil, err
......@@ -127,7 +127,7 @@ func (v *vote) ExecLocal_CommitVote(payload *vty.CommitVote, tx *types.Transacti
func (v *vote) ExecLocal_CloseVote(payload *vty.CloseVote, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
dbSet := &types.LocalDBSet{}
table := newVoteTable(v.GetLocalDB())
row, err := table.GetData([]byte(formatVoteID(payload.GetVoteID())))
row, err := table.GetData([]byte(payload.GetVoteID()))
if err != nil {
elog.Error("execLocal closeVote", "txHash", hex.EncodeToString(tx.Hash()), "voteTable get", err)
return nil, err
......
......@@ -76,7 +76,9 @@ func (v *vote) Query_GetVotes(in *vty.ReqStrings) (types.Message, error) {
}
voteList = append(voteList, info)
}
return classifyVoteList(voteList), nil
reply := &vty.ReplyVoteList{CurrentTimestamp: types.Now().Unix()}
reply.VoteList = filterVoteWithStatus(voteList, 0, reply.CurrentTimestamp)
return reply, nil
}
......@@ -124,13 +126,18 @@ func (v *vote) Query_ListGroup(in *vty.ReqListItem) (types.Message, error) {
table := newGroupTable(v.GetLocalDB())
var primaryKey []byte
primaryKey = append(primaryKey, []byte(in.StartItemID)...)
list := &vty.GroupInfos{}
rows, err := table.ListIndex(groupTablePrimary, nil, primaryKey, in.Count, in.Direction)
// 已经没有数据,直接返回
if err == types.ErrNotFound {
return list, nil
}
if err != nil {
elog.Error("query listGroup", "err", err, "param", in)
return nil, err
}
list := &vty.GroupInfos{GroupList: make([]*vty.GroupInfo, 0, len(rows))}
list.GroupList = make([]*vty.GroupInfo, 0, len(rows))
for _, row := range rows {
info, ok := row.Data.(*vty.GroupInfo)
if !ok {
......@@ -157,7 +164,16 @@ func (v *vote) Query_ListVote(in *vty.ReqListVote) (types.Message, error) {
prefix = []byte(groupID)
}
primaryKey = append(primaryKey, []byte(in.GetListReq().GetStartItemID())...)
rows, err := table.ListIndex(indexName, prefix, primaryKey, in.GetListReq().Count, in.GetListReq().Direction)
reply := &vty.ReplyVoteList{CurrentTimestamp: types.Now().Unix()}
listCount := in.ListReq.GetCount()
listMore:
rows, err := table.ListIndex(indexName, prefix, primaryKey, listCount, in.GetListReq().Direction)
// 已经没有数据,直接返回
if err == types.ErrNotFound {
return reply, nil
}
if err != nil {
elog.Error("query listVote", "err", err, "param", in)
return nil, err
......@@ -171,8 +187,16 @@ func (v *vote) Query_ListVote(in *vty.ReqListVote) (types.Message, error) {
}
list = append(list, info)
}
primaryKey = append(primaryKey[:0], []byte(list[len(list)-1].ID)...)
list = filterVoteWithStatus(list, in.Status, reply.CurrentTimestamp)
reply.VoteList = append(reply.VoteList, list...)
//经过筛选后,数量小于请求数量,则需要再次list, 需要满足len(rows)==listCount, 否则表示已经没有数据
if len(rows) == int(listCount) && int(listCount) > len(list) {
listCount -= int32(len(list))
goto listMore
}
return classifyVoteList(list), nil
return reply, nil
}
func (v *vote) Query_ListMember(in *vty.ReqListItem) (types.Message, error) {
......@@ -183,13 +207,18 @@ func (v *vote) Query_ListMember(in *vty.ReqListItem) (types.Message, error) {
table := newMemberTable(v.GetLocalDB())
var primaryKey []byte
primaryKey = append(primaryKey, []byte(in.StartItemID)...)
list := &vty.MemberInfos{}
rows, err := table.ListIndex(memberTablePrimary, nil, primaryKey, in.Count, in.Direction)
// 已经没有数据,直接返回
if err == types.ErrNotFound {
return list, nil
}
if err != nil {
elog.Error("query listMember", "err", err, "param", in)
return nil, err
}
list := &vty.MemberInfos{MemberList: make([]*vty.MemberInfo, 0, len(rows))}
list.MemberList = make([]*vty.MemberInfo, 0, len(rows))
for _, row := range rows {
info, ok := row.Data.(*vty.MemberInfo)
if !ok {
......
......@@ -91,14 +91,12 @@ func decodeCommitInfo(data []byte) *vty.CommitInfo {
return info
}
func classifyVoteList(voteList []*vty.VoteInfo) *vty.ReplyVoteList {
func filterVoteWithStatus(voteList []*vty.VoteInfo, status uint32, currentTime int64) []*vty.VoteInfo {
reply := &vty.ReplyVoteList{}
currentTime := types.Now().Unix()
var filterList []*vty.VoteInfo
for _, voteInfo := range voteList {
if voteInfo.Status == voteStatusClosed {
continue
} else if voteInfo.BeginTimestamp > currentTime {
voteInfo.Status = voteStatusPending
} else if voteInfo.EndTimestamp > currentTime {
......@@ -106,8 +104,14 @@ func classifyVoteList(voteList []*vty.VoteInfo) *vty.ReplyVoteList {
} else {
voteInfo.Status = voteStatusFinished
}
//remove vote info with other status
if status == voteInfo.Status {
filterList = append(filterList, voteInfo)
}
}
//设置了状态筛选,返回对应的筛选列表
if status > 0 {
return filterList
}
reply.CurrentTimestamp = currentTime
reply.VoteList = voteList
return reply
return voteList
}
......@@ -124,13 +124,14 @@ message ReqStrings {
//列表请求结构
message ReqListItem {
string startItemID = 1; //列表开始的ID,如请求组列表即groupID,不包含在结果中
int32 count = 2; //请求列表项数量
int32 direction = 3; // 0表示根据ID降序,1表示升序
int32 count = 2; //请求列表项数量, 0表示请求所有
int32 direction = 3; // 0表示根据ID降序,1表示升序,目前ID和区块高度正相关
}
message ReqListVote {
string groupID = 1; //所属组ID
string groupID = 1; //指定所属组ID
ReqListItem listReq = 2; //列表请求
uint32 status = 3; //指定投票状态
}
message ReplyVoteList {
......
......@@ -353,9 +353,9 @@ curl -kd '{"method":"Chain33.Query","params":[{"execer":"vote","funcName":"GetM
```proto
//列表请求结构
message ReqListItem {
string startItemID = 1; //列表开始的投票组ID,不包含在结果中
int32 count = 2; //列表项单次请求数量
int32 direction = 3; // 0表示根据ID降序,1表示升序
string startItemID = 1; //列表开始的ID,如请求组列表即groupID,不包含在结果中
int32 count = 2; //请求列表项数量, 0表示请求所有
int32 direction = 3; // 0表示根据ID降序,1表示升序,目前ID和区块高度正相关
}
```
......@@ -381,14 +381,15 @@ curl -kd '{"method":"Chain33.Query","params":[{"execer":"vote","funcName":"List
```proto
//列表请求结构
message ReqListVote {
string groupID = 1; //所属组ID,不填时获取全局的投票列表
string groupID = 1; //指定所属组ID
ReqListItem listReq = 2; //列表请求
uint32 status = 3; //指定投票状态
}
message ReqListItem {
string startItemID = 1; //列表开始的投票ID,不包含在结果中
int32 count = 2; //列表项单次请求数量
int32 direction = 3; // 0表示根据ID降序,1表示升序
string startItemID = 1; //列表开始的ID,如请求组列表即groupID,不包含在结果中
int32 count = 2; //请求列表项数量, 0表示请求所有
int32 direction = 3; // 0表示根据ID降序,1表示升序,目前ID和区块高度正相关
}
```
......@@ -418,9 +419,9 @@ curl -kd '{"method":"Chain33.Query","params":[{"execer":"vote","funcName":"List
```proto
//列表请求结构
message ReqListItem {
string startItemID = 1; //列表开始的用户ID(地址)
int32 count = 2; //列表项单次请求数量
int32 direction = 3; // 0表示根据ID降序,1表示升序
string startItemID = 1; //列表开始的ID,如请求组列表即groupID,不包含在结果中
int32 count = 2; //请求列表项数量, 0表示请求所有
int32 direction = 3; // 0表示根据ID降序,1表示升序,目前ID和区块高度正相关
}
```
......
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