24 Commits
v1.6.6 ... raft

Author SHA1 Message Date
dong9297
821add2a62 add write boolean to util 2022-05-26 20:29:33 +08:00
CaiHQ
488fbb4a63 Merge branch 'master' of https://gitee.com/BDWare/common 2022-05-23 17:07:29 +08:00
CaiHQ
6366b2fc1f doc: docker deploy
feat: support docker debug
2022-05-23 17:06:54 +08:00
CaiHQ
f292deae14 refactor: add bdledger dependencies
feat: IRPUtil
feat: support debug in docker
2022-05-20 22:50:13 +08:00
CaiHQ
f746ccb7dc prune: update modules for mvn repository 2022-05-13 23:51:46 +08:00
CaiHQ
6a7b355b18 keepsync 2022-05-05 13:29:02 +08:00
CaiHQ
8d15c15192 Merge branch 'master' of https://gitee.com/BDWare/common 2022-05-05 13:28:49 +08:00
CaiHQ
0df8ff0278 update docker configs 2022-05-05 13:28:29 +08:00
CaiHQ
b78c6a0c98 feat: support onCreateParams 2022-05-02 20:49:37 +08:00
WangXuxin
7a9ce393cd feat: support @RouteInfo byFunc 2022-04-27 10:23:33 +08:00
CaiHQ
8bf4b1c57d feat: support createParam
feat: docker script
2022-04-21 10:18:48 +08:00
CaiHQ
47661ad923 fix: null point exception 2022-04-12 16:30:24 +08:00
CaiHQ
1d3efbdb97 support serialzable json 2022-04-07 18:49:57 +08:00
CaiHQ
4047f03151 feat: support onCreateParams 2022-04-07 15:48:39 +08:00
2d6c0e4874 fix setGlobal 2022-03-16 14:38:04 +08:00
CaiHQ
9856f08003 fix: MultiPointCooperationExecutor 2022-02-18 11:14:11 +08:00
Frank.R.Wu
62533a0da1 feat: update event mechanism
add second centers for event topics
2022-01-20 20:55:49 +08:00
CaiHQ
f77305c0ef upgrade doipsdk 2022-01-08 23:43:21 +08:00
CaiHQ
2ce57c111e fix: binarySearch bugs in TimeSerialIndex
feat: support multi tag time index util
2022-01-06 20:33:46 +08:00
CaiHQ
7032dd3b28 prune: unused jarbackup 2022-01-04 10:46:01 +08:00
CaiHQ
7d543f1085 prune: use stable doip-sdk 2022-01-04 10:16:20 +08:00
yanghuanyu
f0c75d4b3c fix: unpackInternal bug 2021-12-31 15:05:30 +08:00
Frank.R.Wu
43282a3201 feat: remove txHash in REvent.getContentStr
remove txHash in REvent.getContentStr to suit EventRecorder
2021-12-29 20:24:03 +08:00
CaiHQ
94c9b867de optimize receive file 2021-12-29 11:52:24 +08:00
34 changed files with 762 additions and 56 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
/build/
/testoutput/
*/build/*
# Compiled class file
*.class

View File

@@ -1,8 +1,19 @@
plugins {
id 'java'
id 'java-library'
id 'maven-publish'
id 'signing'
id 'com.github.johnrengelman.shadow' version '4.0.2'
}
shadowJar {
classifier = "jar"
dependencies {
include(dependency(fileTree(dir: 'lib', include: '*.jar')))
// exclude(dependency('com.google.code.gson:gson:2.8.6'))
// relocate 'com.google.code.gson', 'irs.com.google.code.gson'
}
}
sourceSets {
main {
java {
@@ -24,24 +35,24 @@ sourceSets {
sourceCompatibility = 1.8
dependencies {
api project(":gmhelper")
api 'org.bdware.bdcontract:gmhelper:0.2.0'
api 'berkeleydb:je:3.2.76'
api 'com.fifesoft:rsyntaxtextarea:3.1.3'
// api 'com.fifesoft:rsyntaxtextarea:3.1.3'
api 'commons-io:commons-io:2.11.0'
api 'io.netty:netty-all:4.1.63.Final'
api 'io.netty:netty-all:4.1.72.Final'
api 'org.antlr:antlr4:4.9.2'
api 'commons-codec:commons-codec:1.5'
api 'org.apache.logging.log4j:log4j-core:2.17.0'
api 'org.apache.logging.log4j:log4j-api:2.17.0'
api 'org.apache.logging.log4j:log4j-core:2.17.2'
api 'org.apache.logging.log4j:log4j-api:2.17.2'
//api 'org.apache.velocity:velocity-engine-core:2.3'
api 'org.rocksdb:rocksdbjni:6.22.1'
api fileTree(dir: 'lib', include: '*.jar')
// api fileTree(dir: 'lib', include: '*.jar')
testImplementation 'junit:junit:4.13.2'
}
group = "org.bdware.sc"
version = "1.5.6"
tasks.processResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
task copyLibs(type: Copy) {
@@ -54,3 +65,82 @@ task copyJar(type: Copy) {
into "./build/output/"
rename { String fileName -> "yjs.jar" }
}
//task classJar(type: Jar, dependsOn: classes) {
// classifier = "jar"
//}
task sourceJar(type: Jar, dependsOn: classes) {
archiveClassifier = "sources"
classifier = "sources"
from sourceSets.main.allSource
}
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
task javadocJar(type: Jar, dependsOn: javadoc) {
archiveClassifier = 'javadoc'
classifier = "javadoc"
exclude {
details -> details.file.getAbsolutePath().contains("/gm/")
}
from javadoc.destinationDir
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId project.group
artifactId "common"
version "${version}"
from components.java
artifact sourceJar
artifact javadocJar
artifact shadowJar
pom {
name = "bdware-common"
description = "common"
url = "https://gitee.com/BDWare/common"
licenses {
license {
name = "Mulan PSL v2"
url = "http://license.coscl.org.cn/MulanPSL2"
}
}
developers {
developer {
id = "dataware"
email = "caihq@pku.edu.cn"
}
}
scm {
connection = "scm:git:https://gitee.com/BDWare/common.git"
developerConnection = "scm:git:https://gitee.com/BDWare/common.git"
url = "https://gitee.com/BDWare/common"
}
}
}
}
repositories {
maven {
name 'bdwareSnapshotRepository'
url 'https://oss.sonatype.org/content/repositories/snapshots'
credentials {
username = "${NEXUS_USERNAME}"
password = "${NEXUS_PASSWORD}"
}
}
maven {
name 'bdwareRepository'
url 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
credentials {
username = "${NEXUS_USERNAME}"
password = "${NEXUS_PASSWORD}"
}
}
}
}
signing {
sign publishing.publications.mavenJava
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,61 @@
package org.bdware.sc;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.bdware.sc.util.JsonUtil;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
public class ContractGenerator {
static VelocityEngine ve = init();
public static String generateAPIContract(long availableDate, String url, String method, Map<String, String> header,
Map<String, String> arg) {
VelocityContext ctx = new VelocityContext();
Template script = ve.getTemplate("org/bdware/sc/sc/datacontract.vm");
String[][] initList = {{"availableDate", availableDate + ""}, {"baseUrl", stringify(url)},
{"method", stringify(method)}, {"headers", convertMap(header)}, {"arg", convertMap(arg)}};
ctx.put("initList", initList);
ctx.put("validate", "YancloudUtil.currentTimeMillis()>Global.availableDate");
ctx.put("validateFailedMsg", "\"key expired\"");
StringWriter writer = new StringWriter();
script.merge(ctx, writer);
return writer.toString();
}
private static VelocityEngine init() {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
return ve;
}
public static String generateFTPContract(long availableDate) {
VelocityContext ctx = new VelocityContext();
Template script = ve.getTemplate("org/bdware/sc/sc/ftpcontract.vm");
String[][] initList = {{"availableDate", availableDate + ""}};
ctx.put("initList", initList);
ctx.put("validate", "YancloudUtil.currentTimeMillis()>Global.availableDate");
StringWriter writer = new StringWriter();
script.merge(ctx, writer);
return writer.toString();
}
private static String stringify(String url) {
return JsonUtil.toJson(url);
}
private static String convertMap(Map<String, String> arg) {
if (arg == null) {
arg = new HashMap<>();
}
return JsonUtil.toJson(arg);
}
}

View File

@@ -111,7 +111,7 @@ public class ContractPort {
KeyValueDBUtil.instance.setValue(CMTables.ContractInfo.toString(), USED_PORTS, JsonUtil.toJson(usedPorts));
}
interface PortVisitor {
public interface PortVisitor {
/**
* check if the port is listened
*
@@ -119,5 +119,6 @@ public class ContractPort {
* @return whether some program listens to the port
*/
boolean visit(int port);
boolean visit(String host,int port);
}
}

View File

@@ -0,0 +1,44 @@
package org.bdware.sc;
import com.android.dx.cf.direct.DirectClassFile;
import com.android.dx.cf.direct.StdAttributeFactory;
import com.android.dx.command.dexer.DxContext;
import com.android.dx.dex.DexOptions;
import com.android.dx.dex.cf.CfOptions;
import com.android.dx.dex.cf.CfTranslator;
import com.android.dx.dex.file.ClassDefItem;
import com.android.dx.dex.file.DexFile;
import java.util.Map;
public class DX {
public static byte[] clzsToDex(Map<String, byte[]> clzs) {
try {
DxContext context = new DxContext();
DexOptions dexOptions = new DexOptions();
dexOptions.minSdkVersion = 26;
CfOptions cfOptions = new CfOptions();
DexFile outputDex = new DexFile(dexOptions);
for (String str : clzs.keySet()) {
DirectClassFile dcf =
new DirectClassFile(
clzs.get(str),
toInternalName(str) + ".class",
cfOptions.strictNameCheck);
dcf.setAttributeFactory(StdAttributeFactory.THE_ONE);
ClassDefItem item =
CfTranslator.translate(
context, dcf, clzs.get(str), cfOptions, dexOptions, outputDex);
outputDex.add(item);
}
return outputDex.toDex(null, false);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String toInternalName(String plain) {
return plain.replaceAll("\\.", "/");
}
}

View File

@@ -21,4 +21,5 @@ public abstract class JSEngine implements ScriptEngine {
*/
public abstract ContractResult loadContract(
Contract contract, ContractNode contractNode, boolean isInsnLim);
}

View File

@@ -80,6 +80,14 @@ public class ByteUtil {
}
}
public static boolean readBoolean(ByteArrayInputStream bi) {
return bi.read() == 0x01;
}
public static void writeBoolean(ByteArrayOutputStream bo, boolean b) {
bo.write(b ? 0x01 : 0x00);
}
public static byte[] readBytes(ByteArrayInputStream bi, int len) {
byte[] ret = new byte[len];
bi.read(ret, 0, len);

View File

@@ -55,11 +55,10 @@ public class ServiceServer extends Thread {
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
b.group(bossGroup, workerGroup)
b.group(bossGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.option(ChannelOption.SO_REUSEADDR, false)

View File

@@ -17,6 +17,7 @@ public class REvent extends SM2Verifiable implements Serializable {
private String sender;
private REventSemantics semantics = REventSemantics.AT_LEAST_ONCE;
private boolean forward = true;
private String center;
public REvent(String topic, REventType type, String content, String requestID) {
this.topic = topic;
@@ -37,13 +38,6 @@ public class REvent extends SM2Verifiable implements Serializable {
return content;
}
public boolean isForward() {
return forward;
}
public void setForward(boolean forward) {
this.forward = forward;
}
public String getHash() {
return hash;
@@ -73,6 +67,22 @@ public class REvent extends SM2Verifiable implements Serializable {
this.semantics = semantics;
}
public boolean isForward() {
return forward;
}
public void setForward(boolean forward) {
this.forward = forward;
}
public String getCenter() {
return center;
}
public void setCenter(String center) {
this.center = center;
}
public void doSignature(String pubKey, String privKey) {
this.setPublicKey(pubKey);
this.doSignature(privKey);
@@ -91,13 +101,12 @@ public class REvent extends SM2Verifiable implements Serializable {
@Override
public String getContentStr() {
return String.format("{\"topic\":\"%s\",\"content\":\"%s\",\"type\":\"%s\"," +
"\"semantics\":\"%s\",\"requestID\":\"%s\",\"txHash\":\"%s\"}",
"\"semantics\":\"%s\",\"requestID\":\"%s\"}",
topic,
content,
type,
semantics,
requestID,
(null == txHash ? "" : txHash));
requestID);
}
public enum REventType {

View File

@@ -1,5 +1,6 @@
package org.bdware.sc.node;
import com.google.gson.JsonElement;
import org.bdware.sc.bean.ContractExecType;
import org.bdware.sc.py.bean.PYPackage;
@@ -18,6 +19,7 @@ public class ContractManifest {
private List<Permission> permissions;
public boolean startAtUnpack;
public String sourcePath;
public JsonElement createParam;
public List<PYPackage> getPyDependences() {
return pyDependences;

View File

@@ -10,11 +10,13 @@ public enum Permission {
RocksDB,
MongoDB,
BDWareTimeSeriesDB,
MultiTagIndexDB,
SM2,
AES,
Ledger,
CM,
DOIP,
IRP,
Async;
public static Permission parse(String str) {

View File

@@ -0,0 +1,14 @@
package org.bdware.sc;
import org.bdware.doip.application.DoipServiceConfig;
import org.bdware.doip.endpoint.doipServer.DoipServiceInfo;
public interface DoipServiceInfoConfigurer {
String getServiceHandle();
String resetServiceConfig(String configJson);
DoipServiceConfig getDoipServiceConfig();
DoipServiceInfo getDoipServiceInfo();
}

View File

@@ -275,6 +275,14 @@ public class YJSPacker {
if (!parent.exists()) {
LOGGER.trace("create directory " + parent.getAbsolutePath() + ": " + parent.mkdirs());
}
if (!target.exists()) {
if (entry.isDirectory()) {
target.mkdirs();
} else {
target.createNewFile();
}
}
if (!entry.isDirectory()) {
FileOutputStream fout = new FileOutputStream(target);
for (int k; (k = zin.read(buff)) > 0; ) {
fout.write(buff, 0, k);
@@ -282,6 +290,7 @@ public class YJSPacker {
fout.close();
zin.closeEntry();
}
}
zin.close();
} catch (Exception e) {
e.printStackTrace();

View File

@@ -40,6 +40,8 @@ public class Contract extends SM2Verifiable implements Serializable {
private boolean stateful = true; // manifest可配置
private YjsType yjsType;
private SerializableJson createParam;
public int getShardingId() {
return this.shardingId;
}
@@ -226,4 +228,13 @@ public class Contract extends SM2Verifiable implements Serializable {
this.buildTime = buildTime;
}
public void setCreateParam(JsonElement ele) {
createParam = new SerializableJson(ele);
}
public JsonElement getCreateParam() {
if (createParam != null)
return createParam.getJsonElement();
return null;
}
}

View File

@@ -23,7 +23,6 @@ public class ContractRequest extends SM2Verifiable
String requesterDOI;
// action = rsaPrivKeyEncoded(aesKey);
String action;
// arg = aesKeyEncoded({action,arg});
JsonElement arg;
String requestID;
boolean fromDebug = false;

View File

@@ -3,5 +3,6 @@ package org.bdware.sc.bean;
public enum DefaultRouteRule {
byRequester,
byArgHash,
byTarget;
byTarget,
byFunc;
}

View File

@@ -1,6 +1,5 @@
package org.bdware.sc.bean;
import com.google.gson.JsonElement;
import org.bdware.sc.node.AnnotationNode;
import org.bdware.sc.node.ContractNode;
import org.bdware.sc.util.JsonUtil;
@@ -9,7 +8,7 @@ import java.io.Serializable;
public class JoinInfo implements Serializable {
public String joinRule;
public JsonElement joinCount;
public int joinCount;
public static JoinInfo create(AnnotationNode annotationNode, ContractNode contractNode) {
JoinInfo info = new JoinInfo();

View File

@@ -9,6 +9,8 @@ import java.io.Serializable;
public class RouteInfo implements Serializable {
public DefaultRouteRule useDefault;
public String funcName;
public static RouteInfo create(AnnotationNode annotationNode, ContractNode contractNode) {
RouteInfo info = new RouteInfo();
info.useDefault = null;

View File

@@ -0,0 +1,25 @@
package org.bdware.sc.bean;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.Serializable;
public class SerializableJson implements Serializable {
transient JsonElement jsonElement;
String content;
public SerializableJson(JsonElement jsonElement) {
this.jsonElement = jsonElement;
if (jsonElement != null)
content = jsonElement.toString();
}
public JsonElement getJsonElement() {
if (jsonElement != null)
return jsonElement;
if (content != null)
return JsonParser.parseString(content);
return null;
}
}

View File

@@ -18,6 +18,7 @@ import java.util.*;
public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
private static final Logger LOGGER = LogManager.getLogger(MultiIndexTimeRocksDBUtil.class);
private final Map<String, TimeSerialIndex> secondaryIndex;
static String primaryKey = "_DB_primary_";
public String dbPath;
public String tableName;
Random random = new Random();
@@ -42,7 +43,7 @@ public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
Options options = new Options();
options.setCreateIfMissing(true);
File lockFile = new File(file, "LOCK");
File timeIndex = new File(dbPath + "/" + tableName + "/DB.primary.timeindex");
File timeIndex = new File(dbPath + "/" + tableName + "/" + primaryIndex + ".timeindex");
LOGGER.trace("create directory " + file.getAbsolutePath() + ": " + file.mkdirs());
LOGGER.trace("delete file" + lockFile.getAbsolutePath() + ": " + lockFile.delete());
try {
@@ -51,8 +52,16 @@ public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
} catch (RocksDBException e) {
e.printStackTrace();
}
primaryIndex = new TimeSerialIndex(timeIndex.getAbsolutePath());
secondaryIndex.put(primaryKey, primaryIndex);
for (File f : timeIndex.getParentFile().listFiles()) {
if (!f.getName().endsWith(".timeindex")) continue;
if (f.getName().equals(timeIndex.getName())) continue;
TimeSerialIndex index =
new TimeSerialIndex(f.getAbsolutePath());
secondaryIndex.putIfAbsent(f.getName().substring(0,
f.getName().length() - ".timeindex".length()), index);
}
}
public synchronized void put(String label, String val) {
@@ -141,8 +150,13 @@ public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
public long size(String label) {
try {
if (label == null || label.length() == 0) {
return primaryIndex.size();
}
if (secondaryIndex.containsKey(label)) {
TimeSerialIndex index = getIndex(label);
if (index != null) return index.size();
return index.size();
}
} catch (Exception e) {
e.printStackTrace();
}
@@ -152,17 +166,23 @@ public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
public List<JsonObject> queryByOffset(String label, long offset, int count) {
List<JsonObject> ret = new ArrayList<>();
TimeSerialIndex index = getIndex(label);
List<Long> data = index.request(offset, count);
for (Long l : data) {
List<TimeSerialIndex.IndexEntry> data = index.requestIndexEntry(offset, count);
for (TimeSerialIndex.IndexEntry entry : data) {
try {
String t = new String(db.get(longToByte(l)));
String t = new String(db.get(longToByte(entry.value)));
JsonObject jo;
if (!t.isEmpty()) {
try {
jo = JsonUtil.parseStringAsJsonObject(t);
} catch (Exception e) {
jo = new JsonObject();
jo.addProperty("data", t);
}
} else {
jo = new JsonObject();
}
jo.addProperty("key", l.toString());
jo.addProperty("key", entry.value);
jo.addProperty("timestamp", entry.key);
ret.add(jo);
} catch (Exception e) {
e.printStackTrace();
@@ -181,7 +201,7 @@ public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
}
private TimeSerialIndex getIndex(String label) {
if (null == label) {
if (null == label || label.length() == 0) {
return primaryIndex;
}
if (secondaryIndex.containsKey(label)) {
@@ -275,6 +295,31 @@ public class MultiIndexTimeRocksDBUtil implements MultiIndexTimeDBUtilIntf {
// manuellyIndex可参考LenVarTimeIndex写法
}
public void close() {
db.close();
}
public List<String> getIndexStartWith(String prefix) {
List<String> ret = new ArrayList<>();
if (prefix == null) {
prefix = "";
}
for (String key : secondaryIndex.keySet()) {
if (key.startsWith(prefix))
ret.add(key);
}
ret.remove(primaryKey);
return ret;
}
public List<String> getAllIndexKey() {
Set<String> data = secondaryIndex.keySet();
List<String> ret = new ArrayList<>();
ret.addAll(data);
ret.remove(primaryKey);
return ret;
}
// TODO
static class BytesPair {
byte[] key, value;

View File

@@ -85,6 +85,39 @@ public class TimeSerialIndex {
} else return new ArrayList<>();
}
public static class IndexEntry {
public long key, value;
}
public synchronized List<IndexEntry> requestIndexEntry(long offset, int len) {
List<IndexEntry> ret = new ArrayList<>();
if (offset < 0) offset = 0;
if (offset < fileSize) {
long pos = 0;
try {
pos = file.getFilePointer();
file.seek(2L * offset * 8L);
for (; offset < fileSize && len > 0; len--) {
IndexEntry entry = new IndexEntry();
entry.key = file.readLong();
entry.value = file.readLong();
ret.add(entry);
offset++;
}
file.seek(pos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
file.seek(pos);
} catch (IOException e) {
e.printStackTrace();
}
}
return ret;
} else return new ArrayList<>();
}
private long getIndex(long offset) {
if (offset < fileSize) {
try {
@@ -109,7 +142,7 @@ public class TimeSerialIndex {
public synchronized long findNearest(long timeStamp) {
try {
long pos = file.getFilePointer();
long ret = binarySearch(0L, fileSize + 1, timeStamp);
long ret = binarySearch(0L, fileSize, timeStamp);
file.seek(pos);
return ret;
} catch (IOException e) {
@@ -120,9 +153,14 @@ public class TimeSerialIndex {
}
private long binarySearch(long start, long end, long timeStamp) {
if (start >= end - 1) return start;
if (end <= 0) return end;
if (start >= end - 1) {
long key = getIndex(end - 1);
if (key >= timeStamp) {
return end - 1;
} else return end;
}
long mid = (start + end) / 2;
if (mid >= end - 1) return mid;
long key = getIndex(mid);
if (key >= timeStamp) {
return binarySearch(start, mid, timeStamp);

View File

@@ -0,0 +1,15 @@
package org.bdware.sc.commParser;
public class BDLedgerAdapter {
public Transaction tx;
public org.bdware.bdledger.api.grpc.pb.CommonProto.Transaction ledgerChain;
public BDLedgerAdapter(org.bdware.bdledger.api.grpc.pb.CommonProto.Transaction chaintrans) {
super();
this.ledgerChain = chaintrans;
this.tx = new Transaction();
this.tx.Data = chaintrans.getData().toStringUtf8().getBytes();
this.tx.SrcID = chaintrans.getFrom().toByteArray(); //本地库中SrcID可以是key
this.tx.Txid = chaintrans.getHash().toByteArray();
}
}

View File

@@ -0,0 +1,69 @@
package org.bdware.sc.contractGen;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.bdware.sc.util.JsonUtil;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
public class ContractGenerator {
static VelocityEngine ve = init();
public static String generateAPIContract(Map<String, String> arg) {
VelocityContext ctx = new VelocityContext();
Template script = ve.getTemplate("org/bdware/sc/sc/contractGen/timeindexcontract.vm");
ctx.put("indexData", arg.get("indexData"));
ctx.put("datalen", arg.get("datalen"));
ctx.put("sdkpath", arg.get("sdkpath"));
ctx.put("syncbyheight", arg.get("syncbyheight"));
StringWriter writer = new StringWriter();
script.merge(ctx, writer);
return writer.toString();
}
public static String generateIndexContract(long availableDate, String url, String method, Map<String, String> header,
Map<String, String> arg) {
VelocityContext ctx = new VelocityContext();
Template script = ve.getTemplate("com/yancloud/sc/indexcontract.vm");
String onblock = "var hash = com.yancloud.sc.util.HashUtil.str16ToBytes(args);\n print(hash)";
ctx.put("onBlock", onblock);
StringWriter writer = new StringWriter();
script.merge(ctx, writer);
return writer.toString();
}
private static VelocityEngine init() {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
return ve;
}
public static String generateFTPContract(long availableDate) {
VelocityContext ctx = new VelocityContext();
Template script = ve.getTemplate("org/bdware/sc/sc/ftpcontract.vm");
String[][] initList = {{"availableDate", availableDate + ""}};
ctx.put("initList", initList);
ctx.put("validate", "YancloudUtil.currentTimeMillis()>Global.availableDate");
StringWriter writer = new StringWriter();
script.merge(ctx, writer);
return writer.toString();
}
private static String stringify(String url) {
return JsonUtil.toJson(url);
}
private static String convertMap(Map<String, String> arg) {
if (null == arg)
arg = new HashMap<>();
return JsonUtil.toJson(arg);
}
}

View File

@@ -0,0 +1,131 @@
package org.bdware.sc.contractGen;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
public class ContractGeneratorUI {
public static void main(String[] args) {
try {
BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InitGlobalFont(new Font("alias", Font.PLAIN, 15));
JFrame frame = new JFrame("索引生成");//创建Frame窗口
JPanel checkbox_panel;
final JCheckBox checkBox_time;
final JCheckBox checkBox_account;
checkbox_panel = new JPanel();//面板索引选择
checkBox_time = new JCheckBox("时间索引");
checkBox_account = new JCheckBox("账户索引");
checkbox_panel.add(checkBox_time);
checkbox_panel.add(checkBox_account);
final JPanel input_sdk = new JPanel();//SDK包名;
final JLabel sdk_label = new JLabel("SDK类名");
final JTextField sdk = new JTextField(10);
final JLabel datalen_label = new JLabel("数据长度");
final JTextField data_length = new JTextField(10);
final JCheckBox height_sync_cbox = new JCheckBox("使用高度同步");
input_sdk.add(sdk_label);
input_sdk.add(sdk);
input_sdk.add(datalen_label);
input_sdk.add(data_length);
input_sdk.add(height_sync_cbox);
final JTabbedPane tab = new JTabbedPane();//选项卡
final JPanel cards = new JPanel(); //卡片式布局的面板
cards.add(input_sdk);
cards.add(checkbox_panel);
cards.add(tab);
frame.add(cards);
frame.setBounds(100, 200, 800, 800);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Map<String, MainFrame> framemap = new HashMap<>();
checkBox_time.addActionListener(e -> {
System.out.println("performed");
if (checkBox_time.isSelected()) {
if (sdk.getText().length() != 0 && data_length.getText().length() != 0) {
Map<String, String> map = new HashMap<>();
map.put("indexData", "./RocksDB");
map.put("datalen", data_length.getText());
map.put("sdkpath", sdk.getText());
if (height_sync_cbox.isSelected()) {
map.put("syncbyheight", "true");
}
String res = ContractGenerator.generateAPIContract(map);
MainFrame time_panel = new MainFrame();
framemap.put("time", time_panel);
time_panel.textfield.setText(res);
tab.add(time_panel, "时间索引");
try {
File file = new File("./output/timeindex.yjs");
file.createNewFile();
FileWriter writer = new FileWriter(file);
BufferedWriter out = new BufferedWriter(writer);
out.write(res);
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} else {
tab.remove(framemap.get("time"));
}
});
checkBox_account.addActionListener(e -> {
if (checkBox_account.isSelected()) {
Map<String, String> map = new HashMap<>();
map.put("indexData", "./RocksDB");
String res = ContractGenerator.generateAPIContract(map);
MainFrame account_panel = new MainFrame();
framemap.put("account", account_panel);
account_panel.textfield.setText(res);
tab.add(account_panel, "账户索引");
try {
File file = new File("./output/accountindex.yjs");
file.createNewFile();
FileWriter writer = new FileWriter(file);
BufferedWriter out = new BufferedWriter(writer);
out.write(res);
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
tab.remove(framemap.get("account"));
}
});
}
/**
* 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体
*/
private static void InitGlobalFont(Font font) {
FontUIResource fontRes = new FontUIResource(font);
for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, fontRes);
}
}
}
}

View File

@@ -0,0 +1,55 @@
package org.bdware.sc.contractGen;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
public class MainFrame extends JPanel {
final RSyntaxTextArea textfield;
JLabel sdk_label;
JLabel datalen_label;
JPanel p1;
JPanel p2;
JButton generate;
JButton reset;
public MainFrame() {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
p1 = new JPanel();//面板OnBlock
p2 = new JPanel();//面板OnBlock
generate = new JButton("生成合约");
reset = new JButton("重置内容");
p2.add(generate);
p2.add(reset);
this.textfield = new RSyntaxTextArea(30, 80);//height,width
textfield.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT);
textfield.setCodeFoldingEnabled(true);
JScrollPane scroll = new JScrollPane(textfield);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
p1.add(scroll);
this.add(p1);
this.add(p2);
reset.addActionListener(arg0 -> {
// TODO Auto-generated method stub
textfield.setText("");
});
generate.addActionListener(e -> {
// TODO Auto-generated method stub
// use new file to generate yjs
Map<String, String> map = new HashMap<>();
map.put("indexData", "./RocksDB");
String res = ContractGenerator.generateAPIContract(map);
});
}
}

View File

@@ -0,0 +1,15 @@
filter.threshold.type=ThresholdFilter
filter.threshold.level=debug
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%highlight{[%-5p] %d{HH:mm:ss.SSS} %m (%F:%L)[%M]%n}{FATAL=Bright Red,ERROR=Red,WARN=Yellow,INFO=Green,DEBUG=Blue,TRACE=White}
appender.rolling.type=File
appender.rolling.name=log
appender.rolling.append=true
appender.rolling.fileName=./log/cm.log
appender.rolling.layout.type=PatternLayout
appender.rolling.layout.pattern=%d-%m%n
rootLogger.level=info
rootLogger.appenderRef.stdout.ref=STDOUT
rootLogger.appenderRef.log.ref=log

View File

@@ -0,0 +1,51 @@
package org.bdware;
import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.sc.db.MultiIndexTimeRocksDBUtil;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
public class MultiIndextTimeRocksDBUtilTest {
static Logger LOGGER = LogManager.getLogger(MultiIndextTimeRocksDBUtilTest.class);
MultiIndexTimeRocksDBUtil util;
@Before
public void init() {
util = new MultiIndexTimeRocksDBUtil("testoutput/timeIndexDB", "defaultTable");
}
@Test
public void put() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
util.put("abc_123", "ddd " + System.currentTimeMillis());
}
}
@Test
public void listAll() {
List<JsonObject> tt = util.queryByOffset(null, 0, 100);
for (int i = 0; i < tt.size(); i++) {
long val = tt.get(i).get("timestamp").getAsLong();
LOGGER.info(String.format("pre %d cu %d suc %d", util.queryOffset("abc_123", val - 1L),
util.queryOffset("abc_123", val), util.queryOffset("abc_123", val + 1L), util.queryOffset("abc_123", val - 1L)));
}
}
@Test
public void run() {
LOGGER.info(util.queryOffset("abc_123", 1641454140657L));
LOGGER.info(util.queryOffset("abc_123", 1641454140669L));
LOGGER.info(util.queryOffset("abc_123", 1641450028482L));
LOGGER.info(util.queryOffset("abc_123", 1641450028483L));
}
}

View File

@@ -1,20 +1,16 @@
package org.bdware.sc;
import org.bdware.bdledger.api.grpc.Client;
//import org.bdware.bdledger.api.grpc.pb.QueryOuterClass;
import org.bdware.bdledger.api.grpc.pb.QueryProto;
import org.junit.Test;
public class LedgerUtilTest {
@Test
public void query(){
String str = "0828c62576606e27ef8d079dea60cadd6eb03351";
Client c = new Client("022.node.internetapi.cn",21121);
QueryProto.GetTransactionByHashResponse syncResult = c.getTransactionByHashSync("default", "5c79fb8d71771615a5c8173b8089cd4841c3adfb");
System.out.println(new String(syncResult.getTransaction().getData().toByteArray()));
System.out.println(syncResult.getTransaction().getData().toByteArray().length);
// String str = "0828c62576606e27ef8d079dea60cadd6eb03351";
// Client c = new Client("022.node.internetapi.cn",21121);
// QueryProto.GetTransactionByHashResponse syncResult = c.getTransactionByHashSync("default", "5c79fb8d71771615a5c8173b8089cd4841c3adfb");
// System.out.println(new String(syncResult.getTransaction().getData().toByteArray()));
// System.out.println(syncResult.getTransaction().getData().toByteArray().length);
// 09247.107.94.135
// 093120.79.215.187
// QueryOuterClass.GetTransactionByHashResponse hash = c.getTransactionByHashSync("test", str);

View File

@@ -1,5 +1,6 @@
package org.bdware.sc.boundary.utils;
import org.bdware.sc.bean.ContractRequest;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
@@ -10,7 +11,6 @@ import org.junit.Test;
import org.zz.gmhelper.BCECUtil;
import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util;
import sun.misc.BASE64Encoder;
import java.math.BigInteger;
import java.security.KeyPair;
@@ -19,14 +19,27 @@ public class SM2UtilTest {
String keyPairStr =
"{\"publicKey\":\"0442e39c0b1623e6f95ef896d37f41aa3cd98fbc3aea71250eedba4335c6c3aff51b5fa2352f920d1063b50a30738427c18375df886985a63e0f496293fdad22a8\",\"privateKey\":\"b7e504cf951792554bf4a9874be37cb1279d38939444388e2046ee9603b4f0c4\"}";
SM2KeyPair keyPair = SM2KeyPair.fromJson(keyPairStr);
@Test
public void verify2(){
String toVerify = "GeneralContract|plainTextTransmission|{\"ledger\":\"test\",\"data\":\"上链信息为json格式\"}|04398dfde44290595cd098cd2f904b36367c69f9011719d43fb0955f823cf1386764769bc7c0a5649dcb316d552998a5c106afd268d9db8b6482ce527544a7bd15";
String sign = "30440220137771d7505abde700c89cfa376f67c564dfdec6ffdb08cb08ff70544be6cb6f0220587296439bedf43f4b0bfec2d7030a8250d369e53af623e50a734e3b6cb780cb";
String pubkey = "04398dfde44290595cd098cd2f904b36367c69f9011719d43fb0955f823cf1386764769bc7c0a5649dcb316d552998a5c106afd268d9db8b6482ce527544a7bd15";
ContractRequest cr = new ContractRequest();
cr.setContractID("GeneralContract");
cr.setAction("plainTextTransmission");
cr.setArg("{\"ledger\":\"test\",\"data\":\"上链信息为json格式\"}");
cr.setPublicKey("04398dfde44290595cd098cd2f904b36367c69f9011719d43fb0955f823cf1386764769bc7c0a5649dcb316d552998a5c106afd268d9db8b6482ce527544a7bd15");
cr.setSignature("30440220137771d7505abde700c89cfa376f67c564dfdec6ffdb08cb08ff70544be6cb6f0220587296439bedf43f4b0bfec2d7030a8250d369e53af623e50a734e3b6cb780cb");
System.out.println(cr.verifySignature());
}
@Test
public void base64() {
String gstr =
"09146332716e7767327923056946406e49570e5909146332716e7767327923056946406e49570e59";
byte[] bytes = ByteUtils.fromHexString(gstr);
String str = new BASE64Encoder().encode(bytes);
System.out.println(str);
// String str = new BASE64Encoder().encode(bytes);
// System.out.println(str);
}
@Test