You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

transaction.md 10 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. ## 交易
  2. ### 1. 请求
  3. > `JD Chain`限制单笔交易数据大小不得超过 `4M`
  4. ```java
  5. /**
  6. * 交易请求;
  7. */
  8. @DataContract(code= DataCodes.TX_REQUEST)
  9. public interface TransactionRequest {
  10. /**
  11. * 交易哈希;
  12. */
  13. @DataField(order = 1, primitiveType = PrimitiveType.BYTES)
  14. HashDigest getTransactionHash();
  15. /**
  16. * 交易内容;
  17. */
  18. @DataField(order = 2, refContract = true)
  19. TransactionContent getTransactionContent();
  20. /**
  21. * 终端用户的签名列表;
  22. */
  23. @DataField(order = 3, list = true, refContract = true)
  24. DigitalSignature[] getEndpointSignatures();
  25. /**
  26. * 接入交易的节点的签名;
  27. */
  28. @DataField(order=4, list=true, refContract=true)
  29. DigitalSignature[] getNodeSignatures();
  30. }
  31. ```
  32. 交易内容:
  33. ```java
  34. /**
  35. * 交易内容;
  36. */
  37. @DataContract(code = DataCodes.TX_CONTENT)
  38. public interface TransactionContent {
  39. /**
  40. * 执行交易的账本地址;
  41. *
  42. * 注:除了账本的创世交易之外,任何交易的账本地址都不允许为 null;
  43. */
  44. @DataField(order = 1, primitiveType = PrimitiveType.BYTES)
  45. HashDigest getLedgerHash();
  46. /**
  47. * 操作列表;
  48. */
  49. @DataField(order = 2, list = true, refContract = true, genericContract = true)
  50. Operation[] getOperations();
  51. /**
  52. * 生成交易的时间;<br>
  53. * 以毫秒为单位,表示距离 1970-1-1 00:00:00 (UTC) 的毫秒数;<br>
  54. */
  55. @DataField(order = 3, primitiveType = PrimitiveType.INT64)
  56. long getTimestamp();
  57. }
  58. ```
  59. ### 2. 操作
  60. `Operation`接口实现类均为`JD Chain`交易中支持的操作类型
  61. #### 2.1 共识信息变更
  62. ```java
  63. @DataContract(code= DataCodes.TX_OP_CONSENSUS_SETTINGS_UPDATE)
  64. public interface ConsensusSettingsUpdateOperation extends Operation{
  65. /**
  66. * 配置列表
  67. */
  68. @DataField(order = 0, primitiveType = PrimitiveType.BYTES, list = true)
  69. Property[] getProperties();
  70. }
  71. ```
  72. #### 2.2 合约部署
  73. ```java
  74. @DataContract(code= DataCodes.TX_OP_CONTRACT_DEPLOY)
  75. public interface ContractCodeDeployOperation extends Operation {
  76. /**
  77. * 合约账户信息
  78. */
  79. @DataField(order=2, refContract = true)
  80. BlockchainIdentity getContractID();
  81. /**
  82. * 合约代码字节
  83. */
  84. @DataField(order=3, primitiveType=PrimitiveType.BYTES)
  85. byte[] getChainCode();
  86. /**
  87. * 合约版本
  88. */
  89. @DataField(order=5, primitiveType=PrimitiveType.INT64)
  90. long getChainCodeVersion();
  91. }
  92. ```
  93. #### 2.3 合约调用
  94. ```java
  95. @DataContract(code = DataCodes.TX_OP_CONTRACT_EVENT_SEND)
  96. public interface ContractEventSendOperation extends Operation {
  97. /**
  98. * 合约地址;
  99. */
  100. @DataField(order = 2, primitiveType = PrimitiveType.BYTES)
  101. Bytes getContractAddress();
  102. /**
  103. * 合约方法名;
  104. *
  105. * @return
  106. */
  107. @DataField(order = 3, primitiveType = PrimitiveType.TEXT)
  108. String getEvent();
  109. /**
  110. * 合约方法调用参数;
  111. *
  112. * @return
  113. */
  114. @DataField(order = 4, refContract = true)
  115. BytesValueList getArgs();
  116. /**
  117. * 合约版本;
  118. */
  119. @DataField(order = 5, primitiveType = PrimitiveType.INT64)
  120. long getVersion();
  121. }
  122. ```
  123. #### 2.4 注册数据账户
  124. ```java
  125. @DataContract(code= DataCodes.TX_OP_DATA_ACC_REG)
  126. public interface DataAccountRegisterOperation extends Operation {
  127. /**
  128. * 数据账户信息;
  129. */
  130. @DataField(order=1, refContract = true)
  131. BlockchainIdentity getAccountID();
  132. }
  133. ```
  134. #### 2.5 写KV操作
  135. ```java
  136. @DataContract(code= DataCodes.TX_OP_DATA_ACC_SET)
  137. public interface DataAccountKVSetOperation extends Operation {
  138. /**
  139. * 数据账户
  140. */
  141. @DataField(order=2, primitiveType=PrimitiveType.BYTES)
  142. Bytes getAccountAddress();
  143. /**
  144. * KV列表
  145. */
  146. @DataField(order=3, list=true, refContract=true)
  147. KVWriteEntry[] getWriteSet();
  148. @DataContract(code=DataCodes.TX_OP_DATA_ACC_SET_KV)
  149. public static interface KVWriteEntry{
  150. @DataField(order=1, primitiveType=PrimitiveType.TEXT)
  151. String getKey();
  152. @DataField(order=2, refContract = true)
  153. BytesValue getValue();
  154. @DataField(order=3, primitiveType=PrimitiveType.INT64)
  155. long getExpectedVersion();
  156. }
  157. }
  158. ```
  159. #### 2.6 注册事件账户
  160. ```java
  161. @DataContract(code = DataCodes.TX_OP_EVENT_ACC_REG)
  162. public interface EventAccountRegisterOperation extends Operation {
  163. @DataField(order = 2, refContract = true)
  164. BlockchainIdentity getEventAccountID();
  165. }
  166. ```
  167. #### 2.7 发布事件
  168. ```java
  169. @DataContract(code = DataCodes.TX_OP_EVENT_PUBLISH)
  170. public interface EventPublishOperation extends Operation {
  171. /**
  172. * 事件地址
  173. */
  174. @DataField(order = 1, primitiveType = PrimitiveType.BYTES)
  175. Bytes getEventAddress();
  176. /**
  177. * 事件列表
  178. */
  179. @DataField(order = 2, list = true, refContract = true)
  180. EventEntry[] getEvents();
  181. @DataContract(code = DataCodes.TX_OP_EVENT_PUBLISH_ENTITY)
  182. interface EventEntry {
  183. @DataField(order = 1, primitiveType = PrimitiveType.TEXT)
  184. String getName();
  185. @DataField(order = 2, refContract = true)
  186. BytesValue getContent();
  187. @DataField(order = 3, primitiveType = PrimitiveType.INT64)
  188. long getSequence();
  189. }
  190. }
  191. ```
  192. #### 2.8 账本初始化
  193. ```java
  194. @DataContract(code= DataCodes.TX_OP_LEDGER_INIT)
  195. public interface LedgerInitOperation extends Operation{
  196. /**
  197. * 账本初始化配置
  198. */
  199. @DataField(order=1, refContract=true)
  200. LedgerInitSetting getInitSetting();
  201. }
  202. ```
  203. #### 2.9 参与方状态变更
  204. ```java
  205. @DataContract(code= DataCodes.TX_OP_PARTICIPANT_STATE_UPDATE)
  206. public interface ParticipantStateUpdateOperation extends Operation {
  207. /**
  208. * 参与方身份
  209. */
  210. @DataField(order = 0, refContract = true)
  211. BlockchainIdentity getParticipantID();
  212. /**
  213. * 新状态
  214. */
  215. @DataField(order = 1, refEnum = true)
  216. ParticipantNodeState getState();
  217. }
  218. ```
  219. #### 2.10 角色赋权
  220. ```java
  221. @DataContract(code = DataCodes.TX_OP_ROLE_CONFIGURE)
  222. public interface RolesConfigureOperation extends Operation {
  223. /**
  224. * 角色权限列表
  225. */
  226. @DataField(order = 2, refContract = true, list = true)
  227. RolePrivilegeEntry[] getRoles();
  228. @DataContract(code = DataCodes.TX_OP_ROLE_CONFIGURE_ENTRY)
  229. public static interface RolePrivilegeEntry {
  230. /**
  231. * 角色名
  232. */
  233. @DataField(order = 1, primitiveType = PrimitiveType.TEXT)
  234. String getRoleName();
  235. /**
  236. * 开启的账本权限列表
  237. */
  238. @DataField(order = 2, refEnum = true, list = true)
  239. LedgerPermission[] getEnableLedgerPermissions();
  240. /**
  241. * 关闭的账本权限列表
  242. */
  243. @DataField(order = 3, refEnum = true, list = true)
  244. LedgerPermission[] getDisableLedgerPermissions();
  245. /**
  246. * 开启的交易权限列表
  247. */
  248. @DataField(order = 4, refEnum = true, list = true)
  249. TransactionPermission[] getEnableTransactionPermissions();
  250. /**
  251. * 关闭的交易权限列表
  252. */
  253. @DataField(order = 5, refEnum = true, list = true)
  254. TransactionPermission[] getDisableTransactionPermissions();
  255. }
  256. }
  257. ```
  258. #### 2.11 用户赋权
  259. ```java
  260. @DataContract(code = DataCodes.TX_OP_USER_ROLES_AUTHORIZE)
  261. public interface UserAuthorizeOperation extends Operation {
  262. /**
  263. * 用户角色列表
  264. */
  265. @DataField(order = 2, refContract = true, list = true)
  266. UserRolesEntry[] getUserRolesAuthorizations();
  267. @DataContract(code = DataCodes.TX_OP_USER_ROLE_AUTHORIZE_ENTRY)
  268. public static interface UserRolesEntry {
  269. /**
  270. * 用户地址;
  271. */
  272. @DataField(order = 0, primitiveType = PrimitiveType.BYTES, list = true)
  273. Bytes[] getUserAddresses();
  274. /**
  275. * 要更新的多角色权限策略;
  276. */
  277. @DataField(order = 2, refEnum = true)
  278. RolesPolicy getPolicy();
  279. /**
  280. * 授权的角色清单;
  281. */
  282. @DataField(order = 3, primitiveType = PrimitiveType.TEXT, list = true)
  283. String[] getAuthorizedRoles();
  284. /**
  285. * 取消授权的角色清单;
  286. */
  287. @DataField(order = 4, primitiveType = PrimitiveType.TEXT, list = true)
  288. String[] getUnauthorizedRoles();
  289. }
  290. }
  291. ```
  292. #### 2.12 注册用户
  293. ```java
  294. @DataContract(code = DataCodes.TX_OP_USER_REG)
  295. public interface UserRegisterOperation extends Operation {
  296. /**
  297. * 用户身份信息
  298. */
  299. @DataField(order = 2, refContract = true)
  300. BlockchainIdentity getUserID();
  301. }
  302. ```
  303. ### 3. 结果
  304. 交易执行结果数据结构如下:
  305. ```java
  306. @DataContract(code = DataCodes.TX_RESULT)
  307. public interface TransactionResult {
  308. /**
  309. * 交易哈希;
  310. */
  311. @DataField(order = 1, primitiveType = PrimitiveType.BYTES)
  312. HashDigest getTransactionHash();
  313. /**
  314. * 交易被包含的区块高度;
  315. */
  316. @DataField(order = 2, primitiveType = PrimitiveType.INT64)
  317. long getBlockHeight();
  318. /**
  319. * 交易的执行结果;
  320. */
  321. @DataField(order = 3, refEnum = true)
  322. TransactionState getExecutionState();
  323. /**
  324. * 交易中操作的返回结果;顺序与操作列表的顺序一致;
  325. */
  326. @DataField(order = 4, list = true, refContract = true)
  327. OperationResult[] getOperationResults();
  328. /**
  329. * 账本数据快照;
  330. */
  331. @DataField(order = 5, refContract = true)
  332. LedgerDataSnapshot getDataSnapshot();
  333. }
  334. ```
  335. ### 4. 查询
  336. `SDK`查询交易详情数据使用`LedgerTransaction`接口实现
  337. #### 4.1 结构
  338. ```java
  339. @DataContract(code = DataCodes.TX_RECORD)
  340. public interface LedgerTransaction {
  341. /**
  342. * 交易请求;
  343. */
  344. @DataField(order = 1, refContract = true)
  345. TransactionRequest getRequest();
  346. /**
  347. * 交易结果;
  348. */
  349. @DataField(order = 2, refContract = true)
  350. TransactionResult getResult();
  351. }
  352. ```
  353. #### 4.2 解析
  354. - 成功/失败:
  355. ```java
  356. getResult().getExecutionState();
  357. ```
  358. `TransactionState.SUCCESS`为成功,其他失败。
  359. - 操作解析
  360. ```java
  361. for(Operation operation : tx.getRequest().getTransactionContent().getOperations()) {
  362. // 注册用户
  363. if(operation instanceof UserRegisterOperation) {
  364. UserRegisterOperation userRegisterOperation = (UserRegisterOperation) operation;
  365. // ...
  366. // 注册数据账户
  367. } else if(operation instanceof DataAccountRegisterOperation) {
  368. DataAccountRegisterOperation dataAccountRegisterOperation = (DataAccountRegisterOperation) operation;
  369. // ...
  370. } // ...
  371. }
  372. ```
  373. 上诉仅以注册用户/注册数据账户为例,其他操作类型嗯参照[所有操作类型](#2-操作)进行解析。