Commit 261bb43b authored by 袁兴强's avatar 袁兴强

add query db

parent a4b37c31
......@@ -25,6 +25,7 @@ func Cmd() *cobra.Command {
cmdCreateContract(),
cmdUpdateContract(),
cmdCallContract(),
cmdQueryContract(),
)
return cmd
......@@ -82,21 +83,45 @@ func cmdCallContract() *cobra.Command {
return cmd
}
func checkContract(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
name, _ := cmd.Flags().GetString("name")
func cmdQueryContract() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Short: "query wasm contract",
Args: cobra.MinimumNArgs(1),
}
params := rpctypes.Query4Jrpc{
Execer: wasmtypes.WasmX,
FuncName: "Check",
Payload: types.MustPBToJSON(&wasmtypes.QueryCheckContract{
Name: name,
}),
cmd.AddCommand(
cmdQueryStateDB(),
cmdQueryLocalDB(),
)
return cmd
}
func cmdQueryStateDB() *cobra.Command {
cmd := &cobra.Command{
Use: "state",
Short: "query kv in state db",
Run: queryStateDB,
}
cmd.Flags().StringP("contract", "n", "", "contract name")
cmd.Flags().StringP("key", "k", "", "key of state db")
_ = cmd.MarkFlagRequired("contract")
_ = cmd.MarkFlagRequired("key")
return cmd
}
var resp types.Reply
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &resp)
ctx.Run()
func cmdQueryLocalDB() *cobra.Command {
cmd := &cobra.Command{
Use: "local",
Short: "query kv in local db",
Run: queryLocalDB,
}
cmd.Flags().StringP("contract", "n", "", "contract name")
cmd.Flags().StringP("key", "k", "", "key of local db")
_ = cmd.MarkFlagRequired("contract")
_ = cmd.MarkFlagRequired("key")
return cmd
}
func createContract(cmd *cobra.Command, args []string) {
......@@ -174,3 +199,58 @@ func callContract(cmd *cobra.Command, args []string) {
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, nil)
ctx.RunWithoutMarshal()
}
func checkContract(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
name, _ := cmd.Flags().GetString("name")
params := rpctypes.Query4Jrpc{
Execer: wasmtypes.WasmX,
FuncName: "Check",
Payload: types.MustPBToJSON(&wasmtypes.QueryCheckContract{
Name: name,
}),
}
var resp types.Reply
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &resp)
ctx.Run()
}
func queryStateDB(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
contract, _ := cmd.Flags().GetString("contract")
key, _ := cmd.Flags().GetString("key")
params := rpctypes.Query4Jrpc{
Execer: wasmtypes.WasmX,
FuncName: "QueryStateDB",
Payload: types.MustPBToJSON(&wasmtypes.QueryContractDB{
Contract: contract,
Key: key,
}),
}
var resp types.ReplyString
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &resp)
ctx.Run()
}
func queryLocalDB(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
contract, _ := cmd.Flags().GetString("contract")
key, _ := cmd.Flags().GetString("key")
params := rpctypes.Query4Jrpc{
Execer: wasmtypes.WasmX,
FuncName: "QueryLocalDB",
Payload: types.MustPBToJSON(&wasmtypes.QueryContractDB{
Contract: contract,
Key: key,
}),
}
var resp types.ReplyString
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &resp)
ctx.Run()
}
......@@ -32,11 +32,3 @@ func calcLocalPrefix(contract string) []byte {
prefix = append(prefix, '-')
return prefix
}
func (w *Wasm) contractExist(name string) bool {
_, err := w.GetStateDB().Get(contractKey(name))
if err != nil && err != types.ErrNotFound {
panic(err)
}
return err == nil
}
......@@ -9,5 +9,36 @@ func (w *Wasm) Query_Check(query *types2.QueryCheckContract) (types.Message, err
if query == nil {
return nil, types.ErrInvalidParam
}
return &types.Reply{IsOk: w.contractExist(query.Name)}, nil
_, err := w.GetStateDB().Get(contractKey(query.Name))
if err == nil {
return &types.Reply{IsOk: true}, nil
}
if err == types.ErrNotFound {
return &types.Reply{}, nil
}
return nil, err
}
func (w *Wasm) Query_QueryStateDB(query *types2.QueryContractDB) (types.Message, error) {
if query == nil {
return nil, types.ErrInvalidParam
}
key := append(calcStatePrefix(query.Contract), query.Key...)
v, err := w.GetStateDB().Get(key)
if err != nil {
return nil, err
}
return &types.ReplyString{Data: string(v)}, nil
}
func (w *Wasm) Query_QueryLocalDB(query *types2.QueryContractDB) (types.Message, error) {
if query == nil {
return nil, types.ErrInvalidParam
}
key := append(calcLocalPrefix(query.Contract), query.Key...)
v, err := w.GetLocalDB().Get(key)
if err != nil {
return nil, err
}
return &types.ReplyString{Data: string(v)}, nil
}
......@@ -32,6 +32,11 @@ message queryCheckContract {
string name = 1;
}
message queryContractDB {
string contract = 1;
string key = 2;
}
message customLog {
repeated string info = 1;
}
......
......@@ -319,6 +319,53 @@ func (m *QueryCheckContract) GetName() string {
return ""
}
type QueryContractDB struct {
Contract string `protobuf:"bytes,1,opt,name=contract,proto3" json:"contract,omitempty"`
Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *QueryContractDB) Reset() { *m = QueryContractDB{} }
func (m *QueryContractDB) String() string { return proto.CompactTextString(m) }
func (*QueryContractDB) ProtoMessage() {}
func (*QueryContractDB) Descriptor() ([]byte, []int) {
return fileDescriptor_7d78909ad64e3bbb, []int{5}
}
func (m *QueryContractDB) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_QueryContractDB.Unmarshal(m, b)
}
func (m *QueryContractDB) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_QueryContractDB.Marshal(b, m, deterministic)
}
func (m *QueryContractDB) XXX_Merge(src proto.Message) {
xxx_messageInfo_QueryContractDB.Merge(m, src)
}
func (m *QueryContractDB) XXX_Size() int {
return xxx_messageInfo_QueryContractDB.Size(m)
}
func (m *QueryContractDB) XXX_DiscardUnknown() {
xxx_messageInfo_QueryContractDB.DiscardUnknown(m)
}
var xxx_messageInfo_QueryContractDB proto.InternalMessageInfo
func (m *QueryContractDB) GetContract() string {
if m != nil {
return m.Contract
}
return ""
}
func (m *QueryContractDB) GetKey() string {
if m != nil {
return m.Key
}
return ""
}
type CustomLog struct {
Info []string `protobuf:"bytes,1,rep,name=info,proto3" json:"info,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
......@@ -330,7 +377,7 @@ func (m *CustomLog) Reset() { *m = CustomLog{} }
func (m *CustomLog) String() string { return proto.CompactTextString(m) }
func (*CustomLog) ProtoMessage() {}
func (*CustomLog) Descriptor() ([]byte, []int) {
return fileDescriptor_7d78909ad64e3bbb, []int{5}
return fileDescriptor_7d78909ad64e3bbb, []int{6}
}
func (m *CustomLog) XXX_Unmarshal(b []byte) error {
......@@ -370,7 +417,7 @@ func (m *CreateContractLog) Reset() { *m = CreateContractLog{} }
func (m *CreateContractLog) String() string { return proto.CompactTextString(m) }
func (*CreateContractLog) ProtoMessage() {}
func (*CreateContractLog) Descriptor() ([]byte, []int) {
return fileDescriptor_7d78909ad64e3bbb, []int{6}
return fileDescriptor_7d78909ad64e3bbb, []int{7}
}
func (m *CreateContractLog) XXX_Unmarshal(b []byte) error {
......@@ -417,7 +464,7 @@ func (m *UpdateContractLog) Reset() { *m = UpdateContractLog{} }
func (m *UpdateContractLog) String() string { return proto.CompactTextString(m) }
func (*UpdateContractLog) ProtoMessage() {}
func (*UpdateContractLog) Descriptor() ([]byte, []int) {
return fileDescriptor_7d78909ad64e3bbb, []int{7}
return fileDescriptor_7d78909ad64e3bbb, []int{8}
}
func (m *UpdateContractLog) XXX_Unmarshal(b []byte) error {
......@@ -465,7 +512,7 @@ func (m *CallContractLog) Reset() { *m = CallContractLog{} }
func (m *CallContractLog) String() string { return proto.CompactTextString(m) }
func (*CallContractLog) ProtoMessage() {}
func (*CallContractLog) Descriptor() ([]byte, []int) {
return fileDescriptor_7d78909ad64e3bbb, []int{8}
return fileDescriptor_7d78909ad64e3bbb, []int{9}
}
func (m *CallContractLog) XXX_Unmarshal(b []byte) error {
......@@ -519,7 +566,7 @@ func (m *LocalDataLog) Reset() { *m = LocalDataLog{} }
func (m *LocalDataLog) String() string { return proto.CompactTextString(m) }
func (*LocalDataLog) ProtoMessage() {}
func (*LocalDataLog) Descriptor() ([]byte, []int) {
return fileDescriptor_7d78909ad64e3bbb, []int{9}
return fileDescriptor_7d78909ad64e3bbb, []int{10}
}
func (m *LocalDataLog) XXX_Unmarshal(b []byte) error {
......@@ -560,6 +607,7 @@ func init() {
proto.RegisterType((*WasmUpdate)(nil), "types.wasmUpdate")
proto.RegisterType((*WasmCall)(nil), "types.wasmCall")
proto.RegisterType((*QueryCheckContract)(nil), "types.queryCheckContract")
proto.RegisterType((*QueryContractDB)(nil), "types.queryContractDB")
proto.RegisterType((*CustomLog)(nil), "types.customLog")
proto.RegisterType((*CreateContractLog)(nil), "types.createContractLog")
proto.RegisterType((*UpdateContractLog)(nil), "types.updateContractLog")
......@@ -570,29 +618,30 @@ func init() {
func init() { proto.RegisterFile("wasm.proto", fileDescriptor_7d78909ad64e3bbb) }
var fileDescriptor_7d78909ad64e3bbb = []byte{
// 370 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0x4d, 0x4b, 0xfb, 0x40,
0x10, 0xc6, 0x9b, 0xe6, 0xe5, 0xdf, 0xcc, 0xbf, 0xd8, 0x76, 0x91, 0x12, 0x3c, 0x68, 0x08, 0x08,
0x01, 0xa1, 0x07, 0x15, 0x2f, 0x3d, 0x69, 0x3d, 0xf4, 0xe0, 0x69, 0xc1, 0xa3, 0x87, 0x75, 0xbb,
0xda, 0xd2, 0x4d, 0x36, 0x6e, 0x36, 0x95, 0x7c, 0x21, 0x3f, 0xa7, 0xec, 0x4b, 0xa4, 0xa0, 0x88,
0xf4, 0x36, 0xb3, 0xf3, 0x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, 0x86, 0x58, 0xc9, 0x24, 0x05, 0xf4, 0x5e,
0xb3, 0xb2, 0x99, 0xaf, 0x19, 0xdd, 0xce, 0xdb, 0xf7, 0x7f, 0xc9, 0x9b, 0xdc, 0xc3, 0xc0, 0x90,
0x16, 0x5a, 0xcc, 0x0e, 0x46, 0x1c, 0x82, 0xbb, 0x65, 0x8d, 0xcd, 0xa7, 0x64, 0x72, 0x01, 0x21,
0xad, 0x2b, 0x29, 0xb2, 0x47, 0xf1, 0xa6, 0xbe, 0xb0, 0xc9, 0x5f, 0x45, 0xe4, 0xe8, 0x28, 0x5a,
0x27, 0x53, 0x18, 0x99, 0x01, 0xb7, 0x9f, 0xb0, 0xe0, 0xc1, 0xd6, 0x85, 0xb6, 0x75, 0x53, 0x18,
0x99, 0x81, 0x1f, 0x63, 0x7e, 0x86, 0x81, 0x5a, 0x82, 0x7d, 0xeb, 0x31, 0xed, 0x1f, 0x43, 0x50,
0xb2, 0xaa, 0xe6, 0x52, 0x2f, 0x9b, 0x8f, 0x6d, 0x95, 0xdc, 0x41, 0x9f, 0x0b, 0x4a, 0xf8, 0x82,
0x48, 0xa2, 0xde, 0xb6, 0xbd, 0x71, 0xf4, 0xe4, 0x95, 0x44, 0xa7, 0x76, 0xdb, 0xec, 0x36, 0x98,
0xe2, 0x25, 0xd0, 0x7f, 0xcc, 0xcd, 0x57, 0x00, 0x00, 0x00, 0xff, 0xff, 0xf2, 0xc4, 0xdc, 0xc3,
0x3f, 0x03, 0x00, 0x00,
}
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