All Products
Search
Document Center

Function Overview

Last Updated: Jan 09, 2020

This topic describes functions that can be called during contract development and their features.

In addition to Solidity, the Ant Blockchain contract platform supports some function libraries, and can both construct and parse JSON- or XML-formatted data. These libraries are incorporated into the platform through contract precompilation and enable the following operations:

  1. pragma solidity ^0.4.23;
  2. contract test_parse {
  3. // test json interface
  4. function test_property_parse_json() public returns (uint)
  5. {
  6. string memory property_value;
  7. int property_type;
  8. 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}}}";
  9. property_type = 0;
  10. // Call property_parse.
  11. uint handler = property_parse(property_value, property_type);
  12. // Obtain JSON-formatted data properties.
  13. test_property_get_bool(handler);
  14. test_property_get_int(handler);
  15. test_property_get_uint(handler);
  16. test_property_get_string(handler);
  17. test_property_get_array(handler);
  18. test_property_get_list_count(handler);
  19. // Release resources.
  20. test_property_destroy(handler);
  21. return handler;
  22. }
  23. // test xml interface
  24. function test_property_parse_xml() public returns (uint)
  25. {
  26. string memory property_value;
  27. int property_type;
  28. 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>";
  29. property_type = 1;
  30. // Call property_parse.
  31. uint handler = property_parse(property_value, property_type);
  32. // Obtain XML-formatted data properties.
  33. test_property_get_bool(handler);
  34. test_property_get_int(handler);
  35. test_property_get_uint(handler);
  36. test_property_get_string(handler);
  37. test_property_get_array(handler);
  38. test_property_get_cdata(handler);
  39. test_property_get_attr(handler);
  40. test_property_get_list_count(handler);
  41. // Release resources.
  42. test_property_destroy(handler);
  43. return handler;
  44. }
  45. function test_property_destroy(uint hanlder) public returns (bool)
  46. {
  47. return property_destroy(hanlder);
  48. }
  49. function test_property_get_bool(uint hanlder) public returns (int, bool)
  50. {
  51. string memory path;
  52. path = "node_config.identity";
  53. int err;
  54. bool ret;
  55. (err, ret) = property_get_bool(hanlder, path);
  56. return (err, ret);
  57. }
  58. function test_property_get_int(uint hanlder) public returns (int, int)
  59. {
  60. string memory path;
  61. path = "node_config.node.port";
  62. int err;
  63. int ret;
  64. (err, ret) = property_get_int(hanlder, path);
  65. return (err, ret);
  66. }
  67. function test_property_get_uint(uint hanlder) public returns (int, uint)
  68. {
  69. string memory path;
  70. path = "node_config.node.port";
  71. int err;
  72. uint ret;
  73. (err, ret) = property_get_uint(hanlder, path);
  74. return (err, ret);
  75. }
  76. function test_property_get_string(uint hanlder) public returns (int, string memory)
  77. {
  78. string memory path;
  79. path = "node_config.node.ip";
  80. int err;
  81. string memory ret;
  82. (err, ret) = property_get_string(hanlder, path);
  83. return (err, ret);
  84. }
  85. function test_property_get_array(uint hanlder) public returns (int, string memory)
  86. {
  87. // string memory path;
  88. // path = "node_config.user_keys[0].key_name";
  89. int err;
  90. string memory ret;
  91. (err, ret) = property_get_string(hanlder, "node_config.user_keys[0].key_name");
  92. return (err, ret);
  93. }
  94. function test_property_get_list_count(uint hanlder) public returns (uint) {
  95. return property_get_list_count(hanlder, "node_config.user_keys");
  96. }
  97. function test_property_get_cdata(uint hanlder) public returns (int, string memory)
  98. {
  99. string memory path;
  100. path = "node_config.<xmlcdata>";
  101. int err;
  102. string memory ret;
  103. (err, ret) = property_get_string(hanlder, path);
  104. return (err, ret);
  105. }
  106. function test_property_get_attr(uint hanlder) public returns (int, string memory)
  107. {
  108. string memory path;
  109. path = "node_config.<xmlattr>.key_path";
  110. int err;
  111. string memory ret;
  112. (err, ret) = property_get_string(hanlder, path);
  113. return (err, ret);
  114. }
  115. }
  116. contract test_write {
  117. ///////test this interface only
  118. function test_property_write() public returns (string memory)
  119. {
  120. string memory property_value;
  121. int property_type = 0;
  122. // Call property_parse.
  123. uint handler = property_parse(property_value, property_type);
  124. // Set properties.
  125. test_property_set_bool(handler);
  126. test_property_set_int(handler);
  127. test_property_set_uint(handler);
  128. test_property_set_string(handler);
  129. property_type = 1;
  130. // Generate JSON- or XML-formatted data.
  131. string memory ret = property_write(handler, property_type);
  132. property_destroy(handler);
  133. return ret;
  134. }
  135. function test_property_set_bool(uint handler) public returns (bool)
  136. {
  137. string memory path;
  138. path = "a.b.c";
  139. return property_set_bool(handler, path, true);
  140. }
  141. function test_property_set_int(uint handler) public returns (bool)
  142. {
  143. string memory path;
  144. path = "e.f.g.h";
  145. return property_set_int(handler, path, 0x12);
  146. }
  147. function test_property_set_uint(uint handler) public returns (bool)
  148. {
  149. string memory path;
  150. path = "h.m.l";
  151. return property_set_uint(handler, path, 1);
  152. }
  153. function test_property_set_string(uint handler) public returns (bool)
  154. {
  155. string memory path;
  156. path = "a.b.g";
  157. string memory val;
  158. val = "testhahahaha";
  159. return property_set_string(handler, path, val);
  160. }
  161. function test_property_set_cdata(uint handler) public returns (bool)
  162. {
  163. string memory path;
  164. path = "zz.<xmlcdata>";
  165. string memory val;
  166. val = "cdata";
  167. return property_set_string(handler, path, val);
  168. }
  169. function test_property_set_attr(uint handler) public returns (bool)
  170. {
  171. string memory path;
  172. path = "zz.<xmlattr>.attr";
  173. string memory val;
  174. val = "attr";
  175. return property_set_string(handler, path, val);
  176. }
  177. function test_property_remove(uint handler) public returns (bool)
  178. {
  179. string memory path;
  180. path = "a.b.g";
  181. return property_remove(handler, path);
  182. }
  183. }