Skip to content

Commit

Permalink
Merge branch 'hotfix/1.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
graup committed Jun 17, 2019
2 parents a9a0eee + cfb4d56 commit bda5526
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 26 deletions.
28 changes: 19 additions & 9 deletions contract/system_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ static int systemPrint(lua_State *L)
return 0;
}

static char *getDbKey(lua_State *L)
static char *getDbKey(lua_State *L, int *len)
{
size_t size;
char *key;

lua_pushvalue(L, 1); /* prefix key */
lua_concat(L, 2); /* dbKey(prefix..key) */
return (char *)lua_tostring(L, -1);
key = (char *)lua_tolstring(L, -1, &size);
*len = size;
return key;
}

int setItemWithPrefix(lua_State *L)
Expand All @@ -37,6 +42,7 @@ int setItemWithPrefix(lua_State *L)
char *jsonValue;
int *service = (int *)getLuaExecContext(L);
char *errStr;
int keylen;

if (service == NULL) {
luaL_error(L, "cannot find execution context");
Expand All @@ -45,13 +51,14 @@ int setItemWithPrefix(lua_State *L)
luaL_checkstring(L, 1);
luaL_checkany(L, 2);
luaL_checkstring(L, 3);
dbKey = getDbKey(L);
dbKey = getDbKey(L, &keylen);

jsonValue = lua_util_get_json (L, 2, false);
if (jsonValue == NULL) {
luaL_throwerror(L);
}

if ((errStr = LuaSetDB(L, service, dbKey, jsonValue)) != NULL) {
if ((errStr = LuaSetDB(L, service, dbKey, keylen, jsonValue)) != NULL) {
free(jsonValue);
strPushAndRelease(L, errStr);
luaL_throwerror(L);
Expand All @@ -76,6 +83,7 @@ int getItemWithPrefix(lua_State *L)
char *jsonValue;
char *blkno = NULL;
struct LuaGetDB_return ret;
int keylen;

if (service == NULL) {
luaL_error(L, "cannot find execution context");
Expand All @@ -93,9 +101,9 @@ int getItemWithPrefix(lua_State *L)
}
luaL_checkstring(L, 3);
}
dbKey = getDbKey(L);
dbKey = getDbKey(L, &keylen);

ret = LuaGetDB(L, service, dbKey, blkno);
ret = LuaGetDB(L, service, dbKey, keylen, blkno);
if (ret.r1 != NULL) {
strPushAndRelease(L, ret.r1);
luaL_throwerror(L);
Expand Down Expand Up @@ -128,14 +136,15 @@ int delItemWithPrefix(lua_State *L)
int *service = (int *)getLuaExecContext(L);
char *jsonValue;
char *ret;
int keylen;

if (service == NULL) {
luaL_error(L, "cannot find execution context");
}
luaL_checkstring(L, 1);
luaL_checkstring(L, 2);
dbKey = getDbKey(L);
ret = LuaDelDB(L, service, dbKey);
dbKey = getDbKey(L, &keylen);
ret = LuaDelDB(L, service, dbKey, keylen);
if (ret != NULL) {
strPushAndRelease(L, ret);
luaL_throwerror(L);
Expand Down Expand Up @@ -203,11 +212,12 @@ static int getCreator(lua_State *L)
{
int *service = (int *)getLuaExecContext(L);
struct LuaGetDB_return ret;
int keylen = 7;

if (service == NULL) {
luaL_error(L, "cannot find execution context");
}
ret = LuaGetDB(L, service, "Creator", 0);
ret = LuaGetDB(L, service, "Creator", keylen, 0);
if (ret.r1 != NULL) {
strPushAndRelease(L, ret.r1);
luaL_throwerror(L);
Expand Down
6 changes: 4 additions & 2 deletions contract/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,15 @@ const char *vm_pcall(lua_State *L, int argc, int *nresult)
return NULL;
}

const char *vm_get_json_ret(lua_State *L, int nresult)
const char *vm_get_json_ret(lua_State *L, int nresult, int* err)
{
int top = lua_gettop(L);
char *json_ret = lua_util_get_json_from_stack(L, top - nresult + 1, top, true);

if (json_ret == NULL)
if (json_ret == NULL) {
*err = 1;
return lua_tostring(L, -1);
}

lua_pushstring(L, json_ret);
free(json_ret);
Expand Down
10 changes: 8 additions & 2 deletions contract/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func resolveFunction(contractState *state.ContractState, name string, constructo
if constructor {
return nil, nil
}
if defaultFunc != nil {
if len(name) == 0 && defaultFunc != nil {
return defaultFunc, nil
}
return nil, errors.New("not found function: " + name)
Expand Down Expand Up @@ -478,7 +478,13 @@ func (ce *Executor) call(target *LState) C.int {
return 0
}
if target == nil {
ce.jsonRet = C.GoString(C.vm_get_json_ret(ce.L, nret))
var errRet C.int
retMsg := C.GoString(C.vm_get_json_ret(ce.L, nret, &errRet))
if errRet == 1 {
ce.err = errors.New(retMsg)
} else {
ce.jsonRet = retMsg
}
} else {
C.luaL_disablemaxmem(target)
if cErrMsg := C.vm_copy_result(ce.L, target, nret); cErrMsg != nil {
Expand Down
2 changes: 1 addition & 1 deletion contract/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void vm_get_constructor(lua_State *L);
void vm_remove_constructor(lua_State *L);
const char *vm_loadbuff(lua_State *L, const char *code, size_t sz, char *hex_id, int *service);
const char *vm_pcall(lua_State *L, int argc, int* nresult);
const char *vm_get_json_ret(lua_State *L, int nresult);
const char *vm_get_json_ret(lua_State *L, int nresult, int *err);
const char *vm_copy_result(lua_State *L, lua_State *target, int cnt);
sqlite3 *vm_get_db(lua_State *L);
void vm_get_abi_function(lua_State *L, char *fname);
Expand Down
16 changes: 8 additions & 8 deletions contract/vm_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func addUpdateSize(s *StateSet, updateSize int64) error {
}

//export LuaSetDB
func LuaSetDB(L *LState, service *C.int, key *C.char, value *C.char) *C.char {
func LuaSetDB(L *LState, service *C.int, key unsafe.Pointer, keyLen C.int, value *C.char) *C.char {
stateSet := curStateSet[*service]
if stateSet == nil {
return C.CString("[System.LuaSetDB] contract state not found")
Expand All @@ -63,7 +63,7 @@ func LuaSetDB(L *LState, service *C.int, key *C.char, value *C.char) *C.char {
return C.CString("[System.LuaSetDB] set not permitted in query")
}
val := []byte(C.GoString(value))
if err := stateSet.curContract.callState.ctrState.SetData([]byte(C.GoString(key)), val); err != nil {
if err := stateSet.curContract.callState.ctrState.SetData(C.GoBytes(key, keyLen), val); err != nil {
return C.CString(err.Error())
}
if err := addUpdateSize(stateSet, int64(types.HashIDLength+len(val))); err != nil {
Expand All @@ -74,15 +74,15 @@ func LuaSetDB(L *LState, service *C.int, key *C.char, value *C.char) *C.char {
}

//export LuaGetDB
func LuaGetDB(L *LState, service *C.int, key *C.char, blkno *C.char) (*C.char, *C.char) {
func LuaGetDB(L *LState, service *C.int, key unsafe.Pointer, keyLen C.int, blkno *C.char) (*C.char, *C.char) {
stateSet := curStateSet[*service]
if stateSet == nil {
return nil, C.CString("[System.LuaGetDB] contract state not found")
}
if blkno != nil {
bigNo, _ := new(big.Int).SetString(strings.TrimSpace(C.GoString(blkno)), 10)
if bigNo == nil || bigNo.Sign() < 0 {
return nil, C.CString("[System.LuaGetDB] invalid blockheight value :"+C.GoString(blkno))
return nil, C.CString("[System.LuaGetDB] invalid blockheight value :" + C.GoString(blkno))
}
blkNo := bigNo.Uint64()

Expand All @@ -104,7 +104,7 @@ func LuaGetDB(L *LState, service *C.int, key *C.char, blkno *C.char) (*C.char, *
if err != nil {
return nil, C.CString("[System.LuaGetDB] failed to get snapshot state for account")
} else if contractProof.Inclusion {
trieKey := common.Hasher([]byte(C.GoString(key)))
trieKey := common.Hasher(C.GoBytes(key, keyLen))
varProof, err := stateSet.bs.GetVarAndProof(trieKey, contractProof.GetState().GetStorageRoot(), false)
if err != nil {
return nil, C.CString("[System.LuaGetDB] failed to get snapshot state variable in contract")
Expand All @@ -120,7 +120,7 @@ func LuaGetDB(L *LState, service *C.int, key *C.char, blkno *C.char) (*C.char, *
}
}

data, err := stateSet.curContract.callState.ctrState.GetData([]byte(C.GoString(key)))
data, err := stateSet.curContract.callState.ctrState.GetData(C.GoBytes(key, keyLen))
if err != nil {
return nil, C.CString(err.Error())
}
Expand All @@ -131,15 +131,15 @@ func LuaGetDB(L *LState, service *C.int, key *C.char, blkno *C.char) (*C.char, *
}

//export LuaDelDB
func LuaDelDB(L *LState, service *C.int, key *C.char) *C.char {
func LuaDelDB(L *LState, service *C.int, key unsafe.Pointer, keyLen C.int) *C.char {
stateSet := curStateSet[*service]
if stateSet == nil {
return C.CString("[System.LuaDelDB] contract state not found")
}
if stateSet.isQuery {
return C.CString("[System.LuaDelDB] delete not permitted in query")
}
if err := stateSet.curContract.callState.ctrState.DeleteData([]byte(C.GoString(key))); err != nil {
if err := stateSet.curContract.callState.ctrState.DeleteData(C.GoBytes(key, keyLen)); err != nil {
return C.CString(err.Error())
}
if err := addUpdateSize(stateSet, int64(32)); err != nil {
Expand Down
55 changes: 51 additions & 4 deletions contract/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1480,12 +1480,11 @@ abi.register(r)`
NewLuaTxDef("ktlee", "r", 0, definition),
tx,
)
if err != nil {
if err == nil {
t.Error(err)
}
receipt := bc.getReceipt(tx.hash())
if receipt.GetRet() != `nested table error` {
t.Errorf("contract Call ret error :%s", receipt.GetRet())
if err.Error() != `nested table error` {
t.Errorf("contract Call ret error :%s", err.Error())
}
}

Expand Down Expand Up @@ -2904,6 +2903,10 @@ abi.register(default)
if err != nil {
t.Error(err)
}
err = bc.Query("default", `{"Name":"a"}`, "not found function: a", "")
if err != nil {
t.Error(err)
}
}

func TestBignum(t *testing.T) {
Expand Down Expand Up @@ -4461,4 +4464,48 @@ func TestSnapshot(t *testing.T) {
t.Error(err)
}
}

func TestByteKey(t *testing.T) {
bk := `
state.var {
c = state.map(),
}
function constructor()
c[fromhex('00')] = "kk"
c[fromhex('61')] = "kk"
system.setItem(fromhex('00'), "kk")
end
function fromhex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
function get()
return c[fromhex('00')], system.getItem(fromhex('00')), system.getItem(fromhex('0000'))
end
function getcre()
return system.getCreator()
end
abi.register(get, getcre)
`
bc, _ := LoadDummyChain()
err := bc.ConnectBlock(
NewLuaTxAccount("ktlee", 100000),
NewLuaTxDef("ktlee", "bk", 0, bk),
)
if err != nil {
t.Error(err)
}
err = bc.Query("bk", `{"Name":"get"}`, "", `["kk","kk"]`)
if err != nil {
t.Error(err)
}
err = bc.Query("bk", `{"Name":"getcre"}`, "", `"Amg6nZWXKB6YpNgBPv9atcjdm6hnFvs5wMdRgb2e9DmaF5g9muF2"`)
if err != nil {
t.Error(err)
}
}

// end of test-cases

0 comments on commit bda5526

Please sign in to comment.