mineTxList) {
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/listener/VersionUpdateListener.java:
--------------------------------------------------------------------------------
1 | package org.inchain.listener;
2 |
3 | public interface VersionUpdateListener {
4 |
5 | /**
6 | * 开始下载
7 | */
8 | void startDownload();
9 |
10 | /**
11 | * 下载完成率
12 | * @param filename
13 | * @param rate
14 | */
15 | void downloading(String filename, float rate);
16 |
17 | /**
18 | * 下载完成
19 | */
20 | void onComplete();
21 | }
22 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/mempool/Mempool.java:
--------------------------------------------------------------------------------
1 | package org.inchain.mempool;
2 |
3 | import org.inchain.crypto.Sha256Hash;
4 | import org.inchain.transaction.Transaction;
5 |
6 | /**
7 | * 内存池,存放所有待打包的交易
8 | * @author ln
9 | *
10 | */
11 | public interface Mempool {
12 |
13 | /**
14 | * 将交易放入内存池中
15 | * @param tx 交易
16 | * @return boolean
17 | */
18 | boolean add(Transaction tx);
19 |
20 | /**
21 | * 从内存池中移除交易
22 | * @param hash 交易hash
23 | * @return boolean
24 | */
25 | boolean remove(Sha256Hash hash);
26 |
27 | /**
28 | * 批量从内存池中移除交易
29 | * @param hashs 交易hash数组
30 | * @return boolean
31 | */
32 | boolean bathRemove(Sha256Hash[] hashs);
33 |
34 | /**
35 | * 获取最早的交易,同时从内存池中移除交易
36 | * @return Transaction
37 | */
38 | Transaction get();
39 |
40 | /**
41 | * 获取交易,不会移除
42 | * @param hash 交易hash
43 | * @return Transaction
44 | */
45 | Transaction get(Sha256Hash hash);
46 |
47 | /**
48 | * 批量获取最早的交易,获取之后同时从内存池中移除交易
49 | * @param max 最大获取数量
50 | * @return Transaction[]
51 | */
52 | Transaction[] getNewest(int max);
53 |
54 | /**
55 | * 获取内存里面交易数量
56 | * @return int
57 | */
58 | int getTxCount();
59 | }
60 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/DataNotFoundMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.core.exception.ProtocolException;
7 | import org.inchain.crypto.Sha256Hash;
8 | import org.inchain.network.NetworkParams;
9 |
10 | /**
11 | * 数据没有找到消息,用户回应GetDatasMessage
12 | * @author ln
13 | *
14 | */
15 | public class DataNotFoundMessage extends Message {
16 |
17 | private Sha256Hash hash;
18 |
19 | public DataNotFoundMessage(NetworkParams network, Sha256Hash hash) {
20 | super();
21 | this.hash = hash;
22 | }
23 |
24 | public DataNotFoundMessage(NetworkParams network, byte[] payload) throws ProtocolException {
25 | this(network, payload, 0);
26 | }
27 |
28 | public DataNotFoundMessage(NetworkParams network, byte[] payload, int offset) throws ProtocolException {
29 | super(network, payload, offset);
30 | }
31 |
32 | @Override
33 | protected void parse() throws ProtocolException {
34 | hash = readHash();
35 | length = cursor - offset;
36 | }
37 |
38 | @Override
39 | protected void serializeToStream(OutputStream stream) throws IOException {
40 | stream.write(hash.getReversedBytes());
41 | }
42 |
43 | public Sha256Hash getHash() {
44 | return hash;
45 | }
46 |
47 | @Override
48 | public String toString() {
49 | return "DataNotFoundMessage [hash=" + hash + "]";
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/EmptyMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.core.exception.ProtocolException;
7 | import org.inchain.network.NetworkParams;
8 |
9 | /**
10 | * 空消息的定义
11 | * @author ln
12 | *
13 | */
14 | public abstract class EmptyMessage extends Message {
15 |
16 | public EmptyMessage() {
17 | length = 0;
18 | }
19 |
20 | public EmptyMessage(NetworkParams params) {
21 | super(params);
22 | length = 0;
23 | }
24 |
25 | public EmptyMessage(NetworkParams params, byte[] payload, int offset) throws ProtocolException {
26 | super(params, payload, offset);
27 | length = 0;
28 | }
29 |
30 | @Override
31 | protected final void serializeToStream(OutputStream stream) throws IOException {
32 | }
33 |
34 | @Override
35 | protected void parse() throws ProtocolException {
36 | }
37 |
38 | @Override
39 | public byte[] baseSerialize() {
40 | return new byte[0];
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/GetAddressMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.core.exception.ProtocolException;
7 | import org.inchain.network.NetworkParams;
8 | import org.inchain.utils.Utils;
9 |
10 | /**
11 | * 向对等体获取网络地址列表
12 | * @author ln
13 | *
14 | */
15 | public class GetAddressMessage extends Message {
16 |
17 | private long time;
18 |
19 | public GetAddressMessage(NetworkParams network) {
20 | super(network);
21 | }
22 |
23 | public GetAddressMessage(NetworkParams network, long time) {
24 | super(network);
25 | this.time = time;
26 | }
27 |
28 | public GetAddressMessage(NetworkParams network, byte[] payloadBytes) {
29 | super(network, payloadBytes, 0);
30 | }
31 |
32 | @Override
33 | protected void serializeToStream(OutputStream stream) throws IOException {
34 | Utils.int64ToByteStreamLE(time, stream);
35 | }
36 |
37 | @Override
38 | protected void parse() throws ProtocolException {
39 | time = readInt64();
40 | length = cursor;
41 | }
42 |
43 | public long getTime() {
44 | return time;
45 | }
46 |
47 | public void setTime(long time) {
48 | this.time = time;
49 | }
50 |
51 | @Override
52 | public String toString() {
53 | return "GetAddressMessage [time=" + time + "]";
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/GetBlocksMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.core.exception.ProtocolException;
7 | import org.inchain.crypto.Sha256Hash;
8 | import org.inchain.network.NetworkParams;
9 |
10 | /**
11 | * 获取区块信息消息,收到该消息的节点必须应答,返回相应的区块inv消息
12 | * @author ln
13 | *
14 | */
15 | public class GetBlocksMessage extends Message {
16 |
17 | //开始区块的hash
18 | private Sha256Hash startHash;
19 | //结束区块的hash
20 | private Sha256Hash stopHash;
21 |
22 | public GetBlocksMessage(NetworkParams network, byte[] payloadBytes) {
23 | super(network, payloadBytes, 0);
24 | }
25 |
26 | public GetBlocksMessage(NetworkParams network, Sha256Hash startHash, Sha256Hash stopHash) {
27 | this.startHash = startHash;
28 | this.stopHash = stopHash;
29 | }
30 |
31 | /**
32 | * 序列化
33 | */
34 | @Override
35 | protected void serializeToStream(OutputStream stream) throws IOException {
36 | stream.write(startHash.getReversedBytes());
37 | stream.write(stopHash.getReversedBytes());
38 | }
39 |
40 | /**
41 | * 反序列化
42 | */
43 | @Override
44 | protected void parse() throws ProtocolException {
45 | this.startHash = readHash();
46 | this.stopHash = readHash();
47 | length = cursor;
48 | }
49 |
50 | @Override
51 | public String toString() {
52 | return "GetBlockMessage [startHash=" + startHash + ", stopHash=" + stopHash + "]";
53 | }
54 |
55 | public Sha256Hash getStartHash() {
56 | return startHash;
57 | }
58 |
59 | public void setStartHash(Sha256Hash startHash) {
60 | this.startHash = startHash;
61 | }
62 |
63 | public Sha256Hash getStopHash() {
64 | return stopHash;
65 | }
66 |
67 | public void setStopHash(Sha256Hash stopHash) {
68 | this.stopHash = stopHash;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/GetDatasMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import org.inchain.network.NetworkParams;
4 |
5 | /**
6 | * 下载数据,包括最新区块和交易信息
7 | * @author ln
8 | *
9 | */
10 | public class GetDatasMessage extends InventoryMessage {
11 |
12 | public static final long MAX_INVENTORY_ITEMS = 10000;
13 |
14 | public GetDatasMessage(NetworkParams network, InventoryItem item) {
15 | super(network, item);
16 | }
17 |
18 | public GetDatasMessage(NetworkParams network, byte[] payloadBytes) {
19 | super(network, payloadBytes);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/InventoryItem.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import org.inchain.core.exception.ProtocolException;
4 | import org.inchain.crypto.Sha256Hash;
5 |
6 | /**
7 | * 向量清单
8 | * @author ln
9 | *
10 | */
11 | public class InventoryItem {
12 |
13 | //单个清单长度,1 byte 的type, 32 byte的hash
14 | public static final int MESSAGE_LENGTH = 33;
15 |
16 | public enum Type {
17 |
18 | Transaction,
19 | Block,
20 | NewBlock,
21 | Consensus,
22 | Unknown;
23 |
24 | public static Type from(int code) {
25 | Type[] types = Type.values();
26 | for (Type type : types) {
27 | if(type.ordinal() == code) {
28 | return type;
29 | }
30 | }
31 | Type res = Type.Unknown;
32 | res.setCode(code);
33 | return res;
34 | }
35 |
36 | int code;
37 | private void setCode(int code) {
38 | this.code = code;
39 | }
40 | public int getCode() {
41 | if(code > 0) {
42 | return code;
43 | } else {
44 | return ordinal();
45 | }
46 | }
47 | }
48 | //类型
49 | private final Type type;
50 | //hash
51 | private final Sha256Hash hash;
52 |
53 | public InventoryItem(Type type, Sha256Hash hash) {
54 | if(type == Type.Unknown) {
55 | throw new ProtocolException("Unknown inv type: " + type.getCode());
56 | }
57 | this.type = type;
58 | this.hash = hash;
59 | }
60 |
61 | public Type getType() {
62 | return type;
63 | }
64 |
65 | public Sha256Hash getHash() {
66 | return hash;
67 | }
68 |
69 | @Override
70 | public String toString() {
71 | return type+":"+hash;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/NewBlockMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import org.inchain.network.NetworkParams;
4 |
5 | /**
6 | * 新区块诞生消息
7 | * @author ln
8 | *
9 | */
10 | public class NewBlockMessage extends Block {
11 |
12 | public NewBlockMessage(NetworkParams network, byte[] payloadBytes) {
13 | super(network, payloadBytes);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/PingMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.core.exception.ProtocolException;
7 | import org.inchain.network.NetworkParams;
8 | import org.inchain.utils.Utils;
9 |
10 | /**
11 | * Instances of this class are not safe for use by multiple threads.
12 | */
13 | public class PingMessage extends Message {
14 | private long nonce;
15 |
16 | public PingMessage(NetworkParams params, byte[] payloadBytes) throws ProtocolException {
17 | super(params, payloadBytes, 0);
18 | }
19 |
20 | public PingMessage(long nonce) {
21 | this.nonce = nonce;
22 | }
23 |
24 | @Override
25 | public void serializeToStream(OutputStream stream) throws IOException {
26 | Utils.int64ToByteStreamLE(nonce, stream);
27 | }
28 |
29 | @Override
30 | protected void parse() throws ProtocolException {
31 | nonce = readInt64();
32 | length = 8;
33 | }
34 |
35 | public long getNonce() {
36 | return nonce;
37 | }
38 |
39 | @Override
40 | public String toString() {
41 | return "PingMessage [nonce=" + nonce + "]";
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/PongMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.core.exception.ProtocolException;
7 | import org.inchain.network.NetworkParams;
8 | import org.inchain.utils.Utils;
9 |
10 | public class PongMessage extends Message {
11 | private long nonce;
12 |
13 | public PongMessage(NetworkParams params, byte[] payloadBytes) throws ProtocolException {
14 | super(params, payloadBytes, 0);
15 | }
16 |
17 | public PongMessage(long nonce) {
18 | this.nonce = nonce;
19 | }
20 |
21 | @Override
22 | protected void parse() throws ProtocolException {
23 | nonce = readInt64();
24 | length = 8;
25 | }
26 |
27 | @Override
28 | public void serializeToStream(OutputStream stream) throws IOException {
29 | Utils.int64ToByteStreamLE(nonce, stream);
30 | }
31 |
32 | public long getNonce() {
33 | return nonce;
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return "PongMessage [nonce=" + nonce + "]";
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/RejectMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.core.exception.ProtocolException;
7 | import org.inchain.crypto.Sha256Hash;
8 | import org.inchain.network.NetworkParams;
9 |
10 | /**
11 | * 拒绝消息
12 | * @author ln
13 | *
14 | */
15 | public class RejectMessage extends Message {
16 |
17 | private Sha256Hash hash;
18 |
19 | public RejectMessage(NetworkParams network, Sha256Hash hash) {
20 | super();
21 | this.hash = hash;
22 | }
23 |
24 | public RejectMessage(NetworkParams network, byte[] payload) throws ProtocolException {
25 | this(network, payload, 0);
26 | }
27 |
28 | public RejectMessage(NetworkParams network, byte[] payload, int offset) throws ProtocolException {
29 | super(network, payload, offset);
30 | }
31 |
32 | @Override
33 | protected void parse() throws ProtocolException {
34 | hash = readHash();
35 | }
36 |
37 | @Override
38 | protected void serializeToStream(OutputStream stream) throws IOException {
39 | stream.write(hash.getReversedBytes());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/UnknownMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import org.inchain.core.exception.ProtocolException;
4 | import org.inchain.network.NetworkParams;
5 | import org.inchain.utils.Hex;
6 |
7 | /**
8 | * Instances of this class are not safe for use by multiple threads.
9 | */
10 | public class UnknownMessage extends EmptyMessage {
11 |
12 | private String name;
13 |
14 | public UnknownMessage(NetworkParams params, String name, byte[] payloadBytes) throws ProtocolException {
15 | super(params, payloadBytes, 0);
16 | this.name = name;
17 | }
18 |
19 | @Override
20 | public String toString() {
21 | return "Unknown message [" + name + "]: " + (payload == null ? "" : Hex.encode(payload));
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/message/VerackMessage.java:
--------------------------------------------------------------------------------
1 | package org.inchain.message;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 | import java.net.UnknownHostException;
6 |
7 | import org.inchain.core.exception.ProtocolException;
8 | import org.inchain.network.NetworkParams;
9 | import org.inchain.utils.Utils;
10 |
11 | public class VerackMessage extends Message {
12 |
13 | private long time;
14 | private long nonce;
15 |
16 | public VerackMessage(NetworkParams params, byte[] payload) throws ProtocolException {
17 | super(params, payload, 0);
18 | }
19 |
20 | public VerackMessage(NetworkParams params, long time, long nonce) throws UnknownHostException {
21 | super(params);
22 | this.time = time;
23 | this.nonce = nonce;
24 | }
25 |
26 | @Override
27 | protected void parse() throws ProtocolException {
28 | time = readInt64();
29 | nonce = readInt64();
30 | length = cursor;
31 | }
32 |
33 | @Override
34 | protected void serializeToStream(OutputStream stream) throws IOException {
35 | Utils.int64ToByteStreamLE(time, stream);
36 | Utils.int64ToByteStreamLE(nonce, stream);
37 | }
38 |
39 | public long getTime() {
40 | return time;
41 | }
42 |
43 | public void setTime(long time) {
44 | this.time = time;
45 | }
46 |
47 | public long getNonce() {
48 | return nonce;
49 | }
50 |
51 | public void setNonce(long nonce) {
52 | this.nonce = nonce;
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/msgprocess/DataNotFoundMessageProcess.java:
--------------------------------------------------------------------------------
1 | package org.inchain.msgprocess;
2 |
3 | import org.inchain.core.DataSynchronizeHandler;
4 | import org.inchain.core.Peer;
5 | import org.inchain.message.DataNotFoundMessage;
6 | import org.inchain.message.Message;
7 | import org.slf4j.Logger;
8 | import org.slf4j.LoggerFactory;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.stereotype.Service;
11 |
12 | /**
13 | * 数据没有找到处理
14 | * @author ln
15 | *
16 | */
17 | @Service
18 | public class DataNotFoundMessageProcess implements MessageProcess {
19 |
20 | private static final Logger log = LoggerFactory.getLogger(DataNotFoundMessageProcess.class);
21 |
22 | @Autowired
23 | private DataSynchronizeHandler dataSynchronizeHandler;
24 |
25 | @Override
26 | public MessageProcessResult process(Message message, Peer peer) {
27 |
28 | if(log.isDebugEnabled()) {
29 | log.debug("receive DataNotFoundMessage message: {}", message);
30 | }
31 |
32 | DataNotFoundMessage dataNotFoundMessage = (DataNotFoundMessage) message;
33 |
34 | //信息没有找到,有可能是同步区块时,没有更多消息,交给下载器去判断处理
35 | dataSynchronizeHandler.dataNotFoundCheck(dataNotFoundMessage.getHash());
36 |
37 | return new MessageProcessResult(dataNotFoundMessage.getHash(), false);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/msgprocess/DefaultMessageProcessFactory.java:
--------------------------------------------------------------------------------
1 | package org.inchain.msgprocess;
2 |
3 | import org.inchain.SpringContextUtils;
4 | import org.inchain.core.Definition;
5 | import org.inchain.message.Message;
6 |
7 | /**
8 | * 消息处理器工厂
9 | * @author ln
10 | *
11 | */
12 | public class DefaultMessageProcessFactory implements MessageProcessFactory {
13 |
14 | private static final MessageProcessFactory INSTANCE = new DefaultMessageProcessFactory();
15 |
16 | private DefaultMessageProcessFactory() {
17 | }
18 |
19 | public static MessageProcessFactory getInstance() {
20 | return INSTANCE;
21 | }
22 |
23 | @Override
24 | public MessageProcess getFactory(Message message) {
25 |
26 | if(message == null) {
27 | return SpringContextUtils.getBean(UnknownMessageProcess.class);
28 | }
29 | String processId = Definition.PROCESS_FACTORYS.get(message.getClass());
30 | if(processId == null) {
31 | return SpringContextUtils.getBean(UnknownMessageProcess.class);
32 | }
33 | MessageProcess messageProcess = SpringContextUtils.getBean(processId);
34 | if(messageProcess == null) {
35 | messageProcess = SpringContextUtils.getBean(UnknownMessageProcess.class);
36 | }
37 | return messageProcess;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/msgprocess/MessageProcess.java:
--------------------------------------------------------------------------------
1 | package org.inchain.msgprocess;
2 |
3 | import org.inchain.core.Peer;
4 | import org.inchain.message.Message;
5 |
6 | public interface MessageProcess {
7 |
8 | MessageProcessResult process(Message message, Peer peer);
9 | }
10 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/msgprocess/MessageProcessFactory.java:
--------------------------------------------------------------------------------
1 | package org.inchain.msgprocess;
2 |
3 | import org.inchain.message.Message;
4 |
5 | /**
6 | * 消息处理
7 | * @author ln
8 | *
9 | */
10 | public interface MessageProcessFactory {
11 |
12 | MessageProcess getFactory(Message message);
13 | }
14 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/msgprocess/MessageProcessResult.java:
--------------------------------------------------------------------------------
1 | package org.inchain.msgprocess;
2 |
3 | import org.inchain.crypto.Sha256Hash;
4 | import org.inchain.message.Message;
5 |
6 | /**
7 | * 消息处理结果
8 | * @author ln
9 | *
10 | */
11 | public class MessageProcessResult {
12 |
13 | //消息的hash值
14 | private Sha256Hash hash;
15 | //消息是否处理成功
16 | private final boolean success;
17 | //错误代码
18 | private int errorCode;
19 | //是否回复消息,当不为空时回复
20 | private Message replyMessage;
21 |
22 | public MessageProcessResult(boolean success) {
23 | this(null, success, null);
24 | }
25 |
26 | public MessageProcessResult(Sha256Hash hash, boolean success) {
27 | this(hash, success, null);
28 | }
29 |
30 | public MessageProcessResult(Sha256Hash hash, boolean success, Message replyMessage) {
31 | this.hash = hash;
32 | this.success = success;
33 | this.replyMessage = replyMessage;
34 | }
35 |
36 | public boolean isSuccess() {
37 | return success;
38 | }
39 | public Message getReplyMessage() {
40 | return replyMessage;
41 | }
42 | public Sha256Hash getHash() {
43 | return hash;
44 | }
45 |
46 | public int getErrorCode() {
47 | return errorCode;
48 | }
49 |
50 | public void setErrorCode(int errorCode) {
51 | this.errorCode = errorCode;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/msgprocess/PingMessageProcess.java:
--------------------------------------------------------------------------------
1 | package org.inchain.msgprocess;
2 |
3 | import org.inchain.core.Peer;
4 | import org.inchain.message.Message;
5 | import org.inchain.message.PingMessage;
6 | import org.inchain.message.PongMessage;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.stereotype.Service;
9 |
10 | @Service
11 | public class PingMessageProcess implements MessageProcess {
12 |
13 | private static final org.slf4j.Logger log = LoggerFactory.getLogger(PingMessageProcess.class);
14 |
15 | @Override
16 | public MessageProcessResult process(Message message, Peer peer) {
17 | if(log.isDebugEnabled()) {
18 | log.debug("{} {}", peer.getAddress(), message);
19 | }
20 | return new MessageProcessResult(null, true, new PongMessage(((PingMessage)message).getNonce()));
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/msgprocess/PongMessageProcess.java:
--------------------------------------------------------------------------------
1 | package org.inchain.msgprocess;
2 |
3 | import org.inchain.core.Peer;
4 | import org.inchain.message.Message;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.stereotype.Service;
7 |
8 | @Service
9 | public class PongMessageProcess implements MessageProcess {
10 |
11 | private static final org.slf4j.Logger log = LoggerFactory.getLogger(PongMessageProcess.class);
12 |
13 | @Override
14 | public MessageProcessResult process(Message message, Peer peer) {
15 |
16 | if(log.isDebugEnabled()) {
17 | log.debug("{} {}", peer.getAddress(), message);
18 | }
19 |
20 | return new MessageProcessResult(null, true);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/msgprocess/UnknownMessageProcess.java:
--------------------------------------------------------------------------------
1 | package org.inchain.msgprocess;
2 |
3 | import org.inchain.core.Peer;
4 | import org.inchain.message.Message;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.stereotype.Service;
7 |
8 | @Service
9 | public class UnknownMessageProcess implements MessageProcess {
10 |
11 | private static final org.slf4j.Logger log = LoggerFactory.getLogger(UnknownMessageProcess.class);
12 |
13 | @Override
14 | public MessageProcessResult process(Message message, Peer peer) {
15 | log.warn("receive unknown message {}", message);
16 | return null;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/net/AbstractTimeoutHandler.java:
--------------------------------------------------------------------------------
1 | package org.inchain.net;
2 |
3 | import java.util.Timer;
4 | import java.util.TimerTask;
5 |
6 | public abstract class AbstractTimeoutHandler {
7 |
8 | private TimerTask timeoutTask;
9 | private long timeoutMillis = 0;
10 | private boolean timeoutEnabled = true;
11 |
12 | private static final Timer timeoutTimer = new Timer("AbstractTimeoutHandler timeouts", true);
13 |
14 | public synchronized final void setTimeoutEnabled(boolean timeoutEnabled) {
15 | this.timeoutEnabled = timeoutEnabled;
16 | resetTimeout();
17 | }
18 |
19 | public synchronized final void setSocketTimeout(int timeoutMillis) {
20 | this.timeoutMillis = timeoutMillis;
21 | resetTimeout();
22 | }
23 |
24 | protected synchronized void resetTimeout() {
25 | if (timeoutTask != null)
26 | timeoutTask.cancel();
27 | if (timeoutMillis == 0 || !timeoutEnabled)
28 | return;
29 | timeoutTask = new TimerTask() {
30 | @Override
31 | public void run() {
32 | timeoutOccurred();
33 | }
34 | };
35 | timeoutTimer.schedule(timeoutTask, timeoutMillis);
36 | }
37 |
38 | protected abstract void timeoutOccurred();
39 | }
40 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/net/ClientConnectionManager.java:
--------------------------------------------------------------------------------
1 | package org.inchain.net;
2 |
3 | import java.io.IOException;
4 | import java.net.InetSocketAddress;
5 | import java.util.concurrent.Future;
6 |
7 | import org.inchain.listener.NewInConnectionListener;
8 | import org.inchain.network.Seed;
9 |
10 | public interface ClientConnectionManager {
11 |
12 | Future openConnection(InetSocketAddress address, StreamConnection connection);
13 |
14 | int getConnectedClientCount();
15 |
16 | void closeConnections(int n);
17 |
18 | void setNewInConnectionListener(NewInConnectionListener newInConnectionListener);
19 |
20 | void start();
21 |
22 | void stop() throws IOException;
23 | }
24 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/net/MessageWriteTarget.java:
--------------------------------------------------------------------------------
1 | package org.inchain.net;
2 |
3 | import java.io.IOException;
4 |
5 | public interface MessageWriteTarget {
6 |
7 | void writeBytes(byte[] message) throws IOException;
8 |
9 | void closeConnection();
10 | }
11 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/net/StreamConnection.java:
--------------------------------------------------------------------------------
1 | package org.inchain.net;
2 |
3 | import java.nio.ByteBuffer;
4 |
5 | public interface StreamConnection {
6 |
7 | void connectionClosed();
8 |
9 | void connectionOpened();
10 |
11 | int receiveBytes(ByteBuffer buff) throws Exception;
12 |
13 | void setWriteTarget(MessageWriteTarget writeTarget);
14 |
15 | int getMaxMessageSize();
16 | }
17 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/net/StreamConnectionFactory.java:
--------------------------------------------------------------------------------
1 | package org.inchain.net;
2 |
3 | import java.net.InetAddress;
4 |
5 | public interface StreamConnectionFactory {
6 | StreamConnection getNewConnection(InetAddress inetAddress, int port);
7 | }
8 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/network/Networks.java:
--------------------------------------------------------------------------------
1 | package org.inchain.network;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 |
6 | /**
7 | * Utility class that holds all the registered NetworkParameters types used for Address auto discovery.
8 | * By default only MainNetParams and TestNet3Params are used. If you want to use TestNet2, RegTestParams or
9 | * UnitTestParams use the register and unregister the TestNet3Params as they don't have their own address
10 | * version/type code.
11 | */
12 | public class Networks {
13 | /** Registered networks */
14 | private static Set networks = new HashSet();
15 |
16 | public static Set extends NetworkParams> get() {
17 | return networks;
18 | }
19 |
20 | public static void register(NetworkParams network) {
21 | networks.add(network);
22 | }
23 |
24 | public static void register(Set networks) {
25 | Networks.networks = networks;
26 | }
27 |
28 | public static void unregister(NetworkParams network) {
29 | if (networks.contains(network)) {
30 | networks.remove(network);
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/network/NodeSeedManager.java:
--------------------------------------------------------------------------------
1 | package org.inchain.network;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * 配置节点
8 | * @author ln
9 | *
10 | */
11 | public class NodeSeedManager implements SeedManager {
12 |
13 | private List list = new ArrayList();
14 |
15 | public boolean add(Seed seed) {
16 | return list.add(seed);
17 | }
18 |
19 | @Override
20 | public synchronized List getSeedList(int maxConnectionCount) {
21 | List newList = new ArrayList();
22 | List removeList = new ArrayList();
23 | //排除连接失败且不重试的
24 | for (Seed seed : list) {
25 | if(seed.getStaus() == Seed.SEED_CONNECT_WAIT ||
26 | (seed.getStaus() == Seed.SEED_CONNECT_FAIL && seed.isRetry() &&
27 | (System.currentTimeMillis() > seed.getLastTime() + seed.getRetryInterval()))) {
28 | newList.add(seed);
29 | } else if(seed.getStaus() == Seed.SEED_CONNECT_FAIL && !seed.isRetry()) {
30 | removeList.add(seed);
31 | }
32 | }
33 | list.removeAll(removeList);
34 | return newList;
35 | }
36 |
37 | @Override
38 | public boolean hasMore() {
39 | for (Seed seed : list) {
40 | if(seed.getStaus() == Seed.SEED_CONNECT_WAIT ||
41 | (seed.getStaus() == Seed.SEED_CONNECT_FAIL && seed.isRetry() &&
42 | (System.currentTimeMillis() > seed.getLastTime() + seed.getRetryInterval()))) {
43 | return true;
44 | }
45 | }
46 | return false;
47 | }
48 |
49 | /**
50 | * 添加一个dns种子
51 | * @param domain
52 | * @return boolean
53 | */
54 | public boolean addDnsSeed(String domain) {
55 | return false;
56 | }
57 |
58 | /**
59 | * 重置种子节点
60 | */
61 | public void reset() {
62 | list = new ArrayList();
63 | }
64 |
65 | @Override
66 | public List getAllSeeds() {
67 | return list;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/network/SeedManager.java:
--------------------------------------------------------------------------------
1 | package org.inchain.network;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * 种子节点管理
7 | * @author ln
8 | *
9 | */
10 | public interface SeedManager {
11 |
12 | /**
13 | * 添加一个种子节点
14 | * @param seed
15 | * @return boolean
16 | */
17 | boolean add(Seed seed);
18 |
19 | /**
20 | * 添加一个dns种子
21 | * @param domain
22 | * @return boolean
23 | */
24 | boolean addDnsSeed(String domain);
25 |
26 | /**
27 | * 获取一个可用的种子节点列表
28 | * @param maxConnectionCount 最大数量
29 | * @return List
30 | */
31 | List getSeedList(int maxConnectionCount);
32 |
33 | /**
34 | * 获取所有的节点,不管可不可用
35 | * @return
36 | */
37 | List getAllSeeds();
38 |
39 | /**
40 | * 是否有更多的可用节点
41 | * @return boolean
42 | */
43 | boolean hasMore();
44 |
45 | /**
46 | * 重置种子节点
47 | */
48 | void reset();
49 | }
50 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/rpc/Server.java:
--------------------------------------------------------------------------------
1 | package org.inchain.rpc;
2 |
3 | import java.io.IOException;
4 |
5 | public interface Server {
6 |
7 | public void stop();
8 |
9 | public void start() throws IOException;
10 | }
11 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/rule/SystemRule.java:
--------------------------------------------------------------------------------
1 | package org.inchain.rule;
2 |
3 | /**
4 | * 系统规则
5 | * @author ln
6 | *
7 | */
8 | public interface SystemRule {
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/service/BlockForkService.java:
--------------------------------------------------------------------------------
1 | package org.inchain.service;
2 |
3 | import java.util.List;
4 |
5 | import org.inchain.crypto.Sha256Hash;
6 | import org.inchain.message.Block;
7 | import org.inchain.message.BlockHeader;
8 |
9 | /**
10 | * 块分叉处理
11 | * @author ln
12 | *
13 | */
14 | public interface BlockForkService {
15 |
16 | void startSyn();
17 |
18 | void start();
19 |
20 | void stop();
21 |
22 | /**
23 | * 添加分叉块
24 | * @param block
25 | */
26 | void addBlockFork(Block block);
27 |
28 | /**
29 | * 添加到待处罚列表
30 | * @param block
31 | * @param type 违规类型, 1 重复出块, 2大量垃圾块攻击, 3打包不合法的交易
32 | */
33 | void addBlockInPenalizeList(Block block, int type);
34 |
35 | /**
36 | * 获取并移除待处罚列表
37 | * @return List
38 | */
39 | List getAndRemovePenalize();
40 |
41 | /**
42 | * 获取一个块
43 | * @param hash
44 | * @return Block
45 | */
46 | Block getBlock(Sha256Hash hash);
47 | }
48 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/service/CreditCollectionService.java:
--------------------------------------------------------------------------------
1 | package org.inchain.service;
2 |
3 | /**
4 | * 信用服务,存放信用累计凭证,这样能快速的验证
5 | * @author ln
6 | *
7 | */
8 | public interface CreditCollectionService {
9 |
10 | /**
11 | * 验证是否可以获得信用
12 | * @param type 类型,参考 Definition 里的定义
13 | * @param hash160 信用获得人
14 | * @param time 以凭证打包进区块的时间为准,也就是以凭证所在的区块时间为准来判断
15 | * @return boolean
16 | */
17 | boolean verification(int type, byte[] hash160, long time);
18 |
19 | /**
20 | * 增加信用
21 | * @param type 类型,参考 Definition 里的定义
22 | * @param hash160 信用获得人
23 | * @param time 凭证所在的区块时间
24 | * @return boolean
25 | */
26 | boolean addCredit(int type, byte[] hash160, long time);
27 |
28 | /**
29 | * 移除最近的记录,主链上的块回滚时调用
30 | * @param type 类型,参考 Definition 里的定义
31 | * @param hash160 信用获得人
32 | * @return boolean
33 | */
34 | boolean removeCredit(int type, byte[] hash160);
35 |
36 | /**
37 | * 系统启动初始加载
38 | * 会阻塞直到加载完成
39 | * @return boolean
40 | */
41 | boolean onload();
42 |
43 | /**
44 | * 清理
45 | * @return boolean
46 | */
47 | boolean clean();
48 | }
49 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/service/SystemStatusService.java:
--------------------------------------------------------------------------------
1 | package org.inchain.service;
2 |
3 | /**
4 | * 系统状态服务
5 | * @author ln
6 | *
7 | */
8 | public interface SystemStatusService {
9 |
10 | /**
11 | * 初始化中
12 | */
13 | public final static int INITING = 0;
14 | /**
15 | * 数据同步中
16 | */
17 | public final static int DATA_SYNCHRONIZE = 1;
18 | /**
19 | * 数据修复中
20 | */
21 | public final static int DATA_RESET = 2;
22 |
23 | /**
24 | * 设置系统当前状态
25 | * @param status
26 | */
27 | public void setStatus(int status);
28 |
29 | /**
30 | * 获取当前系统状态
31 | * @return int
32 | */
33 | public int getStatus();
34 |
35 | /**
36 | * 判断当前系统状态是否是同步数据中
37 | * @return boolean
38 | */
39 | public boolean isDataSynchronize();
40 |
41 | /**
42 | * 判断当前是否在数据修复中
43 | * @return boolean
44 | */
45 | public boolean isDataReset();
46 | }
47 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/service/impl/SystemStatusServiceImpl.java:
--------------------------------------------------------------------------------
1 | package org.inchain.service.impl;
2 |
3 | import org.inchain.core.DataSynchronizeHandler;
4 | import org.inchain.service.SystemStatusService;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 |
8 | /**
9 | * 系统状态服务
10 | * @author ln
11 | *
12 | */
13 | @Service
14 | public class SystemStatusServiceImpl implements SystemStatusService {
15 |
16 | private int status;
17 |
18 | @Autowired
19 | private DataSynchronizeHandler dataSynchronizeHandler;
20 |
21 | @Override
22 | public void setStatus(int status) {
23 | this.status = status;
24 | }
25 |
26 | @Override
27 | public int getStatus() {
28 | return status;
29 | }
30 |
31 | @Override
32 | public boolean isDataSynchronize() {
33 | if(dataSynchronizeHandler.hasComplete()) {
34 | return false;
35 | } else {
36 | return true;
37 | }
38 | }
39 |
40 | @Override
41 | public boolean isDataReset() {
42 | return status == DATA_RESET;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/signers/TransactionSigner.java:
--------------------------------------------------------------------------------
1 | package org.inchain.signers;
2 |
3 | import org.inchain.crypto.ECKey;
4 | import org.inchain.transaction.Transaction;
5 |
6 | public interface TransactionSigner {
7 |
8 | boolean isReady();
9 |
10 | byte[] serialize();
11 |
12 | void deserialize(byte[] data);
13 |
14 | boolean signInputs(Transaction tx, ECKey key);
15 | boolean signOneInputs(Transaction tx, ECKey key,int inputIndex);
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/store/BlockForkStore.java:
--------------------------------------------------------------------------------
1 | package org.inchain.store;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.core.exception.ProtocolException;
7 | import org.inchain.message.Block;
8 | import org.inchain.network.NetworkParams;
9 |
10 | /**
11 | * 分叉块的存储
12 | * @author ln
13 | *
14 | */
15 | public class BlockForkStore extends Store {
16 |
17 | //状态
18 | private int status;
19 | private Block block;
20 | private int processCount;
21 |
22 | public BlockForkStore(NetworkParams network) {
23 | super(network);
24 | }
25 |
26 | public BlockForkStore(NetworkParams network, byte[] content) {
27 | super(network, content, 0);
28 | }
29 |
30 | public BlockForkStore(NetworkParams network, Block block, int status) {
31 | super(network);
32 | this.block = block;
33 | this.status = status;
34 | }
35 |
36 | @Override
37 | public void parse() throws ProtocolException {
38 |
39 | block = new Block(network, payload);
40 | status = readBytes(1)[0] & 0xff;
41 | length = cursor;
42 | }
43 |
44 | /**
45 | * 序列化区块
46 | */
47 | @Override
48 | public void serializeToStream(OutputStream stream) throws IOException {
49 |
50 | block.serializeToStream(stream);
51 | stream.write(status);
52 | }
53 |
54 | public Block getBlock() {
55 | return block;
56 | }
57 |
58 | public void setBlock(Block block) {
59 | this.block = block;
60 | }
61 |
62 | public int getStatus() {
63 | return status;
64 | }
65 |
66 | public void setStatus(int status) {
67 | this.status = status;
68 | }
69 |
70 | public int getProcessCount() {
71 | return processCount;
72 | }
73 |
74 | public void addProcessCount() {
75 | this.processCount++;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/store/Store.java:
--------------------------------------------------------------------------------
1 | package org.inchain.store;
2 |
3 | import org.inchain.core.exception.ProtocolException;
4 | import org.inchain.message.Message;
5 | import org.inchain.network.NetworkParams;
6 | import org.inchain.network.NetworkParams.ProtocolVersion;
7 |
8 | public abstract class Store extends Message {
9 |
10 | protected byte[] key;
11 |
12 | public Store() {
13 | }
14 |
15 | protected Store(NetworkParams network) {
16 | this.network = network;
17 | serializer = network.getDefaultSerializer();
18 | }
19 |
20 | protected Store(NetworkParams network, byte[] payload, int offset) throws ProtocolException {
21 | this(network, payload, offset, network.getProtocolVersionNum(ProtocolVersion.CURRENT));
22 | }
23 |
24 | protected Store(NetworkParams network, byte[] payload, int offset, int protocolVersion) throws ProtocolException {
25 | super(network, payload, offset, protocolVersion, network.getDefaultSerializer(), UNKNOWN_LENGTH);
26 | }
27 |
28 | public byte[] getKey() {
29 | return key;
30 | }
31 |
32 | public void setKey(byte[] key) {
33 | this.key = key;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/store/StoreProvider.java:
--------------------------------------------------------------------------------
1 | package org.inchain.store;
2 |
3 | import java.io.IOException;
4 |
5 | /**
6 | * 存储服务
7 | * @author ln
8 | *
9 | */
10 | public interface StoreProvider {
11 |
12 | void put(Store store);
13 |
14 | Store get(byte[] key);
15 |
16 | void delete(byte[] key);
17 |
18 | void close() throws IOException;
19 | }
20 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/transaction/Input.java:
--------------------------------------------------------------------------------
1 | package org.inchain.transaction;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 | import java.util.List;
6 |
7 | import org.inchain.script.Script;
8 |
9 | public interface Input {
10 |
11 | /**
12 | * 序列化交易输入
13 | * @param stream
14 | * @throws IOException
15 | */
16 | public void serialize(OutputStream stream) throws IOException;
17 |
18 | public byte[] getScriptBytes();
19 |
20 | public void setScriptBytes(byte[] scriptBytes);
21 |
22 | void clearScriptBytes();
23 | void setScriptSig(Script scriptSig);
24 | Script getScriptSig();
25 |
26 | Script getFromScriptSig();
27 |
28 | boolean addFrom(TransactionOutput from);
29 | List getFroms();
30 | void setFroms(List froms);
31 | }
32 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/transaction/Output.java:
--------------------------------------------------------------------------------
1 | package org.inchain.transaction;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.script.Script;
7 |
8 | public interface Output {
9 |
10 | /**
11 | * 序列化交易输出
12 | * @param stream
13 | * @throws IOException
14 | */
15 | public void serialize(OutputStream stream) throws IOException;
16 |
17 | public byte[] getScriptBytes();
18 |
19 | public void setScriptBytes(byte[] scriptBytes);
20 |
21 | void setScript(Script scriptSig);
22 |
23 | Script getScript();
24 |
25 | long getValue();
26 |
27 | long getLockTime();
28 | }
29 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/transaction/business/AssetsTransferTransaction.java:
--------------------------------------------------------------------------------
1 | package org.inchain.transaction.business;
2 |
3 | import org.inchain.account.Address;
4 | import org.inchain.core.Coin;
5 | import org.inchain.core.Definition;
6 | import org.inchain.core.VarInt;
7 | import org.inchain.core.exception.ProtocolException;
8 | import org.inchain.core.exception.VerificationException;
9 | import org.inchain.crypto.Sha256Hash;
10 | import org.inchain.network.NetworkParams;
11 | import org.inchain.utils.Utils;
12 |
13 | import java.io.IOException;
14 | import java.io.OutputStream;
15 | import java.util.Arrays;
16 |
17 | /**
18 | * 资产交易
19 | */
20 | public class AssetsTransferTransaction extends AssetsIssuedTransaction {
21 |
22 | public AssetsTransferTransaction(NetworkParams network) {
23 | super(network);
24 | type = Definition.TYPE_ASSETS_TRANSFER;
25 | }
26 |
27 | public AssetsTransferTransaction(NetworkParams params, byte[] payloadBytes) throws ProtocolException {
28 | this(params, payloadBytes, 0);
29 | }
30 |
31 | public AssetsTransferTransaction(NetworkParams params, byte[] payloadBytes, int offset) throws ProtocolException {
32 | super(params, payloadBytes, offset);
33 | }
34 |
35 | public AssetsTransferTransaction(NetworkParams params, Sha256Hash assetsHash, byte[] receiver, Long amount,byte[]remark) {
36 | super(params);
37 | this.amount = amount;
38 | this.assetsHash = assetsHash;
39 | this.receiver = receiver;
40 | this.remark = remark;
41 | type = Definition.TYPE_ASSETS_TRANSFER;
42 | }
43 |
44 | @Override
45 | public void verify() throws VerificationException {
46 | super.verify();
47 | }
48 |
49 | @Override
50 | protected void serializeBodyToStream(OutputStream stream) throws IOException {
51 | super.serializeBodyToStream(stream);
52 | }
53 |
54 | @Override
55 | protected void parseBody() throws ProtocolException {
56 | super.parseBody();
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/transaction/business/CertAccountUpdateTransaction.java:
--------------------------------------------------------------------------------
1 | package org.inchain.transaction.business;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | import org.inchain.account.AccountBody;
7 | import org.inchain.core.Definition;
8 | import org.inchain.core.exception.ProtocolException;
9 | import org.inchain.core.exception.VerificationException;
10 | import org.inchain.network.NetworkParams;
11 |
12 | /**
13 | * 认证账户信息修改
14 | * @author ln
15 | *
16 | */
17 | public class CertAccountUpdateTransaction extends CertAccountRegisterTransaction {
18 |
19 | public CertAccountUpdateTransaction(NetworkParams network, byte[] hash160, byte[][] mgPubkeys, byte[][] trPubkeys, AccountBody body,byte[] superhash160,int superlevel) {
20 | super(network, hash160, mgPubkeys, trPubkeys, body,superhash160,superlevel-1);
21 | this.setType(Definition.TYPE_CERT_ACCOUNT_UPDATE);
22 | }
23 |
24 | public CertAccountUpdateTransaction(NetworkParams params, byte[] payloadBytes) throws ProtocolException {
25 | this(params, payloadBytes, 0);
26 | }
27 |
28 | public CertAccountUpdateTransaction(NetworkParams params, byte[] payloadBytes, int offset) throws ProtocolException {
29 | super(params, payloadBytes, offset);
30 | }
31 |
32 | /**
33 | * 反序列化交易
34 | */
35 | protected void parseBody() {
36 | super.parseBody();
37 | }
38 |
39 | @Override
40 | protected void serializeBodyToStream(OutputStream stream) throws IOException {
41 | super.serializeBodyToStream(stream);
42 | }
43 |
44 | /**
45 | * 验证交易的合法性
46 | */
47 | @Override
48 | public void verify() throws VerificationException {
49 | super.verify();
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/transaction/business/UnkonwTransaction.java:
--------------------------------------------------------------------------------
1 | package org.inchain.transaction.business;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 | import java.util.Arrays;
6 |
7 | import org.inchain.core.exception.ProtocolException;
8 | import org.inchain.core.exception.VerificationException;
9 | import org.inchain.network.NetworkParams;
10 | import org.inchain.utils.Utils;
11 |
12 | /**
13 | * 未知的交易,用于老版本兼容新协议
14 | * @author ln
15 | *
16 | */
17 | public class UnkonwTransaction extends CommonlyTransaction {
18 |
19 | private byte[] content;
20 |
21 | public UnkonwTransaction(NetworkParams params, byte[] payloadBytes, int offset) throws ProtocolException {
22 | super(params);
23 | long bodyLength = Utils.readUint32(payloadBytes, 1);
24 | this.content = Arrays.copyOfRange(payloadBytes, 0, (int)bodyLength);
25 | length = content.length;
26 | }
27 |
28 | @Override
29 | public void verify() throws VerificationException {
30 | }
31 |
32 | @Override
33 | public void verifyScript() {
34 | }
35 |
36 | @Override
37 | protected void parse() throws ProtocolException {
38 | }
39 |
40 | @Override
41 | protected void serializeToStream(OutputStream stream) throws IOException {
42 | stream.write(content);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/utils/ByteArrayTool.java:
--------------------------------------------------------------------------------
1 | package org.inchain.utils;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.IOException;
5 |
6 | /**
7 | * 字节流工具
8 | * @author ln
9 | *
10 | */
11 | public class ByteArrayTool {
12 |
13 | private ByteArrayOutputStream array;
14 |
15 | public ByteArrayTool() {
16 | array = new ByteArrayOutputStream();
17 | }
18 |
19 | public ByteArrayTool(int size) {
20 | array = new ByteArrayOutputStream(size);
21 | }
22 |
23 | public ByteArrayTool(byte[] bytes) {
24 | array = new ByteArrayOutputStream(bytes.length);
25 | try {
26 | array.write(bytes);
27 | } catch (IOException e) {
28 | e.printStackTrace();
29 | }
30 | }
31 |
32 | public void append(byte b) {
33 | try {
34 | array.write(new byte[]{b});
35 | } catch (IOException e) {
36 | e.printStackTrace();
37 | }
38 | }
39 |
40 | public void append(int i) {
41 | append((byte)i);
42 | }
43 |
44 | public void append(byte[] bytes) {
45 | try {
46 | array.write(bytes);
47 | } catch (IOException e) {
48 | e.printStackTrace();
49 | }
50 | }
51 |
52 | public void append(long val) {
53 | try {
54 | Utils.uint32ToByteStreamLE(val, array);
55 | } catch (IOException e) {
56 | e.printStackTrace();
57 | }
58 | }
59 |
60 | public void append64(long val) {
61 | try {
62 | Utils.int64ToByteStreamLE(val, array);
63 | } catch (IOException e) {
64 | e.printStackTrace();
65 | }
66 | }
67 |
68 | public byte[] toArray() {
69 | try {
70 | return array.toByteArray();
71 | } finally {
72 | try {
73 | array.close();
74 | } catch (IOException e) {
75 | e.printStackTrace();
76 | }
77 | }
78 | }
79 |
80 | public void close() {
81 | try {
82 | array.close();
83 | } catch (IOException e) {
84 | e.printStackTrace();
85 | }
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/utils/ByteStreams.java:
--------------------------------------------------------------------------------
1 | package org.inchain.utils;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.io.OutputStream;
7 |
8 | public class ByteStreams {
9 |
10 | private static final int BUF_SIZE = 0x1000; // 4K
11 |
12 | public static byte[] toByteArray(InputStream in) throws IOException {
13 | ByteArrayOutputStream out = new ByteArrayOutputStream();
14 | copy(in, out);
15 | return out.toByteArray();
16 | }
17 |
18 | public static long copy(InputStream from, OutputStream to)
19 | throws IOException {
20 | Utils.checkNotNull(from);
21 | Utils.checkNotNull(to);
22 | byte[] buf = new byte[BUF_SIZE];
23 | long total = 0;
24 | while (true) {
25 | int r = from.read(buf);
26 | if (r == -1) {
27 | break;
28 | }
29 | to.write(buf, 0, r);
30 | total += r;
31 | }
32 | return total;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/utils/ContextPropagatingThreadFactory.java:
--------------------------------------------------------------------------------
1 | package org.inchain.utils;
2 |
3 | import java.util.concurrent.ThreadFactory;
4 |
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 |
8 | public class ContextPropagatingThreadFactory implements ThreadFactory {
9 | private static final Logger log = LoggerFactory.getLogger(ContextPropagatingThreadFactory.class);
10 | private final String name;
11 | private final int priority;
12 |
13 | public ContextPropagatingThreadFactory(String name, int priority) {
14 | this.name = name;
15 | this.priority = priority;
16 | }
17 |
18 | public ContextPropagatingThreadFactory(String name) {
19 | this(name, Thread.NORM_PRIORITY);
20 | }
21 |
22 | @Override
23 | public Thread newThread(final Runnable r) {
24 | Thread thread = new Thread(new Runnable() {
25 | @Override
26 | public void run() {
27 | try {
28 | r.run();
29 | } catch (Exception e) {
30 | log.error("Exception in thread", e);
31 | }
32 | }
33 | }, name);
34 | thread.setPriority(priority);
35 | thread.setDaemon(true);
36 | return thread;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/utils/Hex.java:
--------------------------------------------------------------------------------
1 | package org.inchain.utils;
2 |
3 | /**
4 | * 16进制编码解码类
5 | */
6 | public class Hex {
7 | /**
8 | * 对字节数据进行16进制编码。
9 | *
10 | * @param src 源字节数组
11 | * @return String 编码后的字符串
12 | */
13 | public static String encode(byte[] src) {
14 | StringBuffer strbuf = new StringBuffer(src.length * 2);
15 | int i;
16 |
17 | for (i = 0; i < src.length; i++) {
18 | if (((int) src[i] & 0xff) < 0x10)
19 | strbuf.append("0");
20 |
21 | strbuf.append(Long.toString((int) src[i] & 0xff, 16));
22 | }
23 |
24 | return strbuf.toString();
25 | }
26 |
27 | /**
28 | * 对16进制编码的字符串进行解码。
29 | *
30 | * @param hexString 源字串
31 | * @return byte[] 解码后的字节数组
32 | */
33 | public static byte[] decode(String hexString) {
34 | byte[] bts = new byte[hexString.length() / 2];
35 | for (int i = 0; i < bts.length; i++) {
36 | bts[i] = (byte) Integer.parseInt(hexString.substring(2 * i, 2 * i + 2), 16);
37 | }
38 | return bts;
39 | }
40 |
41 | }
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/utils/Ints.java:
--------------------------------------------------------------------------------
1 | package org.inchain.utils;
2 |
3 | public class Ints {
4 |
5 | public static int fromBytes(byte b1, byte b2, byte b3, byte b4) {
6 | return b1 << 24 | (b2 & 0xFF) << 16 | (b3 & 0xFF) << 8 | (b4 & 0xFF);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/utils/RandomUtil.java:
--------------------------------------------------------------------------------
1 | package org.inchain.utils;
2 |
3 | /**
4 | * 随机数工具
5 | * @author ln
6 | *
7 | */
8 | public final class RandomUtil {
9 |
10 | /**
11 | * 生成一个长整形随机数
12 | * @return long
13 | */
14 | public static long randomLong() {
15 | return (long)(Math.random()*Long.MAX_VALUE);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/validator/RuleValidator.java:
--------------------------------------------------------------------------------
1 | package org.inchain.validator;
2 |
3 | import org.inchain.rule.SystemRule;
4 |
5 | /**
6 | * 规则验证器,用来杜绝各类攻击
7 | * @author ln
8 | *
9 | */
10 | public class RuleValidator implements Validator {
11 |
12 | @Override
13 | public ValidatorResult valDo(SystemRule t) {
14 | // TODO Auto-generated method stub
15 | return null;
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/validator/TransactionValidatorResult.java:
--------------------------------------------------------------------------------
1 | package org.inchain.validator;
2 |
3 | import org.inchain.core.Coin;
4 |
5 | /**
6 | * 交易验证结果
7 | * @author ln
8 | *
9 | */
10 | public class TransactionValidatorResult {
11 |
12 | /** 错误代码 -- 不存在的交易 **/
13 | public final static int ERROR_CODE_NOT_FOUND = 1;
14 | /** 错误代码 -- 已使用 **/
15 | public final static int ERROR_CODE_USED = 2;
16 | /** 错误代码 -- 已存在 **/
17 | public final static int ERROR_CODE_EXIST = 3;
18 |
19 | private boolean success; //验证结果
20 | private String message; //验证结果信息
21 | private int errorCode; //错误代码
22 | private Coin fee; //交易手续费
23 |
24 | public void setResult(boolean success, String message) {
25 | this.success = success;
26 | this.message = message;
27 | }
28 |
29 | public void setResult(boolean success, int errorCode, String message) {
30 | this.success = success;
31 | this.message = message;
32 | this.errorCode = errorCode;
33 | }
34 |
35 | public boolean isSuccess() {
36 | return success;
37 | }
38 | public void setSuccess(boolean success) {
39 | this.success = success;
40 | }
41 | public String getMessage() {
42 | return message;
43 | }
44 | public void setMessage(String message) {
45 | this.message = message;
46 | }
47 | public Coin getFee() {
48 | return fee;
49 | }
50 | public void setFee(Coin fee) {
51 | this.fee = fee;
52 | }
53 | public int getErrorCode() {
54 | return errorCode;
55 | }
56 |
57 | public void setErrorCode(int errorCode) {
58 | this.errorCode = errorCode;
59 | }
60 |
61 | @Override
62 | public String toString() {
63 | return "TransactionValidatorResult [success=" + success + ", message=" + message + ", errorCode=" + errorCode
64 | + ", fee=" + fee + "]";
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/validator/Validator.java:
--------------------------------------------------------------------------------
1 | package org.inchain.validator;
2 |
3 | /**
4 | * 交易验证器验证器
5 | * @author ln
6 | * @param
7 | *
8 | */
9 | public interface Validator {
10 |
11 | /**
12 | * 交易验证,普通的交易验证
13 | * 不包含共识时的交易验证,共识时的验证是独立的流程
14 | * @param t
15 | * @return ValidatorResult>
16 | */
17 | ValidatorResult> valDo(T t);
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/inchain-core/src/main/java/org/inchain/validator/ValidatorResult.java:
--------------------------------------------------------------------------------
1 | package org.inchain.validator;
2 |
3 | /**
4 | * 验证结果
5 | * @author ln
6 | *
7 | */
8 | public interface ValidatorResult {
9 |
10 | /**
11 | * 获取验证结果
12 | * @return T
13 | */
14 | T getResult();
15 | }
16 |
--------------------------------------------------------------------------------
/inchain-core/src/main/resources/placeholder:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/inchaincodes/inchain/f39b21c6729f363b4011a937ddf47ec94c423f56/inchain-core/src/main/resources/placeholder
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/TestNetBaseTestCase.java:
--------------------------------------------------------------------------------
1 | package org.inchain;
2 |
3 | import org.junit.runner.RunWith;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.context.ApplicationContext;
7 | import org.springframework.test.context.ContextConfiguration;
8 | import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 |
11 | @RunWith(SpringJUnit4ClassRunner.class)
12 | @ContextConfiguration(locations = { "classpath*:applicationContext-testnet.xml", "classpath*:applicationContext.xml" })
13 | public class TestNetBaseTestCase extends AbstractJUnit4SpringContextTests {
14 |
15 | protected Logger log = LoggerFactory.getLogger(getClass());
16 |
17 | public T getBean(Class type) {
18 | return applicationContext.getBean(type);
19 | }
20 |
21 | public Object getBean(String beanName) {
22 | return applicationContext.getBean(beanName);
23 | }
24 |
25 | protected ApplicationContext getContext() {
26 | return applicationContext;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/UnitBaseTestCase.java:
--------------------------------------------------------------------------------
1 | package org.inchain;
2 |
3 | import org.junit.runner.RunWith;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.context.ApplicationContext;
7 | import org.springframework.test.context.ContextConfiguration;
8 | import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 |
11 | @RunWith(SpringJUnit4ClassRunner.class)
12 | @ContextConfiguration(locations = { "classpath*:applicationContext-unit.xml", "classpath*:applicationContext.xml" })
13 | public class UnitBaseTestCase extends AbstractJUnit4SpringContextTests {
14 |
15 | protected Logger log = LoggerFactory.getLogger(getClass());
16 |
17 | public T getBean(Class type) {
18 | return applicationContext.getBean(type);
19 | }
20 |
21 | public Object getBean(String beanName) {
22 | return applicationContext.getBean(beanName);
23 | }
24 |
25 | protected ApplicationContext getContext() {
26 | return applicationContext;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/core/AppKitTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.core;
2 |
3 | import org.inchain.UnitBaseTestCase;
4 | import org.inchain.kits.AppKit;
5 | import org.junit.Test;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 |
8 | public class AppKitTest extends UnitBaseTestCase {
9 |
10 | @Autowired
11 | private AppKit appKit;
12 |
13 | @Test
14 | public void testAppKit() {
15 | appKit.startSyn();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/db/LevelDBTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.db;
2 |
3 | import java.io.IOException;
4 |
5 | import org.inchain.UnitBaseTestCase;
6 | import org.junit.Test;
7 |
8 | public class LevelDBTest extends UnitBaseTestCase {
9 |
10 | private static final String filepath = "./data/db/inchain";
11 |
12 | @Test
13 | public void testLevelDb() throws IOException {
14 | Db storage = new LevelDB(filepath);
15 |
16 | long max = 100000;
17 | for (long i = 0; i < max; i++) {
18 | String value = "测试,this is value of "+i;
19 | byte[] key = ("key_"+i).getBytes();
20 | storage.put(key, value.getBytes());
21 | }
22 | //随机读
23 | long time = System.currentTimeMillis();
24 | int count = 10000;
25 | for (long i = 0; i < count; i++) {
26 | long index = (long) (Math.random()*max);
27 | byte[] key = ("key_"+index).getBytes();
28 | String value = new String(storage.get(key));
29 | System.out.println(value);
30 | }
31 | System.out.println("读取 "+count+" 条数据耗时:"+(System.currentTimeMillis() - time)+" ms");
32 | storage.close();
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/net/PeerGroupTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.net;
2 |
3 | import org.inchain.UnitBaseTestCase;
4 | import org.inchain.kits.PeerKit;
5 | import org.junit.Test;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 |
8 | public class PeerGroupTest extends UnitBaseTestCase {
9 |
10 | @Autowired
11 | private PeerKit peerKit;
12 |
13 | @Test
14 | public void test() {
15 |
16 | peerKit.startSyn();
17 |
18 | // try {
19 | // Thread.sleep(10000l);
20 | // } catch (InterruptedException e) {
21 | // e.printStackTrace();
22 | // }
23 | // peerKit.broadcastTransaction(new Transaction());
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/net/PeerGroupTest2.java:
--------------------------------------------------------------------------------
1 | package org.inchain.net;
2 |
3 | import java.util.Random;
4 |
5 | import org.inchain.UnitBaseTestCase;
6 | import org.inchain.kits.PeerKit;
7 | import org.inchain.message.PingMessage;
8 | import org.junit.Test;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 |
11 | public class PeerGroupTest2 extends UnitBaseTestCase {
12 |
13 | @Autowired
14 | private PeerKit peerKit;
15 |
16 | @Test
17 | public void test() {
18 |
19 | while(true) {
20 | try {
21 | Thread.sleep(8000l);
22 | peerKit.broadcastMessage(new PingMessage(new Random().nextLong()));
23 | } catch (InterruptedException e) {
24 | e.printStackTrace();
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/network/SeedManagerTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.network;
2 |
3 | import java.net.InetAddress;
4 | import java.net.UnknownHostException;
5 |
6 | import org.inchain.utils.IpUtil;
7 |
8 | public class SeedManagerTest {
9 |
10 | public static void main(String[] args) throws UnknownHostException {
11 | InetAddress[] response = InetAddress.getAllByName("test1.seed.inchain.org");
12 |
13 | for (InetAddress inetAddress : response) {
14 | System.out.println(inetAddress);
15 | }
16 |
17 | System.out.println(IpUtil.getIps());
18 |
19 | System.out.println(InetAddress.getLocalHost());
20 |
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/rpc/RPCTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.rpc;
2 |
3 | import java.io.IOException;
4 |
5 | public class RPCTest {
6 |
7 | public static void main(String[] args) throws IOException {
8 | new Thread(new Runnable() {
9 | public void run() {
10 | try {
11 | RPCServer serviceServer = new RPCServer( );
12 | // serviceServer.register(RPCService.class, RPCService.class);
13 | serviceServer.start();
14 | } catch (IOException e) {
15 | e.printStackTrace();
16 | }
17 | }
18 | }).start();
19 | // RPCService service = RPCClient.getRemoteProxyObj(RPCService.class, new InetSocketAddress("localhost", 8036));
20 | // System.out.println(service.sayHi("test"));
21 | }
22 | }
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/rpc/RpcClientTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.rpc;
2 |
3 | import org.codehaus.jettison.json.JSONException;
4 | import org.inchain.core.Coin;
5 | import java.io.IOException;
6 |
7 | public class RpcClientTest {
8 |
9 | public static final String IP_ADDR = "localhost";//服务器地址
10 | public static final int PORT = org.inchain.Configure.RPC_SERVER_PORT;//服务器端口号
11 | public static String cmd_head = "D:/src/inchain/inchain-client/inchain_cli.bat ";
12 | static String cmd_makeanti= "createantifakewithoutproduct {count:1} inchain123 0 ckDVgizRL9MLNTW2AS6BseMCqxtMxobPGz";
13 | static String[] paras = cmd_makeanti.split(" ");
14 | public static void main(String [] ar){
15 | for(int j =0;j<100;j ++) {
16 | new Thread() {
17 | public void run() {
18 | for (int i = 0; i < 10000; i++) {
19 | try {
20 | RPCClient.main(paras);
21 | } catch (IOException e) {
22 | e.printStackTrace();
23 | } catch (JSONException e) {
24 | e.printStackTrace();
25 | }
26 | }
27 | }
28 | }.start();
29 | }
30 | }
31 |
32 | /*
33 | Runtime runtime=Runtime.getRuntime();
34 | try{
35 | runtime.exec("cmd "+cmd_head + cmd_makeanti);
36 | }catch(Exception e){
37 | System.out.println("Error!");
38 | }
39 | }
40 | */
41 | }
42 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/rpc/ServerTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.rpc;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 | import java.io.PrintWriter;
7 | import java.net.ServerSocket;
8 | import java.net.Socket;
9 |
10 | public class ServerTest
11 | {
12 | private ServerSocket ss;
13 | private Socket socket;
14 | private BufferedReader in;
15 | private PrintWriter out;
16 |
17 | public ServerTest()
18 | {
19 | try
20 | {
21 | ss = new ServerSocket(10000);
22 |
23 | System.out.println("The server is waiting your input...");
24 |
25 | while(true)
26 | {
27 | socket = ss.accept();
28 |
29 | in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
30 | out = new PrintWriter(socket.getOutputStream(), true);
31 | String line = in.readLine();
32 |
33 | System.out.println("you input is : " + line);
34 |
35 | //out.println("you input is :" + line);
36 |
37 | out.close();
38 | in.close();
39 | socket.close();
40 |
41 | if(line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit"))
42 | break;
43 | }
44 |
45 | ss.close();
46 |
47 | } catch (IOException e) {
48 | e.printStackTrace();
49 | }
50 | }
51 |
52 | }
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/store/ChainstateStoreProviderTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.store;
2 |
3 | import static org.junit.Assert.assertEquals;
4 | import static org.junit.Assert.assertNotNull;
5 |
6 | import java.io.IOException;
7 | import java.math.BigInteger;
8 |
9 | import org.inchain.UnitBaseTestCase;
10 | import org.inchain.account.AccountTool;
11 | import org.inchain.account.Address;
12 | import org.inchain.crypto.ECKey;
13 | import org.inchain.network.NetworkParams;
14 | import org.inchain.utils.Utils;
15 | import org.junit.After;
16 | import org.junit.Before;
17 | import org.junit.Test;
18 | import org.springframework.beans.factory.annotation.Autowired;
19 |
20 | public class ChainstateStoreProviderTest extends UnitBaseTestCase {
21 |
22 | @Autowired
23 | private NetworkParams network;
24 | @Autowired
25 | private ChainstateStoreProvider chainstateStoreProvider;
26 |
27 | @Before
28 | public void init() throws IOException {
29 | }
30 |
31 | @After
32 | public void close() throws IOException {
33 | }
34 |
35 | @Test
36 | public void testInit() throws Exception {
37 | assertNotNull(network);
38 | assertNotNull(chainstateStoreProvider);
39 | }
40 |
41 | @Test
42 | public void testGetCredit() throws Exception {
43 | ECKey key = ECKey.fromPrivate(new BigInteger("61914497277584841097702477783063064420681667313180238384957944936487927892583"));
44 | Address address = AccountTool.newAddress(network, key);
45 | byte[] infos = chainstateStoreProvider.getBytes(address.getHash160());
46 |
47 | long credit = Utils.readUint32BE(infos, 0);
48 |
49 | assertEquals(999999l, credit);
50 |
51 | assertEquals(1, infos[41]);
52 |
53 | long balance = Utils.readUint32BE(infos, 4);
54 | assertEquals(0, balance);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/test/Base16Test.java:
--------------------------------------------------------------------------------
1 | package org.inchain.test;
2 |
3 | import org.inchain.utils.Hex;
4 |
5 | public class Base16Test {
6 |
7 | public static void main(String[] args) {
8 | String res = Hex.encode("12#$%089*)(8df0sa0SfLKALf0sa9ss3456".getBytes());
9 | System.out.println(res);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/test/Base58Test.java:
--------------------------------------------------------------------------------
1 | package org.inchain.test;
2 |
3 | import org.inchain.utils.Base58;
4 |
5 | public class Base58Test {
6 |
7 | public static void main(String[] args) {
8 | byte[] b = Base58.decode("LLxSnHLN2CYyzB5eWTR9K9rS9uWtbTQFb6");
9 | System.out.println(b.length);
10 | System.out.println(new String(b));
11 |
12 | byte[] versionAndDataBytes = Base58.decodeChecked("LLxSnHLN2CYyzB5eWTR9K9rS9uWtbTQFb6");
13 | byte versionByte = versionAndDataBytes[0];
14 |
15 | int version = versionByte & 0xFF;
16 | System.out.println("version is "+version);
17 |
18 | System.out.println(versionAndDataBytes.length);
19 |
20 | byte[] bytes = new byte[versionAndDataBytes.length - 1];
21 | System.arraycopy(versionAndDataBytes, 1, bytes, 0, versionAndDataBytes.length - 1);
22 |
23 | System.out.println(new String(bytes));
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/test/ECkeyTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.test;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 | import java.util.concurrent.ExecutorService;
6 | import java.util.concurrent.Executors;
7 | import java.util.concurrent.TimeUnit;
8 |
9 | import org.inchain.crypto.ECKey;
10 |
11 | public class ECkeyTest {
12 |
13 | public static void main(String[] args) throws InterruptedException {
14 |
15 | ECKey ek = new ECKey();
16 | System.out.println(ek.getPrivKey());
17 |
18 | final Set set = new HashSet();
19 |
20 | long time = System.currentTimeMillis();
21 |
22 | ExecutorService executors = Executors.newFixedThreadPool(4);
23 |
24 | for (int i = 0; i < 100; i++) {
25 |
26 | executors.execute(new Thread(){
27 | @Override
28 | public void run() {
29 | ECKey key = new ECKey();
30 |
31 | set.add(key.getPubKey(true).length);
32 | }
33 | });
34 | }
35 | executors.shutdown();
36 | executors.awaitTermination(1000, TimeUnit.SECONDS);
37 |
38 | System.out.println(set);
39 | System.out.println("耗时:"+(System.currentTimeMillis() - time));
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/test/SpringTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.test;
2 |
3 | import org.inchain.UnitBaseTestCase;
4 | import org.junit.Test;
5 |
6 | public class SpringTest extends UnitBaseTestCase {
7 |
8 | @Test
9 | public void testPrint() {
10 | System.out.println("========");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/test/Test.java:
--------------------------------------------------------------------------------
1 | package org.inchain.test;
2 |
3 | import org.codehaus.jettison.json.JSONArray;
4 | import org.codehaus.jettison.json.JSONException;
5 | import org.codehaus.jettison.json.JSONObject;
6 | import org.inchain.crypto.ECKey;
7 | import org.inchain.store.TransactionStore;
8 |
9 | import java.io.UnsupportedEncodingException;
10 | import java.util.ArrayList;
11 | import java.util.Collections;
12 | import java.util.Comparator;
13 | import java.util.List;
14 | import java.util.concurrent.Callable;
15 | import java.util.concurrent.ExecutionException;
16 | import java.util.concurrent.ExecutorService;
17 | import java.util.concurrent.Executors;
18 | import java.util.concurrent.Future;
19 |
20 | public class Test {
21 |
22 | public static class Mytask implements Callable {
23 | @Override
24 | public String call() throws Exception {
25 | System.out.println("---------------------start");
26 | Thread.sleep(4000l);
27 | System.out.println("---------------------end");
28 | return "result";
29 | }
30 |
31 | }
32 |
33 | public static class MytaskListener {
34 | public void done() {
35 |
36 | }
37 | }
38 |
39 | public static void main(String[] args){
40 | // ECKey key1 = new ECKey();
41 | // ECKey key2 = new ECKey();
42 | //
43 | // System.out.println(new String(key1.getPubKey()));
44 | //
45 | // System.out.println(new String(key2.getPubKey()));
46 |
47 | long max = Long.MAX_VALUE;
48 |
49 | if(3 == 0x7fffffffffffffffL + 0x7fffffffffffffffL + 5) {
50 | System.out.println("yes");
51 | }else {
52 | System.out.println("no");
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/test/ThreadLockTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.test;
2 |
3 | import java.util.concurrent.locks.Lock;
4 | import java.util.concurrent.locks.ReentrantLock;
5 |
6 | public class ThreadLockTest {
7 |
8 | private Lock lock = new ReentrantLock();
9 | private int index;
10 |
11 | public void setIndex(int index) {
12 | this.index = index;
13 | }
14 |
15 | public int getIndex() {
16 | return index;
17 | }
18 |
19 | public void test() {
20 | lock.lock();
21 | try {
22 | System.out.println("in");
23 | final Thread t = new Thread() {
24 | @Override
25 | public void run() {
26 | index++;
27 | try {
28 | Thread.sleep(5000l);
29 | synchronized (this) {
30 | notifyAll();
31 | }
32 | } catch (InterruptedException e) {
33 | e.printStackTrace();
34 | }
35 | }
36 | };
37 | try {
38 | t.start();
39 | synchronized (t) {
40 | t.wait(1000);
41 | }
42 | } catch (Exception e) {
43 | e.printStackTrace();
44 | }
45 | System.out.println("index is "+index);
46 | } finally {
47 | lock.unlock();
48 | }
49 | }
50 |
51 | public static void main(String[] args) {
52 | final ThreadLockTest test = new ThreadLockTest();
53 |
54 | for (int i = 0; i < 3; i++) {
55 | new Thread() {
56 | public void run() {
57 | test.test();
58 | };
59 | }.start();
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/transaction/AntifakeCodeVerifyTransactionTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.transaction;
2 |
3 | import java.io.IOException;
4 | import java.util.concurrent.ExecutionException;
5 | import java.util.concurrent.TimeoutException;
6 |
7 | import javax.annotation.PostConstruct;
8 |
9 | import org.inchain.TestNetBaseTestCase;
10 | import org.inchain.core.BroadcastResult;
11 | import org.inchain.core.VerifyAntifakeCodeResult;
12 | import org.inchain.kits.AccountKit;
13 | import org.inchain.kits.AppKit;
14 | import org.inchain.network.NetworkParams;
15 | import org.junit.Before;
16 | import org.junit.Test;
17 | import org.springframework.beans.factory.annotation.Autowired;
18 |
19 | /**
20 | * 防伪码验证测试
21 | * @author ln
22 | *
23 | */
24 | public class AntifakeCodeVerifyTransactionTest extends TestNetBaseTestCase {
25 |
26 | @Autowired
27 | private NetworkParams network;
28 | @Autowired
29 | private AppKit appKit;
30 | @Autowired
31 | private AccountKit accountKit;
32 |
33 | @PostConstruct
34 | public void init() throws IOException {
35 | appKit.start();
36 | }
37 |
38 | @Before
39 | public void waitAminute() {
40 | try {
41 | Thread.sleep(5000l);
42 | } catch (InterruptedException e) {
43 | e.printStackTrace();
44 | }
45 | }
46 |
47 | @Test
48 | public void testVerifyAntifakeCode() throws InterruptedException, ExecutionException, TimeoutException, IOException {
49 |
50 | log.info("best block height {}", network.getBestBlockHeight());
51 |
52 | String antifakeCodeContent = "xFdeHFv8RsHn6PHYRE8JTDLwRKPRe7mcej2R5c";
53 | VerifyAntifakeCodeResult result = accountKit.verifyAntifakeCode(antifakeCodeContent);
54 |
55 | log.info("{}", result);
56 |
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/inchain-core/src/test/java/org/inchain/transaction/RegConsensusTranslationTest.java:
--------------------------------------------------------------------------------
1 | package org.inchain.transaction;
2 |
3 | import java.math.BigInteger;
4 |
5 | import org.inchain.UnitBaseTestCase;
6 | import org.inchain.account.Account;
7 | import org.inchain.account.AccountTool;
8 | import org.inchain.account.Address;
9 | import org.inchain.crypto.ECKey;
10 | import org.inchain.kits.PeerKit;
11 | import org.inchain.network.NetworkParams;
12 | import org.inchain.transaction.business.RegConsensusTransaction;
13 | import org.junit.Assert;
14 | import org.junit.Test;
15 | import org.springframework.beans.factory.annotation.Autowired;
16 |
17 | public class RegConsensusTranslationTest extends UnitBaseTestCase {
18 |
19 | @Autowired
20 | private NetworkParams network;
21 | @Autowired
22 | private PeerKit peerKit;
23 |
24 | @Test
25 | public void testTranslation() {
26 |
27 | //共识账户
28 | ECKey key = ECKey.fromPrivate(new BigInteger("61914497277584841097702477783063064420681667313180238384957944936487927892583"));
29 | RegConsensusTransaction regConsensusTransaction = new RegConsensusTransaction(network, 1l, 1478070769l, null);
30 | Address address = AccountTool.newAddress(network, key);
31 |
32 | Account account = new Account(network);
33 | account.setAddress(address);
34 | account.setEcKey(key);
35 | regConsensusTransaction.sign(account);
36 |
37 | regConsensusTransaction.verify();
38 | regConsensusTransaction.verifyScript();
39 |
40 | RegConsensusTransaction regConsensusTransactionTemp = new RegConsensusTransaction(network, regConsensusTransaction.baseSerialize(), 0);
41 |
42 | Assert.assertEquals(regConsensusTransaction.getHash(), regConsensusTransactionTemp.getHash());
43 |
44 | try {
45 | Thread.sleep(3000l);
46 |
47 | peerKit.broadcastMessage(regConsensusTransactionTemp);
48 |
49 | Thread.sleep(10000l);
50 | } catch (InterruptedException e) {
51 | e.printStackTrace();
52 | }
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/inchain-core/src/test/resources/applicationContext-mainnet.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/inchain-core/src/test/resources/applicationContext-testnet.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/inchain-core/src/test/resources/applicationContext-unit.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/inchain-core/src/test/resources/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/inchain-core/src/test/resources/config.properties:
--------------------------------------------------------------------------------
1 | run.mode = 2
2 | mining = false
3 | port = 6888
4 | data.dir = ./data
5 | account.auto.init = false
6 |
--------------------------------------------------------------------------------
/inchain-core/src/test/resources/log4j.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/inchain-tools/.gitignore:
--------------------------------------------------------------------------------
1 | /data
2 | /logs
3 | /.idea
4 | /target
5 | *.class
6 | *.classpath
7 | *.iml
8 | *.log
9 | *.logs
--------------------------------------------------------------------------------
/inchain-tools/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 |
9 | org.inchain
10 | inchain
11 | 0.1
12 |
13 |
14 | inchain-tools
15 | inchain-tools
16 | ${inchain.version}
17 | A Java Inchain Library
18 |
19 | jar
20 |
21 | https://www.inchain.org
22 |
23 |
24 |
25 | The Inchain Team
26 | dev@inchain.org
27 |
28 |
29 |
30 |
31 |
32 |
33 | org.inchain
34 | inchain-core
35 | 0.1
36 |
37 |
38 |
--------------------------------------------------------------------------------
/inchain-tools/src/main/java/org/inchain/kit/AppKitDemo.java:
--------------------------------------------------------------------------------
1 | package org.inchain.kit;
2 |
3 | import java.io.File;
4 | import java.net.InetSocketAddress;
5 |
6 | import org.inchain.Configure;
7 | import org.inchain.account.AccountBody;
8 | import org.inchain.kits.AccountKit;
9 | import org.inchain.kits.AppKit;
10 | import org.inchain.listener.Listener;
11 | import org.inchain.network.NetworkParams;
12 | import org.inchain.network.NodeSeedManager;
13 | import org.inchain.network.Seed;
14 | import org.inchain.network.SeedManager;
15 | import org.inchain.network.TestNetworkParams;
16 |
17 | public class AppKitDemo {
18 |
19 | public static void main(String[] args) throws Exception {
20 |
21 | SeedManager seedManager = new NodeSeedManager();
22 | seedManager.add(new Seed(new InetSocketAddress("127.0.0.1", 8322), true, 25000));
23 |
24 | NetworkParams network = new TestNetworkParams(seedManager);
25 |
26 | //测试前先清空帐户目录
27 | File dir = new File(Configure.DATA_ACCOUNT);
28 | if(dir.listFiles() != null) {
29 | for (File file : dir.listFiles()) {
30 | file.delete();
31 | }
32 | }
33 |
34 | final AppKit kit = new AppKit();
35 | kit.startSyn();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/inchain-tools/src/main/resources/applicationContext-mainnet.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/inchain-tools/src/main/resources/applicationContext-testnet.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/inchain-tools/src/main/resources/applicationContext-unit.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/inchain-tools/src/main/resources/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/inchain-tools/src/main/resources/config.properties:
--------------------------------------------------------------------------------
1 | run.mode = 2
2 | #data.dir = d:/data/test
3 | mining = true
4 | port = 11862
5 |
6 | account.auto.init = true
7 |
8 | transfer.preferred = 2
9 |
10 | min.connect.count = 1
11 | max.connect.count = 10
--------------------------------------------------------------------------------
/inchain-tools/src/main/resources/log4j.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/inchain-tools/src/test/java/org/inchain/TestNetBaseTestCase.java:
--------------------------------------------------------------------------------
1 | package org.inchain;
2 |
3 | import org.junit.runner.RunWith;
4 | import org.springframework.context.ApplicationContext;
5 | import org.springframework.test.context.ContextConfiguration;
6 | import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8 |
9 | @RunWith(SpringJUnit4ClassRunner.class)
10 | @ContextConfiguration(locations = { "classpath*:applicationContext-testnet.xml", "classpath*:applicationContext.xml" })
11 | public class TestNetBaseTestCase extends AbstractJUnit4SpringContextTests {
12 |
13 | public T getBean(Class type) {
14 | return applicationContext.getBean(type);
15 | }
16 |
17 | public Object getBean(String beanName) {
18 | return applicationContext.getBean(beanName);
19 | }
20 |
21 | protected ApplicationContext getContext() {
22 | return applicationContext;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/inchain-tools/src/test/java/org/inchain/UnitBaseTestCase.java:
--------------------------------------------------------------------------------
1 | package org.inchain;
2 |
3 | import org.junit.runner.RunWith;
4 | import org.springframework.context.ApplicationContext;
5 | import org.springframework.test.context.ContextConfiguration;
6 | import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
7 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8 |
9 | @RunWith(SpringJUnit4ClassRunner.class)
10 | @ContextConfiguration(locations = { "classpath*:applicationContext-unit.xml", "classpath*:applicationContext.xml" })
11 | public class UnitBaseTestCase extends AbstractJUnit4SpringContextTests {
12 |
13 | public T getBean(Class type) {
14 | return applicationContext.getBean(type);
15 | }
16 |
17 | public Object getBean(String beanName) {
18 | return applicationContext.getBean(beanName);
19 | }
20 |
21 | protected ApplicationContext getContext() {
22 | return applicationContext;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/inchain-tools/src/test/resources/log4j.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------