Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<feat>(transaction): simplify sending transactions when cross fabric chains #543

Merged
merged 5 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/com/webank/wecross/account/ChainAccountDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ChainAccountDetails {
private String type;

private Boolean isDefault;
private String chainDefault;

private String pubKey;
private String secKey;
Expand All @@ -27,6 +28,7 @@ public Map<String, Object> toProperties() {
properties.put("keyID", keyID);
properties.put("type", type);
properties.put("isDefault", isDefault);
properties.put("chainDefault", chainDefault);
properties.put("pubKey", pubKey);
properties.put("secKey", secKey);
properties.put("ext0", ext0);
Expand Down Expand Up @@ -91,6 +93,16 @@ public void setIsDefault(Boolean aDefault) {
isDefault = aDefault;
}

@JsonGetter("chainDefault")
public String getChainDefault(){
return chainDefault;
}

@JsonSetter("chainDefault")
public void setChainDefault(String chainName){
chainDefault = chainName;
}

public String getPubKey() {
return pubKey;
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/webank/wecross/account/UniversalAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -31,6 +34,45 @@ public class UniversalAccount {

private Map<String, Account> type2DefaultAccount = new HashMap<>();

// make every chain has a default account
private Map<String,Account> chain2DefaultChainAccount = new HashMap<>();

// chain name is like "payment.fabric-mychannel"
public Account getChainAccount(String type,String chainName){
String typeChain = type + ":" + chainName;
return chain2DefaultChainAccount.get(typeChain);
}

// chain name is like "payment.fabric-mychannel"
public void setDefaultChainAccount(String chainName,Account account){
String typeChain = account.getType() + ":" + chainName;
chain2DefaultChainAccount.put(typeChain, account);
logger.info("setDefaultChainAccount {} {}", chainName, account);
}

// this func should be added to utils
public static boolean checkChainName(String chainName){
if (chainName.length()==0){
return false;
}
String regex="[a-zA-Z0-9_-]+\\.[a-zA-Z0-9_-]+";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(chainName);
return m.matches();
}

// only use in asyncSendTransaction, asyncCall. maybe asyncCustomCommand can use too.
// if not set default chain account, use the universal default type account.
public Account getSendAccount(String type,String chainName){

Account account = getChainAccount(type,chainName);
if (account!=null){
return account;
}

return getAccount(type);
}

public Account getAccount(String type) {
return type2DefaultAccount.get(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public UniversalAccount buildUA(UADetails uaDetails) throws WeCrossException {
if (details.getIsDefault().booleanValue()) {
ua.setDefaultAccount(type, account);
}
if (UniversalAccount.checkChainName(details.getChainDefault())){
ua.setDefaultChainAccount(details.getChainDefault(), account);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public AuthFilter newAuthFilter() {
remoteAuthFilter.registerAuthUri("/auth/addChainAccount");
remoteAuthFilter.registerAuthUri("/auth/removeChainAccount");
remoteAuthFilter.registerAuthUri("/auth/setDefaultAccount");
remoteAuthFilter.registerAuthUri("/auth/setDefaultChainAccount");
remoteAuthFilter.registerAuthUri("/auth/listAccount");
remoteAuthFilter.registerAuthUri("/auth/authCode");
remoteAuthFilter.registerAuthUri("/auth/pub");
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/webank/wecross/resource/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public void asyncCall(
return;
}

Account account = ua.getAccount(stubType);
// Account account = ua.getAccount(stubType);
Account account = ua.getSendAccount(stubType, path.toChainName());
logger.info("asynCall use account by key: {}, path: {}", account.getKeyID(),this.path); //this log only for test
TransactionContext context =
new TransactionContext(account, this.path, this.resourceInfo, this.blockManager);
boolean isRawTransaction =
Expand Down Expand Up @@ -134,7 +136,9 @@ public void asyncSendTransaction(
return;
}

Account account = ua.getAccount(stubType);
// Account account = ua.getAccount(stubType);
Account account = ua.getSendAccount(stubType, path.toChainName());
logger.info("asynSendTransaction use account by key: {}, path: {}", account.getKeyID(),this.path); //this log only for test
TransactionContext context =
new TransactionContext(account, this.path, this.resourceInfo, this.blockManager);
boolean isRawTransaction =
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/webank/wecross/stub/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ public String toString() {
}
}

// compact zone and chain as ChainName
public String toChainName(){
return zone + "." + chain;
}

//whether in chain
public boolean isInChain(String chainName){
return chainName.equals(toChainName());
}

public String toURI() {
if (Objects.nonNull(resource)) {
return zone + "/" + chain + "/" + resource;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.webank.wecross.test.account;

import com.webank.wecross.account.UniversalAccount;
import org.junit.Assert;
import org.junit.Test;

public class CheckChainNameTest {
@Test
public void isCheckRight() throws Exception {
Assert.assertTrue(UniversalAccount.checkChainName("") == false);
Assert.assertTrue(UniversalAccount.checkChainName("chainName") == false);
Assert.assertTrue(UniversalAccount.checkChainName("payment.fabric") == true);
Assert.assertTrue(UniversalAccount.checkChainName("payment.fabric-mychannel") == true);
Assert.assertTrue(UniversalAccount.checkChainName("payment.bcos-group1") == true);
Assert.assertTrue(UniversalAccount.checkChainName("payment.bcos-group2.helloworld") == false);
Assert.assertTrue(UniversalAccount.checkChainName("testbcos-group2") == false);
}
}