Commit 8cb966a4 authored by LiXiaoyu's avatar LiXiaoyu

process get kv failure

parent fb366bca
......@@ -4,33 +4,42 @@
#include "myContract.hpp"
myContract::myContract(eosio::account_name n, uint64_t h) : eosio::contract(n, h) {
//open();
}
myContract::myContract(eosio::account_name n, uint64_t h) : eosio::contract(n, h) {}
void
myContract::action1() {
std::string k = "key1";
std::string v = "value1";
set(k, v);
bool ret = set(k, v);
if(ret == true)
std::cout << "action1, set kv: key=" << k << " value=" << v << '\n';
else
std::cout << "action1, set kv failed: key=" << k << " value=" << v << '\n';
}
void
myContract::action2() {
std::string k = "key1";
std::string v;
get(k, v);
bool ret = get(k, v);
if(ret == true)
std::cout << "action2, get kv: key=" << k << " value=" << v << '\n';
else
std::cout << "action2 get kv failed: key=" << k << '\n';
}
void
myContract::action3(const std::vector<char> &key, const std::vector<char> &value) {
set(key, value);
bool ret = set(key, value);
if(ret != true)
std::cout << "action3, set kv failed\n";
}
void
bool
myContract::action4(const std::vector<char> &key, std::vector<char> &value) {
//todo failure
get(key, value);
bool ret = get(key, value);
if(ret != true)
return false;
//std::cout << "action4, get kv failed\n";
return true;
}
......@@ -18,7 +18,7 @@ public:
void action3(const std::vector<char> &key, const std::vector<char> &value);
void action4(const std::vector<char> &key, std::vector<char> &value);
bool action4(const std::vector<char> &key, std::vector<char> &value);
};
......@@ -28,22 +28,28 @@ func main() {
//action1
C.myContractAction1(c)
//action2
C.myContractAction2(c)
//action3
key := []byte{104, 101, 1, 108, 111}
//value := []byte{1, 2, 3, 4, 99, 6, 7, 8, 9, 0}
//C.myContractAction3(c, unsafe.Pointer(&key[0]), C.size_t(len(key)), unsafe.Pointer(&value[0]), C.size_t(len(value)))
//fmt.Println("action3, set kv: key =", key, " value =", value)
key := []byte{104, 101, 108, 108, 111}
value := []byte{1, 2, 3, 4, 99, 6, 7, 8, 9, 0}
C.myContractAction3(c, unsafe.Pointer(&key[0]), C.size_t(len(key)), unsafe.Pointer(&value[0]), C.size_t(len(value)))
fmt.Println("action3, set kv: key =", key, " value =", value)
//action4
var buf unsafe.Pointer
//pbuf := &buf
var buf_len C.size_t
C.myContractAction4(c, unsafe.Pointer(&key[0]), C.size_t(len(key)), &buf, &buf_len)
ret := C.myContractAction4(c, unsafe.Pointer(&key[0]), C.size_t(len(key)), &buf, &buf_len)
if ret == -1 {
fmt.Println("action4, get kv failed: key =", key)
} else {
bb := *(*[10]byte)(buf)
//fmt.Println(buf_len)
//fmt.Println(bb)
fmt.Println("action4, get kv: key =", key, " value =", bb)
C.free(buf)
}
//free
C.myContractFree(c)
......
......@@ -56,13 +56,15 @@ void myContractAction3(myContract_c c, const void* key, size_t key_len, const vo
con->action3(kk, vv);
}
void myContractAction4(myContract_c c, const void* key, size_t key_len, void** value, size_t* value_len) {
int myContractAction4(myContract_c c, const void* key, size_t key_len, void** value, size_t* value_len) {
//void myContractAction4(myContract_c c, const void* key, size_t key_len) {
myContract* con = reinterpret_cast<myContract*>(c);
const char *k = reinterpret_cast<const char*>(key);
std::vector<char> kk(k, k + key_len);
std::vector<char> vv;
con->action4(kk, vv);
bool ret = con->action4(kk, vv);
if(ret != true)
return -1;
/*
for(auto e : vv) {
std::cout << static_cast<int>(e) << ' ';
......@@ -70,8 +72,10 @@ void myContractAction4(myContract_c c, const void* key, size_t key_len, void** v
std::cout << '\n';
*/
*value_len = vv.size();
*value = new char[vv.size()];
//*value = new char[vv.size()];
*value = malloc(vv.size());
std::copy(vv.begin(), vv.end(), reinterpret_cast<char*>(*value));
return 0;
}
......@@ -19,7 +19,7 @@ void myContractFree(myContract_c);
void myContractAction1(myContract_c);
void myContractAction2(myContract_c);
void myContractAction3(myContract_c c, const void* key, size_t key_len, const void* value, size_t value_len);
void myContractAction4(myContract_c c, const void* key, size_t key_len, void** value, size_t* value_len);
int myContractAction4(myContract_c c, const void* key, size_t key_len, void** value, size_t* value_len);
//void myContractAction4(myContract_c c, const void* key, size_t key_len);
#ifdef __cplusplus
......
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