Commit aa7b2750 authored by 袁兴强's avatar 袁兴强 Committed by vipwzw

update evidence contract

parent f1f72ca2
......@@ -3,9 +3,8 @@
./chain33-cli send wasm create -n evidence -p evidence合约路径 -k 用户私钥
# 调用合约
# -p 0 表示传给set方法的参数
# -v "string1","string2","string3" 表示设置三个环境变量,可以在合约内部分别调用getENV(0), getENV(1), getENV(2)读取
# -v "string1","string2" 表示设置2个环境变量,可以在合约内部分别调用getENV(0), getENV(1)读取
./chain33-cli send wasm call -n evidence -m set -p 0 -v "string1","string2","string3" -k 用户私钥
./chain33-cli send wasm call -n evidence -m AddStateTx -v "string1","string2" -k 用户私钥
```
#include "../common.h"
#include "evidence.hpp"
#include <string.h>
int set(int64_t n) {
size_t l = getENVSize(n);
//暂不支持动态数组 char* value = new char[l]; 给定静态数组长度表示字符串长度上限
char value[1024] = {0};
getENV(n, value, l);
printlog(value, l);
return 0;
}
\ No newline at end of file
#include "../common.h"
#include "evidence.hpp"
#include <string.h>
#define SUCC 0
//----------------------------------------------------------------------------------------------
//-m AddStateTx -v "TestKey001","TestValue001"
int AddStateTx()
{
if(totalENV() != 2) return -1;
char ChKey[128] = {0};
char ChValue[128] = {0};
//环境变量索引是从0开始的,0,1,2,3...
getENV(0, ChKey, getENVSize(0));
getENV(1, ChValue, getENVSize(1));
if (string_size(ChKey) == 0) return -1;
//1.检查存在这样的记录
if (getStateDBSize(ChKey, string_size(ChKey)) != 0) return -1;
//2.插入到链上数据库
setStateDB(ChKey, string_size(ChKey), ChValue, string_size(ChValue));
return SUCC;
}
//----------------------------------------------------------------------------------------------
int DelStateTx()
{
if(totalENV() !=1) return -1;
//1.从环境变量中获取变量值
char ChKey[128] = {0};
getENV(0, ChKey, getENVSize(0));
if (string_size(ChKey) == 0) return -1;
//2.获取对应的记录
if(getStateDBSize(ChKey, string_size(ChKey)) == 0) {
const char info[128] = "DelStateTx::getStateDBSize Not Exists\0";
printlog(info, string_size(info));
return -1;
}
//size_t getStateDB(const char* key, size_t k_len, char* value, size_t v_len);
char ChValue[128] = {0};
getStateDB(ChKey, string_size(ChKey), ChValue, sizeof(ChValue));
if(string_size(ChValue) == 0) {
//记录存在,但是之前删除过了
const char info[128] = "DelStateTx::getStateDB Has Been Deleted\0";
printlog(info, string_size(info));
return -1;
}
char ChNull[128] = {0};
setStateDB(ChKey, string_size(ChKey), ChNull, string_size(ChNull));
const char info[128] = "DelStateTx::Exec setStateDB Del OK\0";
printlog(info, string_size(info));
return SUCC;
}
//----------------------------------------------------------------------------------------------
int ModStateTx()
{
if(totalENV() != 2) return -1;
char ChKey[128] = {0};
char ChValue[128] = {0};
getENV(0, ChKey, getENVSize(0));
getENV(1, ChValue, getENVSize(1));
if ((string_size(ChKey) == 0) || (string_size(ChValue) == 0)) return -1;
//查询旧值,非空为存在旧值,为空则表示这个记录已经被删除过
if(getStateDBSize(ChKey, string_size(ChKey)) == 0) {
const char info[128] = "ModStateTx::getStateDBSize failed\0";
printlog(info, string_size(info));
return -1;
}
//在判断这条记录value是否为空,如果是则为已经被删除的记录,如此修改操作不允许
char value[128] = {0};
getStateDB(ChKey, string_size(ChKey), value, sizeof(value));
if(string_size(value) == 0) {
const char info[128] = "ModStateTx::getStateDB Has Been Deleted\0";
printlog(info, string_size(info));
return -1;
}
setStateDB(ChKey, string_size(ChKey), ChValue, string_size(ChValue));
const char info[128] = "ModStateTx::Exec setStateDB Update OK\0";
printlog(info, string_size(info));
return SUCC;
}
//----------------------------------------------------------------------------------------------
int AddLocalTx()
{
if(2 != totalENV()) return -1;
char ChKey[128] = {0};
char ChValue[128] = {0};
getENV(0, ChKey, getENVSize(0));
getENV(1, ChValue, getENVSize(1));
if (string_size(ChKey) == 0) return -1;
//1.检查存在这样的记录
if (getLocalDBSize(ChKey, string_size(ChKey)) != 0) return -1;
//2.存储本地数据
setLocalDB(ChKey, string_size(ChKey), ChValue, string_size(ChValue));
return SUCC;
}
//----------------------------------------------------------------------------------------------
int DelLocalTx()
{
if(totalENV() != 1) return -1;
char ChKey[128] = {0};
char szValue[128] = {0};
getENV(0, ChKey, getENVSize(0));
if (string_size(ChKey) == 0) return -1;
if(getLocalDBSize(ChKey, string_size(ChKey)) == 0) {
const char info[128] = "DelLocalTx::getLocalDBSize Length is 0\0";
printlog(info, string_size(info));
return -1;
}
char ChValue[128] = {0};
getLocalDB(ChKey, string_size(ChKey), ChValue, sizeof(ChValue));
if(string_size(ChValue) == 0) {
//记录存在,但是之前删除过了
const char info[128] = "DelLocalTx::getLocalDB Has Been Deleted\0";
printlog(info, string_size(info));
return -1;
}
char ChNull[128] = {0};
setLocalDB(ChKey, string_size(ChKey), ChNull, string_size(ChNull));
const char info[128] = "DelLocalTx::Exec Delete OK\0";
printlog(info, string_size(info));
return SUCC;
}
//----------------------------------------------------------------------------------------------
int ModLocalTx()
{
if(totalENV() != 2) return -1;
char ChKey[128] = {0};
char ChValue[128] = {0};
getENV(0, ChKey, getENVSize(0));
getENV(1, ChValue, getENVSize(1));
if (string_size(ChKey) == 0) return -1;
//1.检查数据记录是否存在
if(getLocalDBSize(ChKey, string_size(ChKey)) == 0) {
const char info[128] = "ModLocalTx::getLocalDBSize Not Exist\0";
printlog(info, string_size(info));
return -1;
}
char value[128] = {0};
getLocalDB(ChKey, string_size(ChKey), value, sizeof(value));
if(string_size(value) == 0) {
const char info[128] = "ModLocalTx::getLocalDB Has Been Deleted\0";
printlog(info, string_size(info));
return -1;
}
setLocalDB(ChKey, string_size(ChKey), ChValue, string_size(ChValue));
const char info[128] = "ModLocalTx::Exec setLocalDB Update OK\0";
printlog(info, string_size(info));
return SUCC;
}
//----------------------------------------------------------------------------------------------
#ifdef __cplusplus //而这一部分就是告诉编译器,如果定义了__cplusplus(即如果是cpp文件,
extern "C" { //因为cpp文件默认定义了该宏),则采用C语言方式进行编译
#endif
int set(int64_t n);
#ifdef __cplusplus
}
#endif
\ No newline at end of file
#ifdef __cplusplus //而这一部分就是告诉编译器,如果定义了__cplusplus(即如果是cpp文件,
extern "C" { //因为cpp文件默认定义了该宏),则采用C语言方式进行编译
#endif
/**合约开发
新建 cpp 和 hpp 文件,并导入 common.h 头文件,其中 common.h 中声明了 chain33 中的回调函数,是合约调用 chain33 系统方法的接口。
合约中的导出方法的所有参数都只能是数字类型,且必须有一个数字类型的返回值,其中非负值表示执行成功,负值表示执行失败。 */
//1.调用上链数据操作接口
int AddStateTx();
int ModStateTx();
int DelStateTx();
//2.调用本地数据操作接口
int AddLocalTx();
int ModLocalTx();
int DelLocalTx();
#ifdef __cplusplus
}
#endif
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