Skip to content

Commit

Permalink
<feat>(connection): refresh stub config
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfang2 committed Aug 6, 2024
1 parent 1c6f5aa commit f0b92a1
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 356 deletions.
96 changes: 81 additions & 15 deletions src/main/java/com/webank/wecross/stub/web3/Web3Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.moandjiezana.toml.Toml;
import com.webank.wecross.stub.Connection;
import com.webank.wecross.stub.Request;
import com.webank.wecross.stub.ResourceInfo;
Expand All @@ -12,18 +13,26 @@
import com.webank.wecross.stub.web3.common.Web3Constant;
import com.webank.wecross.stub.web3.common.Web3RequestType;
import com.webank.wecross.stub.web3.common.Web3StatusCode;
import com.webank.wecross.stub.web3.config.Web3StubConfig;
import com.webank.wecross.stub.web3.protocol.request.TransactionParams;
import com.webank.wecross.stub.web3.protocol.response.TransactionPair;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionDecoder;
import org.web3j.protocol.core.methods.response.EthBlock;
Expand All @@ -41,10 +50,49 @@ public class Web3Connection implements Connection {
private ConnectionEventHandler eventHandler;
private final Map<String, String> properties = new HashMap<>();
private final ClientWrapper clientWrapper;
private final BigInteger chainId;
private final String stubConfigFilePath;
private long stubConfigFileLastModified;

public Web3Connection(ClientWrapper clientWrapper) {
public Web3Connection(ClientWrapper clientWrapper, Web3StubConfig web3StubConfig)
throws IOException {
this.objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
this.clientWrapper = clientWrapper;
this.chainId = clientWrapper.ethChainId();
this.stubConfigFilePath = web3StubConfig.getStubConfigPath();
// init
refreshStubConfig(web3StubConfig);

// refresh
ScheduledExecutorService executorService =
new ScheduledThreadPoolExecutor(1, new CustomizableThreadFactory("refreshStubConfig-"));
executorService.scheduleAtFixedRate(
() -> {
try {
PathMatchingResourcePatternResolver resolver =
new PathMatchingResourcePatternResolver();
Resource resource = resolver.getResource(stubConfigFilePath);
long lastModified = resource.getFile().lastModified();
if (Objects.equals(stubConfigFileLastModified, lastModified)) {
return;
}
Web3StubConfig newWeb3StubConfig =
new Toml().read(resource.getInputStream()).to(Web3StubConfig.class);
newWeb3StubConfig.setStubConfigPath(stubConfigFilePath);
// changed
refreshStubConfig(newWeb3StubConfig);
if (Objects.nonNull(eventHandler)) {
// refresh remote resource
eventHandler.onResourcesChange(resourceInfoList);
}
this.stubConfigFileLastModified = lastModified;
} catch (IOException e) {
logger.error("refreshStubConfig Exception:", e);
}
},
30000,
30000,
TimeUnit.MILLISECONDS);
}

@Override
Expand Down Expand Up @@ -75,6 +123,16 @@ public void asyncSend(Request request, Callback callback) {
}
}

@Override
public Map<String, String> getProperties() {
return properties;
}

@Override
public void setConnectionEventHandler(ConnectionEventHandler eventHandler) {
this.eventHandler = eventHandler;
}

private void handleAsyncGetTransaction(Request request, Callback callback) {
Response response = new Response();
response.setErrorCode(Web3StatusCode.Success);
Expand Down Expand Up @@ -279,6 +337,24 @@ private void handleAsyncTransactionRequest(Request request, Callback callback) {
}
}

private synchronized void refreshStubConfig(Web3StubConfig web3StubConfig) throws IOException {
this.resourceInfoList = web3StubConfig.convertToResourceInfos();

addProperty(Web3Constant.WEB3_PROPERTY_CHAIN_ID, chainId.toString());
addProperty(Web3Constant.WEB3_PROPERTY_STUB_TYPE, web3StubConfig.getCommon().getType());
addProperty(Web3Constant.WEB3_PROPERTY_CHAIN_URL, web3StubConfig.getService().getUrl());
List<Web3StubConfig.Resource> resources = web3StubConfig.getResources();
if (!resources.isEmpty()) {
for (Web3StubConfig.Resource resource : resources) {
String name = resource.getName();
// name->address
this.addProperty(name, resource.getAddress());
// name+ABI->abi
this.addAbi(name, resource.getAbi());
}
}
}

public boolean hasProxyDeployed() {
return getProperties().containsKey(Web3Constant.WEB3_PROXY_NAME);
}
Expand All @@ -291,20 +367,6 @@ public List<ResourceInfo> getResourceInfoList() {
return resourceInfoList;
}

public void setResourceInfoList(List<ResourceInfo> resourceInfoList) {
this.resourceInfoList = resourceInfoList;
}

@Override
public Map<String, String> getProperties() {
return properties;
}

@Override
public void setConnectionEventHandler(ConnectionEventHandler eventHandler) {
this.eventHandler = eventHandler;
}

public void addProperty(String key, String value) {
this.properties.put(key, value);
}
Expand All @@ -324,4 +386,8 @@ public String getAbi(String key) {
public ClientWrapper getClientWrapper() {
return clientWrapper;
}

public BigInteger getChainId() {
return chainId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import com.webank.wecross.stub.web3.client.ClientWrapper;
import com.webank.wecross.stub.web3.client.ClientWrapperFactory;
import com.webank.wecross.stub.web3.common.Web3Constant;
import com.webank.wecross.stub.web3.config.Web3StubConfig;
import com.webank.wecross.stub.web3.config.Web3StubConfigParser;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -22,34 +19,8 @@ public static Web3Connection build(String stubConfigPath, String configName) thr
}

public static Web3Connection build(Web3StubConfig web3StubConfig) throws IOException {
ClientWrapper clientWrapper = ClientWrapperFactory.createClientWrapperInstance(web3StubConfig);
return build(web3StubConfig, clientWrapper);
}

public static Web3Connection build(Web3StubConfig web3StubConfig, ClientWrapper clientWrapper)
throws IOException {
logger.info("web3StubConfig: {}", web3StubConfig);
BigInteger chainId = clientWrapper.ethChainId();

Web3Connection connection = new Web3Connection(clientWrapper);
connection.setResourceInfoList(web3StubConfig.convertToResourceInfos());
connection.addProperty(Web3Constant.WEB3_PROPERTY_CHAIN_ID, chainId.toString());
connection.addProperty(
Web3Constant.WEB3_PROPERTY_STUB_TYPE, web3StubConfig.getCommon().getType());
connection.addProperty(
Web3Constant.WEB3_PROPERTY_CHAIN_URL, web3StubConfig.getService().getUrl());
// from config build resources
List<Web3StubConfig.Resource> resources = web3StubConfig.getResources();
if (!resources.isEmpty()) {
// addProperty
for (Web3StubConfig.Resource resource : resources) {
String name = resource.getName();
// name->address
connection.addProperty(name, resource.getAddress());
// name+ABI->abi
connection.addAbi(name, resource.getAbi());
}
}
return connection;
ClientWrapper clientWrapper = ClientWrapperFactory.createClientWrapperInstance(web3StubConfig);
return new Web3Connection(clientWrapper, web3StubConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Web3AccountConfig {

private Account account;

private String accountConfigPath;

public static class Account {
private String type;
private String accountFile;
Expand Down Expand Up @@ -61,8 +63,22 @@ public void setAccount(Account account) {
this.account = account;
}

public String getAccountConfigPath() {
return accountConfigPath;
}

public void setAccountConfigPath(String accountConfigPath) {
this.accountConfigPath = accountConfigPath;
}

@Override
public String toString() {
return "Web3AccountConfig{" + "account=" + account + '}';
return "Web3AccountConfig{"
+ "account="
+ account
+ ", accountConfigPath='"
+ accountConfigPath
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public Web3AccountConfigParser(String configPath, String configName) {
public Web3AccountConfig loadConfig() throws IOException {
Web3Toml web3Toml = new Web3Toml(getConfigPath());
Toml toml = web3Toml.getToml();
return toml.to(Web3AccountConfig.class);
Web3AccountConfig web3AccountConfig = toml.to(Web3AccountConfig.class);
web3AccountConfig.setAccountConfigPath(getConfigPath());
return web3AccountConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Web3StubConfig {
private Service service;
private List<Resource> resources;

private String stubConfigPath;

public List<ResourceInfo> convertToResourceInfos() {
List<ResourceInfo> resourceInfos = new ArrayList<>();
for (Resource resource : this.getResources()) {
Expand Down Expand Up @@ -149,6 +151,14 @@ public void setResources(List<Resource> resources) {
this.resources = resources;
}

public String getStubConfigPath() {
return stubConfigPath;
}

public void setStubConfigPath(String stubConfigPath) {
this.stubConfigPath = stubConfigPath;
}

@Override
public String toString() {
return "Web3StubConfig{"
Expand All @@ -158,6 +168,9 @@ public String toString() {
+ service
+ ", resources="
+ resources
+ ", stubConfigPath='"
+ stubConfigPath
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public Web3StubConfigParser(String configPath, String configName) {
public Web3StubConfig loadConfig() throws IOException {
Web3Toml web3Toml = new Web3Toml(getConfigPath());
Toml toml = web3Toml.getToml();
return toml.to(Web3StubConfig.class);
Web3StubConfig web3StubConfig = toml.to(Web3StubConfig.class);
web3StubConfig.setStubConfigPath(getConfigPath());
return web3StubConfig;
}
}
Loading

0 comments on commit f0b92a1

Please sign in to comment.