Commit 9b962259 authored by liuyuhang's avatar liuyuhang Committed by 33cn

add db table to rule project

parent af4f3531
......@@ -259,10 +259,13 @@ func addShowProposalBoardflags(cmd *cobra.Command) {
cmd.MarkFlagRequired("type")
cmd.Flags().StringP("proposalID", "p", "", "proposal ID")
cmd.Flags().Uint32P("status", "s", 0, "status")
cmd.Flags().StringP("addr", "a", "", "address")
cmd.Flags().Int32P("count", "c", 1, "count, default is 1")
cmd.Flags().Int32P("direction", "d", -1, "direction, default is reserve")
cmd.Flags().Int64P("index", "i", -1, "index, default is -1")
cmd.Flags().Int64P("height", "h", -1, "height, default is -1")
cmd.Flags().Int32P("index", "i", -1, "index, default is -1")
}
func showProposalBoard(cmd *cobra.Command, args []string) {
......@@ -270,9 +273,11 @@ func showProposalBoard(cmd *cobra.Command, args []string) {
typ, _ := cmd.Flags().GetUint32("type")
propID, _ := cmd.Flags().GetString("proposalID")
status, _ := cmd.Flags().GetUint32("status")
addr, _ := cmd.Flags().GetString("addr")
count, _ := cmd.Flags().GetInt32("count")
direction, _ := cmd.Flags().GetInt32("direction")
index, _ := cmd.Flags().GetInt64("index")
height, _ := cmd.Flags().GetInt64("height")
index, _ := cmd.Flags().GetInt32("index")
var params rpctypes.Query4Jrpc
var rep interface{}
......@@ -286,8 +291,10 @@ func showProposalBoard(cmd *cobra.Command, args []string) {
} else if 1 == typ {
req := auty.ReqQueryProposalBoard{
Status: int32(status),
Addr: addr,
Count: count,
Direction: direction,
Height: height,
Index: index,
}
params.FuncName = auty.ListProposalBoard
......
......@@ -286,10 +286,13 @@ func addShowProposalProjectflags(cmd *cobra.Command) {
cmd.MarkFlagRequired("type")
cmd.Flags().StringP("proposalID", "p", "", "proposal ID")
cmd.Flags().Uint32P("status", "s", 0, "status")
cmd.Flags().StringP("addr", "a", "", "address")
cmd.Flags().Int32P("count", "c", 1, "count, default is 1")
cmd.Flags().Int32P("direction", "d", -1, "direction, default is reserve")
cmd.Flags().Int64P("index", "i", -1, "index, default is -1")
cmd.Flags().Int64P("height", "h", -1, "height, default is -1")
cmd.Flags().Int32P("index", "i", -1, "index, default is -1")
}
func showProposalProject(cmd *cobra.Command, args []string) {
......@@ -297,9 +300,11 @@ func showProposalProject(cmd *cobra.Command, args []string) {
typ, _ := cmd.Flags().GetUint32("type")
propID, _ := cmd.Flags().GetString("proposalID")
status, _ := cmd.Flags().GetUint32("status")
addr, _ := cmd.Flags().GetString("addr")
count, _ := cmd.Flags().GetInt32("count")
direction, _ := cmd.Flags().GetInt32("direction")
index, _ := cmd.Flags().GetInt64("index")
height, _ := cmd.Flags().GetInt64("height")
index, _ := cmd.Flags().GetInt32("index")
var params rpctypes.Query4Jrpc
var rep interface{}
......@@ -314,8 +319,10 @@ func showProposalProject(cmd *cobra.Command, args []string) {
} else if 1 == typ {
req := auty.ReqQueryProposalProject{
Status: int32(status),
Addr: addr,
Count: count,
Direction: direction,
Height: height,
Index: index,
}
params.FuncName = auty.ListProposalProject
......
......@@ -226,10 +226,13 @@ func addShowProposalRuleflags(cmd *cobra.Command) {
cmd.MarkFlagRequired("type")
cmd.Flags().StringP("proposalID", "p", "", "proposal ID")
cmd.Flags().Uint32P("status", "s", 0, "status")
cmd.Flags().StringP("addr", "a", "", "address")
cmd.Flags().Int32P("count", "c", 1, "count, default is 1")
cmd.Flags().Int32P("direction", "d", -1, "direction, default is reserve")
cmd.Flags().Int64P("index", "i", -1, "index, default is -1")
cmd.Flags().Int64P("height", "h", -1, "height, default is -1")
cmd.Flags().Int32P("index", "i", -1, "index, default is -1")
}
func showProposalRule(cmd *cobra.Command, args []string) {
......@@ -237,9 +240,11 @@ func showProposalRule(cmd *cobra.Command, args []string) {
typ, _ := cmd.Flags().GetUint32("type")
propID, _ := cmd.Flags().GetString("proposalID")
status, _ := cmd.Flags().GetUint32("status")
addr, _ := cmd.Flags().GetString("addr")
count, _ := cmd.Flags().GetInt32("count")
direction, _ := cmd.Flags().GetInt32("direction")
index, _ := cmd.Flags().GetInt64("index")
height, _ := cmd.Flags().GetInt64("height")
index, _ := cmd.Flags().GetInt32("index")
var params rpctypes.Query4Jrpc
var rep interface{}
......@@ -253,8 +258,10 @@ func showProposalRule(cmd *cobra.Command, args []string) {
} else if 1 == typ {
req := auty.ReqQueryProposalRule{
Status: int32(status),
Addr: addr,
Count: count,
Direction: direction,
Height: height,
Index: index,
}
params.FuncName = auty.ListProposalRule
......@@ -366,6 +373,7 @@ func addShowProposalCommentflags(cmd *cobra.Command) {
cmd.MarkFlagRequired("proposalID")
cmd.Flags().Int32P("count", "c", 1, "count, default is 1")
cmd.Flags().Int32P("direction", "d", -1, "direction, default is reserve")
cmd.Flags().Int64P("height", "h", -1, "height, default is -1")
cmd.Flags().Int64P("index", "i", -1, "index, default is -1")
}
......@@ -374,7 +382,8 @@ func showProposalComment(cmd *cobra.Command, args []string) {
propID, _ := cmd.Flags().GetString("proposalID")
count, _ := cmd.Flags().GetInt32("count")
direction, _ := cmd.Flags().GetInt32("direction")
index, _ := cmd.Flags().GetInt64("index")
height, _ := cmd.Flags().GetInt64("height")
index, _ := cmd.Flags().GetInt32("index")
var params rpctypes.Query4Jrpc
var rep interface{}
......@@ -384,6 +393,7 @@ func showProposalComment(cmd *cobra.Command, args []string) {
ProposalID: propID,
Count: count,
Direction: direction,
Height: height,
Index: index,
}
params.FuncName = auty.ListProposalComment
......
......@@ -37,32 +37,14 @@ func (a *Autonomy) execLocalBoard(receiptData *types.ReceiptData) (*types.LocalD
break
}
}
kvs, err := table.Save()
if err != nil {
return nil, err
}
dbSet.KV = append(dbSet.KV, kvs...)
return dbSet, nil
}
func saveBoardHeightIndex(res *auty.ReceiptProposalBoard) (kvs []*types.KeyValue) {
// 先将之前的状态删除掉,再做更新
if res.Current.Status > 1 {
kv := &types.KeyValue{}
kv.Key = calcBoardKey4StatusHeight(res.Prev.Status, dapp.HeightIndexStr(res.Prev.Height, int64(res.Prev.Index)))
kv.Value = nil
kvs = append(kvs, kv)
}
kv := &types.KeyValue{}
kv.Key = calcBoardKey4StatusHeight(res.Current.Status, dapp.HeightIndexStr(res.Current.Height, int64(res.Current.Index)))
kv.Value = types.Encode(res.Current)
kvs = append(kvs, kv)
return kvs
}
func (a *Autonomy) execDelLocalBoard(receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
dbSet := &types.LocalDBSet{}
table := NewBoardTable(a.GetLocalDB())
......@@ -99,25 +81,9 @@ func (a *Autonomy) execDelLocalBoard(receiptData *types.ReceiptData) (*types.Loc
return nil, err
}
dbSet.KV = append(dbSet.KV, kvs...)
return dbSet, nil
}
func delBoardHeightIndex(res *auty.ReceiptProposalBoard) (kvs []*types.KeyValue) {
kv := &types.KeyValue{}
kv.Key = calcBoardKey4StatusHeight(res.Current.Status, dapp.HeightIndexStr(res.Current.Height, int64(res.Current.Index)))
kv.Value = nil
kvs = append(kvs, kv)
if res.Current.Status > 1 {
kv := &types.KeyValue{}
kv.Key = calcBoardKey4StatusHeight(res.Prev.Status, dapp.HeightIndexStr(res.Prev.Height, int64(res.Prev.Index)))
kv.Value = types.Encode(res.Prev)
kvs = append(kvs, kv)
}
return kvs
}
func (a *Autonomy) getProposalBoard(req *types.ReqString) (types.Message, error) {
if req == nil {
return nil, types.ErrInvalidParam
......@@ -186,8 +152,6 @@ func (a *Autonomy) listProposalBoard(req *auty.ReqQueryProposalBoard) (types.Mes
return &rep, nil
}
func genHeightIndexStr(index int64) string {
return fmt.Sprintf("%018d", index)
}
......@@ -12,10 +12,12 @@ import (
"github.com/33cn/chain33/util"
auty "github.com/33cn/plugin/plugin/dapp/autonomy/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/33cn/chain33/common/db"
)
func TestExecLocalBoard(t *testing.T) {
_, _, kvdb := util.CreateTestDB()
_, sdb, kvdb := util.CreateTestDB()
au := &Autonomy{}
au.SetLocalDB(kvdb)
//TyLogPropBoard
......@@ -40,14 +42,16 @@ func TestExecLocalBoard(t *testing.T) {
set, err := au.execLocalBoard(receipt)
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcBoardKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
//save to database
saveKvs(sdb, set.KV)
// check
checkExecLocalBoard(t, kvdb, cur)
// TyLogRvkPropBoard
pre1 := copyAutonomyProposalBoard(cur)
cur.Status = auty.AutonomyStatusRvkPropBoard
cur.Height = 2
cur.Index = 3
receiptBoard1 := &auty.ReceiptProposalBoard{
Prev: pre1,
Current: cur,
......@@ -59,11 +63,12 @@ func TestExecLocalBoard(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcBoardKey4StatusHeight(pre1.Status,
dapp.HeightIndexStr(pre1.Height, int64(pre1.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
require.Equal(t, set.KV[1].Key, calcBoardKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
//save to database
saveKvs(sdb, set.KV)
// check
checkExecLocalBoard(t, kvdb, cur)
// TyLogVotePropBoard
cur.Status = auty.AutonomyStatusProposalBoard
......@@ -71,8 +76,9 @@ func TestExecLocalBoard(t *testing.T) {
cur.Index = 2
pre2 := copyAutonomyProposalBoard(cur)
cur.Status = auty.AutonomyStatusVotePropBoard
cur.Height = 2
cur.Index = 3
cur.Height = 1
cur.Index = 2
cur.Address = "2222222222222"
receiptBoard2 := &auty.ReceiptProposalBoard{
Prev: pre2,
Current: cur,
......@@ -84,15 +90,16 @@ func TestExecLocalBoard(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcBoardKey4StatusHeight(pre2.Status,
dapp.HeightIndexStr(pre1.Height, int64(pre2.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
require.Equal(t, set.KV[1].Key, calcBoardKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
//save to database
saveKvs(sdb, set.KV)
// check
checkExecLocalBoard(t, kvdb, cur)
}
func TestExecDelLocalBoard(t *testing.T) {
_, sdb, kvdb := util.CreateTestDB()
au := &Autonomy{}
au.SetLocalDB(kvdb)
//TyLogPropBoard
cur := &auty.AutonomyProposalBoard{
PropBoard: &auty.ProposalBoard{},
......@@ -112,22 +119,51 @@ func TestExecDelLocalBoard(t *testing.T) {
{Ty: auty.TyLogPropBoard, Log: types.Encode(receiptBoard)},
},
}
set, err := au.execDelLocalBoard(receipt)
// 先执行local然后进行删除
set, err := au.execLocalBoard(receipt)
require.NoError(t, err)
require.NotNil(t, set)
saveKvs(sdb, set.KV)
set, err = au.execDelLocalBoard(receipt)
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcBoardKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
saveKvs(sdb, set.KV)
// check
table := NewBoardTable(au.GetLocalDB())
query := table.GetQuery(kvdb)
_, err = query.ListIndex("primary", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
_, err = query.ListIndex("addr", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
_, err = query.ListIndex("status", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
_, err = query.ListIndex("addr_status", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
// TyLogVotePropBoard
pre1 := copyAutonomyProposalBoard(cur)
cur.Status = auty.AutonomyStatusVotePropBoard
cur.Height = 2
cur.Index = 3
cur.Height = 1
cur.Index = 2
receiptBoard2 := &auty.ReceiptProposalBoard{
Prev: pre1,
Current: cur,
}
// 先执行local然后进行删除
set, err = au.execLocalBoard(&types.ReceiptData{
Logs: []*types.ReceiptLog{
{Ty: auty.TyLogVotePropBoard, Log: types.Encode(receiptBoard2)},
},
})
require.NoError(t, err)
require.NotNil(t, set)
saveKvs(sdb, set.KV)
// check
checkExecLocalBoard(t, kvdb, cur)
set, err = au.execDelLocalBoard(&types.ReceiptData{
Logs: []*types.ReceiptLog{
{Ty: auty.TyLogVotePropBoard, Log: types.Encode(receiptBoard2)},
......@@ -135,12 +171,10 @@ func TestExecDelLocalBoard(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcBoardKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
require.Equal(t, set.KV[1].Key, calcBoardKey4StatusHeight(pre1.Status,
dapp.HeightIndexStr(pre1.Height, int64(pre1.Index))))
require.NotNil(t, set.KV[1].Value)
saveKvs(sdb, set.KV)
// check
checkExecLocalBoard(t, kvdb, pre1)
}
func TestGetProposalBoard(t *testing.T) {
......@@ -161,7 +195,7 @@ func TestListProposalBoard(t *testing.T) {
au := &Autonomy{
dapp.DriverBase{},
}
_, _, kvdb := util.CreateTestDB()
_, sdb, kvdb := util.CreateTestDB()
au.SetLocalDB(kvdb)
type statu struct {
......@@ -192,16 +226,23 @@ func TestListProposalBoard(t *testing.T) {
Height: 1,
Index: 2,
}
//将数据保存下去
var kvs []*types.KeyValue
table := NewBoardTable(kvdb)
for _, tcase := range testcase {
key := calcBoardKey4StatusHeight(tcase.status,
dapp.HeightIndexStr(tcase.height, int64(tcase.index)))
cur.Status = tcase.status
cur.Height = tcase.height
cur.Index = int32(tcase.index)
value := types.Encode(cur)
kvdb.Set(key, value)
err := table.Replace(cur)
require.NoError(t, err)
kv, err := table.Save()
require.NoError(t, err)
kvs = append(kvs, kv...)
}
saveKvs(sdb, kvs)
// 反向查找
req := &auty.ReqQueryProposalBoard{
Status: auty.AutonomyStatusProposalBoard,
......@@ -264,3 +305,39 @@ func TestListProposalBoard(t *testing.T) {
require.Equal(t, rsp.(*auty.ReplyQueryProposalBoard).PropBoards[1].Height, testcase2[0].height)
require.Equal(t, rsp.(*auty.ReplyQueryProposalBoard).PropBoards[1].Index, int32(testcase2[0].index))
}
func checkExecLocalBoard(t *testing.T, kvdb db.KVDB, cur *auty.AutonomyProposalBoard) {
table := NewBoardTable(kvdb)
query := table.GetQuery(kvdb)
rows, err := query.ListIndex("primary", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, string(rows[0].Primary), dapp.HeightIndexStr(1, 2))
rows, err = query.ListIndex("addr", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, 1, len(rows))
rows, err = query.ListIndex("status", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, 1, len(rows))
rows, err = query.ListIndex("addr_status", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, 1, len(rows))
prop, ok := rows[0].Data.(*auty.AutonomyProposalBoard)
assert.Equal(t, true, ok)
assert.Equal(t, prop, cur)
}
func saveKvs(sdb db.DB, kvs []*types.KeyValue) {
for _, kv := range kvs {
if kv.Value == nil {
sdb.Delete(kv.Key)
} else {
sdb.Set(kv.Key, kv.Value)
}
}
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ data: autonomy board
index: status, addr
*/
var opt = &table.Option{
var boardOpt = &table.Option{
Prefix: "LODB-autonomy",
Name: "board",
Primary: "heightindex",
......@@ -27,7 +27,7 @@ var opt = &table.Option{
//NewTable 新建表
func NewBoardTable(kvdb db.KV) *table.Table {
rowmeta := NewBoardRow()
table, err := table.NewTable(rowmeta, kvdb, opt)
table, err := table.NewTable(rowmeta, kvdb, boardOpt)
if err != nil {
panic(err)
}
......
......@@ -12,7 +12,7 @@ import (
func (a *Autonomy) execLocalProject(receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
dbSet := &types.LocalDBSet{}
var set []*types.KeyValue
table := NewProjectTable(a.GetLocalDB())
for _, log := range receiptData.Logs {
switch log.Ty {
case auty.TyLogPropProject,
......@@ -26,73 +26,61 @@ func (a *Autonomy) execLocalProject(receiptData *types.ReceiptData) (*types.Loca
if err != nil {
return nil, err
}
kv := saveProjectHeightIndex(&receipt)
set = append(set, kv...)
err = table.Replace(receipt.Current)
if err != nil {
return nil, err
}
}
default:
break
}
}
dbSet.KV = append(dbSet.KV, set...)
return dbSet, nil
}
func saveProjectHeightIndex(res *auty.ReceiptProposalProject) (kvs []*types.KeyValue) {
// 先将之前的状态删除掉,再做更新
if res.Current.Status > 1 {
kv := &types.KeyValue{}
kv.Key = calcProjectKey4StatusHeight(res.Prev.Status, dapp.HeightIndexStr(res.Prev.Height, int64(res.Prev.Index)))
kv.Value = nil
kvs = append(kvs, kv)
kvs, err := table.Save()
if err != nil {
return nil, err
}
kv := &types.KeyValue{}
kv.Key = calcProjectKey4StatusHeight(res.Current.Status, dapp.HeightIndexStr(res.Current.Height, int64(res.Current.Index)))
kv.Value = types.Encode(res.Current)
kvs = append(kvs, kv)
return kvs
dbSet.KV = append(dbSet.KV, kvs...)
return dbSet, nil
}
func (a *Autonomy) execDelLocalProject(receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
dbSet := &types.LocalDBSet{}
var set []*types.KeyValue
table := NewProjectTable(a.GetLocalDB())
for _, log := range receiptData.Logs {
var receipt auty.ReceiptProposalProject
err := types.Decode(log.Log, &receipt)
if err != nil {
return nil, err
}
switch log.Ty {
case auty.TyLogPropProject,
auty.TyLogRvkPropProject,
case auty.TyLogPropProject:
{
heightIndex := dapp.HeightIndexStr(receipt.Current.Height, int64(receipt.Current.Index))
err = table.Del([]byte(heightIndex))
if err != nil {
return nil, err
}
}
case auty.TyLogRvkPropProject,
auty.TyLogVotePropProject,
auty.TyLogPubVotePropProject,
auty.TyLogTmintPropProject:
{
var receipt auty.ReceiptProposalProject
err := types.Decode(log.Log, &receipt)
err = table.Replace(receipt.Prev)
if err != nil {
return nil, err
}
kv := delProjectHeightIndex(&receipt)
set = append(set, kv...)
}
default:
break
}
}
dbSet.KV = append(dbSet.KV, set...)
return dbSet, nil
}
func delProjectHeightIndex(res *auty.ReceiptProposalProject) (kvs []*types.KeyValue) {
kv := &types.KeyValue{}
kv.Key = calcProjectKey4StatusHeight(res.Current.Status, dapp.HeightIndexStr(res.Current.Height, int64(res.Current.Index)))
kv.Value = nil
kvs = append(kvs, kv)
if res.Current.Status > 1 {
kv := &types.KeyValue{}
kv.Key = calcProjectKey4StatusHeight(res.Prev.Status, dapp.HeightIndexStr(res.Prev.Height, int64(res.Prev.Index)))
kv.Value = types.Encode(res.Prev)
kvs = append(kvs, kv)
kvs, err := table.Save()
if err != nil {
return nil, err
}
return kvs
dbSet.KV = append(dbSet.KV, kvs...)
return dbSet, nil
}
func (a *Autonomy) getProposalProject(req *types.ReqString) (types.Message, error) {
......@@ -117,34 +105,47 @@ func (a *Autonomy) listProposalProject(req *auty.ReqQueryProposalProject) (types
if req == nil {
return nil, types.ErrInvalidParam
}
//var key []byte
//var values [][]byte
//var err error
//
//localDb := a.GetLocalDB()
//if req.GetIndex() == -1 {
// key = nil
//} else { //翻页查找指定的txhash列表
// heightstr := genHeightIndexStr(req.GetIndex())
// key = calcProjectKey4StatusHeight(req.Status, heightstr)
//}
//prefix := calcProjectKey4StatusHeight(req.Status, "")
//values, err = localDb.List(prefix, key, req.Count, req.GetDirection())
//if err != nil {
// return nil, err
//}
//if len(values) == 0 {
// return nil, types.ErrNotFound
//}
localDb := a.GetLocalDB()
query := NewProjectTable(localDb).GetQuery(localDb)
var primary []byte
if req.Height > 0 {
primary = []byte(dapp.HeightIndexStr(req.Height, int64(req.Index)))
}
indexName := ""
if req.Status > 0 && req.Addr != "" {
indexName = "addr_status"
} else if req.Status > 0 {
indexName = "status"
} else if req.Addr != "" {
indexName = "addr"
}
cur := &ProjectRow{
AutonomyProposalProject: &auty.AutonomyProposalProject{},
}
cur.Address = req.Addr
cur.Status = req.Status
cur.Height = req.Height
cur.Index = req.Index
prefix, err := cur.Get(indexName)
rows, err := query.ListIndex(indexName, prefix, primary, req.Count, req.Direction)
if err != nil {
alog.Error("query List failed", "indexName", indexName, "prefix", "prefix", "key", string(primary), "err", err)
return nil, err
}
if len(rows) == 0 {
return nil, types.ErrNotFound
}
var rep auty.ReplyQueryProposalProject
//for _, value := range values {
// prop := &auty.AutonomyProposalProject{}
// err = types.Decode(value, prop)
// if err != nil {
// return nil, err
// }
// rep.PropProjects = append(rep.PropProjects, prop)
//}
for _, row := range rows {
r, ok := row.Data.(*auty.AutonomyProposalProject)
if !ok {
alog.Error("listProposalProject", "err", "bad row type")
return nil, types.ErrDecode
}
rep.PropProjects = append(rep.PropProjects, r)
}
return &rep, nil
}
......@@ -12,10 +12,14 @@ import (
"github.com/33cn/chain33/util"
auty "github.com/33cn/plugin/plugin/dapp/autonomy/types"
"github.com/stretchr/testify/require"
"github.com/33cn/chain33/common/db"
"github.com/stretchr/testify/assert"
)
func TestExecLocalProject(t *testing.T) {
_, sdb, kvdb := util.CreateTestDB()
au := &Autonomy{}
au.SetLocalDB(kvdb)
//TyLogPropProject
cur := &auty.AutonomyProposalProject{
PropProject: &auty.ProposalProject{},
......@@ -40,14 +44,15 @@ func TestExecLocalProject(t *testing.T) {
set, err := au.execLocalProject(receipt)
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcProjectKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
//save to database
saveKvs(sdb, set.KV)
// check
checkExecLocalProject(t, kvdb, cur)
// TyLogRvkPropProject
pre1 := copyAutonomyProposalProject(cur)
cur.Status = auty.AutonomyStatusRvkPropProject
cur.Height = 2
cur.Index = 3
receiptProject1 := &auty.ReceiptProposalProject{
Prev: pre1,
Current: cur,
......@@ -59,11 +64,12 @@ func TestExecLocalProject(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcProjectKey4StatusHeight(pre1.Status,
dapp.HeightIndexStr(pre1.Height, int64(pre1.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
require.Equal(t, set.KV[1].Key, calcProjectKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
//save to database
saveKvs(sdb, set.KV)
// check
checkExecLocalProject(t, kvdb, cur)
// TyLogVotePropProject
cur.Status = auty.AutonomyStatusProposalProject
......@@ -71,8 +77,9 @@ func TestExecLocalProject(t *testing.T) {
cur.Index = 2
pre2 := copyAutonomyProposalProject(cur)
cur.Status = auty.AutonomyStatusVotePropProject
cur.Height = 2
cur.Index = 3
cur.Height = 1
cur.Index = 2
cur.Address = "2222222222222"
receiptProject2 := &auty.ReceiptProposalProject{
Prev: pre2,
Current: cur,
......@@ -84,15 +91,18 @@ func TestExecLocalProject(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcProjectKey4StatusHeight(pre2.Status,
dapp.HeightIndexStr(pre1.Height, int64(pre2.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
require.Equal(t, set.KV[1].Key, calcProjectKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
//save to database
saveKvs(sdb, set.KV)
// check
checkExecLocalProject(t, kvdb, cur)
}
func TestExecDelLocalProject(t *testing.T) {
_, sdb, kvdb := util.CreateTestDB()
au := &Autonomy{}
au.SetLocalDB(kvdb)
//TyLogPropProject
cur := &auty.AutonomyProposalProject{
PropProject: &auty.ProposalProject{},
......@@ -114,22 +124,49 @@ func TestExecDelLocalProject(t *testing.T) {
{Ty: auty.TyLogPropProject, Log: types.Encode(receiptProject)},
},
}
set, err := au.execDelLocalProject(receipt)
// 先执行local然后进行删除
set, err := au.execLocalProject(receipt)
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcProjectKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
saveKvs(sdb, set.KV)
set, err = au.execDelLocalProject(receipt)
require.NoError(t, err)
require.NotNil(t, set)
saveKvs(sdb, set.KV)
// check
table := NewBoardTable(au.GetLocalDB())
query := table.GetQuery(kvdb)
_, err = query.ListIndex("primary", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
_, err = query.ListIndex("addr", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
_, err = query.ListIndex("status", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
_, err = query.ListIndex("addr_status", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
// TyLogVotePropProject
pre1 := copyAutonomyProposalProject(cur)
cur.Status = auty.AutonomyStatusVotePropProject
cur.Height = 2
cur.Index = 3
cur.Height = 1
cur.Index = 2
receiptProject2 := &auty.ReceiptProposalProject{
Prev: pre1,
Current: cur,
}
// 先执行local然后进行删除
set, err = au.execLocalProject(&types.ReceiptData{
Logs: []*types.ReceiptLog{
{Ty: auty.TyLogVotePropProject, Log: types.Encode(receiptProject2)},
},
})
require.NoError(t, err)
require.NotNil(t, set)
saveKvs(sdb, set.KV)
// check
checkExecLocalProject(t, kvdb, cur)
set, err = au.execDelLocalProject(&types.ReceiptData{
Logs: []*types.ReceiptLog{
{Ty: auty.TyLogVotePropProject, Log: types.Encode(receiptProject2)},
......@@ -137,12 +174,9 @@ func TestExecDelLocalProject(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcProjectKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
require.Equal(t, set.KV[1].Key, calcProjectKey4StatusHeight(pre1.Status,
dapp.HeightIndexStr(pre1.Height, int64(pre1.Index))))
require.NotNil(t, set.KV[1].Value)
saveKvs(sdb, set.KV)
// check
checkExecLocalProject(t, kvdb, pre1)
}
func TestGetProposalProject(t *testing.T) {
......@@ -163,7 +197,7 @@ func TestListProposalProject(t *testing.T) {
au := &Autonomy{
dapp.DriverBase{},
}
_, _, kvdb := util.CreateTestDB()
_, sdb, kvdb := util.CreateTestDB()
au.SetLocalDB(kvdb)
type statu struct {
......@@ -196,15 +230,22 @@ func TestListProposalProject(t *testing.T) {
Height: 1,
Index: 2,
}
//将数据保存下去
var kvs []*types.KeyValue
table := NewProjectTable(kvdb)
for _, tcase := range testcase {
key := calcProjectKey4StatusHeight(tcase.status,
dapp.HeightIndexStr(tcase.height, int64(tcase.index)))
cur.Status = tcase.status
cur.Height = tcase.height
cur.Index = int32(tcase.index)
value := types.Encode(cur)
kvdb.Set(key, value)
err := table.Replace(cur)
require.NoError(t, err)
kv, err := table.Save()
require.NoError(t, err)
kvs = append(kvs, kv...)
}
saveKvs(sdb, kvs)
// 反向查找
req := &auty.ReqQueryProposalProject{
......@@ -268,3 +309,29 @@ func TestListProposalProject(t *testing.T) {
require.Equal(t, rsp.(*auty.ReplyQueryProposalProject).PropProjects[1].Height, testcase2[0].height)
require.Equal(t, rsp.(*auty.ReplyQueryProposalProject).PropProjects[1].Index, int32(testcase2[0].index))
}
func checkExecLocalProject(t *testing.T, kvdb db.KVDB, cur *auty.AutonomyProposalProject) {
table := NewProjectTable(kvdb)
query := table.GetQuery(kvdb)
rows, err := query.ListIndex("primary", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, string(rows[0].Primary), dapp.HeightIndexStr(1, 2))
rows, err = query.ListIndex("addr", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, 1, len(rows))
rows, err = query.ListIndex("status", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, 1, len(rows))
rows, err = query.ListIndex("addr_status", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, 1, len(rows))
prop, ok := rows[0].Data.(*auty.AutonomyProposalProject)
assert.Equal(t, true, ok)
assert.Equal(t, prop, cur)
}
\ No newline at end of file
package executor
import (
"fmt"
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/common/db/table"
"github.com/33cn/chain33/types"
auty "github.com/33cn/plugin/plugin/dapp/autonomy/types"
"github.com/33cn/chain33/system/dapp"
)
/*
table struct
data: autonomy project
index: status, addr
*/
var projectOpt = &table.Option{
Prefix: "LODB-autonomy",
Name: "project",
Primary: "heightindex",
Index: []string{"addr", "status", "addr_status"},
}
//NewTable 新建表
func NewProjectTable(kvdb db.KV) *table.Table {
rowmeta := NewProjectRow()
table, err := table.NewTable(rowmeta, kvdb, projectOpt)
if err != nil {
panic(err)
}
return table
}
//ProjectRow table meta 结构
type ProjectRow struct {
*auty.AutonomyProposalProject
}
//NewProjectRow 新建一个meta 结构
func NewProjectRow() *ProjectRow {
return &ProjectRow{AutonomyProposalProject: &auty.AutonomyProposalProject{}}
}
//CreateRow 新建数据行(注意index 数据一定也要保存到数据中,不能就保存heightindex)
func (r *ProjectRow) CreateRow() *table.Row {
return &table.Row{Data: &auty.AutonomyProposalProject{}}
}
//SetPayload 设置数据
func (r *ProjectRow) SetPayload(data types.Message) error {
if d, ok := data.(*auty.AutonomyProposalProject); ok {
r.AutonomyProposalProject = d
return nil
}
return types.ErrTypeAsset
}
//Get 按照indexName 查询 indexValue
func (r *ProjectRow) Get(key string) ([]byte, error) {
if key == "heightindex" {
return []byte(dapp.HeightIndexStr(r.Height, int64(r.Index))), nil
} else if key == "status" {
return []byte(fmt.Sprintf("%2d", r.Status)), nil
} else if key == "addr" {
return []byte(r.Address), nil
} else if key == "addr_status" {
return []byte(fmt.Sprintf("%s:%2d", r.Address, r.Status)), nil
}
return nil, types.ErrNotFound
}
......@@ -12,7 +12,7 @@ import (
func (a *Autonomy) execLocalRule(receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
dbSet := &types.LocalDBSet{}
var set []*types.KeyValue
table := NewRuleTable(a.GetLocalDB())
for _, log := range receiptData.Logs {
switch log.Ty {
case auty.TyLogPropRule,
......@@ -25,73 +25,62 @@ func (a *Autonomy) execLocalRule(receiptData *types.ReceiptData) (*types.LocalDB
if err != nil {
return nil, err
}
kv := saveRuleHeightIndex(&receipt)
set = append(set, kv...)
err = table.Replace(receipt.Current)
if err != nil {
return nil, err
}
}
default:
break
}
}
dbSet.KV = append(dbSet.KV, set...)
return dbSet, nil
}
func saveRuleHeightIndex(res *auty.ReceiptProposalRule) (kvs []*types.KeyValue) {
// 先将之前的状态删除掉,再做更新
if res.Current.Status > 1 {
kv := &types.KeyValue{}
kv.Key = calcRuleKey4StatusHeight(res.Prev.Status, dapp.HeightIndexStr(res.Prev.Height, int64(res.Prev.Index)))
kv.Value = nil
kvs = append(kvs, kv)
kvs, err := table.Save()
if err != nil {
return nil, err
}
kv := &types.KeyValue{}
kv.Key = calcRuleKey4StatusHeight(res.Current.Status, dapp.HeightIndexStr(res.Current.Height, int64(res.Current.Index)))
kv.Value = types.Encode(res.Current)
kvs = append(kvs, kv)
return kvs
dbSet.KV = append(dbSet.KV, kvs...)
return dbSet, nil
}
func (a *Autonomy) execDelLocalRule(receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
dbSet := &types.LocalDBSet{}
var set []*types.KeyValue
table := NewRuleTable(a.GetLocalDB())
for _, log := range receiptData.Logs {
var receipt auty.ReceiptProposalRule
err := types.Decode(log.Log, &receipt)
if err != nil {
return nil, err
}
switch log.Ty {
case auty.TyLogPropRule,
auty.TyLogRvkPropRule,
case auty.TyLogPropRule:
{
heightIndex := dapp.HeightIndexStr(receipt.Current.Height, int64(receipt.Current.Index))
err = table.Del([]byte(heightIndex))
if err != nil {
return nil, err
}
}
case auty.TyLogRvkPropRule,
auty.TyLogVotePropRule,
auty.TyLogTmintPropRule:
{
var receipt auty.ReceiptProposalRule
err := types.Decode(log.Log, &receipt)
err = table.Replace(receipt.Prev)
if err != nil {
return nil, err
}
kv := delRuleHeightIndex(&receipt)
set = append(set, kv...)
}
default:
break
}
}
dbSet.KV = append(dbSet.KV, set...)
kvs, err := table.Save()
if err != nil {
return nil, err
}
dbSet.KV = append(dbSet.KV, kvs...)
return dbSet, nil
}
func delRuleHeightIndex(res *auty.ReceiptProposalRule) (kvs []*types.KeyValue) {
kv := &types.KeyValue{}
kv.Key = calcRuleKey4StatusHeight(res.Current.Status, dapp.HeightIndexStr(res.Current.Height, int64(res.Current.Index)))
kv.Value = nil
kvs = append(kvs, kv)
if res.Current.Status > 1 {
kv := &types.KeyValue{}
kv.Key = calcRuleKey4StatusHeight(res.Prev.Status, dapp.HeightIndexStr(res.Prev.Height, int64(res.Prev.Index)))
kv.Value = types.Encode(res.Prev)
kvs = append(kvs, kv)
}
return kvs
}
func (a *Autonomy) getProposalRule(req *types.ReqString) (types.Message, error) {
if req == nil {
......@@ -115,35 +104,49 @@ func (a *Autonomy) listProposalRule(req *auty.ReqQueryProposalRule) (types.Messa
if req == nil {
return nil, types.ErrInvalidParam
}
//var key []byte
//var values [][]byte
//var err error
//
//localDb := a.GetLocalDB()
//if req.GetIndex() == -1 {
// key = nil
//} else { //翻页查找指定的txhash列表
// heightstr := genHeightIndexStr(req.GetIndex())
// key = calcRuleKey4StatusHeight(req.Status, heightstr)
//}
//prefix := calcRuleKey4StatusHeight(req.Status, "")
//values, err = localDb.List(prefix, key, req.Count, req.GetDirection())
//if err != nil {
// return nil, err
//}
//if len(values) == 0 {
// return nil, types.ErrNotFound
//}
localDb := a.GetLocalDB()
query := NewRuleTable(localDb).GetQuery(localDb)
var primary []byte
if req.Height > 0 {
primary = []byte(dapp.HeightIndexStr(req.Height, int64(req.Index)))
}
indexName := ""
if req.Status > 0 && req.Addr != "" {
indexName = "addr_status"
} else if req.Status > 0 {
indexName = "status"
} else if req.Addr != "" {
indexName = "addr"
}
cur := &RuleRow{
AutonomyProposalRule: &auty.AutonomyProposalRule{},
}
cur.Address = req.Addr
cur.Status = req.Status
cur.Height = req.Height
cur.Index = req.Index
prefix, err := cur.Get(indexName)
rows, err := query.ListIndex(indexName, prefix, primary, req.Count, req.Direction)
if err != nil {
alog.Error("query List failed", "indexName", indexName, "prefix", "prefix", "key", string(primary), "err", err)
return nil, err
}
if len(rows) == 0 {
return nil, types.ErrNotFound
}
var rep auty.ReplyQueryProposalRule
//for _, value := range values {
// prop := &auty.AutonomyProposalRule{}
// err = types.Decode(value, prop)
// if err != nil {
// return nil, err
// }
// rep.PropRules = append(rep.PropRules, prop)
//}
for _, row := range rows {
r, ok := row.Data.(*auty.AutonomyProposalRule)
if !ok {
alog.Error("listProposalRule", "err", "bad row type")
return nil, types.ErrDecode
}
rep.PropRules = append(rep.PropRules, r)
}
return &rep, nil
}
......@@ -224,10 +227,10 @@ func (a *Autonomy) listProposalComment(req *auty.ReqQueryProposalComment) (types
var err error
localDb := a.GetLocalDB()
if req.GetIndex() == -1 {
if req.Height <= 0 {
key = nil
} else { //翻页查找指定的txhash列表
heightstr := genHeightIndexStr(req.GetIndex())
heightstr := dapp.HeightIndexStr(req.Height, int64(req.Index))
key = calcCommentHeight(req.ProposalID, heightstr)
}
prefix := calcCommentHeight(req.ProposalID, "")
......
......@@ -12,10 +12,14 @@ import (
"github.com/33cn/chain33/util"
auty "github.com/33cn/plugin/plugin/dapp/autonomy/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/33cn/chain33/common/db"
)
func TestExecLocalRule(t *testing.T) {
_, sdb, kvdb := util.CreateTestDB()
au := &Autonomy{}
au.SetLocalDB(kvdb)
//TyLogPropRule
cur := &auty.AutonomyProposalRule{
PropRule: &auty.ProposalRule{},
......@@ -38,14 +42,15 @@ func TestExecLocalRule(t *testing.T) {
set, err := au.execLocalRule(receipt)
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcRuleKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
//save to database
saveKvs(sdb, set.KV)
// check
checkExecLocalRule(t, kvdb, cur)
// TyLogRvkPropRule
pre1 := copyAutonomyProposalRule(cur)
cur.Status = auty.AutonomyStatusRvkPropRule
cur.Height = 2
cur.Index = 3
receiptRule1 := &auty.ReceiptProposalRule{
Prev: pre1,
Current: cur,
......@@ -57,11 +62,11 @@ func TestExecLocalRule(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcRuleKey4StatusHeight(pre1.Status,
dapp.HeightIndexStr(pre1.Height, int64(pre1.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
require.Equal(t, set.KV[1].Key, calcRuleKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
//save to database
saveKvs(sdb, set.KV)
// check
checkExecLocalRule(t, kvdb, cur)
// TyLogVotePropRule
cur.Status = auty.AutonomyStatusProposalRule
......@@ -69,8 +74,9 @@ func TestExecLocalRule(t *testing.T) {
cur.Index = 2
pre2 := copyAutonomyProposalRule(cur)
cur.Status = auty.AutonomyStatusVotePropRule
cur.Height = 2
cur.Index = 3
cur.Height = 1
cur.Index = 2
cur.Address = "2222222222222"
receiptRule2 := &auty.ReceiptProposalRule{
Prev: pre2,
Current: cur,
......@@ -82,15 +88,16 @@ func TestExecLocalRule(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcRuleKey4StatusHeight(pre2.Status,
dapp.HeightIndexStr(pre1.Height, int64(pre2.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
require.Equal(t, set.KV[1].Key, calcRuleKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
//save to database
saveKvs(sdb, set.KV)
// check
checkExecLocalRule(t, kvdb, cur)
}
func TestExecDelLocalRule(t *testing.T) {
_, sdb, kvdb := util.CreateTestDB()
au := &Autonomy{}
au.SetLocalDB(kvdb)
//TyLogPropRule
cur := &auty.AutonomyProposalRule{
PropRule: &auty.ProposalRule{},
......@@ -110,22 +117,47 @@ func TestExecDelLocalRule(t *testing.T) {
{Ty: auty.TyLogPropRule, Log: types.Encode(receiptRule)},
},
}
set, err := au.execDelLocalRule(receipt)
// 先执行local然后进行删除
set, err := au.execLocalRule(receipt)
require.NoError(t, err)
require.NotNil(t, set)
saveKvs(sdb, set.KV)
set, err = au.execDelLocalRule(receipt)
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcRuleKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
saveKvs(sdb, set.KV)
// check
table := NewRuleTable(au.GetLocalDB())
query := table.GetQuery(kvdb)
_, err = query.ListIndex("primary", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
_, err = query.ListIndex("addr", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
_, err = query.ListIndex("status", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
_, err = query.ListIndex("addr_status", nil, nil, 10, 0)
assert.Equal(t, err, types.ErrNotFound)
// TyLogVotePropRule
pre1 := copyAutonomyProposalRule(cur)
cur.Status = auty.AutonomyStatusVotePropRule
cur.Height = 2
cur.Index = 3
receiptRule2 := &auty.ReceiptProposalRule{
Prev: pre1,
Current: cur,
}
// 先执行local然后进行删除
set, err = au.execLocalRule(&types.ReceiptData{
Logs: []*types.ReceiptLog{
{Ty: auty.TyLogVotePropRule, Log: types.Encode(receiptRule2)},
},
})
require.NoError(t, err)
require.NotNil(t, set)
saveKvs(sdb, set.KV)
// check
checkExecLocalRule(t, kvdb, cur)
set, err = au.execDelLocalRule(&types.ReceiptData{
Logs: []*types.ReceiptLog{
{Ty: auty.TyLogVotePropRule, Log: types.Encode(receiptRule2)},
......@@ -133,12 +165,35 @@ func TestExecDelLocalRule(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, set)
require.Equal(t, set.KV[0].Key, calcRuleKey4StatusHeight(cur.Status,
dapp.HeightIndexStr(cur.Height, int64(cur.Index))))
require.Equal(t, set.KV[0].Value, []byte(nil))
require.Equal(t, set.KV[1].Key, calcRuleKey4StatusHeight(pre1.Status,
dapp.HeightIndexStr(pre1.Height, int64(pre1.Index))))
require.NotNil(t, set.KV[1].Value)
saveKvs(sdb, set.KV)
// check
checkExecLocalRule(t, kvdb, pre1)
}
func checkExecLocalRule(t *testing.T, kvdb db.KVDB, cur *auty.AutonomyProposalRule) {
table := NewRuleTable(kvdb)
query := table.GetQuery(kvdb)
rows, err := query.ListIndex("primary", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, string(rows[0].Primary), dapp.HeightIndexStr(1, 2))
rows, err = query.ListIndex("addr", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, 1, len(rows))
rows, err = query.ListIndex("status", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, 1, len(rows))
rows, err = query.ListIndex("addr_status", nil, nil, 10, 0)
assert.Equal(t, err, nil)
assert.Equal(t, 1, len(rows))
prop, ok := rows[0].Data.(*auty.AutonomyProposalRule)
assert.Equal(t, true, ok)
assert.Equal(t, prop, cur)
}
func TestGetProposalRule(t *testing.T) {
......@@ -159,7 +214,7 @@ func TestListProposalRule(t *testing.T) {
au := &Autonomy{
dapp.DriverBase{},
}
_, _, kvdb := util.CreateTestDB()
_, sdb, kvdb := util.CreateTestDB()
au.SetLocalDB(kvdb)
type statu struct {
......@@ -190,15 +245,22 @@ func TestListProposalRule(t *testing.T) {
Height: 1,
Index: 2,
}
//将数据保存下去
var kvs []*types.KeyValue
table := NewRuleTable(kvdb)
for _, tcase := range testcase {
key := calcRuleKey4StatusHeight(tcase.status,
dapp.HeightIndexStr(tcase.height, int64(tcase.index)))
cur.Status = tcase.status
cur.Height = tcase.height
cur.Index = int32(tcase.index)
value := types.Encode(cur)
kvdb.Set(key, value)
err := table.Replace(cur)
require.NoError(t, err)
kv, err := table.Save()
require.NoError(t, err)
kvs = append(kvs, kv...)
}
saveKvs(sdb, kvs)
// 反向查找
req := &auty.ReqQueryProposalRule{
......@@ -407,12 +469,12 @@ func TestListProposalComment(t *testing.T) {
require.Equal(t, height, testcase2[2].height)
require.Equal(t, index, int32(testcase2[2].index))
//
Index := height*types.MaxTxsPerBlock + int64(index)
req = &auty.ReqQueryProposalComment{
ProposalID: propID2,
Count: 10,
Direction: 0,
Index: Index,
Height: height,
Index: index,
}
rsp, err = au.listProposalComment(req)
require.NoError(t, err)
......
package executor
import (
"fmt"
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/common/db/table"
"github.com/33cn/chain33/types"
auty "github.com/33cn/plugin/plugin/dapp/autonomy/types"
"github.com/33cn/chain33/system/dapp"
)
/*
table struct
data: autonomy rule
index: status, addr
*/
var ruleOpt = &table.Option{
Prefix: "LODB-autonomy",
Name: "rule",
Primary: "heightindex",
Index: []string{"addr", "status", "addr_status"},
}
//NewTable 新建表
func NewRuleTable(kvdb db.KV) *table.Table {
rowmeta := NewRuleRow()
table, err := table.NewTable(rowmeta, kvdb, ruleOpt)
if err != nil {
panic(err)
}
return table
}
//RuleRow table meta 结构
type RuleRow struct {
*auty.AutonomyProposalRule
}
//NewRuleRow 新建一个meta 结构
func NewRuleRow() *RuleRow {
return &RuleRow{AutonomyProposalRule: &auty.AutonomyProposalRule{}}
}
//CreateRow 新建数据行(注意index 数据一定也要保存到数据中,不能就保存heightindex)
func (r *RuleRow) CreateRow() *table.Row {
return &table.Row{Data: &auty.AutonomyProposalRule{}}
}
//SetPayload 设置数据
func (r *RuleRow) SetPayload(data types.Message) error {
if d, ok := data.(*auty.AutonomyProposalRule); ok {
r.AutonomyProposalRule = d
return nil
}
return types.ErrTypeAsset
}
//Get 按照indexName 查询 indexValue
func (r *RuleRow) Get(key string) ([]byte, error) {
if key == "heightindex" {
return []byte(dapp.HeightIndexStr(r.Height, int64(r.Index))), nil
} else if key == "status" {
return []byte(fmt.Sprintf("%2d", r.Status)), nil
} else if key == "addr" {
return []byte(r.Address), nil
} else if key == "addr_status" {
return []byte(fmt.Sprintf("%s:%2d", r.Address, r.Status)), nil
}
return nil, types.ErrNotFound
}
......@@ -65,12 +65,12 @@ message LocalProposalBoard {
// query
message ReqQueryProposalBoard {
int32 count = 1;
int32 direction = 2;
int64 height = 3;
int32 index = 4;
int32 status = 5;
string addr = 6;
int32 status = 1;
string addr = 2;
int32 count = 3;
int32 direction = 4;
int64 height = 5;
int32 index = 6;
}
message ReplyQueryProposalBoard {
......
......@@ -82,12 +82,12 @@ message LocalProposalProject {
// query
message ReqQueryProposalProject {
int32 count = 1;
int32 direction = 2;
int64 height = 3;
int32 index = 4;
int32 status = 5;
string addr = 6;
int32 status = 1;
string addr = 2;
int32 count = 3;
int32 direction = 4;
int64 height = 5;
int32 index = 6;
}
message ReplyQueryProposalProject {
......
......@@ -61,12 +61,12 @@ message LocalProposalRule {
// query
message ReqQueryProposalRule {
int32 count = 1;
int32 direction = 2;
int64 height = 3;
int32 index = 4;
int32 status = 5;
string addr = 6;
int32 status = 1;
string addr = 2;
int32 count = 3;
int32 direction = 4;
int64 height = 5;
int32 index = 6;
}
message ReplyQueryProposalRule {
......@@ -98,7 +98,8 @@ message ReqQueryProposalComment {
string proposalID = 1;
int32 count = 2;
int32 direction = 3;
int64 index = 4;
int64 height = 4;
int32 index = 5;
}
message RelationCmt {
......
......@@ -262,12 +262,12 @@ func (m *LocalProposalBoard) GetComments() []string {
// query
type ReqQueryProposalBoard struct {
Count int32 `protobuf:"varint,1,opt,name=count" json:"count,omitempty"`
Direction int32 `protobuf:"varint,2,opt,name=direction" json:"direction,omitempty"`
Height int64 `protobuf:"varint,3,opt,name=height" json:"height,omitempty"`
Index int32 `protobuf:"varint,4,opt,name=index" json:"index,omitempty"`
Status int32 `protobuf:"varint,5,opt,name=status" json:"status,omitempty"`
Addr string `protobuf:"bytes,6,opt,name=addr" json:"addr,omitempty"`
Status int32 `protobuf:"varint,1,opt,name=status" json:"status,omitempty"`
Addr string `protobuf:"bytes,2,opt,name=addr" json:"addr,omitempty"`
Count int32 `protobuf:"varint,3,opt,name=count" json:"count,omitempty"`
Direction int32 `protobuf:"varint,4,opt,name=direction" json:"direction,omitempty"`
Height int64 `protobuf:"varint,5,opt,name=height" json:"height,omitempty"`
Index int32 `protobuf:"varint,6,opt,name=index" json:"index,omitempty"`
}
func (m *ReqQueryProposalBoard) Reset() { *m = ReqQueryProposalBoard{} }
......@@ -275,46 +275,46 @@ func (m *ReqQueryProposalBoard) String() string { return proto.Compac
func (*ReqQueryProposalBoard) ProtoMessage() {}
func (*ReqQueryProposalBoard) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{7} }
func (m *ReqQueryProposalBoard) GetCount() int32 {
func (m *ReqQueryProposalBoard) GetStatus() int32 {
if m != nil {
return m.Count
return m.Status
}
return 0
}
func (m *ReqQueryProposalBoard) GetDirection() int32 {
func (m *ReqQueryProposalBoard) GetAddr() string {
if m != nil {
return m.Direction
return m.Addr
}
return 0
return ""
}
func (m *ReqQueryProposalBoard) GetHeight() int64 {
func (m *ReqQueryProposalBoard) GetCount() int32 {
if m != nil {
return m.Height
return m.Count
}
return 0
}
func (m *ReqQueryProposalBoard) GetIndex() int32 {
func (m *ReqQueryProposalBoard) GetDirection() int32 {
if m != nil {
return m.Index
return m.Direction
}
return 0
}
func (m *ReqQueryProposalBoard) GetStatus() int32 {
func (m *ReqQueryProposalBoard) GetHeight() int64 {
if m != nil {
return m.Status
return m.Height
}
return 0
}
func (m *ReqQueryProposalBoard) GetAddr() string {
func (m *ReqQueryProposalBoard) GetIndex() int32 {
if m != nil {
return m.Addr
return m.Index
}
return ""
return 0
}
type ReplyQueryProposalBoard struct {
......@@ -348,40 +348,39 @@ func init() {
func init() { proto.RegisterFile("board.proto", fileDescriptor1) }
var fileDescriptor1 = []byte{
// 545 bytes of a gzipped FileDescriptorProto
// 544 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x4d, 0x6f, 0xd3, 0x40,
0x10, 0x95, 0xe3, 0x38, 0x1f, 0x53, 0x15, 0xb5, 0x4b, 0x5a, 0xac, 0xaa, 0x42, 0x91, 0x0f, 0x28,
0x02, 0x29, 0x82, 0xf2, 0x21, 0x0e, 0x5c, 0x08, 0x20, 0x81, 0x04, 0x12, 0xac, 0x10, 0x9c, 0xb7,
0xf6, 0xb4, 0xb1, 0xea, 0xec, 0x2e, 0xeb, 0x75, 0x44, 0x6e, 0xfc, 0x19, 0x4e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4, 0x76, 0xa2, 0x4d, 0x54, 0x29, 0xad, 0x17, 0x68,
0x0d, 0xf8, 0x31, 0x8c, 0xd2, 0xcc, 0x60, 0x62, 0x33, 0x25, 0xbd, 0xe7, 0x36, 0x81, 0x86, 0xb9,
0x82, 0xed, 0xe6, 0xea, 0x37, 0xcc, 0x15, 0x7d, 0x85, 0x3b, 0x02, 0x75, 0x5e, 0x6d, 0x69, 0xf4,
0x45, 0x7d, 0xcd, 0x79, 0xed, 0x06, 0x36, 0xe9, 0xfe, 0x73, 0x31, 0x0d, 0xfe, 0x69, 0x9f, 0xfe,
0xca, 0x1e, 0xff, 0x09, 0x00, 0x00, 0xff, 0xff, 0x3d, 0x91, 0x96, 0xe2, 0xef, 0x04, 0x00, 0x00,
}
This diff is collapsed.
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