Skip to content

Commit

Permalink
V1.0.1 (#33)
Browse files Browse the repository at this point in the history
- 修改了合约接口创建器
- 添加了AccountGroup
- 修复了国密的配置漏洞,移除了国密配置
- 添加了提示性的日志
- 添加了代码注释
- 部分代码重构
  • Loading branch information
dalaocu committed Mar 22, 2021
1 parent 240ea2b commit a8b66c3
Show file tree
Hide file tree
Showing 31 changed files with 790 additions and 781 deletions.
11 changes: 10 additions & 1 deletion change_log.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
2021-03-22(V1.0.1)

2021-01(V1.0)
- 修改了合约接口创建器
- 添加了AccountGroup
- 修复了国密的配置漏洞,移除了国密配置
- 添加了提示性的日志
- 添加了代码注释
- 部分代码重构


2021-01(V1.0.0)

- 正式发布,包含了合约、SDK和demo
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,20 @@
@AllArgsConstructor
@Getter
public enum AccountStatusEnum {
NORMAL(0),
FROZEN(1),
CLOSED(2);
NORMAL(0, "normal"),
FROZEN(1, "frozen"),
CLOSED(2, "closed");

private int status;
private String name;

public static String getNameByStatics(int status) {
AccountStatusEnum[] enums = values();
for (AccountStatusEnum e : enums) {
if (e.status == status) {
return e.getName();
}
}
return "undefined";
}
}
34 changes: 23 additions & 11 deletions src/main/java/com/webank/blockchain/gov/acct/enums/RequestEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,28 @@
@AllArgsConstructor
@Getter
public enum RequestEnum {
OPER_CREATE_ACCOUNT(BigInteger.valueOf(1)),
OPER_CHANGE_CREDENTIAL(BigInteger.valueOf(2)),
OPER_FREEZE_ACCOUNT(BigInteger.valueOf(3)),
OPER_UNFREEZE_ACCOUNT(BigInteger.valueOf(4)),
OPER_CANCEL_ACCOUNT(BigInteger.valueOf(5)),
OPER_RESET_MANAGER_TYPE(BigInteger.valueOf(6)),
OPER_RESET_THRESHOLD(BigInteger.valueOf(10)),
OPER_RESET_WEIGHT(BigInteger.valueOf(11)),
OPER_ADD_WEIGHT(BigInteger.valueOf(12)),
OPER_RM_WEIGHT(BigInteger.valueOf(13)),
OPER_RESET_ACCOUNT_MANAGER(BigInteger.valueOf(50));
OPER_CREATE_ACCOUNT(BigInteger.valueOf(1), "create account"),
OPER_CHANGE_CREDENTIAL(BigInteger.valueOf(2), "change credential"),
OPER_FREEZE_ACCOUNT(BigInteger.valueOf(3), "freeze account"),
OPER_UNFREEZE_ACCOUNT(BigInteger.valueOf(4), "unfreeze account"),
OPER_CANCEL_ACCOUNT(BigInteger.valueOf(5), "cancel account"),
OPER_RESET_MANAGER_TYPE(BigInteger.valueOf(6), "reset manager type"),
OPER_RESET_THRESHOLD(BigInteger.valueOf(10), "reset threshold"),
OPER_RESET_WEIGHT(BigInteger.valueOf(11), "reset weight"),
OPER_ADD_WEIGHT(BigInteger.valueOf(12), "add weight"),
OPER_RM_WEIGHT(BigInteger.valueOf(13), "remove weight"),
OPER_RESET_ACCOUNT_MANAGER(BigInteger.valueOf(50), "reset account manager");

private BigInteger type;
private String name;

public static String getNameByStatics(int type) {
RequestEnum[] enums = values();
for (RequestEnum e : enums) {
if (e.type.intValue() == type) {
return e.getName();
}
}
return "undefined";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@
public enum UserStaticsEnum {
// 0-none, 1- social vote, 2-governance, 3-both 1 or 2;

NONE(0),
SOCIAL(1);
NONE(0, "default"),
SOCIAL(1, "social");

private int statics;
private String name;

public static String getNameByStatics(int statics) {
UserStaticsEnum[] enums = values();
for (UserStaticsEnum e : enums) {
if (e.statics == statics) {
return e.getName();
}
}
return "undefined";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.webank.blockchain.gov.acct.exception.InvalidParamException;
import com.webank.blockchain.gov.acct.manager.AdminModeGovernManager;
import com.webank.blockchain.gov.acct.manager.EndUserOperManager;
import com.webank.blockchain.gov.acct.manager.GovernAccountInitializer;
import com.webank.blockchain.gov.acct.manager.GovernContractInitializer;
import com.webank.blockchain.gov.acct.manager.SocialVoteManager;
import com.webank.blockchain.gov.acct.manager.VoteModeGovernManager;
import lombok.Data;
Expand Down Expand Up @@ -54,8 +54,8 @@ public AccountGovernManagerFactory(
this.accountManager = AccountManager.load(acctManagerAddress, client, credentials);
}

public GovernAccountInitializer newGovernAccountInitializer() {
GovernAccountInitializer manager = new GovernAccountInitializer();
public GovernContractInitializer newGovernContractInitializer() {
GovernContractInitializer manager = new GovernContractInitializer();
manager.setClient(client).setCredentials(credentials);
return manager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
*/
package com.webank.blockchain.gov.acct.manager;

import com.webank.blockchain.gov.acct.contract.WEGovernance;
import com.webank.blockchain.gov.acct.enums.RequestEnum;
import com.webank.blockchain.gov.acct.exception.InvalidParamException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.stereotype.Service;

/**
Expand All @@ -24,32 +31,53 @@
* @data Feb 22, 2020 11:20:09 AM
*/
@Service
@Slf4j
public class AdminModeGovernManager extends BasicManager {

public AdminModeGovernManager() {
super();
}

public AdminModeGovernManager(WEGovernance governance, Client client, CryptoKeyPair credentials)
throws ContractException {
super(governance, client, credentials);
}

public TransactionReceipt transferAdminAuth(String newAdminAddr) throws Exception {
log.info(
"Contract [ {} ] transfer owner to address [ {} ]",
governance.getContractAddress(),
newAdminAddr);
return governance.transferOwner(newAdminAddr);
}

public TransactionReceipt resetAccount(String oldAccount, String newAccount) throws Exception {
if (StringUtils.equalsAnyIgnoreCase(oldAccount, newAccount)) {
throw new InvalidParamException("The oldAccount is equal to new Account");
}
log.info("reset account to [ {} ] from [ {} ]", newAccount, oldAccount);
return governance.setExternalAccount(
RequestEnum.OPER_CHANGE_CREDENTIAL.getType(), newAccount, oldAccount);
}

public TransactionReceipt freezeAccount(String externalAccount) throws Exception {
log.info("freeze account [ {} ]", externalAccount);
return governance.doOper(
RequestEnum.OPER_FREEZE_ACCOUNT.getType(),
externalAccount,
RequestEnum.OPER_FREEZE_ACCOUNT.getType());
}

public TransactionReceipt unfreezeAccount(String externalAccount) throws Exception {
log.info("unfreeze account [ {} ]", externalAccount);
return governance.doOper(
RequestEnum.OPER_UNFREEZE_ACCOUNT.getType(),
externalAccount,
RequestEnum.OPER_UNFREEZE_ACCOUNT.getType());
}

public TransactionReceipt cancelAccount(String userAccount) throws Exception {
log.info("cancel account [ {} ]", userAccount);
return governance.doOper(
RequestEnum.OPER_CANCEL_ACCOUNT.getType(),
userAccount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
import com.webank.blockchain.gov.acct.contract.WEGovernance;
import com.webank.blockchain.gov.acct.exception.TransactionReceiptException;
import com.webank.blockchain.gov.acct.service.JavaSDKBasicService;
import com.webank.blockchain.gov.acct.tool.JacksonUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.transaction.codec.decode.TransactionDecoderInterface;
import org.fisco.bcos.sdk.transaction.codec.decode.TransactionDecoderService;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.fisco.bcos.sdk.transaction.tools.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
Expand All @@ -47,6 +49,17 @@ public class BasicManager extends JavaSDKBasicService {
@Autowired(required = false)
protected AccountManager accountManager;

public BasicManager() {}

public BasicManager(WEGovernance governance, Client client, CryptoKeyPair credentials)
throws ContractException {
super.client = client;
super.credentials = credentials;
this.governance = governance;
this.accountManager =
AccountManager.load(governance.getAccountManager(), client, credentials);
}

public String getAccountAddress() throws Exception {
return accountManager.getUserAccount(credentials.getAddress());
}
Expand All @@ -58,11 +71,12 @@ public String createAccount(String externalAccount) throws Exception {
public String createAccount(AccountManager accountManager, String externalAccount)
throws Exception {
if (hasAccount(externalAccount)) {
log.info("Account [ {} ] already created.", externalAccount);
return getBaseAccountAddress(externalAccount);
}
TransactionReceipt tr = accountManager.newAccount(externalAccount);
if (!tr.getStatus().equalsIgnoreCase("0x0")) {
log.error("create new Account error: {}", JacksonUtils.toJson(tr));
log.error("create new Account error: {}", JsonUtils.toJson(tr));
throw new TransactionReceiptException("Error create account error");
}

Expand All @@ -73,20 +87,20 @@ public String createAccount(AccountManager accountManager, String externalAccoun
decoder.decodeReceiptWithValues(AccountManager.ABI, "newAccount", tr)
.getValuesList()
.get(1);
log.info("new acct {}, created by {}", addr, externalAccount);
log.info("new account created: [ {} ], created by [ {} ]", addr, externalAccount);
return addr;
}

public String getExternalAccount(String userAccount) throws Exception {
String externalAddress = accountManager.getExternalAccount(userAccount);
log.info("external address is {}, userAccount is {}", externalAddress, userAccount);
log.info("external address is [ {} ], userAccount is [ {} ]", externalAddress, userAccount);
return externalAddress;
}

public UserAccount getUserAccount(String externalAccount) throws Exception {
String configAddress = accountManager.getUserAccount(externalAccount);
log.info(
"Account config address is {}, cryptoKeyPair is {}",
log.debug(
"User account config address is [ {} ], cryptoKeyPair is [ {} ]",
configAddress,
credentials.getAddress());
return UserAccount.load(configAddress, client, credentials);
Expand Down Expand Up @@ -114,6 +128,10 @@ public boolean isExternalAccountNormal(String externalAccount) throws Exception
}

public void changeCredentials(CryptoKeyPair credentials) throws Exception {
log.info(
"credentials change to [ {} ] from [ {} ]",
credentials.getAddress(),
this.credentials.getAddress());
this.credentials = credentials;
this.governance =
WEGovernance.load(this.governance.getContractAddress(), client, credentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
package com.webank.blockchain.gov.acct.manager;

import com.webank.blockchain.gov.acct.contract.UserAccount;
import com.webank.blockchain.gov.acct.contract.WEGovernance;
import com.webank.blockchain.gov.acct.enums.UserStaticsEnum;
import com.webank.blockchain.gov.acct.exception.InvalidParamException;
import com.webank.blockchain.gov.acct.tool.JacksonUtils;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.fisco.bcos.sdk.transaction.tools.JsonUtils;
import org.springframework.stereotype.Service;

/**
Expand All @@ -30,15 +35,30 @@
* @data Feb 20, 2020 4:53:02 PM
*/
@Service
@Slf4j
public class EndUserOperManager extends BasicManager {

public EndUserOperManager() {
super();
}

public EndUserOperManager(WEGovernance governance, Client client, CryptoKeyPair credentials)
throws ContractException {
super(governance, client, credentials);
}

public TransactionReceipt setRelatedAccount(String account, int value) throws Exception {
UserAccount accountConfig = getUserAccount(credentials.getAddress());
int type = accountConfig._statics().intValue();
if (type == UserStaticsEnum.SOCIAL.getStatics()) {
if (accountConfig.getWeightInfo().getValue1().size() >= 3 && value > 0) {
throw new InvalidParamException("Already too many voters.");
}
log.info(
"External account [{}] set related account: [ {} ], weight: [ {} ]",
credentials.getAddress(),
account,
value);
return accountConfig.setWeight(account, BigInteger.valueOf(value));
} else {
throw new InvalidParamException("error account types.");
Expand All @@ -59,6 +79,7 @@ public TransactionReceipt modifyManagerType() throws Exception {
if (statics.intValue() == UserStaticsEnum.NONE.getStatics()) {
throw new InvalidParamException("Modify the same type.");
}
log.info("Set External account {} to default reset mode.\n ", credentials.getAddress());
return userAccount.setStatics();
}

Expand All @@ -74,19 +95,25 @@ public TransactionReceipt modifyManagerType(List<String> voters) throws Exceptio
}
List<BigInteger> value =
voters.stream().map(c -> BigInteger.ONE).collect(Collectors.toList());
System.out.println("ac owner is: " + userAccount._owner());
System.out.println(credentials.getAddress());
TransactionReceipt tr = userAccount.setVoteStatics(voters, value, BigInteger.valueOf(2));
System.out.println(tr.getStatus());
System.out.println("Init vote: " + JacksonUtils.toJson(userAccount.getWeightInfo()));
log.info(
"\n Set Account [ {} ] to social reset mode.\n -------------------------------------- \n threshold is {} \n Voters: {} \n ",
credentials.getAddress(),
2,
JsonUtils.toJson(userAccount.getWeightInfo().getValue1()));
return tr;
}

public TransactionReceipt resetAccount(String newCredential) throws Exception {
log.info(
"External account [ {} ] reset by self to new external account [ {} ] ",
credentials.getAddress(),
newCredential);
return accountManager.setExternalAccountByUser(newCredential);
}

public TransactionReceipt cancelAccount() throws Exception {
log.info("External account canceled by self: [ {} ] ", credentials.getAddress());
return accountManager.cancelByUser();
}

Expand Down
Loading

0 comments on commit a8b66c3

Please sign in to comment.