Browse Source

Solve the bug that the contract plug-in file cannot be deleted!

tags/1.1.1^2
shaozhuguang 5 years ago
parent
commit
3078572052
2 changed files with 57 additions and 6 deletions
  1. +38
    -0
      source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayInterceptServiceHandler.java
  2. +19
    -6
      source/ledger/ledger-model/src/main/java/com/jd/blockchain/contract/ContractJarUtils.java

+ 38
- 0
source/gateway/src/main/java/com/jd/blockchain/gateway/service/GatewayInterceptServiceHandler.java View File

@@ -5,15 +5,26 @@ import com.jd.blockchain.gateway.PeerService;
import com.jd.blockchain.ledger.ContractCodeDeployOperation;
import com.jd.blockchain.ledger.Operation;
import com.jd.blockchain.ledger.TransactionRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.net.URL;

@Service
public class GatewayInterceptServiceHandler implements GatewayInterceptService {

private static String contractsPath;

@Autowired
private PeerService peerService;

static {
contractsPath = jarRootDir();
}

@Override
public void intercept(TransactionRequest txRequest) {
// 当前仅处理合约发布的请求
@@ -29,7 +40,34 @@ public class GatewayInterceptServiceHandler implements GatewayInterceptService {
}

private void contractCheck(final ContractCodeDeployOperation contractOP) {

// 校验chainCode
ContractJarUtils.verify(contractOP.getChainCode());
}

private static String jarRootDir() {

try {
URL url = GatewayInterceptServiceHandler.class.getProtectionDomain().getCodeSource().getLocation();
String currPath = java.net.URLDecoder.decode(url.getPath(), "UTF-8");
if (currPath.contains("!/")) {
currPath = currPath.substring(5, currPath.indexOf("!/"));
}
if (currPath.endsWith(".jar")) {
currPath = currPath.substring(0, currPath.lastIndexOf("/") + 1);
}
File file = new File(currPath);

String homeDir = file.getParent();

String jarRootPath = homeDir + File.separator + "contracts";

FileUtils.forceMkdir(new File(jarRootPath));

return jarRootPath;

} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}

+ 19
- 6
source/ledger/ledger-model/src/main/java/com/jd/blockchain/contract/ContractJarUtils.java View File

@@ -110,16 +110,16 @@ public class ContractJarUtils {
return dotClassName;
}

public static void verify(byte[] chainCode) {
public static void verify(String contractPath, byte[] chainCode) {
if (chainCode == null || chainCode.length == 0) {
throw new IllegalStateException("Contract's chaincode is empty !!!");
}
// 首先生成合约文件
File jarFile = newJarFile();
File jarFile = newJarFile(contractPath);
try {
FileUtils.writeByteArrayToFile(jarFile, chainCode);
// 校验合约文件
verify(jarFile);
verify(contractPath, jarFile);
} catch (Exception e) {
throw new IllegalStateException(e);
} finally {
@@ -132,7 +132,11 @@ public class ContractJarUtils {
}
}

private static void verify(File jarFile) throws Exception {
public static void verify(byte[] chainCode) {
verify(null, chainCode);
}

private static void verify(String contractPath, File jarFile) throws Exception {
// 首先判断jarFile中是否含有META-INF/JDCHAIN.TXT,并将其读出
URL jarUrl = new URL("jar:file:" + jarFile.getPath() + "!/" + CONTRACT_MF);
InputStream inputStream = jarUrl.openStream();
@@ -152,7 +156,7 @@ public class ContractJarUtils {
String txt = new String(bytes, StandardCharsets.UTF_8);

// 生成新的Jar包文件,该文件路径与JarFile基本一致
File tempJar = newJarFile();
File tempJar = newJarFile(contractPath);

// 复制除JDCHAIN.TXT之外的部分
copy(jarFile, tempJar, null, null, CONTRACT_MF);
@@ -223,7 +227,16 @@ public class ContractJarUtils {
}
}

private static File newJarFile() {
private static File newJarFile(String contractPath) {

if (contractPath != null && contractPath.length() > 0) {
return new File(contractPath + File.separator +
"contract-" +
System.currentTimeMillis() + "-" +
System.nanoTime() + "-" +
FILE_RANDOM.nextInt(1024) +
".jar");
}
return new File("contract-" +
System.currentTimeMillis() + "-" +
System.nanoTime() + "-" +


Loading…
Cancel
Save