mirror of
https://gitee.com/BDWare/ypk-deploy-tool
synced 2026-01-29 08:09:27 +00:00
release 0.4.0
This commit is contained in:
137
src/main/java/org/bdware/ypkdeploy/HTTPTool.java
Normal file
137
src/main/java/org/bdware/ypkdeploy/HTTPTool.java
Normal file
@@ -0,0 +1,137 @@
|
||||
package org.bdware.ypkdeploy;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.bdware.client.ResultCallback;
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
import org.zz.gmhelper.SM2Util;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class HTTPTool {
|
||||
final static String TAG = "[HTTPTool] ";
|
||||
|
||||
static class DeployConfig {
|
||||
//common config
|
||||
String host;
|
||||
String ypkPath;
|
||||
String privateKey;
|
||||
String publicKey;
|
||||
String killBeforeStart;
|
||||
JsonElement createParam;
|
||||
//config for http deploy
|
||||
int agentPort;
|
||||
}
|
||||
|
||||
|
||||
public static void batchRun(String path, boolean restart) {
|
||||
DeployConfig config = null;
|
||||
try {
|
||||
config = new Gson().fromJson(new FileReader(path), DeployConfig.class);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println(TAG + " parse config failed!");
|
||||
}
|
||||
deployUseHttp(config);
|
||||
SM2KeyPair keyPair = SM2KeyPair.fromJson(new Gson().toJson(config));
|
||||
SmartContractClientExt ext = new SmartContractClientExt(String.format("ws://%s:%d/SCIDE/SCExecutor", config.host, config.agentPort), keyPair);
|
||||
ext.waitForConnect();
|
||||
ext.login();
|
||||
for (; !ext.isLoggedIn; )
|
||||
Thread.yield();
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
try {
|
||||
|
||||
if (config.killBeforeStart != null)
|
||||
ext.kill(config.killBeforeStart, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(JsonObject r) {
|
||||
System.out.println(TAG + r);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
});
|
||||
for (; counter.get() == 0; )
|
||||
Thread.yield();
|
||||
;
|
||||
Thread.sleep(200);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
File f = new File(config.ypkPath);
|
||||
ext.startContract(f.getName()
|
||||
.replaceAll(".zip", "")
|
||||
.replaceAll(".ypk", ""), config.createParam, new ResultCallback() {
|
||||
@Override
|
||||
public void onResult(JsonObject r) {
|
||||
System.out.println(TAG + r);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
});
|
||||
for (; counter.get() == 1; )
|
||||
Thread.yield();
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
public static void deployUseHttp(DeployConfig config) {
|
||||
String url = "http://%s:%d/Upload?%s&sign=%s";
|
||||
File file = new File(config.ypkPath);
|
||||
String path = config.ypkPath;
|
||||
String argWithoutSig = "path=%s&fileName=%s&isPrivate=true&order=%d&count=%d&pubKey=%s";
|
||||
SM2KeyPair keyPair = SM2KeyPair.fromJson(String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", config.publicKey, config.privateKey));
|
||||
String pubKey = keyPair.getPublicKeyStr();
|
||||
int trunc = 490 * 1024;
|
||||
byte[] buff = new byte[trunc];
|
||||
int order = 0;
|
||||
int count = (int) ((file.length() - 1) / (trunc)) + 1;
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
for (int len = 0; (len = fin.read(buff)) > 0; order++) {
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
String arg = String.format(argWithoutSig, "./", file.getName(), order, count, pubKey);
|
||||
String sign = ByteUtils.toHexString(SM2Util.sign(keyPair.getPrivateKeyParameter(), arg.getBytes(StandardCharsets.UTF_8)));
|
||||
String urlStr = String.format(url, config.host, config.agentPort, arg, sign);
|
||||
HttpPost httpPost = new HttpPost(urlStr);
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
if (len == trunc)
|
||||
builder.addBinaryBody(
|
||||
"file", buff,
|
||||
ContentType.APPLICATION_OCTET_STREAM, file.getName());
|
||||
else {
|
||||
byte[] bu = new byte[len];
|
||||
System.arraycopy(buff, 0, bu, 0, len);
|
||||
builder.addBinaryBody(
|
||||
"file", bu,
|
||||
ContentType.APPLICATION_OCTET_STREAM, file.getName());
|
||||
|
||||
}
|
||||
HttpEntity multipart = builder.build();
|
||||
httpPost.setEntity(multipart);
|
||||
CloseableHttpResponse response = client.execute(httpPost);
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
response.getEntity().writeTo(bo);
|
||||
System.out.println("[YpkDeployTool] " + order
|
||||
+ " " + count + " " + bo);
|
||||
}
|
||||
} catch (IOException | CryptoException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
batchRun("./deployconfig.json", true);
|
||||
}
|
||||
}
|
||||
308
src/main/java/org/bdware/ypkdeploy/SmartContractClientExt.java
Normal file
308
src/main/java/org/bdware/ypkdeploy/SmartContractClientExt.java
Normal file
@@ -0,0 +1,308 @@
|
||||
package org.bdware.ypkdeploy;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bdware.client.ResultCallback;
|
||||
import org.bdware.client.SmartContractClient;
|
||||
import org.bdware.client.ws.Action;
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.zz.gmhelper.SM2KeyPair;
|
||||
import org.zz.gmhelper.SM2Util;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SmartContractClientExt extends SmartContractClient {
|
||||
private static final Logger LOGGER = LogManager.getLogger(SmartContractClientExt.class);
|
||||
public int count = 0;
|
||||
public boolean isLoggedIn = false;
|
||||
public String nodePubkey = null;
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
|
||||
public SmartContractClientExt(String uri, SM2KeyPair pair) {
|
||||
super(uri, pair);
|
||||
}
|
||||
|
||||
public void startContract(String project) {
|
||||
startContract(project, null, null);
|
||||
}
|
||||
|
||||
public void startContract(String project, JsonElement createParam) {
|
||||
startContract(project, createParam, null);
|
||||
}
|
||||
|
||||
public void startContractByYpk(String path, boolean isPrivate, JsonElement createParam, ResultCallback rc) {
|
||||
String reqID = System.currentTimeMillis() + "";
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "startContractByYPK");
|
||||
ret.put("isPrivate", isPrivate);
|
||||
ret.put("owner", getKeyPair().getPublicKeyStr());
|
||||
ret.put("requestID", reqID);
|
||||
ret.put("createParam", createParam);
|
||||
ret.put("path", path);
|
||||
String content =
|
||||
String.format("Sole|%s|%s", ret.get("path"), getKeyPair().getPublicKeyStr());
|
||||
String sig;
|
||||
try {
|
||||
sig =
|
||||
ByteUtils.toHexString(
|
||||
SM2Util.sign(
|
||||
getKeyPair().getPrivateKeyParameter(), content.getBytes()));
|
||||
ret.put("signature", sig);
|
||||
} catch (CryptoException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (rc != null) {
|
||||
cbs.put(reqID, rc);
|
||||
}
|
||||
this.sendMsg(new Gson().toJson(ret));
|
||||
}
|
||||
|
||||
public void startPublicContract(String project, JsonElement createParam, ResultCallback rc) {
|
||||
String reqID = System.currentTimeMillis() + "";
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "startContract");
|
||||
ret.put("isPrivate", false);
|
||||
ret.put("owner", getKeyPair().getPublicKeyStr());
|
||||
ret.put("requestID", reqID);
|
||||
ret.put("path", "/" + project + "/manifest.json");
|
||||
ret.put("createParam", createParam);
|
||||
String content =
|
||||
String.format("Sole|%s|%s", ret.get("path"), getKeyPair().getPublicKeyStr());
|
||||
String sig;
|
||||
try {
|
||||
sig =
|
||||
ByteUtils.toHexString(
|
||||
SM2Util.sign(
|
||||
getKeyPair().getPrivateKeyParameter(), content.getBytes()));
|
||||
ret.put("signature", sig);
|
||||
} catch (CryptoException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ret.put("script", "empty");
|
||||
if (rc != null) {
|
||||
cbs.put(reqID, rc);
|
||||
}
|
||||
this.sendMsg(new Gson().toJson(ret));
|
||||
}
|
||||
|
||||
public void startContract(String project, JsonElement createParam, ResultCallback rc) {
|
||||
String reqID = System.currentTimeMillis() + "";
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "startContract");
|
||||
ret.put("isPrivate", true);
|
||||
ret.put("owner", getKeyPair().getPublicKeyStr());
|
||||
ret.put("requestID", reqID);
|
||||
ret.put("path", "/" + project + "/manifest.json");
|
||||
ret.put("createParam", createParam);
|
||||
String content =
|
||||
String.format("Sole|%s|%s", ret.get("path"), getKeyPair().getPublicKeyStr());
|
||||
String sig;
|
||||
try {
|
||||
sig =
|
||||
ByteUtils.toHexString(
|
||||
SM2Util.sign(
|
||||
getKeyPair().getPrivateKeyParameter(), content.getBytes()));
|
||||
ret.put("signature", sig);
|
||||
} catch (CryptoException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ret.put("script", "empty");
|
||||
if (rc != null) {
|
||||
cbs.put(reqID, rc);
|
||||
}
|
||||
this.sendMsg(new Gson().toJson(ret));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLogin(JsonObject obj) {
|
||||
LOGGER.info(obj.toString());
|
||||
isLoggedIn = true;
|
||||
}
|
||||
|
||||
public void startMultiContractByScript(String script, String peersID, JsonElement createParam, ResultCallback rc) {
|
||||
String reqID = System.currentTimeMillis() + "" + secureRandom.nextInt();
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "startContractMultiPoint");
|
||||
ret.put("script", script);
|
||||
ret.put("type", 5);
|
||||
ret.put("peersID", peersID);
|
||||
ret.put("owner", getKeyPair().getPublicKeyStr());
|
||||
ret.put("requestID", reqID);
|
||||
ret.put("createParam", createParam);
|
||||
// "Algorithm|" + request.script + "|" + global.sm2Key.publicKey
|
||||
ret.put("signature", doSignature(getKeyPair().getPrivateKeyParameter(), "Sole|" + script + "|" + ret.get("owner")));
|
||||
if (rc != null) {
|
||||
cbs.put(reqID, rc);
|
||||
}
|
||||
String msg = new Gson().toJson(ret);
|
||||
LOGGER.debug(msg);
|
||||
this.sendMsg(msg);
|
||||
}
|
||||
|
||||
public String doSignature(ECPrivateKeyParameters privateKey, String content) {
|
||||
try {
|
||||
return
|
||||
ByteUtils.toHexString(
|
||||
SM2Util.sign(
|
||||
privateKey, content.getBytes()));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class ContractExecType {
|
||||
public static final int Sole = 0; // 单点锚定合约
|
||||
public static final int RequestOnce = 1;// 多点不同步合约
|
||||
public static final int ResponseOnce = 2; // 多点不同步合约,超时的时候再请求一个, 直到获得一个response
|
||||
public static final int RequestAllResponseFirst = 3;
|
||||
public static final int RequestAllResponseHalf = 4;
|
||||
public static final int RequestAllResponseAll = 5;
|
||||
public static final int Sharding = 6;
|
||||
public static final int SelfAdaptiveSharding = 7;
|
||||
public static final int PBFT = 8;
|
||||
|
||||
}
|
||||
|
||||
public void startMultiContract(
|
||||
boolean isPrivate,
|
||||
String ypkName,
|
||||
String peersID,
|
||||
int contractExecType,
|
||||
JsonElement createParam,
|
||||
ResultCallback rc) {
|
||||
String reqID = System.currentTimeMillis() + "" + secureRandom.nextInt();
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "startContractMultiPoint");
|
||||
ret.put("projectName", ypkName);
|
||||
ret.put("isPrivate", isPrivate);
|
||||
ret.put("type", contractExecType);
|
||||
ret.put("peersID", peersID);
|
||||
ret.put("owner", getKeyPair().getPublicKeyStr());
|
||||
ret.put("requestID", reqID);
|
||||
ret.put("createParam", createParam);
|
||||
// "Algorithm|" + request.script + "|" + global.sm2Key.publicKey
|
||||
if (rc != null) {
|
||||
cbs.put(reqID, rc);
|
||||
}
|
||||
String msg = new Gson().toJson(ret);
|
||||
LOGGER.debug(msg);
|
||||
this.sendMsg(msg);
|
||||
}
|
||||
|
||||
public void startContractByScript(String script, ResultCallback rc) {
|
||||
String reqID = System.currentTimeMillis() + "" + Math.random();
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "startContract");
|
||||
ret.put("script", script);
|
||||
ret.put("owner", getKeyPair().getPublicKeyStr());
|
||||
ret.put("requestID", reqID);
|
||||
// "Algorithm|" + request.script + "|" + global.sm2Key.publicKey
|
||||
ret.put(
|
||||
"signature",
|
||||
doSignature(getKeyPair().getPrivateKeyParameter(), "Algorithm|" + script + "|" + ret.get("owner")));
|
||||
if (rc != null) {
|
||||
cbs.put(reqID, rc);
|
||||
}
|
||||
String msg = new Gson().toJson(ret);
|
||||
LOGGER.debug(msg);
|
||||
this.sendMsg(msg);
|
||||
}
|
||||
|
||||
public void kill(String name, ResultCallback rc) {
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "killContractProcess");
|
||||
ret.put("verifiedPubKey", getKeyPair().getPublicKeyStr());
|
||||
ret.put("name", name);
|
||||
String requestID = "kill_" + System.currentTimeMillis();
|
||||
ret.put("requestID", requestID);
|
||||
if (rc != null) {
|
||||
cbs.put(requestID, rc);
|
||||
}
|
||||
this.sendMsg(new Gson().toJson(ret));
|
||||
}
|
||||
|
||||
public void killAllContractProcess() {
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "killAllContract");
|
||||
this.sendMsg(new Gson().toJson(ret));
|
||||
}
|
||||
|
||||
// public void onExecuteResult(JsonObject obj) {
|
||||
// }
|
||||
|
||||
public void genBDCoin(String name) {
|
||||
Map<String, Object> ret = new HashMap<>();
|
||||
ret.put("action", "generateBDCoinProject");
|
||||
ret.put("ContractName", name);
|
||||
ret.put("name", name);
|
||||
}
|
||||
|
||||
public void ping() {
|
||||
this.sendMsg("{\"action\":\"ping\"}");
|
||||
}
|
||||
|
||||
@Action
|
||||
public void pong(JsonObject obj) {
|
||||
count++;
|
||||
}
|
||||
|
||||
@Action
|
||||
public void onStartContractTrustfullyResult(JsonObject jo) {
|
||||
String responseID = jo.get("responseID").getAsString();
|
||||
ResultCallback cb = cbs.get(responseID);
|
||||
if (cb != null) cb.onResult(jo);
|
||||
}
|
||||
|
||||
@Action
|
||||
public void onKillContractProcess(JsonObject jo) {
|
||||
if (!jo.has("responseID")) {
|
||||
LOGGER.error("can't get responseID:" + jo.toString());
|
||||
return;
|
||||
}
|
||||
String responseID = jo.get("responseID").getAsString();
|
||||
ResultCallback cb = cbs.get(responseID);
|
||||
if (cb != null) cb.onResult(jo);
|
||||
}
|
||||
|
||||
public void requestNodeInfo() {
|
||||
this.sendMsg("{\"action\":\"loadNodeConfig\"}");
|
||||
}
|
||||
|
||||
@Action
|
||||
public void onLoadNodeConfig(JsonObject obj) {
|
||||
JsonObject data = obj.get("data").getAsJsonObject();
|
||||
nodePubkey = data.get("nodePubKey").getAsString();
|
||||
}
|
||||
|
||||
public void loginSync() {
|
||||
login();
|
||||
for (; !isLoggedIn; )
|
||||
Thread.yield();
|
||||
}
|
||||
|
||||
@Action
|
||||
public void onQueryAEState(JsonObject obj) {
|
||||
System.out.println("[AEState] " + obj.toString());
|
||||
}
|
||||
|
||||
@Action
|
||||
public void onQueryAECaller(JsonObject obj) {
|
||||
System.out.println("[AECaller] " + obj.toString());
|
||||
}
|
||||
|
||||
public void sendMsgTricky(String msg) {
|
||||
this.sendMsg(msg);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user