Commit 5d7a8a13 authored by 袁兴强's avatar 袁兴强 Committed by vipwzw

support string parameter for wasm contracts

parent 7d52d0f3
...@@ -62,6 +62,7 @@ func cmdCallContract() *cobra.Command { ...@@ -62,6 +62,7 @@ func cmdCallContract() *cobra.Command {
cmd.Flags().StringP("name", "n", "", "contract name") cmd.Flags().StringP("name", "n", "", "contract name")
cmd.Flags().StringP("method", "m", "", "method name") cmd.Flags().StringP("method", "m", "", "method name")
cmd.Flags().IntSliceP("parameters", "p", nil, "parameters of the method which should be num") cmd.Flags().IntSliceP("parameters", "p", nil, "parameters of the method which should be num")
cmd.Flags().StringSliceP("env", "v", nil, "string parameters set to environment")
_ = cmd.MarkFlagRequired("name") _ = cmd.MarkFlagRequired("name")
_ = cmd.MarkFlagRequired("method") _ = cmd.MarkFlagRequired("method")
return cmd return cmd
...@@ -114,6 +115,7 @@ func callContract(cmd *cobra.Command, args []string) { ...@@ -114,6 +115,7 @@ func callContract(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name") name, _ := cmd.Flags().GetString("name")
method, _ := cmd.Flags().GetString("method") method, _ := cmd.Flags().GetString("method")
parameters, _ := cmd.Flags().GetIntSlice("parameters") parameters, _ := cmd.Flags().GetIntSlice("parameters")
env, _ := cmd.Flags().GetStringSlice("env")
var parameters2 []int64 var parameters2 []int64
for _, param := range parameters { for _, param := range parameters {
parameters2 = append(parameters2, int64(param)) parameters2 = append(parameters2, int64(param))
...@@ -123,6 +125,7 @@ func callContract(cmd *cobra.Command, args []string) { ...@@ -123,6 +125,7 @@ func callContract(cmd *cobra.Command, args []string) {
Contract: name, Contract: name,
Method: method, Method: method,
Parameters: parameters2, Parameters: parameters2,
Env: env,
} }
params := rpctypes.CreateTxIn{ params := rpctypes.CreateTxIn{
Execer: wasmtypes.WasmX, Execer: wasmtypes.WasmX,
......
...@@ -60,8 +60,8 @@ wabt/bin/wasm2wat dice.wasm ...@@ -60,8 +60,8 @@ wabt/bin/wasm2wat dice.wasm
### 调用合约 ### 调用合约
```bash ```bash
#其中参数为用逗号分隔的数字 #其中参数为用逗号分隔的数字列表,字符串参数为逗号分隔的字符串列表
./chain33-cli send wasm call -n 发布合约时指定的合约 -m 调用合约方法名 -p 参数 -k 用户私钥 ./chain33-cli send wasm call -n 发布合约时指定的合约 -m 调用合约方法名 -p 参数 -v 字符串参数 -k 用户私钥
``` ```
### 转账及提款 ### 转账及提款
......
...@@ -33,6 +33,8 @@ int64_t getRandom(); ...@@ -33,6 +33,8 @@ int64_t getRandom();
void sha256(const char* data, size_t data_len, char* sum, size_t sum_len); void sha256(const char* data, size_t data_len, char* sum, size_t sum_len);
void printlog(const char* log, size_t len); void printlog(const char* log, size_t len);
void printint(int64_t n); void printint(int64_t n);
size_t getENVSize(int64_t n);
size_t getENV(int64_t n);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -170,3 +170,11 @@ func printlog(s string) { ...@@ -170,3 +170,11 @@ func printlog(s string) {
func sha256(data []byte) []byte { func sha256(data []byte) []byte {
return common.Sha256(data) return common.Sha256(data)
} }
func getENVSize(n int) int {
return len(wasmCB.ENV[n])
}
func getENV(n int) string {
return wasmCB.ENV[n]
}
...@@ -113,6 +113,10 @@ func (w *Wasm) Exec_Call(payload *types2.WasmCall, tx *types.Transaction, index ...@@ -113,6 +113,10 @@ func (w *Wasm) Exec_Call(payload *types2.WasmCall, tx *types.Transaction, index
w.contractName = payload.Contract w.contractName = payload.Contract
w.tx = tx w.tx = tx
w.execAddr = address.ExecAddress(string(types.GetRealExecName(tx.Execer))) w.execAddr = address.ExecAddress(string(types.GetRealExecName(tx.Execer)))
w.ENV = make(map[int]string)
for i, v := range payload.Env {
w.ENV[i] = v
}
wasmCB = w wasmCB = w
defer func() { defer func() {
wasmCB = nil wasmCB = nil
......
...@@ -287,6 +287,21 @@ func (r *Resolver) ResolveFunc(module, field string) exec.FunctionImport { ...@@ -287,6 +287,21 @@ func (r *Resolver) ResolveFunc(module, field string) exec.FunctionImport {
copy(vm.Memory[sumPtr:sumPtr+sumLen], sha256(data)) copy(vm.Memory[sumPtr:sumPtr+sumLen], sha256(data))
return 0 return 0
} }
case "getENVSize":
return func(vm *exec.VirtualMachine) int64 {
n := vm.GetCurrentFrame().Locals[0]
return int64(getENVSize(int(n)))
}
case "getENV":
return func(vm *exec.VirtualMachine) int64 {
n := vm.GetCurrentFrame().Locals[0]
valuePtr := int(uint32(vm.GetCurrentFrame().Locals[1]))
valueLen := int(uint32(vm.GetCurrentFrame().Locals[2]))
value := getENV(int(n))
copy(vm.Memory[valuePtr:valuePtr+valueLen], value)
return int64(len(value))
}
default: default:
log.Error("ResolveFunc", "unknown field", field) log.Error("ResolveFunc", "unknown field", field)
......
...@@ -38,11 +38,13 @@ type Wasm struct { ...@@ -38,11 +38,13 @@ type Wasm struct {
execAddr string execAddr string
contractName string contractName string
VMCache map[string]*exec.VirtualMachine VMCache map[string]*exec.VirtualMachine
ENV map[int]string
} }
func newWasm() drivers.Driver { func newWasm() drivers.Driver {
d := &Wasm{ d := &Wasm{
VMCache: make(map[string]*exec.VirtualMachine), VMCache: make(map[string]*exec.VirtualMachine),
ENV: make(map[int]string),
} }
d.SetChild(d) d.SetChild(d)
d.SetExecutorType(types.LoadExecutorType(driverName)) d.SetExecutorType(types.LoadExecutorType(driverName))
......
...@@ -19,6 +19,7 @@ message wasmCall { ...@@ -19,6 +19,7 @@ message wasmCall {
string contract = 1; string contract = 1;
string method = 2; string method = 2;
repeated int64 parameters = 3; repeated int64 parameters = 3;
repeated string env = 4;
} }
message queryCheckContract { message queryCheckContract {
......
...@@ -5,9 +5,8 @@ package types ...@@ -5,9 +5,8 @@ package types
import ( import (
fmt "fmt" fmt "fmt"
math "math"
proto "github.com/golang/protobuf/proto" proto "github.com/golang/protobuf/proto"
math "math"
) )
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
...@@ -160,6 +159,7 @@ type WasmCall struct { ...@@ -160,6 +159,7 @@ type WasmCall struct {
Contract string `protobuf:"bytes,1,opt,name=contract,proto3" json:"contract,omitempty"` Contract string `protobuf:"bytes,1,opt,name=contract,proto3" json:"contract,omitempty"`
Method string `protobuf:"bytes,2,opt,name=method,proto3" json:"method,omitempty"` Method string `protobuf:"bytes,2,opt,name=method,proto3" json:"method,omitempty"`
Parameters []int64 `protobuf:"varint,3,rep,packed,name=parameters,proto3" json:"parameters,omitempty"` Parameters []int64 `protobuf:"varint,3,rep,packed,name=parameters,proto3" json:"parameters,omitempty"`
Env []string `protobuf:"bytes,4,rep,name=env,proto3" json:"env,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"` XXX_sizecache int32 `json:"-"`
...@@ -211,6 +211,13 @@ func (m *WasmCall) GetParameters() []int64 { ...@@ -211,6 +211,13 @@ func (m *WasmCall) GetParameters() []int64 {
return nil return nil
} }
func (m *WasmCall) GetEnv() []string {
if m != nil {
return m.Env
}
return nil
}
type QueryCheckContract struct { type QueryCheckContract struct {
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_NoUnkeyedLiteral struct{} `json:"-"`
...@@ -449,31 +456,30 @@ func init() { ...@@ -449,31 +456,30 @@ func init() {
proto.RegisterType((*LocalDataLog)(nil), "types.localDataLog") proto.RegisterType((*LocalDataLog)(nil), "types.localDataLog")
} }
func init() { func init() { proto.RegisterFile("wasm.proto", fileDescriptor_7d78909ad64e3bbb) }
proto.RegisterFile("wasm.proto", fileDescriptor_7d78909ad64e3bbb)
}
var fileDescriptor_7d78909ad64e3bbb = []byte{ var fileDescriptor_7d78909ad64e3bbb = []byte{
// 327 bytes of a gzipped FileDescriptorProto // 337 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00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0x57, 0xc2, 0xe4, 0x2f, 0x30, 0x71, 0x0b, 0x3d, 0x6e, 0xfd, 0xcb, 0x02, 0xe6, 0x10, 0x5b, 0x55, 0x37,
0x8f, 0x1c, 0xb9, 0xd5, 0x3e, 0x86, 0x78, 0x2b, 0x5b, 0x27, 0x3b, 0x66, 0x36, 0x24, 0x27, 0xe1, 0x1a, 0xc3, 0x4d, 0x02, 0x63, 0x77, 0x30, 0xd6, 0x46, 0x0a, 0xfd, 0x24, 0x50, 0x38, 0xef, 0x29,
0x4e, 0x61, 0xfb, 0x3e, 0x79, 0x4d, 0xdc, 0xdf, 0x73, 0xfd, 0x1d, 0x00, 0x00, 0xff, 0xff, 0xc3, 0x90, 0x9d, 0x6a, 0xbd, 0xed, 0x98, 0x3b, 0x48, 0x4f, 0xc3, 0xe5, 0xc2, 0x3d, 0x3a, 0xf2, 0x1a,
0xc2, 0x9e, 0xbb, 0x4b, 0x02, 0x00, 0x00, 0xfb, 0xff, 0xe9, 0xe6, 0x2b, 0x00, 0x00, 0xff, 0xff, 0x62, 0xf8, 0x3a, 0xae, 0x5d, 0x02, 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