mirror of
https://gitee.com/BDWare/common
synced 2026-02-15 09:09:29 +00:00
initial commit
This commit is contained in:
105
src/main/pythongen/org/bdware/sc/commParser/BDLedger/Block.java
Normal file
105
src/main/pythongen/org/bdware/sc/commParser/BDLedger/Block.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package org.bdware.sc.commParser.BDLedger;
|
||||
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Block {
|
||||
public BlockHeader blockheader;
|
||||
public BlockBody blockbody;
|
||||
|
||||
public Block(BlockHeader blockheader, BlockBody blockbody) {
|
||||
super();
|
||||
this.blockheader = blockheader;
|
||||
this.blockbody = blockbody;
|
||||
System.out.println(this.blockbody);
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
try {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
DataOutputStream dd = new DataOutputStream(bo);
|
||||
dd.writeInt(blockheader.index);
|
||||
dd.write(blockheader.hash);
|
||||
dd.writeInt(blockheader.timestamp);
|
||||
dd.writeInt(blockheader.version);
|
||||
dd.write(blockheader.prevblockID);
|
||||
dd.write(blockheader.merkleroot);
|
||||
dd.write(blockheader.creatorID);
|
||||
byte[] bytearr = blockbody.toByteArray();
|
||||
dd.writeInt(bytearr.length);
|
||||
dd.write(bytearr);
|
||||
return bo.toByteArray();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// public static Block fromByteArray(byte[] block) {
|
||||
// BlockHeader header = new BlockHeader();
|
||||
// BlockBody body = new BlockBody();
|
||||
// try {
|
||||
// ByteArrayInputStream bi = new ByteArrayInputStream(block);
|
||||
// DataInputStream dd = new DataInputStream(bi);
|
||||
// header.index = dd.readInt();
|
||||
// header.hash = new byte[20];
|
||||
// dd.read(header.hash);
|
||||
// header.timestamp = dd.readInt();
|
||||
// header.version = dd.readInt();
|
||||
// header.prevblockID = new byte[20];
|
||||
// header.merkleroot = new byte[20];
|
||||
// header.creatorID = new byte[20];
|
||||
// dd.read(header.prevblockID);
|
||||
// dd.read(header.merkleroot);
|
||||
// dd.read(header.creatorID);
|
||||
// int bodysize = dd.readInt();
|
||||
// byte[] bodyarr = new byte[bodysize];
|
||||
// dd.read(bodyarr);
|
||||
// body = BlockBody.fromBytes(bodyarr);
|
||||
// Block genblock = new Block(header,body);
|
||||
// return genblock;
|
||||
// } catch (IOException e) {
|
||||
// // TODO Auto-generated catch block
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
private String[] getPrevBlockID(byte[] frombytes) {
|
||||
DataInputStream in = new DataInputStream(new ByteArrayInputStream(frombytes));
|
||||
String[] prevBlockID = new String[3];
|
||||
byte[] b1 = new byte[20];
|
||||
byte[] b2 = new byte[20];
|
||||
byte[] b3 = new byte[20];
|
||||
try {
|
||||
in.read(b1);
|
||||
in.read(b2);
|
||||
in.read(b3);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
prevBlockID[0] = HashUtil.byteArray2Str(b1);
|
||||
prevBlockID[1] = HashUtil.byteArray2Str(b2);
|
||||
prevBlockID[2] = HashUtil.byteArray2Str(b3);
|
||||
return prevBlockID;
|
||||
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("Index", String.valueOf(blockheader.index));
|
||||
ret.put("hash", HashUtil.byteArray2Str(blockheader.hash));
|
||||
ret.put("timestamp", String.valueOf(blockheader.timestamp));
|
||||
ret.put("version", String.valueOf(blockheader.version));
|
||||
String[] prevBlockID = getPrevBlockID(blockheader.prevblockID);
|
||||
ret.put("prevblockID", prevBlockID[0] + "," + prevBlockID[1] + "," + prevBlockID[2]);
|
||||
ret.put("merkleroot", HashUtil.byteArray2Str(blockheader.merkleroot));
|
||||
ret.put("creatorID", HashUtil.byteArray2Str(blockheader.creatorID));
|
||||
return JsonUtil.toJson(ret);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.bdware.sc.commParser.BDLedger;
|
||||
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockBody {
|
||||
public List<Transaction> trans = new ArrayList<>();
|
||||
|
||||
public static BlockBody fromBytes(byte[] input) {
|
||||
try {
|
||||
DataInputStream bin = new DataInputStream(new ByteArrayInputStream(input));
|
||||
int size = bin.readInt();
|
||||
size = HashUtil.toLittleEndian(size);
|
||||
BlockBody ret = new BlockBody();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Transaction trans = new Transaction();
|
||||
ret.trans.add(trans);
|
||||
trans.srcID = new byte[20];
|
||||
trans.dstID = new byte[20];
|
||||
bin.read(trans.srcID);
|
||||
bin.read(trans.dstID);
|
||||
trans.nonce = HashUtil.toLittleEndian(bin.readLong());
|
||||
trans.type = HashUtil.toLittleEndian(bin.readInt());
|
||||
int len = HashUtil.toLittleEndian(bin.readInt());
|
||||
trans.data = new byte[len];
|
||||
bin.read(trans.data);
|
||||
}
|
||||
return ret;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new BlockBody();
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
byte[] arr = new byte[1];
|
||||
try {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
DataOutputStream bin = new DataOutputStream(bo);
|
||||
bin.writeInt(trans.size());
|
||||
for (int i = 0; i < trans.size(); i++) {
|
||||
Transaction tran = trans.get(i);
|
||||
bin.write(tran.srcID);
|
||||
bin.write(tran.dstID);
|
||||
bin.writeLong(tran.nonce);
|
||||
bin.writeInt(tran.type);
|
||||
int len = tran.data.length;
|
||||
bin.writeInt(len);
|
||||
bin.write(tran.data);
|
||||
}
|
||||
return bo.toByteArray();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.bdware.sc.commParser.BDLedger;
|
||||
|
||||
public class BlockHeader {
|
||||
public int index;
|
||||
public byte[] hash;
|
||||
public int version;
|
||||
public int timestamp;
|
||||
public byte[] prevblockID;
|
||||
public byte[] merkleroot;
|
||||
public byte[] creatorID;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package org.bdware.sc.commParser.BDLedger;
|
||||
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Transaction {
|
||||
public byte[] srcID;
|
||||
public byte[] dstID;
|
||||
public long nonce;
|
||||
public int type;
|
||||
public byte[] data;
|
||||
//扩展字段
|
||||
public int timestamp;
|
||||
public byte[] hash;
|
||||
|
||||
public static Transaction fromBytes(byte[] bytes) {
|
||||
try {
|
||||
Transaction trans = new Transaction();
|
||||
DataInputStream bin = new DataInputStream(new ByteArrayInputStream(bytes));
|
||||
trans.srcID = new byte[20];
|
||||
trans.dstID = new byte[20];
|
||||
bin.read(trans.srcID);
|
||||
bin.read(trans.dstID);
|
||||
trans.nonce = bin.readLong();
|
||||
trans.type = bin.readInt();
|
||||
int len = bin.readInt();
|
||||
trans.data = new byte[len];
|
||||
bin.read(trans.data);
|
||||
return trans;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public byte[] getTransactionHash() {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
byte[] encoded = md.digest(toHashByteArray());
|
||||
byte[] truned = HashUtil.truncation(encoded, 20);
|
||||
System.out.println("[Transaction] encoded:" + HashUtil.byteArray2Str(encoded) + " ==> "
|
||||
+ HashUtil.byteArray2Str(truned));
|
||||
return truned;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("SrcID", HashUtil.byteArray2Str(srcID));
|
||||
ret.put("dstID", HashUtil.byteArray2Str(dstID));
|
||||
ret.put("nonce", String.valueOf(nonce));
|
||||
ret.put("type", String.valueOf(type));
|
||||
ret.put("data", HashUtil.byteArray2Str(data));
|
||||
return JsonUtil.toJson(ret);
|
||||
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
try {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
DataOutputStream dd = new DataOutputStream(bo);
|
||||
dd.write(srcID);
|
||||
dd.write(dstID);
|
||||
dd.writeLong(nonce);
|
||||
dd.writeInt(type);
|
||||
dd.writeInt(data.length);
|
||||
dd.write(data);
|
||||
return bo.toByteArray();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public byte[] toHashByteArray() {
|
||||
try {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
DataOutputStream dd = new DataOutputStream(bo);
|
||||
dd.write(srcID);
|
||||
dd.write(dstID);
|
||||
dd.writeLong(HashUtil.toLittleEndian(nonce));
|
||||
dd.writeInt(HashUtil.toLittleEndian(type));
|
||||
dd.write(data);
|
||||
return bo.toByteArray();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.bdware.sc.commParser;
|
||||
|
||||
public interface BlockAdapter<T> {
|
||||
Transaction convert(T t);
|
||||
}
|
||||
38
src/main/pythongen/org/bdware/sc/commParser/BlockBody.java
Normal file
38
src/main/pythongen/org/bdware/sc/commParser/BlockBody.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package org.bdware.sc.commParser;
|
||||
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockBody {
|
||||
public List<Transaction_bak> trans = new ArrayList<>();
|
||||
|
||||
public static BlockBody fromBytes(byte[] input) {
|
||||
try {
|
||||
DataInputStream bin = new DataInputStream(new ByteArrayInputStream(input));
|
||||
int size = bin.readInt();
|
||||
size = HashUtil.toLittleEndian(size);
|
||||
BlockBody ret = new BlockBody();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Transaction_bak trans = new Transaction_bak();
|
||||
ret.trans.add(trans);
|
||||
trans.srcID = new byte[20];
|
||||
trans.dstID = new byte[20];
|
||||
bin.read(trans.srcID);
|
||||
bin.read(trans.dstID);
|
||||
trans.nonce = HashUtil.toLittleEndian(bin.readLong());
|
||||
trans.type = HashUtil.toLittleEndian(bin.readInt());
|
||||
int len = HashUtil.toLittleEndian(bin.readInt());
|
||||
trans.data = new byte[len];
|
||||
bin.read(trans.data);
|
||||
}
|
||||
return ret;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new BlockBody();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.bdware.sc.commParser;
|
||||
|
||||
public class LocalLogAdapter {
|
||||
public Transaction trans;
|
||||
public org.bdware.sc.common.Transaction chaintran;
|
||||
|
||||
public LocalLogAdapter(org.bdware.sc.common.Transaction chaintrans) {
|
||||
super();
|
||||
this.chaintran = chaintrans;
|
||||
this.trans = new Transaction();
|
||||
|
||||
|
||||
this.trans.Data = chaintrans.getData().getBytes();
|
||||
this.trans.SrcID = chaintrans.getKey().getBytes();
|
||||
this.trans.Txid = chaintrans.getData().getBytes(); //交易id对应本地日志库中什么字段呢
|
||||
|
||||
|
||||
//this.trans.Data = chaintrans.getData().toStringUtf8().getBytes();
|
||||
//this.trans.SrcID = chaintrans.getFrom().toByteArray(); //本地库中SrcID可以是key
|
||||
//this.trans.Txid = chaintrans.getHash().toByteArray();
|
||||
}
|
||||
}
|
||||
10
src/main/pythongen/org/bdware/sc/commParser/Transaction.java
Normal file
10
src/main/pythongen/org/bdware/sc/commParser/Transaction.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package org.bdware.sc.commParser;
|
||||
|
||||
public class Transaction {
|
||||
public byte[] Txid;
|
||||
public byte[] SrcID;
|
||||
public byte[] DstID;
|
||||
public byte[] Data;
|
||||
public int Timestamp;
|
||||
public long Nonce;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package org.bdware.sc.commParser;
|
||||
|
||||
import org.bdware.sc.util.HashUtil;
|
||||
import org.bdware.sc.util.JsonUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Transaction_bak {
|
||||
byte[] srcID;
|
||||
byte[] dstID;
|
||||
long nonce;
|
||||
int type;
|
||||
byte[] data;
|
||||
|
||||
public static Transaction_bak fromBytes(byte[] bytes) {
|
||||
try {
|
||||
Transaction_bak trans = new Transaction_bak();
|
||||
DataInputStream bin = new DataInputStream(new ByteArrayInputStream(bytes));
|
||||
trans.srcID = new byte[20];
|
||||
trans.dstID = new byte[20];
|
||||
bin.read(trans.srcID);
|
||||
bin.read(trans.dstID);
|
||||
trans.nonce = bin.readLong();
|
||||
trans.type = bin.readInt();
|
||||
int len = bin.readInt();
|
||||
trans.data = new byte[len];
|
||||
bin.read(trans.data);
|
||||
return trans;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public byte[] getTransactionHash() {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
System.out.println("[Transaction]: getTransactionHash-->" + HashUtil.byteArray2Str(toHashByteArray()));
|
||||
byte[] encoded = md.digest(toHashByteArray());
|
||||
byte[] truned = HashUtil.truncation(encoded, 20);
|
||||
System.out.println("[Transaction] encoded:" + HashUtil.byteArray2Str(encoded) + " ==> "
|
||||
+ HashUtil.byteArray2Str(truned));
|
||||
return truned;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
Map<String, String> ret = new HashMap<>();
|
||||
ret.put("SrcID", HashUtil.byteArray2Str(srcID));
|
||||
ret.put("dstID", HashUtil.byteArray2Str(dstID));
|
||||
ret.put("nonce", nonce + "");
|
||||
ret.put("type", type + "");
|
||||
ret.put("data", HashUtil.byteArray2Str(data));
|
||||
return JsonUtil.toJson(ret);
|
||||
|
||||
}
|
||||
|
||||
public byte[] toByteArray() {
|
||||
try {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
DataOutputStream dd = new DataOutputStream(bo);
|
||||
dd.write(srcID);
|
||||
dd.write(dstID);
|
||||
dd.writeLong(nonce);
|
||||
dd.writeInt(type);
|
||||
dd.writeInt(data.length);
|
||||
dd.write(data);
|
||||
return bo.toByteArray();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public byte[] toHashByteArray() {
|
||||
try {
|
||||
ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
||||
DataOutputStream dd = new DataOutputStream(bo);
|
||||
dd.write(srcID);
|
||||
dd.write(dstID);
|
||||
dd.writeLong(HashUtil.toLittleEndian(nonce));
|
||||
dd.writeInt(HashUtil.toLittleEndian(type));
|
||||
dd.write(data);
|
||||
return bo.toByteArray();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.bdware.sc.commParser;
|
||||
|
||||
public class XuperChainAdapter {
|
||||
// public Transaction trans;
|
||||
// public com.baidu.xuperunion.pb.XchainOuterClass.Transaction chaintran;
|
||||
// public TransAdapter(com.baidu.xuperunion.pb.XchainOuterClass.Transaction chaintrans) {
|
||||
// super();
|
||||
// this.chaintran = chaintrans;
|
||||
// this.trans = new Transaction();
|
||||
// this.trans.Data = chaintrans.getDesc().toStringUtf8().getBytes();
|
||||
// this.trans.Timestamp = (int) (chaintrans.getTimestamp()/1000000000);
|
||||
// this.trans.Txid = chaintrans.getTxid().toByteArray();
|
||||
// if (chaintrans.getTxInputsCount()>0) {
|
||||
// this.trans.SrcID = chaintrans.getTxInputs(0).getFromAddr().toByteArray();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
24
src/main/pythongen/org/bdware/sc/common/Transaction.java
Normal file
24
src/main/pythongen/org/bdware/sc/common/Transaction.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package org.bdware.sc.common;
|
||||
|
||||
/*
|
||||
* Local Log DB
|
||||
*/
|
||||
public class Transaction {
|
||||
String key;
|
||||
String from; //pubKey
|
||||
String to; //action
|
||||
String data;
|
||||
String requestID; //date
|
||||
|
||||
public String getKey() {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return this.from;
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
return this.data;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
55
src/main/pythongen/org/bdware/sc/contractGen/MainFrame.java
Normal file
55
src/main/pythongen/org/bdware/sc/contractGen/MainFrame.java
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
contract TimeIndex {
|
||||
function onCreate(arg){
|
||||
Global.db = com.yancloud.sc.db.RocksDBUtil.loadDB(${indexData},"false");
|
||||
return "success";
|
||||
}
|
||||
export function getDBStatus(arg){
|
||||
return JSON.stringify(Global.db);
|
||||
}
|
||||
export function onBlock(args){
|
||||
YancloudUtil.setTimeOut(onBlockInternal,1,args);
|
||||
return "success";
|
||||
}
|
||||
export function onTransaction(args){
|
||||
var chainblock = Global.dbs[args.ledgerName].queryBlock(prehash);
|
||||
var chaintrans = chainblock.getTransactionsList();
|
||||
for (var i = 0; i < chaintrans.size(); i++) {
|
||||
var chaintran = chaintrans.get(i);
|
||||
var tranadapter = new com.yancloud.sc.commParser.TransAdapter(chaintran);
|
||||
var tran = tranadapter.trans;
|
||||
var transhash = tran.hash;
|
||||
var frombytes = undefined;
|
||||
//if (chaintran.getTxInputsCount() == 0) {
|
||||
// continue;
|
||||
//}
|
||||
var frombytes = tran.srcID;
|
||||
var from = com.yancloud.sc.util.HashUtil.byteArray2Str(frombytes);
|
||||
argg = {};
|
||||
argg.account = from;
|
||||
argg.file = "account";
|
||||
if (Global.rocksdb.get(frombytes) == undefined) {
|
||||
print("undefined account :" + from);
|
||||
argg.dataLength = 32;
|
||||
var acindex = com.yancloud.sc.boundry.TimeIndex.createIndex();
|
||||
var res = acindex.createFile(argg);
|
||||
print(JSON.stringify(res));
|
||||
Global.rocksdb.put(frombytes, frombytes);
|
||||
}
|
||||
argg = {};
|
||||
argg.account = from;
|
||||
argg.file = "account";
|
||||
argg.date = tran.timestamp;
|
||||
argg.content = com.yancloud.sc.util.HashUtil.byteArray2Str(transhash);
|
||||
var acindex = com.yancloud.sc.boundry.TimeIndex.createIndex();
|
||||
var res = acindex.manullyIndex(argg);
|
||||
}
|
||||
export function onQuery(args){
|
||||
//request by from
|
||||
argg = {};
|
||||
var pars = JSON.parse(arg);
|
||||
argg.account = pars.from;
|
||||
print(argg.account);
|
||||
argg.file = pars.type;
|
||||
print(argg.file);
|
||||
argg.startTime = pars.start;
|
||||
argg.endTime = pars.end;
|
||||
var acindex = com.yancloud.sc.boundry.AccountIndex.createIndex();
|
||||
var ss = acindex.requestByTime(argg);
|
||||
print(JSON.stringify(ss));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
contract TimeIndex {
|
||||
function onCreate(arg){
|
||||
Global.db = com.yancloud.sc.db.RocksDBUtil.loadDB("${indexData}","false");
|
||||
var sdk = new ${sdkpath}();
|
||||
Global.sdk = sdk;
|
||||
return "success";
|
||||
}
|
||||
function saveckpt(args){
|
||||
var height = Global.height;
|
||||
}
|
||||
export function getDBStatus(arg){
|
||||
return JSON.stringify(Global.db);
|
||||
}
|
||||
export function onBlock(args){
|
||||
YancloudUtil.setTimeOut(onBlockInternal,1,args);
|
||||
return "success";
|
||||
}
|
||||
export function onTransaction(args){
|
||||
var chainblock = Global.sdk.queryBlock(prehash);//改为对应sdk中的方法
|
||||
var chaintrans = chainblock.getTransactionsList();
|
||||
for (var i = 0; i < chaintrans.size(); i++) {
|
||||
var chaintran = chaintrans.get(i);
|
||||
var tranadapter = new com.yancloud.sc.commParser.TransAdapter(chaintran);
|
||||
var tran = tranadapter.trans;
|
||||
var transhash = tran.hash;
|
||||
var frombytes = undefined;
|
||||
if (chaintran.getTxInputsCount() == 0) {
|
||||
continue;
|
||||
}
|
||||
var frombytes = tran.srcID;
|
||||
var from = com.yancloud.sc.util.HashUtil.byteArray2Str(frombytes);
|
||||
argg = {};
|
||||
argg.account = from;
|
||||
argg.file = "account";
|
||||
if (Global.rocksdb.get(frombytes) == undefined) {
|
||||
print("undefined account :" + from);
|
||||
argg.dataLength = ${datalen};
|
||||
var acindex = com.yancloud.sc.boundry.TimeIndex.createIndex();
|
||||
var res = acindex.createFile(argg);
|
||||
print(JSON.stringify(res));
|
||||
Global.rocksdb.put(frombytes, frombytes);
|
||||
}
|
||||
argg = {};
|
||||
argg.account = from;
|
||||
argg.file = "account";
|
||||
argg.date = tran.timestamp;
|
||||
argg.content = com.yancloud.sc.util.HashUtil.byteArray2Str(transhash);
|
||||
var acindex = com.yancloud.sc.boundry.TimeIndex.createIndex();
|
||||
var res = acindex.manullyIndex(argg);
|
||||
}
|
||||
export function onQuery(args){
|
||||
//request by from
|
||||
argg = {};
|
||||
var pars = JSON.parse(arg);
|
||||
argg.account = pars.from;
|
||||
print(argg.account);
|
||||
argg.file = pars.type;
|
||||
print(argg.file);
|
||||
argg.startTime = pars.start;
|
||||
argg.endTime = pars.end;
|
||||
var acindex = com.yancloud.sc.boundry.AccountIndex.createIndex() var ss = acindex.requestByTime(argg);
|
||||
print(JSON.stringify(ss));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
contract TimeIndex {
|
||||
function onCreate(arg){
|
||||
Global.rocksdb = com.yancloud.sc.db.RocksDBUtil.loadDB(${indexData},"false");
|
||||
Global.height = com.yancloud.sc.util.HashUtil.byteArrayToInt(rocksdb.get("height".getBytes()));
|
||||
return "success";
|
||||
}
|
||||
function saveckpt(args){
|
||||
var height = Global.height;
|
||||
#if($syncbyheight)
|
||||
Global.rocksdb.put("height".getBytes(),com.yancloud.sc.util.HashUtil.intToByteArray(height));
|
||||
}
|
||||
export function getDBStatus(arg){
|
||||
return JSON.stringify(Global.db);
|
||||
}
|
||||
export function onBlock(args){
|
||||
YancloudUtil.setTimeOut(onBlockInternal,1,args);
|
||||
return "success";
|
||||
}
|
||||
export function onTransaction(args){
|
||||
var chainblock = Global.dbs[args.ledgerName].queryBlock(prehash);
|
||||
var chaintrans = chainblock.getTransactionsList();
|
||||
for (var i = 0; i < chaintrans.size(); i++) {
|
||||
var chaintran = chaintrans.get(i);
|
||||
var tranadapter = new com.yancloud.sc.commParser.TransAdapter(chaintran);
|
||||
var tran = tranadapter.trans;
|
||||
var transhash = tran.hash;
|
||||
var frombytes = undefined;
|
||||
if (chaintran.getTxInputsCount() == 0) {
|
||||
continue;
|
||||
}
|
||||
var frombytes = tran.srcID;
|
||||
var from = com.yancloud.sc.util.HashUtil.byteArray2Str(frombytes);
|
||||
argg = {};
|
||||
argg.file = "account";
|
||||
if (Global.rocksdb.get(frombytes) == undefined) {
|
||||
print("undefined account :" + from);
|
||||
argg.dataLength = 32;
|
||||
var acindex = com.yancloud.sc.boundry.TimeIndex.createIndex();
|
||||
var res = acindex.createFile(argg);
|
||||
print(JSON.stringify(res));
|
||||
Global.rocksdb.put(frombytes, frombytes);
|
||||
}
|
||||
argg = {};
|
||||
argg.file = "time";
|
||||
argg.date = tran.timestamp;
|
||||
argg.content = com.yancloud.sc.util.HashUtil.byteArray2Str(transhash);
|
||||
var acindex = com.yancloud.sc.boundry.TimeIndex.createIndex();
|
||||
var res = acindex.manullyIndex(argg);
|
||||
saveckpt({})
|
||||
|
||||
}
|
||||
export function onQuery(args){
|
||||
//request by time
|
||||
argg = {};
|
||||
var pars = JSON.parse(args);
|
||||
argg.file = pars.type;
|
||||
print(argg.file);
|
||||
argg.startTime = pars.start;
|
||||
argg.endTime = pars.end;
|
||||
var acindex = com.yancloud.sc.boundry.AccountIndex.createIndex() var ss = acindex.requestByTime(argg);
|
||||
print(JSON.stringify(ss));
|
||||
return "success";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user