Skip to content

Commit

Permalink
add unit testing of newGroup interface
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhenlong committed Aug 27, 2019
1 parent afef1b5 commit 3b32c96
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,50 @@ public List<String> queryGroups(String senderAddress) throws IOException {
return list;
}

/**
* new group
* @see <a href="https://docs.citahub.com/zh-CN/cita/sys-contract-interface/interface#newgroup">newGroup</a>
* @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<String> accounts,
String adminPrivateKey,
int version,
BigInteger chainId) throws IOException {
// account addresses convert to Address object list
List<Address> 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<Type> inputParameters = Arrays.asList(
new Address(superAdminAddress),//origin
new Bytes32(nameBytes),//name
new DynamicArray<Address>(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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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<String> 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()));
}
}

0 comments on commit 3b32c96

Please sign in to comment.