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>(resource): custom command update resource #6

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.webank.wecross.stub.WeCrossContext;
import com.webank.wecross.stub.web3.account.Web3AccountFactory;
import com.webank.wecross.stub.web3.common.Web3Constant;
import com.webank.wecross.stub.web3.custom.CommandHandlerDispatcher;
import com.webank.wecross.stub.web3.custom.RegisterResourceHandler;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -19,7 +21,10 @@ public void init(WeCrossContext weCrossContext) {}

@Override
public Driver newDriver() {
return new Web3Driver();
CommandHandlerDispatcher commandHandlerDispatcher = new CommandHandlerDispatcher();
commandHandlerDispatcher.registerCommandHandler(
Web3Constant.CUSTOM_COMMAND_REGISTER, new RegisterResourceHandler());
return new Web3Driver(commandHandlerDispatcher);
}

@Override
Expand Down
76 changes: 27 additions & 49 deletions src/main/java/com/webank/wecross/stub/web3/Web3Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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 @@ -20,19 +19,13 @@
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 java.util.concurrent.ConcurrentHashMap;
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 @@ -46,53 +39,18 @@ public class Web3Connection implements Connection {
public static final String RECEIPT_SUCCESS = "0x1";

private final ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
private List<ResourceInfo> resourceInfoList = new ArrayList<>();
private final List<ResourceInfo> resourceInfoList = new ArrayList<>();
private ConnectionEventHandler eventHandler;
private final Map<String, String> properties = new HashMap<>();
private final Map<String, String> properties = new ConcurrentHashMap<>();
private final ClientWrapper clientWrapper;
private final BigInteger chainId;
private final String stubConfigFilePath;
private long stubConfigFileLastModified;

public Web3Connection(ClientWrapper clientWrapper, Web3StubConfig web3StubConfig)
throws IOException {
public Web3Connection(ClientWrapper clientWrapper, String web3StubConfigPath) 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);
this.stubConfigFilePath = web3StubConfigPath;
}

@Override
Expand Down Expand Up @@ -336,9 +294,16 @@ private void handleAsyncTransactionRequest(Request request, Callback callback) {
}
}

private synchronized void refreshStubConfig(Web3StubConfig web3StubConfig) {
this.resourceInfoList = web3StubConfig.convertToResourceInfos();
public synchronized void refreshStubConfig(Web3StubConfig web3StubConfig) {
// update local
if (!resourceInfoList.isEmpty()) {
resourceInfoList.clear();
}
resourceInfoList.addAll(web3StubConfig.convertToResourceInfos());

if (!properties.isEmpty()) {
properties.clear();
}
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());
Expand All @@ -352,6 +317,11 @@ private synchronized void refreshStubConfig(Web3StubConfig web3StubConfig) {
this.addAbi(name, resource.getAbi());
}
}

// update remote
if (Objects.nonNull(eventHandler)) {
eventHandler.onResourcesChange(resourceInfoList);
}
}

public boolean hasProxyDeployed() {
Expand Down Expand Up @@ -389,4 +359,12 @@ public ClientWrapper getClientWrapper() {
public BigInteger getChainId() {
return chainId;
}

public String getStubConfigFilePath() {
return stubConfigFilePath;
}

public ConnectionEventHandler getEventHandler() {
return eventHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ public class Web3ConnectionFactory {
public static Web3Connection build(String stubConfigPath, String configName) throws Exception {
Web3StubConfigParser web3StubConfigParser =
new Web3StubConfigParser(stubConfigPath, configName);
String configPath = web3StubConfigParser.getConfigPath();
Web3StubConfig web3StubConfig = web3StubConfigParser.loadConfig();
return build(web3StubConfig);
return build(web3StubConfig, configPath);
}

public static Web3Connection build(Web3StubConfig web3StubConfig) throws IOException {
public static Web3Connection build(Web3StubConfig web3StubConfig, String configPath)
throws IOException {
logger.info("web3StubConfig: {}", web3StubConfig);
ClientWrapper clientWrapper = ClientWrapperFactory.createClientWrapperInstance(web3StubConfig);
return new Web3Connection(clientWrapper, web3StubConfig);
Web3Connection web3Connection = new Web3Connection(clientWrapper, configPath);
// init
web3Connection.refreshStubConfig(web3StubConfig);
return web3Connection;
}
}
15 changes: 13 additions & 2 deletions src/main/java/com/webank/wecross/stub/web3/Web3Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.webank.wecross.stub.web3.common.Web3StatusCode;
import com.webank.wecross.stub.web3.common.Web3StubException;
import com.webank.wecross.stub.web3.contract.BlockUtility;
import com.webank.wecross.stub.web3.custom.CommandHandler;
import com.webank.wecross.stub.web3.custom.CommandHandlerDispatcher;
import com.webank.wecross.stub.web3.protocol.request.TransactionParams;
import com.webank.wecross.stub.web3.protocol.response.TransactionPair;
import com.webank.wecross.stub.web3.uaproof.Signer;
Expand Down Expand Up @@ -57,12 +59,14 @@ public class Web3Driver implements Driver {
private final ABICodecJsonWrapper codecJsonWrapper;
private final ABICodec abiCodec;
private final ABIDefinitionFactory abiDefinitionFactory;
private final CommandHandlerDispatcher commandHandlerDispatcher;

public Web3Driver() {
public Web3Driver(CommandHandlerDispatcher commandHandlerDispatcher) {
CryptoSuite cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE);
this.codecJsonWrapper = new ABICodecJsonWrapper(true);
this.abiCodec = new ABICodec(cryptoSuite, true);
this.abiDefinitionFactory = new ABIDefinitionFactory(cryptoSuite);
this.commandHandlerDispatcher = commandHandlerDispatcher;
}

@Override
Expand Down Expand Up @@ -402,7 +406,14 @@ public void asyncCustomCommand(
Account account,
BlockManager blockManager,
Connection connection,
CustomCommandCallback callback) {}
CustomCommandCallback callback) {
CommandHandler commandHandler = commandHandlerDispatcher.matchCommandHandler(command);
if (Objects.isNull(commandHandler)) {
callback.onResponse(new Exception("command not supported: " + command), null);
return;
}
commandHandler.handle(path, args, account, blockManager, connection, callback);
}

@Override
public byte[] accountSign(Account account, byte[] message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface Web3Constant {
String STUB_TOML_NAME = "stub.toml";
String ACCOUNT_TOML_NAME = "account.toml";
String WEB3_STUB_TYPE = "WEB3";
String WEB3_CONTRACT_TYPE = "WEB3_CONTRACT";

String WEB3_PROXY_NAME = StubConstant.PROXY_NAME;
String WEB3_HUB_NAME = StubConstant.HUB_NAME;
Expand All @@ -15,4 +16,6 @@ public interface Web3Constant {
String WEB3_PROPERTY_CHAIN_ID = "WEB3_PROPERTY_CHAIN_ID";
String WEB3_PROPERTY_STUB_TYPE = "WEB3_PROPERTY_STUB_TYPE";
String WEB3_PROPERTY_CHAIN_URL = "WEB3_PROPERTY_CHAIN_URL";

String CUSTOM_COMMAND_REGISTER = "register";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class Web3AccountConfig {

private Account account;

private String accountConfigPath;

public static class Account {
private String type;
private String accountFile;
Expand Down Expand Up @@ -63,22 +61,8 @@ 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
+ ", accountConfigPath='"
+ accountConfigPath
+ '\''
+ '}';
return "Web3AccountConfig{" + "account=" + account + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public Web3AccountConfigParser(String configPath, String configName) {
public Web3AccountConfig loadConfig() throws IOException {
Web3Toml web3Toml = new Web3Toml(getConfigPath());
Toml toml = web3Toml.getToml();
Web3AccountConfig web3AccountConfig = toml.to(Web3AccountConfig.class);
web3AccountConfig.setAccountConfigPath(getConfigPath());
return web3AccountConfig;
return toml.to(Web3AccountConfig.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ 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 @@ -71,8 +69,8 @@ public void setUrl(String url) {
}

public static class Resource {
private String type;
private String name;
private String type;
private String address;
private String abi;

Expand Down Expand Up @@ -151,14 +149,6 @@ 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 @@ -168,9 +158,6 @@ public String toString() {
+ service
+ ", resources="
+ resources
+ ", stubConfigPath='"
+ stubConfigPath
+ '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public Web3StubConfigParser(String configPath, String configName) {
public Web3StubConfig loadConfig() throws IOException {
Web3Toml web3Toml = new Web3Toml(getConfigPath());
Toml toml = web3Toml.getToml();
Web3StubConfig web3StubConfig = toml.to(Web3StubConfig.class);
web3StubConfig.setStubConfigPath(getConfigPath());
return web3StubConfig;
return toml.to(Web3StubConfig.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.webank.wecross.stub.web3.custom;

import com.webank.wecross.stub.Account;
import com.webank.wecross.stub.BlockManager;
import com.webank.wecross.stub.Connection;
import com.webank.wecross.stub.Driver;
import com.webank.wecross.stub.Path;

public interface CommandHandler {
/**
* handle custom command
*
* @param path rule id
* @param args command args
* @param account if needs to sign
* @param blockManager if needs to verify transaction
* @param connection chain connection
* @param callback callback
*/
void handle(
Path path,
Object[] args,
Account account,
BlockManager blockManager,
Connection connection,
Driver.CustomCommandCallback callback);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.webank.wecross.stub.web3.custom;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommandHandlerDispatcher {
private static final Logger logger = LoggerFactory.getLogger(CommandHandlerDispatcher.class);

private final Map<String, CommandHandler> commandMapper = new HashMap<>();

public void registerCommandHandler(String command, CommandHandler commandHandler) {
commandMapper.putIfAbsent(command, commandHandler);
}

public CommandHandler matchCommandHandler(String command) {
CommandHandler commandHandler = commandMapper.get(command);
if (Objects.isNull(commandHandler)) {
logger.warn(" Unsupported command: {}", command);
}
return commandHandler;
}
}
Loading
Loading