Skip to content

Commit a8b66c3

Browse files
authored
V1.0.1 (#33)
- 修改了合约接口创建器 - 添加了AccountGroup - 修复了国密的配置漏洞,移除了国密配置 - 添加了提示性的日志 - 添加了代码注释 - 部分代码重构
1 parent 240ea2b commit a8b66c3

31 files changed

+790
-781
lines changed

change_log.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
2021-03-22(V1.0.1)
12

2-
2021-01(V1.0)
3+
- 修改了合约接口创建器
4+
- 添加了AccountGroup
5+
- 修复了国密的配置漏洞,移除了国密配置
6+
- 添加了提示性的日志
7+
- 添加了代码注释
8+
- 部分代码重构
9+
10+
11+
2021-01(V1.0.0)
312

413
- 正式发布,包含了合约、SDK和demo

src/main/java/com/webank/blockchain/gov/acct/enums/AccountStatusEnum.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@
2525
@AllArgsConstructor
2626
@Getter
2727
public enum AccountStatusEnum {
28-
NORMAL(0),
29-
FROZEN(1),
30-
CLOSED(2);
28+
NORMAL(0, "normal"),
29+
FROZEN(1, "frozen"),
30+
CLOSED(2, "closed");
3131

3232
private int status;
33+
private String name;
34+
35+
public static String getNameByStatics(int status) {
36+
AccountStatusEnum[] enums = values();
37+
for (AccountStatusEnum e : enums) {
38+
if (e.status == status) {
39+
return e.getName();
40+
}
41+
}
42+
return "undefined";
43+
}
3344
}

src/main/java/com/webank/blockchain/gov/acct/enums/RequestEnum.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,28 @@
2626
@AllArgsConstructor
2727
@Getter
2828
public enum RequestEnum {
29-
OPER_CREATE_ACCOUNT(BigInteger.valueOf(1)),
30-
OPER_CHANGE_CREDENTIAL(BigInteger.valueOf(2)),
31-
OPER_FREEZE_ACCOUNT(BigInteger.valueOf(3)),
32-
OPER_UNFREEZE_ACCOUNT(BigInteger.valueOf(4)),
33-
OPER_CANCEL_ACCOUNT(BigInteger.valueOf(5)),
34-
OPER_RESET_MANAGER_TYPE(BigInteger.valueOf(6)),
35-
OPER_RESET_THRESHOLD(BigInteger.valueOf(10)),
36-
OPER_RESET_WEIGHT(BigInteger.valueOf(11)),
37-
OPER_ADD_WEIGHT(BigInteger.valueOf(12)),
38-
OPER_RM_WEIGHT(BigInteger.valueOf(13)),
39-
OPER_RESET_ACCOUNT_MANAGER(BigInteger.valueOf(50));
29+
OPER_CREATE_ACCOUNT(BigInteger.valueOf(1), "create account"),
30+
OPER_CHANGE_CREDENTIAL(BigInteger.valueOf(2), "change credential"),
31+
OPER_FREEZE_ACCOUNT(BigInteger.valueOf(3), "freeze account"),
32+
OPER_UNFREEZE_ACCOUNT(BigInteger.valueOf(4), "unfreeze account"),
33+
OPER_CANCEL_ACCOUNT(BigInteger.valueOf(5), "cancel account"),
34+
OPER_RESET_MANAGER_TYPE(BigInteger.valueOf(6), "reset manager type"),
35+
OPER_RESET_THRESHOLD(BigInteger.valueOf(10), "reset threshold"),
36+
OPER_RESET_WEIGHT(BigInteger.valueOf(11), "reset weight"),
37+
OPER_ADD_WEIGHT(BigInteger.valueOf(12), "add weight"),
38+
OPER_RM_WEIGHT(BigInteger.valueOf(13), "remove weight"),
39+
OPER_RESET_ACCOUNT_MANAGER(BigInteger.valueOf(50), "reset account manager");
40+
4041
private BigInteger type;
42+
private String name;
43+
44+
public static String getNameByStatics(int type) {
45+
RequestEnum[] enums = values();
46+
for (RequestEnum e : enums) {
47+
if (e.type.intValue() == type) {
48+
return e.getName();
49+
}
50+
}
51+
return "undefined";
52+
}
4153
}

src/main/java/com/webank/blockchain/gov/acct/enums/UserStaticsEnum.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,19 @@
2727
public enum UserStaticsEnum {
2828
// 0-none, 1- social vote, 2-governance, 3-both 1 or 2;
2929

30-
NONE(0),
31-
SOCIAL(1);
30+
NONE(0, "default"),
31+
SOCIAL(1, "social");
3232

3333
private int statics;
34+
private String name;
35+
36+
public static String getNameByStatics(int statics) {
37+
UserStaticsEnum[] enums = values();
38+
for (UserStaticsEnum e : enums) {
39+
if (e.statics == statics) {
40+
return e.getName();
41+
}
42+
}
43+
return "undefined";
44+
}
3445
}

src/main/java/com/webank/blockchain/gov/acct/factory/AccountGovernManagerFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.webank.blockchain.gov.acct.exception.InvalidParamException;
1919
import com.webank.blockchain.gov.acct.manager.AdminModeGovernManager;
2020
import com.webank.blockchain.gov.acct.manager.EndUserOperManager;
21-
import com.webank.blockchain.gov.acct.manager.GovernAccountInitializer;
21+
import com.webank.blockchain.gov.acct.manager.GovernContractInitializer;
2222
import com.webank.blockchain.gov.acct.manager.SocialVoteManager;
2323
import com.webank.blockchain.gov.acct.manager.VoteModeGovernManager;
2424
import lombok.Data;
@@ -54,8 +54,8 @@ public AccountGovernManagerFactory(
5454
this.accountManager = AccountManager.load(acctManagerAddress, client, credentials);
5555
}
5656

57-
public GovernAccountInitializer newGovernAccountInitializer() {
58-
GovernAccountInitializer manager = new GovernAccountInitializer();
57+
public GovernContractInitializer newGovernContractInitializer() {
58+
GovernContractInitializer manager = new GovernContractInitializer();
5959
manager.setClient(client).setCredentials(credentials);
6060
return manager;
6161
}

src/main/java/com/webank/blockchain/gov/acct/manager/AdminModeGovernManager.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
*/
1414
package com.webank.blockchain.gov.acct.manager;
1515

16+
import com.webank.blockchain.gov.acct.contract.WEGovernance;
1617
import com.webank.blockchain.gov.acct.enums.RequestEnum;
18+
import com.webank.blockchain.gov.acct.exception.InvalidParamException;
19+
import lombok.extern.slf4j.Slf4j;
20+
import org.apache.commons.lang3.StringUtils;
21+
import org.fisco.bcos.sdk.client.Client;
22+
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
1723
import org.fisco.bcos.sdk.model.TransactionReceipt;
24+
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
1825
import org.springframework.stereotype.Service;
1926

2027
/**
@@ -24,32 +31,53 @@
2431
* @data Feb 22, 2020 11:20:09 AM
2532
*/
2633
@Service
34+
@Slf4j
2735
public class AdminModeGovernManager extends BasicManager {
2836

37+
public AdminModeGovernManager() {
38+
super();
39+
}
40+
41+
public AdminModeGovernManager(WEGovernance governance, Client client, CryptoKeyPair credentials)
42+
throws ContractException {
43+
super(governance, client, credentials);
44+
}
45+
2946
public TransactionReceipt transferAdminAuth(String newAdminAddr) throws Exception {
47+
log.info(
48+
"Contract [ {} ] transfer owner to address [ {} ]",
49+
governance.getContractAddress(),
50+
newAdminAddr);
3051
return governance.transferOwner(newAdminAddr);
3152
}
3253

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

3863
public TransactionReceipt freezeAccount(String externalAccount) throws Exception {
64+
log.info("freeze account [ {} ]", externalAccount);
3965
return governance.doOper(
4066
RequestEnum.OPER_FREEZE_ACCOUNT.getType(),
4167
externalAccount,
4268
RequestEnum.OPER_FREEZE_ACCOUNT.getType());
4369
}
4470

4571
public TransactionReceipt unfreezeAccount(String externalAccount) throws Exception {
72+
log.info("unfreeze account [ {} ]", externalAccount);
4673
return governance.doOper(
4774
RequestEnum.OPER_UNFREEZE_ACCOUNT.getType(),
4875
externalAccount,
4976
RequestEnum.OPER_UNFREEZE_ACCOUNT.getType());
5077
}
5178

5279
public TransactionReceipt cancelAccount(String userAccount) throws Exception {
80+
log.info("cancel account [ {} ]", userAccount);
5381
return governance.doOper(
5482
RequestEnum.OPER_CANCEL_ACCOUNT.getType(),
5583
userAccount,

src/main/java/com/webank/blockchain/gov/acct/manager/BasicManager.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
import com.webank.blockchain.gov.acct.contract.WEGovernance;
1919
import com.webank.blockchain.gov.acct.exception.TransactionReceiptException;
2020
import com.webank.blockchain.gov.acct.service.JavaSDKBasicService;
21-
import com.webank.blockchain.gov.acct.tool.JacksonUtils;
2221
import lombok.Data;
2322
import lombok.EqualsAndHashCode;
2423
import lombok.experimental.Accessors;
2524
import lombok.extern.slf4j.Slf4j;
25+
import org.fisco.bcos.sdk.client.Client;
2626
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
2727
import org.fisco.bcos.sdk.model.TransactionReceipt;
2828
import org.fisco.bcos.sdk.transaction.codec.decode.TransactionDecoderInterface;
2929
import org.fisco.bcos.sdk.transaction.codec.decode.TransactionDecoderService;
30+
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
31+
import org.fisco.bcos.sdk.transaction.tools.JsonUtils;
3032
import org.springframework.beans.factory.annotation.Autowired;
3133

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

52+
public BasicManager() {}
53+
54+
public BasicManager(WEGovernance governance, Client client, CryptoKeyPair credentials)
55+
throws ContractException {
56+
super.client = client;
57+
super.credentials = credentials;
58+
this.governance = governance;
59+
this.accountManager =
60+
AccountManager.load(governance.getAccountManager(), client, credentials);
61+
}
62+
5063
public String getAccountAddress() throws Exception {
5164
return accountManager.getUserAccount(credentials.getAddress());
5265
}
@@ -58,11 +71,12 @@ public String createAccount(String externalAccount) throws Exception {
5871
public String createAccount(AccountManager accountManager, String externalAccount)
5972
throws Exception {
6073
if (hasAccount(externalAccount)) {
74+
log.info("Account [ {} ] already created.", externalAccount);
6175
return getBaseAccountAddress(externalAccount);
6276
}
6377
TransactionReceipt tr = accountManager.newAccount(externalAccount);
6478
if (!tr.getStatus().equalsIgnoreCase("0x0")) {
65-
log.error("create new Account error: {}", JacksonUtils.toJson(tr));
79+
log.error("create new Account error: {}", JsonUtils.toJson(tr));
6680
throw new TransactionReceiptException("Error create account error");
6781
}
6882

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

8094
public String getExternalAccount(String userAccount) throws Exception {
8195
String externalAddress = accountManager.getExternalAccount(userAccount);
82-
log.info("external address is {}, userAccount is {}", externalAddress, userAccount);
96+
log.info("external address is [ {} ], userAccount is [ {} ]", externalAddress, userAccount);
8397
return externalAddress;
8498
}
8599

86100
public UserAccount getUserAccount(String externalAccount) throws Exception {
87101
String configAddress = accountManager.getUserAccount(externalAccount);
88-
log.info(
89-
"Account config address is {}, cryptoKeyPair is {}",
102+
log.debug(
103+
"User account config address is [ {} ], cryptoKeyPair is [ {} ]",
90104
configAddress,
91105
credentials.getAddress());
92106
return UserAccount.load(configAddress, client, credentials);
@@ -114,6 +128,10 @@ public boolean isExternalAccountNormal(String externalAccount) throws Exception
114128
}
115129

116130
public void changeCredentials(CryptoKeyPair credentials) throws Exception {
131+
log.info(
132+
"credentials change to [ {} ] from [ {} ]",
133+
credentials.getAddress(),
134+
this.credentials.getAddress());
117135
this.credentials = credentials;
118136
this.governance =
119137
WEGovernance.load(this.governance.getContractAddress(), client, credentials);

src/main/java/com/webank/blockchain/gov/acct/manager/EndUserOperManager.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
package com.webank.blockchain.gov.acct.manager;
1515

1616
import com.webank.blockchain.gov.acct.contract.UserAccount;
17+
import com.webank.blockchain.gov.acct.contract.WEGovernance;
1718
import com.webank.blockchain.gov.acct.enums.UserStaticsEnum;
1819
import com.webank.blockchain.gov.acct.exception.InvalidParamException;
19-
import com.webank.blockchain.gov.acct.tool.JacksonUtils;
2020
import java.math.BigInteger;
2121
import java.util.List;
2222
import java.util.stream.Collectors;
23+
import lombok.extern.slf4j.Slf4j;
24+
import org.fisco.bcos.sdk.client.Client;
25+
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
2326
import org.fisco.bcos.sdk.model.TransactionReceipt;
27+
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
28+
import org.fisco.bcos.sdk.transaction.tools.JsonUtils;
2429
import org.springframework.stereotype.Service;
2530

2631
/**
@@ -30,15 +35,30 @@
3035
* @data Feb 20, 2020 4:53:02 PM
3136
*/
3237
@Service
38+
@Slf4j
3339
public class EndUserOperManager extends BasicManager {
3440

41+
public EndUserOperManager() {
42+
super();
43+
}
44+
45+
public EndUserOperManager(WEGovernance governance, Client client, CryptoKeyPair credentials)
46+
throws ContractException {
47+
super(governance, client, credentials);
48+
}
49+
3550
public TransactionReceipt setRelatedAccount(String account, int value) throws Exception {
3651
UserAccount accountConfig = getUserAccount(credentials.getAddress());
3752
int type = accountConfig._statics().intValue();
3853
if (type == UserStaticsEnum.SOCIAL.getStatics()) {
3954
if (accountConfig.getWeightInfo().getValue1().size() >= 3 && value > 0) {
4055
throw new InvalidParamException("Already too many voters.");
4156
}
57+
log.info(
58+
"External account [{}] set related account: [ {} ], weight: [ {} ]",
59+
credentials.getAddress(),
60+
account,
61+
value);
4262
return accountConfig.setWeight(account, BigInteger.valueOf(value));
4363
} else {
4464
throw new InvalidParamException("error account types.");
@@ -59,6 +79,7 @@ public TransactionReceipt modifyManagerType() throws Exception {
5979
if (statics.intValue() == UserStaticsEnum.NONE.getStatics()) {
6080
throw new InvalidParamException("Modify the same type.");
6181
}
82+
log.info("Set External account {} to default reset mode.\n ", credentials.getAddress());
6283
return userAccount.setStatics();
6384
}
6485

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

85107
public TransactionReceipt resetAccount(String newCredential) throws Exception {
108+
log.info(
109+
"External account [ {} ] reset by self to new external account [ {} ] ",
110+
credentials.getAddress(),
111+
newCredential);
86112
return accountManager.setExternalAccountByUser(newCredential);
87113
}
88114

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

0 commit comments

Comments
 (0)