From 3b32c96d7d2c82a5593c26611d39dc8615950422 Mon Sep 17 00:00:00 2001 From: yangzhenlong Date: Tue, 27 Aug 2019 15:25:07 +0800 Subject: [PATCH] add unit testing of newGroup interface --- .../protocol/system/CITAjSystemContract.java | 44 +++++++++++++++++++ .../cita/tests/GroupManagerExample.java | 18 +++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/cryptape/cita/protocol/system/CITAjSystemContract.java b/core/src/main/java/com/cryptape/cita/protocol/system/CITAjSystemContract.java index 6b4f2613..1da4586b 100644 --- a/core/src/main/java/com/cryptape/cita/protocol/system/CITAjSystemContract.java +++ b/core/src/main/java/com/cryptape/cita/protocol/system/CITAjSystemContract.java @@ -601,6 +601,50 @@ public List queryGroups(String senderAddress) throws IOException { return list; } + /** + * new group + * @see newGroup + * @param superAdminAddress the address of super_admin + * @param groupName the name of group to be created + * @param accounts accounts added to the group + * @param adminPrivateKey the private key of super_admin + * @param version + * @param chainId + * @return the transaction hash for creating group + * @throws IOException + */ + public String newGroup(String superAdminAddress, + String groupName, + List accounts, + String adminPrivateKey, + int version, + BigInteger chainId) throws IOException { + // account addresses convert to Address object list + List
addresses = new ArrayList<>(accounts.size()); + for(String acc : accounts){ + addresses.add(new Address(acc)); + } + + // groupName string convert to bytes32 + String nameHex = Util.addUpTo64Hex(ConvertStrByte.stringToHexString(groupName)); + byte[] nameBytes = ConvertStrByte.hexStringToBytes(Numeric.cleanHexPrefix(nameHex)); + + // build input parameters + List inputParameters = Arrays.asList( + new Address(superAdminAddress),//origin + new Bytes32(nameBytes),//name + new DynamicArray
(addresses)//account + ); + + // encode input parameters + String funcData = CITASystemContract.encodeFunction(USER_MANAGER_NEW_GROUP, inputParameters); + + // send request to create group and return transaction hash + String txHash = CITASystemContract.sendTxAndGetHash( + USER_MANAGER_ADDR, service, adminPrivateKey, funcData, version, chainId); + return txHash; + } + public Transaction constructStoreTransaction(String data, int version, BigInteger chainId) { return new Transaction( STORE_ADDR, Util.getNonce(), DEFAULT_QUOTA, diff --git a/tests/src/main/java/com/cryptape/cita/tests/GroupManagerExample.java b/tests/src/main/java/com/cryptape/cita/tests/GroupManagerExample.java index 026c3c6a..08dac5f4 100644 --- a/tests/src/main/java/com/cryptape/cita/tests/GroupManagerExample.java +++ b/tests/src/main/java/com/cryptape/cita/tests/GroupManagerExample.java @@ -1,7 +1,11 @@ package com.cryptape.cita.tests; +import com.cryptape.cita.protocol.core.methods.response.Log; +import com.cryptape.cita.protocol.system.CITASystemContract; + import java.io.IOException; +import java.util.Arrays; import java.util.List; /** @@ -10,9 +14,21 @@ * https://docs.citahub.com/zh-CN/cita/account-permission/account */ public class GroupManagerExample extends SystemContractExample { - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, InterruptedException { // query groups List groupAddress = sysContract.queryGroups("0xfFFfFFFFFffFFfffFFFFfffffFffffFFfF020009"); System.out.println("groupAddress: " + groupAddress); + + // create a group + String transcationHash = sysContract.newGroup("0xfFFfFFFFFffFFfffFFFFfffffFffffFFfF020009", + "newGroup", + Arrays.asList("e1c4021742730ded647590a1686d5c4bfcbae0b0", "45a50f45cb81c8aedeab917ea0cd3c9178ebdcae"), + adminPriavteKey, + version, + chainId); + System.out.println("transcationHash: " + transcationHash); + // get receipt by hash + Log log = CITASystemContract.getReceiptLog(service, transcationHash, 0); + System.out.println("groupAddress: " + (log == null ? "null" : log.getAddress())); } }