在技术群里有人问到 长安链智能合约中key长度是否有限制,是否可以是中文?这里我们一起来看一下
- 先看一下智能合约调用
PutStateByte
过程,这里分析一下官方智能合约代码,其中fileHash是field,fact 为 key。
PutStateByte("fact", fileHash, bytes)
- 函数逐级调用最终会调用到sysCall函数
code := sysCall(getRequestHeader(ContractMethodPutState), jsonParam)
- gasm启动时注入函数 sysCall
builder.MustSetFunction(waci.WaciModuleName, "sys_call", waciInstance.SysCall)
- sysCall 将调用PutState的实现,对于key 和 field检测过程如下:
func CheckKeyFieldStr(key string, field string) error {
{
s := key
if len(s) > DefaultStateLen {
return fmt.Errorf("key[%s] too long", s)
}
match, err := regexp.MatchString(DefaultStateRegex, s)
if err != nil || !match {
return fmt.Errorf("key[%s] can only consist of numbers, dot, letters and underscores", s)
}
}
{
s := field
if len(s) == 0 {
return nil
}
if len(s) > DefaultStateLen {
return fmt.Errorf("key field[%s] too long", s)
}
match, err := regexp.MatchString(DefaultStateRegex, s)
if err != nil || !match {
return fmt.Errorf("key field[%s] can only consist of numbers, dot, letters and underscores", s)
}
}
return nil
}
DefaultStateRegex = "^[a-zA-Z0-9._-]+$"
上面一段函数,判断key、field长度不能超过64,其中格式进行正则校验,字母、数字、点、下划线、横杠可以通过,所以中文不可以。
为了验证以上观点,我们测试一下:
- 修改合约代码为
package chainmaker_sdk_go
import (
"chainmaker.org/chainmaker-sdk-go/pb/protogo/common"
"fmt"
"github.com/stretchr/testify/require"
"testing"
"time"
)
var (
hashContractName = "myhash001"
hashVersion = "1.0.0"
hashByteCodePath = "./testdata/hash-cc/main.wasm"
)
func TestUserContractHash(t *testing.T) {
fmt.Println("====================== create client ======================")
client, err := createClientWithCertBytes()
require.Nil(t, err)
fmt.Println("====================== create admin1 ======================")
admin1, err := createAdmin(orgId1)
require.Nil(t, err)
fmt.Println("====================== create admin2 ======================")
admin2, err := createAdmin(orgId2)
require.Nil(t, err)
fmt.Println("====================== create admin3 ======================")
admin3, err := createAdmin(orgId3)
require.Nil(t, err)
fmt.Println("====================== create admin4 ======================")
admin4, err := createAdmin(orgId4)
require.Nil(t, err)
fmt.Println("====================== 创建合约 ======================")
testUserContractHashCreate(t, client, admin1, admin2, admin3, admin4, true, true)
fmt.Println("====================== 调用合约 ======================")
fileHash, err := testUserContractHashInvoke(client, "save", true)
require.Nil(t, err)
fmt.Println("====================== 执行合约查询接口 ======================")
//txId := "1cbdbe6106cc4132b464185ea8275d0a53c0261b7b1a470fb0c3f10bd4a57ba6"
//fileHash = txId[len(txId)/2:]
//fileHash := "e1477f17863643a1b4792bc391039568";
params := map[string]string{
"file_hash": fileHash,
}
testUserContractHashQuery(t, client, "find_by_file_hash", params)
}
func testUserContractHashCreate(t *testing.T, client *ChainClient,
admin1, admin2, admin3, admin4 *ChainClient, withSyncResult bool, isIgnoreSameContract bool) {
resp, err := createUserContract(client, admin1, admin2, admin3, admin4,
hashContractName, hashVersion, hashByteCodePath, common.RuntimeType_GASM, []*common.KeyValuePair{}, withSyncResult)
if !isIgnoreSameContract {
require.Nil(t, err)
}
fmt.Printf("CREATE claim contract resp: %+v\n", resp)
}
func testUserContractHashInvoke(client *ChainClient,
method string, withSyncResult bool) (string, error) {
//curTime := fmt.Sprintf("%d", CurrentTimeMillisSeconds())
curTime := time.Now().Format("2006-01-02 15:04:05")
//fileHash := uuid.GetUUID()
fileHash := "我的老嘎就住在这个屯"
params := map[string]string{
"time": "123",
"file_hash": fileHash,
"file_name": fmt.Sprintf("file_%s", curTime),
}
err := invokeUserContract(client, hashContractName, method, "", params, withSyncResult)
//err := invokeUserContractStepByStep(client, claimContractName, method, "", params, withSyncResult)
if err != nil {
return "", err
}
return fileHash, nil
}
func testUserContractHashQuery(t *testing.T, client *ChainClient,
method string, params map[string]string) {
resp, err := client.QueryContract(hashContractName, method, params, -1)
require.Nil(t, err)
fmt.Printf("QUERY claim contract resp: %+v\n", resp)
}
里面的fileHash修改为"我的老嘎就住在这个屯"
。
- 执行invoke命令报错:
Expected nil, but got: &errors.errorString{s:"invoke contract failed, [code:4]/[msg:error message: key field[我的老嘎就住在这个屯] can only consist of numbers, dot, letters and underscores. error message: key field[我的老嘎就住在这个屯] can only consist of numbers, dot, letters and underscores]\n"}
错误很明显field格式不正确。
- fileHash长度超过64字节可自行验证。
网友评论