函数概览
本文罗列了合约开发可调用的函数及其功能,具体函数使用信息可参见相关文档。
蚂蚁区块链合约平台在 Solidity 的基础上,添加了部分函数库的支持,包括对 JSON/XML 数据格式的构造和解析,采用预编译合约的方式加入到蚂蚁区块链合约平台中,其使用方式如下所示:
pragma solidity ^0.4.23;
contract test_parse {
// test json interface
function test_property_parse_json() public returns (uint)
{
string memory property_value;
int property_type;
property_value = "{\"node_config\" : {\"identity\" : \"true\",\"key_path\" : \"../../user\",\"user_keys\" : [{\"key_name\" : \"Tester001.key\",\"key_passwd\" : \"123456a\"},{\"key_name\" : \"Tester002.key\",\"key_passwd\" : \"123456a\"}],\"node\" : {\"ip\" : \"127.0.0.1\",\"port\" : 18130}}}";
property_type = 0;
// 第一步调用 property_parse
uint handler = property_parse(property_value, property_type);
// 然后获取 JSON 属性值;
test_property_get_bool(handler);
test_property_get_int(handler);
test_property_get_uint(handler);
test_property_get_string(handler);
test_property_get_array(handler);
test_property_get_list_count(handler);
// 最后释放资源
test_property_destroy(handler);
return handler;
}
// test xml interface
function test_property_parse_xml() public returns (uint)
{
string memory property_value;
int property_type;
property_value = "<?xml version=\"1.0\" encoding=\"utf-8\"?><node_config key_path=\"../../user\"> <![CDATA[cdata]]><identity>true</identity><user_keys> <key_name>Tester001.key</key_name> <key_passwd>123456a</key_passwd> </user_keys> <user_keys> <key_name>Tester002.key</key_name> <key_passwd>123456a</key_passwd> </user_keys> <node> <ip>127.0.0.1</ip> <port>18130</port> </node> </node_config>";
property_type = 1;
// 第一步调用 property_parse
uint handler = property_parse(property_value, property_type);
// 然后获取 XML 属性值;
test_property_get_bool(handler);
test_property_get_int(handler);
test_property_get_uint(handler);
test_property_get_string(handler);
test_property_get_array(handler);
test_property_get_cdata(handler);
test_property_get_attr(handler);
test_property_get_list_count(handler);
// 最后释放资源
test_property_destroy(handler);
return handler;
}
function test_property_destroy(uint hanlder) public returns (bool)
{
return property_destroy(hanlder);
}
function test_property_get_bool(uint hanlder) public returns (int, bool)
{
string memory path;
path = "node_config.identity";
int err;
bool ret;
(err, ret) = property_get_bool(hanlder, path);
return (err, ret);
}
function test_property_get_int(uint hanlder) public returns (int, int)
{
string memory path;
path = "node_config.node.port";
int err;
int ret;
(err, ret) = property_get_int(hanlder, path);
return (err, ret);
}
function test_property_get_uint(uint hanlder) public returns (int, uint)
{
string memory path;
path = "node_config.node.port";
int err;
uint ret;
(err, ret) = property_get_uint(hanlder, path);
return (err, ret);
}
function test_property_get_string(uint hanlder) public returns (int, string memory)
{
string memory path;
path = "node_config.node.ip";
int err;
string memory ret;
(err, ret) = property_get_string(hanlder, path);
return (err, ret);
}
function test_property_get_array(uint hanlder) public returns (int, string memory)
{
// string memory path;
// path = "node_config.user_keys[0].key_name";
int err;
string memory ret;
(err, ret) = property_get_string(hanlder, "node_config.user_keys[0].key_name");
return (err, ret);
}
function test_property_get_list_count(uint hanlder) public returns (uint) {
return property_get_list_count(hanlder, "node_config.user_keys");
}
function test_property_get_cdata(uint hanlder) public returns (int, string memory)
{
string memory path;
path = "node_config.<xmlcdata>";
int err;
string memory ret;
(err, ret) = property_get_string(hanlder, path);
return (err, ret);
}
function test_property_get_attr(uint hanlder) public returns (int, string memory)
{
string memory path;
path = "node_config.<xmlattr>.key_path";
int err;
string memory ret;
(err, ret) = property_get_string(hanlder, path);
return (err, ret);
}
}
contract test_write {
///////test this interface only
function test_property_write() public returns (string memory)
{
string memory property_value;
int property_type = 0;
// 第一步调用 property_parse
uint handler = property_parse(property_value, property_type);
// 然后设置各属性值
test_property_set_bool(handler);
test_property_set_int(handler);
test_property_set_uint(handler);
test_property_set_string(handler);
property_type = 1;
// 生成 xml/json
string memory ret = property_write(handler, property_type);
property_destroy(handler);
return ret;
}
function test_property_set_bool(uint handler) public returns (bool)
{
string memory path;
path = "a.b.c";
return property_set_bool(handler, path, true);
}
function test_property_set_int(uint handler) public returns (bool)
{
string memory path;
path = "e.f.g.h";
return property_set_int(handler, path, 0x12);
}
function test_property_set_uint(uint handler) public returns (bool)
{
string memory path;
path = "h.m.l";
return property_set_uint(handler, path, 1);
}
function test_property_set_string(uint handler) public returns (bool)
{
string memory path;
path = "a.b.g";
string memory val;
val = "testhahahaha";
return property_set_string(handler, path, val);
}
function test_property_set_cdata(uint handler) public returns (bool)
{
string memory path;
path = "zz.<xmlcdata>";
string memory val;
val = "cdata";
return property_set_string(handler, path, val);
}
function test_property_set_attr(uint handler) public returns (bool)
{
string memory path;
path = "zz.<xmlattr>.attr";
string memory val;
val = "attr";
return property_set_string(handler, path, val);
}
function test_property_remove(uint handler) public returns (bool)
{
string memory path;
path = "a.b.g";
return property_remove(handler, path);
}
}