o) {
52 | return displayName.compareTo(o.displayName);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/gui/HorizontalSeparator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui;
8 |
9 | import java.awt.Color;
10 | import java.awt.Graphics;
11 |
12 | import javax.swing.JComponent;
13 |
14 | public class HorizontalSeparator extends JComponent {
15 |
16 | private static final long serialVersionUID = -5330205221592957711L;
17 |
18 | private final Color leftColor;
19 | private final Color rightColor;
20 |
21 | public HorizontalSeparator() {
22 | this.leftColor = Color.GRAY;
23 | this.rightColor = Color.WHITE;
24 | setOpaque(false);
25 | }
26 |
27 | @Override
28 | protected void paintComponent(Graphics g) {
29 | g.setColor(leftColor);
30 | g.drawLine(0, 0, getWidth(), 0);
31 | g.setColor(rightColor);
32 | g.drawLine(0, 1, getWidth(), 1);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/gui/PlaceHolder.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui;
8 |
9 | import javax.swing.text.JTextComponent;
10 |
11 | /**
12 | * Placeholder of a Swing text component based on ${@link TextPrompt}.
13 | */
14 | public class PlaceHolder extends TextPrompt {
15 |
16 | private static final long serialVersionUID = -1350764114359129512L;
17 |
18 | public PlaceHolder(String text, JTextComponent component) {
19 | super(text, component);
20 | changeAlpha(0.5f);
21 | }
22 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/gui/TextContextMenuItem.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui;
8 |
9 | import javax.swing.text.DefaultEditorKit;
10 | import javax.swing.text.TextAction;
11 |
12 | import org.semux.message.GuiMessages;
13 | import org.semux.util.exception.UnreachableException;
14 |
15 | /**
16 | * This enum maintains mappings of text context menu.
17 | */
18 | enum TextContextMenuItem {
19 | CUT, COPY, PASTE;
20 |
21 | public TextAction toAction() {
22 | switch (this) {
23 | case CUT:
24 | return new DefaultEditorKit.CutAction();
25 | case COPY:
26 | return new DefaultEditorKit.CopyAction();
27 | case PASTE:
28 | return new DefaultEditorKit.PasteAction();
29 | }
30 | throw new UnreachableException();
31 | }
32 |
33 | @Override
34 | public String toString() {
35 | switch (this) {
36 | case CUT:
37 | return GuiMessages.get("Cut");
38 | case COPY:
39 | return GuiMessages.get("Copy");
40 | case PASTE:
41 | return GuiMessages.get("Paste");
42 | }
43 | throw new UnreachableException();
44 | }
45 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/gui/TransactionSender.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui;
8 |
9 | import org.semux.Kernel;
10 | import org.semux.Network;
11 | import org.semux.core.Amount;
12 | import org.semux.core.PendingManager;
13 | import org.semux.core.Transaction;
14 | import org.semux.core.TransactionType;
15 | import org.semux.gui.model.WalletAccount;
16 | import org.semux.util.TimeUtil;
17 |
18 | public class TransactionSender {
19 |
20 | public static PendingManager.ProcessingResult send(Kernel kernel, WalletAccount account, TransactionType type,
21 | byte[] to, Amount value, Amount fee, byte[] data) {
22 | return send(kernel, account, type, to, value, fee, data, 0, Amount.ZERO);
23 | }
24 |
25 | public static PendingManager.ProcessingResult send(Kernel kernel, WalletAccount account, TransactionType type,
26 | byte[] to, Amount value, Amount fee, byte[] data, long gas, Amount gasPrice) {
27 | PendingManager pendingMgr = kernel.getPendingManager();
28 |
29 | Network network = kernel.getConfig().network();
30 | byte[] from = account.getKey().toAddress();
31 | long nonce = pendingMgr.getNonce(from);
32 | long timestamp = TimeUtil.currentTimeMillis();
33 | Transaction tx = new Transaction(network, type, to, value, fee, nonce, timestamp, data, gas, gasPrice);
34 | tx.sign(account.getKey());
35 |
36 | return pendingMgr.addTransactionSync(tx);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/gui/VerticalSeparator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui;
8 |
9 | import java.awt.Color;
10 | import java.awt.Graphics;
11 |
12 | import javax.swing.JComponent;
13 |
14 | public class VerticalSeparator extends JComponent {
15 |
16 | private static final long serialVersionUID = -5802537037684892071L;
17 |
18 | private final Color leftColor;
19 | private final Color rightColor;
20 |
21 | public VerticalSeparator() {
22 | this.leftColor = Color.GRAY;
23 | this.rightColor = Color.WHITE;
24 | setOpaque(false);
25 | }
26 |
27 | @Override
28 | protected void paintComponent(Graphics g) {
29 | g.setColor(leftColor);
30 | g.drawLine(0, 0, 0, getHeight());
31 | g.setColor(rightColor);
32 | g.drawLine(1, 0, 1, getHeight());
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/gui/event/MainFrameStartedEvent.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.event;
8 |
9 | import org.semux.event.PubSubEvent;
10 |
11 | public class MainFrameStartedEvent implements PubSubEvent {
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/gui/event/WalletSelectionDialogShownEvent.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.event;
8 |
9 | import org.semux.event.PubSubEvent;
10 |
11 | public class WalletSelectionDialogShownEvent implements PubSubEvent {
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/message/CliMessages.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.message;
8 |
9 | import java.util.ResourceBundle;
10 |
11 | public final class CliMessages {
12 |
13 | private static final ResourceBundle RESOURCES = ResourceBundles.getDefaultBundle(ResourceBundles.CLI_MESSAGES);
14 |
15 | private CliMessages() {
16 | }
17 |
18 | public static String get(String key, Object... args) {
19 | return MessageFormatter.get(RESOURCES, key, args);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/message/GuiMessages.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.message;
8 |
9 | import java.util.ResourceBundle;
10 |
11 | public final class GuiMessages {
12 |
13 | private static final ResourceBundle RESOURCES = ResourceBundles.getDefaultBundle(ResourceBundles.GUI_MESSAGES);
14 |
15 | private GuiMessages() {
16 | }
17 |
18 | public static String get(String key, Object... args) {
19 | return MessageFormatter.get(RESOURCES, key, args);
20 | }
21 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/message/MessageFormatter.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.message;
8 |
9 | import java.text.MessageFormat;
10 | import java.util.ResourceBundle;
11 |
12 | public final class MessageFormatter {
13 |
14 | private MessageFormatter() {
15 | }
16 |
17 | /**
18 | * Gets a value from this bundle for the given {@code key}. Any second arguments
19 | * will be used to format the value.
20 | *
21 | * @param resourceBundle
22 | * the resource bundle of messages
23 | * @param key
24 | * the bundle key
25 | * @param args
26 | * objects used to format the value.
27 | * @return the formatted value for the given key.
28 | */
29 | public static String get(ResourceBundle resourceBundle, String key, Object... args) {
30 | String value = resourceBundle.getString(key);
31 | return MessageFormat.format(value, args);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/message/ResourceBundles.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.message;
8 |
9 | import java.util.Locale;
10 | import java.util.ResourceBundle;
11 |
12 | /**
13 | * This enum encapsulates available resource bundles of messages and an utility
14 | * function getDefaultBundle for deciding the default locale
15 | *
16 | *
17 | * The locale used is the current value of the default locale for this instance
18 | * of the Java Virtual Machine.
19 | *
20 | */
21 | public enum ResourceBundles {
22 |
23 | GUI_MESSAGES("org/semux/gui/messages"), CLI_MESSAGES("org/semux/cli/messages");
24 |
25 | private final String bundleName;
26 |
27 | ResourceBundles(String bundleName) {
28 | this.bundleName = bundleName;
29 | }
30 |
31 | public String getBundleName() {
32 | return bundleName;
33 | }
34 |
35 | public static ResourceBundle getDefaultBundle(ResourceBundles bundleName) {
36 | ResourceBundle defaultBundle = ResourceBundle.getBundle(bundleName.getBundleName(), Locale.getDefault());
37 | return defaultBundle == null ? ResourceBundle.getBundle(bundleName.getBundleName(), Locale.ENGLISH)
38 | : defaultBundle;
39 | }
40 |
41 | @Override
42 | public String toString() {
43 | return bundleName;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/Capability.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net;
8 |
9 | import org.semux.net.msg.ReasonCode;
10 |
11 | /**
12 | * This enum represents the available capabilities in current version of Semux
13 | * wallet. One peer should be disconnected by
14 | * ${@link ReasonCode#BAD_NETWORK_VERSION} if the peer doesn't support the
15 | * required set of capabilities.
16 | */
17 | public enum Capability {
18 |
19 | // NOTE: legacy issues
20 | // 1) Different names have been used historically;
21 | // 2) Old handshake messages first converts String to Capability, and then to
22 | // Capability set;
23 | // 3) Unknown capability will lead to INVALID_HANDSHAKE;
24 | // 4) Unsorted capability set will lead to INVALID_HANDSHAKE.
25 |
26 | /**
27 | * Mandatory for all network.
28 | */
29 | SEMUX,
30 |
31 | /**
32 | * This client supports the FAST_SYNC protocol.
33 | */
34 | FAST_SYNC,
35 |
36 | /**
37 | * This client supports the LIGHT protocol.
38 | */
39 | LIGHT;
40 |
41 | public static Capability of(String name) {
42 | try {
43 | return valueOf(name);
44 | } catch (IllegalArgumentException | NullPointerException ex) {
45 | return null;
46 | }
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/filter/exception/IpFilterJsonParseException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.filter.exception;
8 |
9 | public class IpFilterJsonParseException extends IllegalArgumentException {
10 |
11 | private static final long serialVersionUID = 1L;
12 |
13 | public IpFilterJsonParseException() {
14 | }
15 |
16 | public IpFilterJsonParseException(String s) {
17 | super(s);
18 | }
19 |
20 | public IpFilterJsonParseException(String message, Throwable cause) {
21 | super(message, cause);
22 | }
23 |
24 | public IpFilterJsonParseException(Throwable cause) {
25 | super(cause);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/MessageException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg;
8 |
9 | import java.io.IOException;
10 |
11 | public class MessageException extends IOException {
12 | private static final long serialVersionUID = 1L;
13 |
14 | public MessageException() {
15 | }
16 |
17 | public MessageException(String s) {
18 | super(s);
19 | }
20 |
21 | public MessageException(String s, Throwable throwable) {
22 | super(s, throwable);
23 | }
24 |
25 | public MessageException(Throwable throwable) {
26 | super(throwable);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/consensus/BlockHeaderMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import org.semux.core.BlockHeader;
10 | import org.semux.net.msg.Message;
11 | import org.semux.net.msg.MessageCode;
12 |
13 | public class BlockHeaderMessage extends Message {
14 |
15 | private final BlockHeader header;
16 |
17 | public BlockHeaderMessage(BlockHeader header) {
18 | super(MessageCode.BLOCK_HEADER, null);
19 |
20 | this.header = header;
21 |
22 | this.body = header.toBytes();
23 | }
24 |
25 | public BlockHeaderMessage(byte[] body) {
26 | super(MessageCode.BLOCK_HEADER, null);
27 |
28 | this.header = BlockHeader.fromBytes(body);
29 |
30 | this.body = body;
31 | }
32 |
33 | public BlockHeader getHeader() {
34 | return header;
35 | }
36 |
37 | @Override
38 | public String toString() {
39 | return "BlockHeaderMessage [header=" + header + "]";
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/consensus/BlockMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import org.semux.core.Block;
10 | import org.semux.net.msg.Message;
11 | import org.semux.net.msg.MessageCode;
12 |
13 | public class BlockMessage extends Message {
14 |
15 | private final Block block;
16 |
17 | public BlockMessage(Block block) {
18 | super(MessageCode.BLOCK, null);
19 |
20 | this.block = block;
21 |
22 | this.body = block.toBytes();
23 | }
24 |
25 | public BlockMessage(byte[] body) {
26 | super(MessageCode.BLOCK, null);
27 |
28 | this.block = Block.fromBytes(body);
29 |
30 | this.body = body;
31 | }
32 |
33 | public Block getBlock() {
34 | return block;
35 | }
36 |
37 | @Override
38 | public String toString() {
39 | return "BlockMessage [block=" + block + "]";
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/consensus/GetBlockHeaderMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import org.semux.net.msg.Message;
10 | import org.semux.net.msg.MessageCode;
11 | import org.semux.util.SimpleDecoder;
12 | import org.semux.util.SimpleEncoder;
13 |
14 | public class GetBlockHeaderMessage extends Message {
15 |
16 | private final long number;
17 |
18 | public GetBlockHeaderMessage(long number) {
19 | super(MessageCode.GET_BLOCK_HEADER, BlockHeaderMessage.class);
20 |
21 | this.number = number;
22 |
23 | SimpleEncoder enc = new SimpleEncoder();
24 | enc.writeLong(number);
25 | this.body = enc.toBytes();
26 | }
27 |
28 | public GetBlockHeaderMessage(byte[] body) {
29 | super(MessageCode.GET_BLOCK_HEADER, BlockHeaderMessage.class);
30 |
31 | SimpleDecoder dec = new SimpleDecoder(body);
32 | this.number = dec.readLong();
33 |
34 | this.body = body;
35 | }
36 |
37 | public long getNumber() {
38 | return number;
39 | }
40 |
41 | @Override
42 | public String toString() {
43 | return "GetBlockHeaderMessage [number=" + number + "]";
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/consensus/GetBlockMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import org.semux.net.msg.Message;
10 | import org.semux.net.msg.MessageCode;
11 | import org.semux.util.SimpleDecoder;
12 | import org.semux.util.SimpleEncoder;
13 |
14 | public class GetBlockMessage extends Message {
15 |
16 | private final long number;
17 |
18 | public GetBlockMessage(long number) {
19 | super(MessageCode.GET_BLOCK, BlockMessage.class);
20 | this.number = number;
21 |
22 | SimpleEncoder enc = new SimpleEncoder();
23 | enc.writeLong(number);
24 | this.body = enc.toBytes();
25 | }
26 |
27 | public GetBlockMessage(byte[] body) {
28 | super(MessageCode.GET_BLOCK, BlockMessage.class);
29 |
30 | SimpleDecoder dec = new SimpleDecoder(body);
31 | this.number = dec.readLong();
32 |
33 | this.body = body;
34 | }
35 |
36 | public long getNumber() {
37 | return number;
38 | }
39 |
40 | @Override
41 | public String toString() {
42 | return "GetBlockMessage [number=" + number + "]";
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/consensus/GetBlockPartsMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import org.semux.net.msg.Message;
10 | import org.semux.net.msg.MessageCode;
11 | import org.semux.util.SimpleDecoder;
12 | import org.semux.util.SimpleEncoder;
13 |
14 | public class GetBlockPartsMessage extends Message {
15 |
16 | private final long number;
17 | private final int parts;
18 |
19 | public GetBlockPartsMessage(long number, int parts) {
20 | super(MessageCode.GET_BLOCK_PARTS, BlockPartsMessage.class);
21 |
22 | this.number = number;
23 | this.parts = parts;
24 |
25 | SimpleEncoder enc = new SimpleEncoder();
26 | enc.writeLong(number);
27 | enc.writeInt(parts);
28 | this.body = enc.toBytes();
29 | }
30 |
31 | public GetBlockPartsMessage(byte[] body) {
32 | super(MessageCode.GET_BLOCK_PARTS, BlockPartsMessage.class);
33 |
34 | SimpleDecoder dec = new SimpleDecoder(body);
35 | this.number = dec.readLong();
36 | this.parts = dec.readInt();
37 |
38 | this.body = body;
39 | }
40 |
41 | public long getNumber() {
42 | return number;
43 | }
44 |
45 | public int getParts() {
46 | return parts;
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "GetBlockPartsMessage [number=" + number + ", parts = " + parts + "]";
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/consensus/NewHeightMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import org.semux.net.msg.Message;
10 | import org.semux.net.msg.MessageCode;
11 | import org.semux.util.SimpleDecoder;
12 | import org.semux.util.SimpleEncoder;
13 |
14 | public class NewHeightMessage extends Message {
15 |
16 | private final long height;
17 |
18 | public NewHeightMessage(long height) {
19 | super(MessageCode.BFT_NEW_HEIGHT, null);
20 | this.height = height;
21 |
22 | SimpleEncoder enc = new SimpleEncoder();
23 | enc.writeLong(height);
24 | this.body = enc.toBytes();
25 | }
26 |
27 | public NewHeightMessage(byte[] body) {
28 | super(MessageCode.BFT_NEW_HEIGHT, null);
29 |
30 | SimpleDecoder dec = new SimpleDecoder(body);
31 | this.height = dec.readLong();
32 |
33 | this.body = body;
34 | }
35 |
36 | public long getHeight() {
37 | return height;
38 | }
39 |
40 | @Override
41 | public String toString() {
42 | return "BFTNewHeightMessage [height=" + height + "]";
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/consensus/NewViewMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import org.semux.consensus.Proof;
10 | import org.semux.net.msg.Message;
11 | import org.semux.net.msg.MessageCode;
12 |
13 | public class NewViewMessage extends Message {
14 |
15 | private final Proof proof;
16 |
17 | public NewViewMessage(Proof proof) {
18 | super(MessageCode.BFT_NEW_VIEW, null);
19 |
20 | this.proof = proof;
21 |
22 | // TODO: consider wrapping by simple codec
23 | this.body = proof.toBytes();
24 | }
25 |
26 | public NewViewMessage(byte[] body) {
27 | super(MessageCode.BFT_NEW_VIEW, null);
28 |
29 | this.proof = Proof.fromBytes(body);
30 |
31 | this.body = body;
32 | }
33 |
34 | public Proof getProof() {
35 | return proof;
36 | }
37 |
38 | public long getHeight() {
39 | return proof.getHeight();
40 | }
41 |
42 | public int getView() {
43 | return proof.getView();
44 | }
45 |
46 | @Override
47 | public String toString() {
48 | return "BFTNewViewMessage [proof=" + proof + "]";
49 | }
50 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/consensus/ProposalMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import org.semux.consensus.Proposal;
10 | import org.semux.net.msg.Message;
11 | import org.semux.net.msg.MessageCode;
12 |
13 | public class ProposalMessage extends Message {
14 |
15 | private final Proposal proposal;
16 |
17 | public ProposalMessage(Proposal proposal) {
18 | super(MessageCode.BFT_PROPOSAL, null);
19 |
20 | this.proposal = proposal;
21 |
22 | // TODO: consider wrapping by simple codec
23 | this.body = proposal.toBytes();
24 | }
25 |
26 | public ProposalMessage(byte[] body) {
27 | super(MessageCode.BFT_PROPOSAL, null);
28 |
29 | this.proposal = Proposal.fromBytes(body);
30 |
31 | this.body = body;
32 | }
33 |
34 | public Proposal getProposal() {
35 | return proposal;
36 | }
37 |
38 | @Override
39 | public String toString() {
40 | return "BFTProposalMessage: " + proposal;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/consensus/VoteMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import org.semux.consensus.Vote;
10 | import org.semux.net.msg.Message;
11 | import org.semux.net.msg.MessageCode;
12 |
13 | public class VoteMessage extends Message {
14 |
15 | private final Vote vote;
16 |
17 | public VoteMessage(Vote vote) {
18 | super(MessageCode.BFT_VOTE, null);
19 |
20 | this.vote = vote;
21 |
22 | // TODO: consider wrapping by simple codec
23 | this.body = vote.toBytes();
24 | }
25 |
26 | public VoteMessage(byte[] body) {
27 | super(MessageCode.BFT_VOTE, null);
28 |
29 | this.vote = Vote.fromBytes(body);
30 |
31 | this.body = body;
32 | }
33 |
34 | public Vote getVote() {
35 | return vote;
36 | }
37 |
38 | @Override
39 | public String toString() {
40 | return "BFTVoteMessage: " + vote;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/p2p/DisconnectMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p;
8 |
9 | import org.semux.net.msg.Message;
10 | import org.semux.net.msg.MessageCode;
11 | import org.semux.net.msg.ReasonCode;
12 | import org.semux.util.SimpleDecoder;
13 | import org.semux.util.SimpleEncoder;
14 |
15 | public class DisconnectMessage extends Message {
16 |
17 | private final ReasonCode reason;
18 |
19 | /**
20 | * Create a DISCONNECT message.
21 | *
22 | * @param reason
23 | */
24 | public DisconnectMessage(ReasonCode reason) {
25 | super(MessageCode.DISCONNECT, null);
26 |
27 | this.reason = reason;
28 |
29 | SimpleEncoder enc = new SimpleEncoder();
30 | enc.writeByte(reason.toByte());
31 | this.body = enc.toBytes();
32 | }
33 |
34 | /**
35 | * Parse a DISCONNECT message from byte array.
36 | *
37 | * @param body
38 | */
39 | public DisconnectMessage(byte[] body) {
40 | super(MessageCode.DISCONNECT, null);
41 |
42 | SimpleDecoder dec = new SimpleDecoder(body);
43 | this.reason = ReasonCode.of(dec.readByte());
44 |
45 | this.body = body;
46 | }
47 |
48 | public ReasonCode getReason() {
49 | return reason;
50 | }
51 |
52 | @Override
53 | public String toString() {
54 | return "DisconnectMessage [reason=" + reason + "]";
55 | }
56 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/p2p/GetNodesMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p;
8 |
9 | import org.semux.net.msg.Message;
10 | import org.semux.net.msg.MessageCode;
11 | import org.semux.util.SimpleEncoder;
12 |
13 | // NOTE: GetNodesMessage is encoded into a single empty frame.
14 |
15 | public class GetNodesMessage extends Message {
16 |
17 | /**
18 | * Create a GET_NODES message.
19 | *
20 | */
21 | public GetNodesMessage() {
22 | super(MessageCode.GET_NODES, NodesMessage.class);
23 |
24 | SimpleEncoder enc = new SimpleEncoder();
25 | this.body = enc.toBytes();
26 | }
27 |
28 | /**
29 | * Parse a GET_NODES message from byte array.
30 | *
31 | * @param body
32 | */
33 | public GetNodesMessage(byte[] body) {
34 | super(MessageCode.GET_NODES, NodesMessage.class);
35 |
36 | this.body = body;
37 | }
38 |
39 | @Override
40 | public String toString() {
41 | return "GetNodesMessage";
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/p2p/PingMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p;
8 |
9 | import org.semux.net.msg.Message;
10 | import org.semux.net.msg.MessageCode;
11 | import org.semux.util.SimpleDecoder;
12 | import org.semux.util.SimpleEncoder;
13 | import org.semux.util.TimeUtil;
14 |
15 | public class PingMessage extends Message {
16 |
17 | private final long timestamp;
18 |
19 | /**
20 | * Create a PING message.
21 | *
22 | */
23 | public PingMessage() {
24 | super(MessageCode.PING, PongMessage.class);
25 |
26 | this.timestamp = TimeUtil.currentTimeMillis();
27 |
28 | SimpleEncoder enc = new SimpleEncoder();
29 | enc.writeLong(timestamp);
30 | this.body = enc.toBytes();
31 | }
32 |
33 | /**
34 | * Parse a PING message from byte array.
35 | *
36 | * @param body
37 | */
38 | public PingMessage(byte[] body) {
39 | super(MessageCode.PING, PongMessage.class);
40 |
41 | SimpleDecoder dec = new SimpleDecoder(body);
42 | this.timestamp = dec.readLong();
43 |
44 | this.body = body;
45 | }
46 |
47 | @Override
48 | public String toString() {
49 | return "PingMessage [timestamp=" + timestamp + "]";
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/p2p/PongMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p;
8 |
9 | import org.semux.net.msg.Message;
10 | import org.semux.net.msg.MessageCode;
11 | import org.semux.util.SimpleDecoder;
12 | import org.semux.util.SimpleEncoder;
13 | import org.semux.util.TimeUtil;
14 |
15 | public class PongMessage extends Message {
16 |
17 | private final long timestamp;
18 |
19 | /**
20 | * Create a PONG message.
21 | */
22 | public PongMessage() {
23 | super(MessageCode.PONG, null);
24 |
25 | this.timestamp = TimeUtil.currentTimeMillis();
26 |
27 | SimpleEncoder enc = new SimpleEncoder();
28 | enc.writeLong(timestamp);
29 | this.body = enc.toBytes();
30 | }
31 |
32 | /**
33 | * Parse a PONG message from byte array.
34 | *
35 | * @param body
36 | */
37 | public PongMessage(byte[] body) {
38 | super(MessageCode.PONG, null);
39 |
40 | SimpleDecoder dec = new SimpleDecoder(body);
41 | this.timestamp = dec.readLong();
42 |
43 | this.body = body;
44 | }
45 |
46 | @Override
47 | public String toString() {
48 | return "PongMessage [timestamp=" + timestamp + "]";
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/p2p/TransactionMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p;
8 |
9 | import org.semux.core.Transaction;
10 | import org.semux.net.msg.Message;
11 | import org.semux.net.msg.MessageCode;
12 |
13 | public class TransactionMessage extends Message {
14 |
15 | private final Transaction transaction;
16 |
17 | /**
18 | * Create a TRANSACTION message.
19 | *
20 | */
21 | public TransactionMessage(Transaction transaction) {
22 | super(MessageCode.TRANSACTION, null);
23 |
24 | this.transaction = transaction;
25 |
26 | // TODO: consider wrapping by simple codec
27 | this.body = transaction.toBytes();
28 | }
29 |
30 | /**
31 | * Parse a TRANSACTION message from byte array.
32 | *
33 | * @param body
34 | */
35 | public TransactionMessage(byte[] body) {
36 | super(MessageCode.TRANSACTION, null);
37 |
38 | this.transaction = Transaction.fromBytes(body);
39 |
40 | this.body = body;
41 | }
42 |
43 | public Transaction getTransaction() {
44 | return transaction;
45 | }
46 |
47 | @Override
48 | public String toString() {
49 | return "TransactionMessage [tx=" + transaction + "]";
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/p2p/handshake/v1/HelloMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p.handshake.v1;
8 |
9 | import org.semux.Network;
10 | import org.semux.crypto.Key;
11 | import org.semux.net.msg.MessageCode;
12 |
13 | public class HelloMessage extends HandshakeMessage {
14 |
15 | /**
16 | * Create a HELLO message.
17 | */
18 | public HelloMessage(Network network, short networkVersion, String peerId, String ip, int port,
19 | String clientId, long latestBlockNumber, Key coinbase) {
20 | super(MessageCode.HELLO, WorldMessage.class,
21 | network, networkVersion, peerId, ip, port, clientId,
22 | latestBlockNumber, coinbase);
23 | }
24 |
25 | /**
26 | * Parse a HELLO message from byte array.
27 | *
28 | * @param body
29 | */
30 | public HelloMessage(byte[] body) {
31 | super(MessageCode.HELLO, WorldMessage.class, body);
32 | }
33 |
34 | @Override
35 | public String toString() {
36 | return "WorldMessage [peer=" + peer + "]";
37 | }
38 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/p2p/handshake/v1/WorldMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p.handshake.v1;
8 |
9 | import org.semux.Network;
10 | import org.semux.crypto.Key;
11 | import org.semux.net.msg.MessageCode;
12 |
13 | public class WorldMessage extends HandshakeMessage {
14 |
15 | /**
16 | * Create a WORLD message.
17 | */
18 | public WorldMessage(Network network, short networkVersion, String peerId, String ip, int port,
19 | String clientId, long latestBlockNumber, Key coinbase) {
20 | super(MessageCode.WORLD, null,
21 | network, networkVersion, peerId, ip, port, clientId, latestBlockNumber, coinbase);
22 | }
23 |
24 | /**
25 | * Parse a WORLD message from byte array.
26 | *
27 | * @param body
28 | */
29 | public WorldMessage(byte[] body) {
30 | super(MessageCode.WORLD, null, body);
31 | }
32 |
33 | @Override
34 | public String toString() {
35 | return "WorldMessage [peer=" + peer + "]";
36 | }
37 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/p2p/handshake/v2/HelloMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p.handshake.v2;
8 |
9 | import java.util.Arrays;
10 |
11 | import org.semux.Network;
12 | import org.semux.crypto.Hex;
13 | import org.semux.crypto.Key;
14 | import org.semux.net.msg.MessageCode;
15 |
16 | public class HelloMessage extends HandshakeMessage {
17 |
18 | public HelloMessage(Network network, short networkVersion, String peerId, int port,
19 | String clientId, String[] capabilities, long latestBlockNumber,
20 | byte[] secret, Key coinbase) {
21 | super(MessageCode.HANDSHAKE_HELLO, WorldMessage.class, network, networkVersion, peerId, port, clientId,
22 | capabilities, latestBlockNumber, secret, coinbase);
23 | }
24 |
25 | public HelloMessage(byte[] encoded) {
26 | super(MessageCode.HANDSHAKE_HELLO, WorldMessage.class, encoded);
27 | }
28 |
29 | @Override
30 | public String toString() {
31 | return "HelloMessage{" +
32 | "peer=" + network +
33 | ", networkVersion=" + networkVersion +
34 | ", peerId='" + peerId + '\'' +
35 | ", port=" + port +
36 | ", clientId='" + clientId + '\'' +
37 | ", capabilities=" + Arrays.toString(capabilities) +
38 | ", latestBlockNumber=" + latestBlockNumber +
39 | ", secret=" + Hex.encode(secret) +
40 | ", timestamp=" + timestamp +
41 | '}';
42 | }
43 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/net/msg/p2p/handshake/v2/WorldMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p.handshake.v2;
8 |
9 | import java.util.Arrays;
10 |
11 | import org.semux.Network;
12 | import org.semux.crypto.Hex;
13 | import org.semux.crypto.Key;
14 | import org.semux.net.msg.MessageCode;
15 |
16 | public class WorldMessage extends HandshakeMessage {
17 |
18 | public WorldMessage(Network network, short networkVersion, String peerId, int port,
19 | String clientId, String[] capabilities, long latestBlockNumber,
20 | byte[] secret, Key coinbase) {
21 | super(MessageCode.HANDSHAKE_WORLD, null, network, networkVersion, peerId, port, clientId,
22 | capabilities, latestBlockNumber, secret, coinbase);
23 | }
24 |
25 | public WorldMessage(byte[] encoded) {
26 | super(MessageCode.HANDSHAKE_WORLD, null, encoded);
27 | }
28 |
29 | @Override
30 | public String toString() {
31 | return "WorldMessage{" +
32 | "network=" + network +
33 | ", networkVersion=" + networkVersion +
34 | ", peerId='" + peerId + '\'' +
35 | ", port=" + port +
36 | ", clientId='" + clientId + '\'' +
37 | ", capabilities=" + Arrays.toString(capabilities) +
38 | ", latestBlockNumber=" + latestBlockNumber +
39 | ", secret=" + Hex.encode(secret) +
40 | ", timestamp=" + timestamp +
41 | '}';
42 | }
43 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/ArrayUtil.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import java.util.Random;
10 | import java.util.concurrent.ThreadLocalRandom;
11 |
12 | public class ArrayUtil {
13 |
14 | /**
15 | * Generate a random permutation of [0...n)
16 | *
17 | * @param n
18 | * @return
19 | */
20 | public static int[] permutation(int n) {
21 | int[] arr = new int[n];
22 | for (int i = 0; i < n; i++) {
23 | arr[i] = i;
24 | }
25 |
26 | shuffle(arr);
27 | return arr;
28 | }
29 |
30 | /**
31 | * Shuffle an integer array.
32 | *
33 | * @param arr
34 | */
35 | public static void shuffle(int[] arr) {
36 | Random r = ThreadLocalRandom.current();
37 |
38 | for (int i = arr.length - 1; i > 0; i--) {
39 | int index = r.nextInt(i + 1);
40 |
41 | int tmp = arr[index];
42 | arr[index] = arr[i];
43 | arr[i] = tmp;
44 | }
45 | }
46 |
47 | private ArrayUtil() {
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/BasicAuth.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import java.util.Base64;
10 |
11 | import org.apache.commons.lang3.tuple.Pair;
12 |
13 | /**
14 | * Basic authentication helper.
15 | *
16 | */
17 | public class BasicAuth {
18 |
19 | /**
20 | * Parses the username and password from the AUTHORIZATION header.
21 | *
22 | * @param auth
23 | * @return a pair of username and password if success, otherwise null
24 | */
25 | public static Pair parseAuth(String auth) {
26 | try {
27 | if (auth != null && auth.startsWith("Basic ")) {
28 | String str = Bytes.toString(Base64.getDecoder().decode(auth.substring(6)));
29 | int idx = str.indexOf(':');
30 | if (idx != -1) {
31 | return Pair.of(str.substring(0, idx), str.substring(idx + 1));
32 | }
33 | }
34 | } catch (IllegalArgumentException e) {
35 | // invalid base64 string
36 | }
37 |
38 | return null;
39 | }
40 |
41 | /**
42 | * Generates the AUTHORIZATION header.
43 | *
44 | * @param username
45 | * @param password
46 | * @return
47 | */
48 | public static String generateAuth(String username, String password) {
49 | return "Basic " + Base64.getEncoder().encodeToString(Bytes.of(username + ":" + password));
50 | }
51 |
52 | private BasicAuth() {
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/CircularFixedSizeList.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import java.util.LinkedList;
10 |
11 | public class CircularFixedSizeList {
12 |
13 | private int size;
14 | int index = 0;
15 | private LinkedList values = new LinkedList<>();
16 |
17 | public CircularFixedSizeList(int size) {
18 | super();
19 | this.size = size;
20 |
21 | }
22 |
23 | /**
24 | * push to current location in list
25 | *
26 | * @param object
27 | * @return
28 | */
29 | public void add(T object) {
30 | while (values.size() >= size) {
31 | values.removeLast();
32 | }
33 | values.add(index, object);
34 | }
35 |
36 | public T forward() {
37 | if (values.isEmpty()) {
38 | return null;
39 | }
40 | index = (index + 1) % values.size();
41 | return values.get(index);
42 | }
43 |
44 | public T back() {
45 | if (values.isEmpty()) {
46 | return null;
47 | }
48 | index = (index - 1) % values.size();
49 | if (index < 0) {
50 | index = values.size() - 1;
51 | }
52 | return values.get(index);
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/ClosableIterator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import java.util.Iterator;
10 |
11 | public interface ClosableIterator extends Iterator {
12 |
13 | /**
14 | * Closes the underlying resources for this iterator.
15 | */
16 | void close();
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/CommandParser.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 | import java.util.regex.Matcher;
12 | import java.util.regex.Pattern;
13 |
14 | public class CommandParser {
15 |
16 | public static List parseInput(String input) {
17 | List commandArguments = new ArrayList<>();
18 | Pattern ptrn = Pattern.compile("\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|\\S+");
19 | Matcher matcher = ptrn.matcher(input);
20 | while (matcher.find()) {
21 | commandArguments.add(sanitize(matcher.group(0)));
22 | }
23 | return commandArguments;
24 | }
25 |
26 | private static String sanitize(String value) {
27 | if (value.startsWith("\"") && value.endsWith("\"")) {
28 | value = value.substring(1, value.length() - 1);
29 | }
30 | return value.replace("\\\"", "\"");
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/MerkleUtil.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 | import org.semux.core.Transaction;
13 | import org.semux.core.TransactionResult;
14 | import org.semux.crypto.Hash;
15 |
16 | public class MerkleUtil {
17 |
18 | /**
19 | * Compute the Merkle root of transactions.
20 | *
21 | * @param txs
22 | * transactions
23 | * @return
24 | */
25 | public static byte[] computeTransactionsRoot(List txs) {
26 | List hashes = new ArrayList<>();
27 | for (Transaction tx : txs) {
28 | hashes.add(tx.getHash());
29 | }
30 | return new MerkleTree(hashes).getRootHash();
31 | }
32 |
33 | /**
34 | * Computes the Merkle root of results.
35 | *
36 | * @param results
37 | * transaction results
38 | * @return
39 | */
40 | public static byte[] computeResultsRoot(List results) {
41 | List hashes = new ArrayList<>();
42 | for (TransactionResult tx : results) {
43 | hashes.add(Hash.h256(tx.toBytesForMerkle()));
44 | }
45 | return new MerkleTree(hashes).getRootHash();
46 | }
47 |
48 | private MerkleUtil() {
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/NullPrintStream.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import java.io.ByteArrayOutputStream;
10 | import java.io.IOException;
11 | import java.io.OutputStream;
12 | import java.io.PrintStream;
13 | import java.io.UnsupportedEncodingException;
14 | import java.nio.charset.StandardCharsets;
15 |
16 | public class NullPrintStream extends PrintStream {
17 |
18 | public NullPrintStream() throws UnsupportedEncodingException {
19 | super(new NullByteArrayOutputStream(), false, StandardCharsets.UTF_8.name());
20 | }
21 |
22 | private static class NullByteArrayOutputStream extends ByteArrayOutputStream {
23 |
24 | @Override
25 | public void write(int b) {
26 | // do nothing
27 | }
28 |
29 | @Override
30 | public void write(byte[] b, int off, int len) {
31 | // do nothing
32 | }
33 |
34 | @Override
35 | public void writeTo(OutputStream out) throws IOException {
36 | // do nothing
37 | }
38 |
39 | }
40 |
41 | }
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/StringUtil.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | public class StringUtil {
10 |
11 | /**
12 | * Returns if the given string is null or empty.
13 | *
14 | * @param str
15 | * @return
16 | */
17 | public static boolean isNullOrEmpty(String str) {
18 | return str == null || str.isEmpty();
19 | }
20 |
21 | private StringUtil() {
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/exception/BytesException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util.exception;
8 |
9 | public class BytesException extends RuntimeException {
10 |
11 | private static final long serialVersionUID = 1L;
12 |
13 | public BytesException() {
14 | }
15 |
16 | public BytesException(String s) {
17 | super(s);
18 | }
19 |
20 | public BytesException(String s, Throwable throwable) {
21 | super(s, throwable);
22 | }
23 |
24 | public BytesException(Throwable throwable) {
25 | super(throwable);
26 | }
27 |
28 | public BytesException(String s, Throwable throwable, boolean b, boolean b1) {
29 | super(s, throwable, b, b1);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/exception/SimpleCodecException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util.exception;
8 |
9 | public class SimpleCodecException extends RuntimeException {
10 | private static final long serialVersionUID = 1L;
11 |
12 | public SimpleCodecException() {
13 | }
14 |
15 | public SimpleCodecException(String s) {
16 | super(s);
17 | }
18 |
19 | public SimpleCodecException(String s, Throwable throwable) {
20 | super(s, throwable);
21 | }
22 |
23 | public SimpleCodecException(Throwable throwable) {
24 | super(throwable);
25 | }
26 |
27 | public SimpleCodecException(String s, Throwable throwable, boolean b, boolean b1) {
28 | super(s, throwable, b, b1);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/util/exception/UnreachableException.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util.exception;
8 |
9 | public class UnreachableException extends RuntimeException {
10 |
11 | private static final long serialVersionUID = 1L;
12 |
13 | public UnreachableException() {
14 | super("Should never reach here");
15 | }
16 |
17 | public UnreachableException(String message, Throwable cause, boolean enableSuppression,
18 | boolean writableStackTrace) {
19 | super(message, cause, enableSuppression, writableStackTrace);
20 | }
21 |
22 | public UnreachableException(String message, Throwable cause) {
23 | super(message, cause);
24 | }
25 |
26 | public UnreachableException(String message) {
27 | super(message);
28 | }
29 |
30 | public UnreachableException(Throwable cause) {
31 | super(cause);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/vm/client/Conversion.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.vm.client;
8 |
9 | import java.math.BigInteger;
10 |
11 | import org.semux.core.Amount;
12 | import org.semux.core.Unit;
13 |
14 | /**
15 | * Conversion between ETH and SEM. The idea is to make 1 SEM = 1 ETH from a
16 | * smart contract viewpoint.
17 | */
18 | public class Conversion {
19 |
20 | private static final BigInteger TEN_POW_NINE = BigInteger.TEN.pow(9);
21 |
22 | public static Amount weiToAmount(BigInteger value) {
23 | BigInteger nanoSEM = value.divide(TEN_POW_NINE);
24 | return Amount.of(nanoSEM.longValue(), Unit.NANO_SEM);
25 | }
26 |
27 | public static BigInteger amountToWei(Amount value) {
28 | return value.toBigInteger().multiply(TEN_POW_NINE);
29 | }
30 |
31 | public static BigInteger amountToWei(long nanoSEM) {
32 | return BigInteger.valueOf(nanoSEM).multiply(TEN_POW_NINE);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/vm/client/SemuxBlock.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.vm.client;
8 |
9 | import java.math.BigInteger;
10 |
11 | import org.ethereum.vm.client.Block;
12 | import org.semux.core.BlockHeader;
13 |
14 | /**
15 | * Facade for BlockHeader -> Block
16 | */
17 | public class SemuxBlock implements Block {
18 |
19 | private final long blockGasLimit;
20 | private final BlockHeader blockHeader;
21 |
22 | public SemuxBlock(BlockHeader block, long blockGasLimit) {
23 | this.blockHeader = block;
24 | this.blockGasLimit = blockGasLimit;
25 | }
26 |
27 | @Override
28 | public long getGasLimit() {
29 | return blockGasLimit;
30 | }
31 |
32 | @Override
33 | public byte[] getParentHash() {
34 | return blockHeader.getParentHash();
35 | }
36 |
37 | @Override
38 | public byte[] getCoinbase() {
39 | return blockHeader.getCoinbase();
40 | }
41 |
42 | @Override
43 | public long getTimestamp() {
44 | return blockHeader.getTimestamp();
45 | }
46 |
47 | @Override
48 | public long getNumber() {
49 | return blockHeader.getNumber();
50 | }
51 |
52 | @Override
53 | public BigInteger getDifficulty() {
54 | return BigInteger.ONE;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/vm/client/SemuxBlockStore.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.vm.client;
8 |
9 | import org.ethereum.vm.client.BlockStore;
10 | import org.semux.core.Blockchain;
11 |
12 | /**
13 | * Facade class for Blockchain to Blockstore
14 | *
15 | * Eventually we'll want to make blockchain just implement blockstore
16 | */
17 | public class SemuxBlockStore implements BlockStore {
18 | private final Blockchain blockchain;
19 |
20 | public SemuxBlockStore(Blockchain blockchain) {
21 | this.blockchain = blockchain;
22 | }
23 |
24 | @Override
25 | public byte[] getBlockHashByNumber(long index) {
26 | return blockchain.getBlockHeader(index).getHash();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/vm/client/SemuxSpec.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.vm.client;
8 |
9 | import org.ethereum.vm.chainspec.ConstantinopleSpec;
10 | import org.ethereum.vm.chainspec.PrecompiledContracts;
11 |
12 | public class SemuxSpec extends ConstantinopleSpec {
13 |
14 | private static final PrecompiledContracts precompiledContracts = new SemuxPrecompiledContracts();
15 |
16 | @Override
17 | public PrecompiledContracts getPrecompiledContracts() {
18 | return precompiledContracts;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/org/semux/vm/client/SemuxTransaction.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.vm.client;
8 |
9 | import static org.semux.vm.client.Conversion.amountToWei;
10 |
11 | import java.math.BigInteger;
12 |
13 | import org.ethereum.vm.client.Transaction;
14 | import org.semux.core.TransactionType;
15 |
16 | /**
17 | * Facade for Transaction -> Transaction
18 | */
19 | public class SemuxTransaction implements Transaction {
20 |
21 | private final org.semux.core.Transaction transaction;
22 |
23 | public SemuxTransaction(org.semux.core.Transaction transaction) {
24 | this.transaction = transaction;
25 | }
26 |
27 | @Override
28 | public boolean isCreate() {
29 | return transaction.getType().equals(TransactionType.CREATE);
30 | }
31 |
32 | @Override
33 | public byte[] getFrom() {
34 | return transaction.getFrom();
35 | }
36 |
37 | @Override
38 | public byte[] getTo() {
39 | return transaction.getTo();
40 | }
41 |
42 | @Override
43 | public long getNonce() {
44 | return transaction.getNonce();
45 | }
46 |
47 | @Override
48 | public BigInteger getValue() {
49 | return amountToWei(transaction.getValue());
50 | }
51 |
52 | @Override
53 | public byte[] getData() {
54 | return transaction.getData();
55 | }
56 |
57 | @Override
58 | public long getGas() {
59 | return transaction.getGas();
60 | }
61 |
62 | @Override
63 | public BigInteger getGasPrice() {
64 | return amountToWei(transaction.getGasPrice());
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/native/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # CMakeList.txt : Top-level CMake project file, do global configuration
2 | # and include sub-projects here.
3 | #
4 | cmake_minimum_required (VERSION 3.5)
5 |
6 | project ("semux")
7 |
8 | # Include sub-projects.
9 | add_subdirectory ("crypto")
10 |
--------------------------------------------------------------------------------
/src/main/native/cmake/toolchain-Darwin-x86_64.cmake:
--------------------------------------------------------------------------------
1 | set(CMAKE_SYSTEM_NAME Darwin)
2 | set(CMAKE_SYSTEM_PROCESSOR x86_64)
3 | SET(CMAKE_C_COMPILER clang)
4 | SET(CMAKE_CXX_COMPILER clang++)
5 | SET(MACOSX_VERSION_MIN 10.8)
6 |
7 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=${MACOSX_VERSION_MIN} -arch ${CMAKE_SYSTEM_PROCESSOR} -march=core2")
8 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=${MACOSX_VERSION_MIN} -arch ${CMAKE_SYSTEM_PROCESSOR} -march=core2 -std=c++98 -stdlib=libc++")
9 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -mmacosx-version-min=${MACOSX_VERSION_MIN} -arch ${CMAKE_SYSTEM_PROCESSOR} -march=core2 -Wl,-dead_strip -Wl,-undefined,error -stdlib=libc++ -lc++")
--------------------------------------------------------------------------------
/src/main/native/crypto/org_semux_crypto_Native.h:
--------------------------------------------------------------------------------
1 | /* DO NOT EDIT THIS FILE - it is machine generated */
2 | #include
3 | /* Header for class org_semux_crypto_Native */
4 |
5 | #ifndef _Included_org_semux_crypto_Native
6 | #define _Included_org_semux_crypto_Native
7 | #ifdef __cplusplus
8 | extern "C" {
9 | #endif
10 | /*
11 | * Class: org_semux_crypto_Native
12 | * Method: h256
13 | * Signature: ([B)[B
14 | */
15 | JNIEXPORT jbyteArray JNICALL Java_org_semux_crypto_Native_h256
16 | (JNIEnv *, jclass, jbyteArray);
17 |
18 | /*
19 | * Class: org_semux_crypto_Native
20 | * Method: h160
21 | * Signature: ([B)[B
22 | */
23 | JNIEXPORT jbyteArray JNICALL Java_org_semux_crypto_Native_h160
24 | (JNIEnv *, jclass, jbyteArray);
25 |
26 | /*
27 | * Class: org_semux_crypto_Native
28 | * Method: sign
29 | * Signature: ([B[B)[B
30 | */
31 | JNIEXPORT jbyteArray JNICALL Java_org_semux_crypto_Native_sign
32 | (JNIEnv *, jclass, jbyteArray, jbyteArray);
33 |
34 | /*
35 | * Class: org_semux_crypto_Native
36 | * Method: verify
37 | * Signature: ([B[B[B)Z
38 | */
39 | JNIEXPORT jboolean JNICALL Java_org_semux_crypto_Native_verify
40 | (JNIEnv *, jclass, jbyteArray, jbyteArray, jbyteArray);
41 |
42 | /*
43 | * Class: org_semux_crypto_Native
44 | * Method: verifyBatch
45 | * Signature: ([[B[[B[[B)Z
46 | */
47 | JNIEXPORT jboolean JNICALL Java_org_semux_crypto_Native_verifyBatch
48 | (JNIEnv *, jclass, jobjectArray, jobjectArray, jobjectArray);
49 |
50 | #ifdef __cplusplus
51 | }
52 | #endif
53 | #endif
54 |
--------------------------------------------------------------------------------
/src/main/native/crypto/ripemd160.h:
--------------------------------------------------------------------------------
1 | #ifndef RIPEMD160_H_
2 | #define RIPEMD160_H_
3 |
4 | #include
5 |
6 | #ifdef __cplusplus
7 | extern "C"
8 | {
9 | #endif
10 |
11 | typedef struct {
12 | uint64_t length;
13 | union {
14 | uint32_t w[16];
15 | uint8_t b[64];
16 | } buf;
17 | uint32_t h[5];
18 | uint8_t bufpos;
19 | } ripemd160_state;
20 |
21 | void ripemd160_init(ripemd160_state *md);
22 | void ripemd160_process(ripemd160_state *md, const unsigned char *in, unsigned long inlen);
23 | void ripemd160_done(ripemd160_state *md, unsigned char *out);
24 | void ripemd160(const void *in, unsigned long length, void *out);
25 |
26 | #ifdef __cplusplus
27 | }
28 | #endif
29 |
30 | #endif
--------------------------------------------------------------------------------
/src/main/resources/genesis/devnet.json:
--------------------------------------------------------------------------------
1 | {
2 | "number": 0,
3 | "coinbase": "0x0000000000000000000000000000000000000000",
4 | "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
5 | "timestamp": 1504742400000,
6 | "data": "semux",
7 | "premine": [
8 | {
9 | "address": "0x23a6049381fd2cfb0661d9de206613b83d53d7df",
10 | "amount": 1000000000000000,
11 | "note": "test"
12 | }
13 | ],
14 | "delegates": {
15 | "semux1": "0x23a6049381fd2cfb0661d9de206613b83d53d7df"
16 | },
17 | "config": {
18 |
19 | }
20 | }
--------------------------------------------------------------------------------
/src/main/resources/genesis/mainnet.json:
--------------------------------------------------------------------------------
1 | {
2 | "number": 0,
3 | "coinbase": "0x0000000000000000000000000000000000000000",
4 | "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
5 | "timestamp": 1516406400000,
6 | "data": "semux",
7 | "premine": [
8 | {
9 | "address": "0xe30c510f3efc6e2bf98ff8f725548e6ece568f89",
10 | "amount": 5000000000000000,
11 | "note": "founder"
12 | },
13 | {
14 | "address": "0x541365fe0818ea0d2d7ab7f7bc79f719f5f72227",
15 | "amount": 10000000000000000,
16 | "note": "development, marketing, promotion, bounties"
17 | },
18 | {
19 | "address": "0x1504263ee17446ea5f8b288e1c35d05749c0e47d",
20 | "amount": 10000000000000000,
21 | "note": "community distribution"
22 | }
23 | ],
24 | "delegates": {
25 | "semux1": "0x230fe26286ef1c588a29fc57e8fa54ee2409c57b",
26 | "semux2": "0xe11145b4e9e05ae06ec606af4a77ae1103661025",
27 | "semux3": "0xd3063743e9906ce8ea05158e4c83590d40c6a4c9",
28 | "semux4": "0xd45d3b25fd1d72e9da4dab7637814f138437f447"
29 | },
30 | "config": {
31 |
32 | }
33 | }
--------------------------------------------------------------------------------
/src/main/resources/genesis/testnet.json:
--------------------------------------------------------------------------------
1 | {
2 | "number": 0,
3 | "coinbase": "0x0000000000000000000000000000000000000000",
4 | "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
5 | "timestamp": 1504742400000,
6 | "data": "semux",
7 | "premine": [
8 | {
9 | "address": "0xc583b6ad1d1cccfc00ae9113db6408f022822b20",
10 | "amount": 1000000000000000,
11 | "note": "test"
12 | },
13 | {
14 | "address": "0xc5f32c2231ff2b60cdfa9c0d3a9c2e71774de472",
15 | "amount": 1000000000000000,
16 | "note": "test"
17 | },
18 | {
19 | "address": "0xfe81ddefe30d7ab28f2ce73bb37d3d42603c4ddf",
20 | "amount": 1000000000000000,
21 | "note": "test"
22 | },
23 | {
24 | "address": "0x837c1e6ceca4ccad20e59849cb7a9678394f7313",
25 | "amount": 1000000000000000,
26 | "note": "test"
27 | }
28 | ],
29 | "delegates": {
30 | "semux1": "0xc583b6ad1d1cccfc00ae9113db6408f022822b20",
31 | "semux2": "0xc5f32c2231ff2b60cdfa9c0d3a9c2e71774de472",
32 | "semux3": "0xfe81ddefe30d7ab28f2ce73bb37d3d42603c4ddf",
33 | "semux4": "0x837c1e6ceca4ccad20e59849cb7a9678394f7313"
34 | },
35 | "config": {
36 |
37 | }
38 | }
--------------------------------------------------------------------------------
/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/src/main/resources/logo.icns:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/logo.icns
--------------------------------------------------------------------------------
/src/main/resources/native/Darwin-x86_64/libsemuxcrypto.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/native/Darwin-x86_64/libsemuxcrypto.dylib
--------------------------------------------------------------------------------
/src/main/resources/native/Linux-aarch64/libsemuxcrypto.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/native/Linux-aarch64/libsemuxcrypto.so
--------------------------------------------------------------------------------
/src/main/resources/native/Linux-x86_64/libsemuxcrypto.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/native/Linux-x86_64/libsemuxcrypto.so
--------------------------------------------------------------------------------
/src/main/resources/native/Windows-x86_64/libsemuxcrypto.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/native/Windows-x86_64/libsemuxcrypto.dll
--------------------------------------------------------------------------------
/src/main/resources/org/semux/api/favicon-16x16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/api/favicon-16x16.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/api/favicon-32x32.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/api/favicon-32x32.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/api/mime.types:
--------------------------------------------------------------------------------
1 | type=text/javascript exts=js
2 | type=text/css exts=css
3 | type=text/html exts=html,htm
4 | type=application/json exts=json,map
5 | type=application/x-yaml exts=yml
6 | type=image/png exts=png
7 | type=image/jpeg exts=jpg
8 | type=image/gif exts=gif
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/banner.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/banner.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/contract.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/contract.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/cycle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/cycle.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/delegates.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/delegates.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/home.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/home.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/inbound.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/inbound.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/lock.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/lock.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/logo.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/logo.ico
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/logo.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/outbound.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/outbound.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/receive.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/receive.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/send.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/send.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/splash.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/splash.png
--------------------------------------------------------------------------------
/src/main/resources/org/semux/gui/transactions.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/transactions.png
--------------------------------------------------------------------------------
/src/test/java/org/semux/IntegrationTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux;
8 |
9 | public interface IntegrationTest {
10 | }
11 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/KernelTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux;
8 |
9 | import static org.awaitility.Awaitility.await;
10 |
11 | import org.junit.Rule;
12 | import org.junit.Test;
13 | import org.semux.Kernel.State;
14 | import org.semux.rules.KernelRule;
15 |
16 | public class KernelTest {
17 |
18 | @Rule
19 | public KernelRule kernelRule = new KernelRule(15160, 15170);
20 |
21 | @Test
22 | public void testStart() {
23 | Kernel kernel = kernelRule.getKernel();
24 |
25 | // start kernel
26 | kernel.start();
27 | await().until(() -> kernel.getNodeManager().isRunning()
28 | && kernel.getPendingManager().isRunning()
29 | && kernel.getApi().isRunning()
30 | && kernel.getP2p().isRunning()
31 | && kernel.getBftManager().isRunning()
32 | && !kernel.getSyncManager().isRunning());
33 |
34 | // stop kernel
35 | kernel.stop();
36 | await().until(() -> kernel.state == State.STOPPED
37 | && !kernel.getNodeManager().isRunning()
38 | && !kernel.getPendingManager().isRunning()
39 | && !kernel.getApi().isRunning()
40 | && !kernel.getP2p().isRunning()
41 | && !kernel.getBftManager().isRunning()
42 | && !kernel.getSyncManager().isRunning());
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/LauncherTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux;
8 |
9 | import static org.mockito.ArgumentMatchers.any;
10 | import static org.mockito.Mockito.mock;
11 | import static org.mockito.Mockito.when;
12 |
13 | import org.apache.commons.cli.Option;
14 | import org.apache.commons.cli.Options;
15 | import org.apache.commons.cli.ParseException;
16 | import org.junit.Rule;
17 | import org.junit.Test;
18 | import org.junit.contrib.java.lang.system.ExpectedSystemExit;
19 | import org.semux.cli.SemuxOption;
20 | import org.semux.message.CliMessages;
21 | import org.semux.util.SystemUtil;
22 |
23 | public class LauncherTest {
24 |
25 | @Rule
26 | public final ExpectedSystemExit exit = ExpectedSystemExit.none();
27 |
28 | @Test
29 | public void testInvalidNetworkOption() throws ParseException {
30 | Launcher launcher = mock(Launcher.class);
31 |
32 | // mock options
33 | Options options = new Options();
34 | Option networkOption = Option.builder()
35 | .longOpt(SemuxOption.NETWORK.toString()).desc(CliMessages.get("SpecifyNetwork"))
36 | .hasArg(true).numberOfArgs(1).optionalArg(false).argName("name").type(String.class)
37 | .build();
38 | options.addOption(networkOption);
39 |
40 | when(launcher.getOptions()).thenReturn(options);
41 | when(launcher.parseOptions(any())).thenCallRealMethod();
42 |
43 | exit.expectSystemExitWithStatus(SystemUtil.Code.INVALID_NETWORK_LABEL);
44 | launcher.parseOptions(new String[] { "--network", "unknown" });
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/api/SemuxApiServiceTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.api;
8 |
9 | import static org.junit.Assert.assertEquals;
10 |
11 | import org.junit.After;
12 | import org.junit.Before;
13 | import org.junit.Rule;
14 | import org.junit.Test;
15 | import org.semux.rules.KernelRule;
16 |
17 | public class SemuxApiServiceTest {
18 |
19 | @Rule
20 | public KernelRule kernelRule = new KernelRule(51610, 51710);
21 |
22 | SemuxApiMock apiMock;
23 |
24 | @Before
25 | public void setUp() {
26 | apiMock = new SemuxApiMock(kernelRule.getKernel());
27 | apiMock.start();
28 | }
29 |
30 | @After
31 | public void tearDown() {
32 | apiMock.stop();
33 | }
34 |
35 | @Test
36 | public void testGetApiBaseUrl() {
37 | assertEquals(String.format("http://127.0.0.1:51710/%s/", ApiVersion.DEFAULT.prefix),
38 | apiMock.getApi().getApiBaseUrl());
39 | }
40 |
41 | @Test
42 | public void testGetApiExplorerUrl() {
43 | assertEquals("http://127.0.0.1:51710/index.html",
44 | apiMock.getApi().getApiExplorerUrl());
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/api/v2/SemuxApiTestBase.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.api.v2;
8 |
9 | import javax.ws.rs.client.Client;
10 | import javax.ws.rs.client.ClientBuilder;
11 | import javax.ws.rs.client.WebTarget;
12 |
13 | import org.glassfish.jersey.client.ClientConfig;
14 | import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
15 | import org.glassfish.jersey.client.proxy.WebResourceFactory;
16 | import org.glassfish.jersey.jackson.JacksonFeature;
17 | import org.junit.Before;
18 | import org.semux.api.ApiVersion;
19 |
20 | public abstract class SemuxApiTestBase extends org.semux.api.SemuxApiTestBase {
21 |
22 | protected org.semux.api.v2.client.SemuxApi api;
23 |
24 | @Before
25 | public void setUp() {
26 | super.setUp();
27 |
28 | HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder()
29 | .nonPreemptive()
30 | .credentials(config.apiUsername(), config.apiPassword())
31 | .build();
32 | ClientConfig clientConfig = new ClientConfig();
33 | clientConfig.register(feature);
34 | clientConfig.register(JacksonFeature.class);
35 |
36 | Client client = ClientBuilder.newClient(clientConfig);
37 |
38 | WebTarget target = client.target("http://localhost:51710/" + ApiVersion.DEFAULT.prefix);
39 | api = WebResourceFactory.newResource(org.semux.api.v2.client.SemuxApi.class, target);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/bench/ApiPerformance.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.bench;
8 |
9 | import java.io.IOException;
10 |
11 | import org.junit.Rule;
12 | import org.junit.Test;
13 | import org.semux.api.SemuxApiMock;
14 | import org.semux.config.Config;
15 | import org.semux.rules.KernelRule;
16 | import org.semux.util.SimpleApiClient;
17 | import org.slf4j.Logger;
18 | import org.slf4j.LoggerFactory;
19 |
20 | /**
21 | * TODO: investigate, significant performance decrease noticed.
22 | */
23 | public class ApiPerformance {
24 | private static final Logger logger = LoggerFactory.getLogger(ApiPerformance.class);
25 |
26 | @Rule
27 | public KernelRule kernelRule = new KernelRule(51610, 51710);
28 |
29 | @Test
30 | public void testBasic() throws IOException {
31 | SemuxApiMock api = new SemuxApiMock(kernelRule.getKernel());
32 | api.start();
33 |
34 | try {
35 | int repeat = 1000;
36 |
37 | Config c = api.getKernel().getConfig();
38 | long t1 = System.nanoTime();
39 | for (int i = 0; i < repeat; i++) {
40 | SimpleApiClient a = new SimpleApiClient(c.apiListenIp(), c.apiListenPort(), c.apiUsername(),
41 | c.apiPassword());
42 | a.get("/info");
43 | }
44 | long t2 = System.nanoTime();
45 | logger.info("Perf_api_basic: " + (t2 - t1) / 1_000 / repeat + " μs/time");
46 | } finally {
47 | api.stop();
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/config/DevnetConfigTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.config;
8 |
9 | import static org.junit.Assert.assertEquals;
10 |
11 | import org.junit.Test;
12 | import org.semux.Network;
13 |
14 | public class DevnetConfigTest {
15 |
16 | @Test
17 | public void testNetworkId() {
18 | Config config = new DevnetConfig(Constants.DEFAULT_ROOT_DIR);
19 | assertEquals(Network.DEVNET, config.network());
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/config/TestnetConfigTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.config;
8 |
9 | import static org.junit.Assert.assertEquals;
10 |
11 | import org.junit.Test;
12 | import org.semux.Network;
13 |
14 | public class TestnetConfigTest {
15 |
16 | @Test
17 | public void testNetworkId() {
18 | Config config = new TestnetConfig(Constants.DEFAULT_ROOT_DIR);
19 | assertEquals(Network.TESTNET, config.network());
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/config/UnitTestnetConfig.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.config;
8 |
9 | import java.util.Collections;
10 | import java.util.Map;
11 |
12 | import org.semux.Network;
13 | import org.semux.core.Fork;
14 |
15 | /**
16 | * The unit tests were written with a specific configuration in mind, however
17 | * the devnet config is changeable for users needs and to aid in local
18 | * development
19 | *
20 | * So we introduce a new network configuration with no forks enabled and normal
21 | * block times.
22 | *
23 | */
24 | public class UnitTestnetConfig extends AbstractConfig {
25 |
26 | public UnitTestnetConfig(String dataDir) {
27 | super(dataDir, Network.DEVNET, Constants.DEVNET_VERSION);
28 |
29 | this.netMaxInboundConnectionsPerIp = Integer.MAX_VALUE;
30 |
31 | this.forkUniformDistributionEnabled = true;
32 | this.forkVirtualMachineEnabled = true;
33 | this.forkVotingPrecompiledUpgradeEnabled = true;
34 | }
35 |
36 | @Override
37 | public Map checkpoints() {
38 | return Collections.emptyMap();
39 | }
40 |
41 | @Override
42 | public Map manuallyActivatedForks() {
43 | return Collections.emptyMap();
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/crypto/AesTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.crypto;
8 |
9 | import static org.junit.Assert.assertArrayEquals;
10 |
11 | import org.junit.Test;
12 | import org.semux.util.Bytes;
13 |
14 | public class AesTest {
15 | private static byte[] raw = Bytes.of("test");
16 | private static byte[] key = Hex.decode("1122334455667788112233445566778811223344556677881122334455667788");
17 | private static byte[] iv = Hex.decode("11223344556677881122334455667788");
18 | private static byte[] encrypted = Hex.decode("182b93aa58d6291381660e5bad673dd4");
19 |
20 | @Test
21 | public void testEncrypt() throws CryptoException {
22 | byte[] bytes = Aes.encrypt(raw, key, iv);
23 |
24 | assertArrayEquals(encrypted, bytes);
25 | }
26 |
27 | @Test
28 | public void testDecrypt() throws CryptoException {
29 | byte[] bytes = Aes.decrypt(encrypted, key, iv);
30 |
31 | assertArrayEquals(raw, bytes);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/crypto/CryptoExceptionTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.crypto;
8 |
9 | import static org.junit.Assert.assertEquals;
10 |
11 | import org.junit.Test;
12 | import org.semux.util.Bytes;
13 |
14 | public class CryptoExceptionTest {
15 |
16 | @Test(expected = CryptoException.class)
17 | public void testCryptoException() throws CryptoException {
18 | Aes.decrypt(Bytes.EMPTY_BYTES, Bytes.EMPTY_BYTES, Bytes.EMPTY_BYTES);
19 | }
20 |
21 | @Test
22 | public void testConstructor() {
23 | String msg = "test";
24 | Throwable th = new Throwable();
25 | CryptoException e = new CryptoException(msg, th);
26 | assertEquals(msg, e.getMessage());
27 | assertEquals(th, e.getCause());
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/crypto/bip32/BaseVectorTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.crypto.bip32;
8 |
9 | import java.io.UnsupportedEncodingException;
10 |
11 | import org.semux.crypto.bip32.key.KeyVersion;
12 |
13 | public abstract class BaseVectorTest {
14 |
15 | public HdKeyPair masterNode;
16 | public HdKeyGenerator hdKeyGenerator = new HdKeyGenerator();
17 |
18 | public BaseVectorTest() throws UnsupportedEncodingException {
19 | masterNode = hdKeyGenerator.getMasterKeyPairFromSeed(getSeed(), KeyVersion.MAINNET, CoinType.BITCOIN);
20 | }
21 |
22 | protected abstract byte[] getSeed();
23 | }
24 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/crypto/bip32/BitUtilTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.crypto.bip32;
8 |
9 | import org.junit.Assert;
10 | import org.junit.Test;
11 | import org.semux.crypto.bip32.util.BitUtil;
12 |
13 | public class BitUtilTest {
14 |
15 | @Test
16 | public void testCheckBitFromLeft() {
17 | byte a = 0x01;
18 | // th 8th bit from the left is one
19 | Assert.assertTrue(BitUtil.checkBit(a, 8));
20 |
21 | a = (byte) 0x80;
22 | Assert.assertTrue(BitUtil.checkBit(a, 1));
23 | }
24 |
25 | @Test
26 | public void testSetBit() {
27 | byte a = 0;
28 | a = BitUtil.setBit(a, 1);
29 | Assert.assertEquals(a, (byte) 0x80);
30 | a = BitUtil.setBit(a, 5);
31 | Assert.assertEquals(a, (byte) 0x88);
32 | }
33 |
34 | @Test
35 | public void testUnsetBit() {
36 | byte a = (byte) 0x88;
37 | a = BitUtil.unsetBit(a, 1);
38 | Assert.assertEquals(a, (byte) 0x08);
39 | a = BitUtil.unsetBit(a, 5);
40 | Assert.assertEquals(a, (byte) 0x00);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/crypto/bip32/PublicKeyChainTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.crypto.bip32;
8 |
9 | import org.junit.Assert;
10 | import org.junit.Test;
11 | import org.semux.crypto.Hex;
12 | import org.semux.crypto.bip32.key.HdPublicKey;
13 | import org.semux.crypto.bip32.key.KeyVersion;
14 |
15 | public class PublicKeyChainTest {
16 |
17 | public static final byte[] SEED = Hex.decode("000102030405060708090a0b0c0d0e0f");
18 |
19 | HdKeyGenerator generator = new HdKeyGenerator();
20 |
21 | @Test
22 | public void testPubKey0() {
23 | HdKeyPair rootKeyPair = generator.getMasterKeyPairFromSeed(SEED, KeyVersion.MAINNET, CoinType.BITCOIN);
24 | HdKeyPair childKeyPair = generator.getChildKeyPair(rootKeyPair, 0, false);
25 | // test that the pub key chain generated from only public key matches the other
26 | HdPublicKey pubKey = generator.getChildPublicKey(rootKeyPair.getPublicKey(), 0, false, Scheme.BIP32);
27 | Assert.assertArrayEquals(childKeyPair.getPublicKey().getKey(), pubKey.getKey());
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/crypto/bip39/Vector.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.crypto.bip39;
8 |
9 | public class Vector {
10 |
11 | private byte[] entropy;
12 | private String mnemonic;
13 | private String seed;
14 | private String hdKey;
15 | private String passphrase;
16 |
17 | public Vector(byte[] entropy, String mnemonic, String seed, String hdKey, String passphrase) {
18 | this.entropy = entropy;
19 | this.mnemonic = mnemonic;
20 | this.seed = seed;
21 | this.hdKey = hdKey;
22 | this.passphrase = passphrase;
23 | }
24 |
25 | public byte[] getEntropy() {
26 | return entropy;
27 | }
28 |
29 | public String getMnemonic() {
30 | return mnemonic;
31 | }
32 |
33 | public String getSeed() {
34 | return seed;
35 | }
36 |
37 | public String getHdKey() {
38 | return hdKey;
39 | }
40 |
41 | public String getPassphrase() {
42 | return passphrase;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/crypto/bip44/Bip44Test.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.crypto.bip44;
8 |
9 | import org.junit.Test;
10 | import org.semux.crypto.Hex;
11 | import org.semux.crypto.bip32.CoinType;
12 | import org.semux.crypto.bip32.HdKeyPair;
13 | import org.semux.crypto.bip32.key.KeyVersion;
14 |
15 | public class Bip44Test {
16 | private Bip44 bip44 = new Bip44();
17 | private byte[] seed = Hex.decode("abcdef");
18 |
19 | @Test
20 | public void testBitcoin() {
21 | HdKeyPair key = bip44.getRootKeyPairFromSeed(seed, KeyVersion.MAINNET, CoinType.BITCOIN);
22 | bip44.getChildKeyPair(key, 0);
23 | }
24 |
25 | @Test
26 | public void testSemux() {
27 | HdKeyPair key = bip44.getRootKeyPairFromSeed(seed, KeyVersion.MAINNET, CoinType.SEMUX_SLIP10);
28 | bip44.getChildKeyPair(key, 0);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/crypto/cache/PublicKeyCacheTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.crypto.cache;
8 |
9 | import static org.junit.Assert.assertNotSame;
10 | import static org.junit.Assert.assertSame;
11 |
12 | import org.junit.Ignore;
13 | import org.junit.Test;
14 | import org.semux.crypto.Key;
15 | import org.slf4j.Logger;
16 | import org.slf4j.LoggerFactory;
17 |
18 | import net.i2p.crypto.eddsa.EdDSAPublicKey;
19 |
20 | public class PublicKeyCacheTest {
21 |
22 | private static final Logger logger = LoggerFactory.getLogger(PublicKeyCacheTest.class);
23 |
24 | @Test
25 | public void testCache() {
26 | Key key = new Key();
27 | byte[] b1 = key.getPublicKey().clone();
28 | byte[] b2 = key.getPublicKey().clone();
29 |
30 | EdDSAPublicKey p1 = PublicKeyCache.computeIfAbsent(b1);
31 | EdDSAPublicKey p2 = PublicKeyCache.computeIfAbsent(b2);
32 | assertNotSame(b1, b2);
33 | assertSame(p1, p2);
34 | }
35 |
36 | @Test
37 | @Ignore
38 | public void testJvmUtilization() {
39 | // RESULTS:
40 | // Soft value based cache is no long good for us, since we automatically
41 | // allocate 80% of free memory. The JVM will be very aggressive and eats a lot
42 | // of memory.
43 |
44 | for (int i = 0; i < 1_000_000; i++) {
45 | PublicKeyCache.computeIfAbsent(new Key().getPublicKey());
46 | if (i % 10_000 == 0) {
47 | logger.info(Runtime.getRuntime().totalMemory() / 1024 / 1024 + " MB");
48 | }
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/BaseTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui;
8 |
9 | import javax.swing.JFrame;
10 |
11 | public class BaseTestApplication extends JFrame {
12 |
13 | private static final long serialVersionUID = 1L;
14 |
15 | public BaseTestApplication() {
16 | super();
17 | setTitle(getClass().getCanonicalName());
18 | setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/MenuBarTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui;
8 |
9 | import java.awt.Dimension;
10 |
11 | import org.semux.KernelMock;
12 | import org.semux.gui.model.WalletModel;
13 |
14 | public class MenuBarTestApplication extends BaseTestApplication {
15 |
16 | private static final long serialVersionUID = 1L;
17 |
18 | SemuxGui gui;
19 |
20 | MenuBar menuBar;
21 |
22 | MenuBarTestApplication(WalletModel walletModel, KernelMock kernelMock) {
23 | super();
24 | gui = new SemuxGui(walletModel, kernelMock);
25 | menuBar = new MenuBar(gui, this);
26 | this.setJMenuBar(menuBar);
27 |
28 | this.setMinimumSize(new Dimension(600, 400));
29 | }
30 | }
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/SplashScreenTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui;
8 |
9 | import java.awt.Dimension;
10 |
11 | public class SplashScreenTestApplication extends BaseTestApplication {
12 |
13 | private static final long serialVersionUID = 7961392121592436000L;
14 |
15 | protected SplashScreen splashScreen;
16 |
17 | SplashScreenTestApplication() {
18 | super();
19 | this.setMinimumSize(new Dimension(960, 600));
20 | splashScreen = new SplashScreen();
21 | }
22 | }
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/StatusBarTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui;
8 |
9 | import java.awt.BorderLayout;
10 | import java.awt.Dimension;
11 |
12 | public class StatusBarTestApplication extends BaseTestApplication {
13 |
14 | private static final long serialVersionUID = 1L;
15 |
16 | protected StatusBar statusBar;
17 |
18 | StatusBarTestApplication() {
19 | super();
20 | this.setMinimumSize(new Dimension(960, 600));
21 | statusBar = new StatusBar(this);
22 | this.add(statusBar, BorderLayout.SOUTH);
23 | getContentPane().add(statusBar);
24 | }
25 | }
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/dialog/AddressBookDialogTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.dialog;
8 |
9 | import java.util.List;
10 |
11 | import org.semux.KernelMock;
12 | import org.semux.gui.AddressBookEntry;
13 | import org.semux.gui.BaseTestApplication;
14 | import org.semux.gui.SemuxGui;
15 | import org.semux.gui.model.WalletModel;
16 |
17 | public class AddressBookDialogTestApplication extends BaseTestApplication {
18 |
19 | private static final long serialVersionUID = 1L;
20 |
21 | SemuxGui gui;
22 |
23 | AddressBookDialog addressBookDialog;
24 |
25 | AddressBookDialogTestApplication(WalletModel walletModel, KernelMock kernelMock, List entries) {
26 | super();
27 | gui = new SemuxGui(walletModel, kernelMock);
28 |
29 | addressBookDialog = new AddressBookDialog(this, gui.getKernel().getWallet(), gui) {
30 | private static final long serialVersionUID = 1L;
31 |
32 | @Override
33 | protected List getAddressBookEntries() {
34 | return entries;
35 | }
36 | };
37 |
38 | addressBookDialog.setVisible(true);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/dialog/ConsoleDialogTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.dialog;
8 |
9 | import org.semux.KernelMock;
10 | import org.semux.gui.BaseTestApplication;
11 | import org.semux.gui.SemuxGui;
12 | import org.semux.gui.model.WalletModel;
13 |
14 | /**
15 | */
16 | public class ConsoleDialogTestApplication extends BaseTestApplication {
17 |
18 | private static final long serialVersionUID = 1L;
19 |
20 | SemuxGui gui;
21 |
22 | ConsoleDialog consoleDialog;
23 |
24 | ConsoleDialogTestApplication(WalletModel walletModel, KernelMock kernelMock) {
25 | super();
26 | gui = new SemuxGui(walletModel, kernelMock);
27 | consoleDialog = new ConsoleDialog(gui, this);
28 | consoleDialog.setVisible(true);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/dialog/DelegateDialogTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.dialog;
8 |
9 | import org.semux.KernelMock;
10 | import org.semux.gui.BaseTestApplication;
11 | import org.semux.gui.SemuxGui;
12 | import org.semux.gui.model.WalletModel;
13 |
14 | public class DelegateDialogTestApplication extends BaseTestApplication {
15 |
16 | private static final long serialVersionUID = 1L;
17 |
18 | SemuxGui gui;
19 |
20 | DelegateDialog delegateDialog;
21 |
22 | DelegateDialogTestApplication(WalletModel walletModel, KernelMock kernelMock) {
23 | super();
24 | gui = new SemuxGui(walletModel, kernelMock);
25 | delegateDialog = new DelegateDialog(gui, this, walletModel.getDelegates().get(0));
26 | delegateDialog.setVisible(true);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/dialog/TransactionDialogTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.dialog;
8 |
9 | import org.semux.KernelMock;
10 | import org.semux.core.Transaction;
11 | import org.semux.core.TransactionResult;
12 | import org.semux.gui.BaseTestApplication;
13 | import org.semux.gui.SemuxGui;
14 | import org.semux.gui.model.WalletModel;
15 |
16 | public class TransactionDialogTestApplication extends BaseTestApplication {
17 |
18 | private static final long serialVersionUID = 1L;
19 |
20 | SemuxGui gui;
21 |
22 | TransactionDialog transactionDialog;
23 |
24 | TransactionDialogTestApplication(WalletModel walletModel, KernelMock kernelMock,
25 | Transaction tx) {
26 | super();
27 | gui = new SemuxGui(walletModel, kernelMock);
28 | TransactionResult result = new TransactionResult(TransactionResult.Code.SUCCESS);
29 | transactionDialog = new TransactionDialog(this, tx, gui.getKernel());
30 | transactionDialog.setVisible(true);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/dialog/TransactionResultDialogTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.dialog;
8 |
9 | import org.semux.KernelMock;
10 | import org.semux.core.Transaction;
11 | import org.semux.core.TransactionResult;
12 | import org.semux.gui.BaseTestApplication;
13 | import org.semux.gui.SemuxGui;
14 | import org.semux.gui.model.WalletModel;
15 |
16 | public class TransactionResultDialogTestApplication extends BaseTestApplication {
17 |
18 | private static final long serialVersionUID = 1L;
19 |
20 | SemuxGui gui;
21 |
22 | TransactionResultDialog dialog;
23 |
24 | TransactionResultDialogTestApplication(WalletModel walletModel, KernelMock kernelMock,
25 | Transaction tx, TransactionResult result) {
26 | super();
27 | gui = new SemuxGui(walletModel, kernelMock);
28 | dialog = new TransactionResultDialog(this, tx, result);
29 | dialog.setVisible(true);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/panel/DelegatesPanelTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.panel;
8 |
9 | import org.semux.KernelMock;
10 | import org.semux.gui.BaseTestApplication;
11 | import org.semux.gui.SemuxGui;
12 | import org.semux.gui.model.WalletModel;
13 |
14 | public class DelegatesPanelTestApplication extends BaseTestApplication {
15 |
16 | private static final long serialVersionUID = 1L;
17 |
18 | SemuxGui gui;
19 |
20 | DelegatesPanel delegatesPanel;
21 |
22 | DelegatesPanelTestApplication(WalletModel walletModel, KernelMock kernelMock) {
23 | super();
24 | gui = new SemuxGui(walletModel, kernelMock);
25 | delegatesPanel = new DelegatesPanel(gui, this);
26 | getContentPane().add(delegatesPanel);
27 | }
28 | }
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/panel/HomePanelTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.panel;
8 |
9 | import org.semux.KernelMock;
10 | import org.semux.gui.BaseTestApplication;
11 | import org.semux.gui.SemuxGui;
12 | import org.semux.gui.model.WalletModel;
13 |
14 | public class HomePanelTestApplication extends BaseTestApplication {
15 |
16 | private static final long serialVersionUID = 1L;
17 |
18 | HomePanel homePanel;
19 |
20 | HomePanelTestApplication(WalletModel walletModel, KernelMock kernelMock) {
21 | super();
22 | SemuxGui gui = new SemuxGui(walletModel, kernelMock);
23 | homePanel = new HomePanel(gui);
24 | getContentPane().add(homePanel);
25 | }
26 | }
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/panel/ReceivePanelTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.panel;
8 |
9 | import org.semux.KernelMock;
10 | import org.semux.gui.BaseTestApplication;
11 | import org.semux.gui.SemuxGui;
12 | import org.semux.gui.model.WalletModel;
13 |
14 | public class ReceivePanelTestApplication extends BaseTestApplication {
15 |
16 | private static final long serialVersionUID = 1L;
17 |
18 | ReceivePanel receivePanel;
19 |
20 | ReceivePanelTestApplication(WalletModel walletModel, KernelMock kernelMock) {
21 | super();
22 | SemuxGui gui = new SemuxGui(walletModel, kernelMock);
23 | receivePanel = new ReceivePanel(gui);
24 | getContentPane().add(receivePanel);
25 | }
26 | }
--------------------------------------------------------------------------------
/src/test/java/org/semux/gui/panel/TransactionsPanelTestApplication.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.gui.panel;
8 |
9 | import org.semux.KernelMock;
10 | import org.semux.gui.BaseTestApplication;
11 | import org.semux.gui.SemuxGui;
12 | import org.semux.gui.model.WalletModel;
13 |
14 | public class TransactionsPanelTestApplication extends BaseTestApplication {
15 |
16 | private static final long serialVersionUID = 1L;
17 |
18 | TransactionsPanel transactionsPanel;
19 |
20 | TransactionsPanelTestApplication(WalletModel walletModel, KernelMock kernelMock) {
21 | super();
22 | SemuxGui gui = new SemuxGui(walletModel, kernelMock);
23 | transactionsPanel = new TransactionsPanel(gui, this);
24 | getContentPane().add(transactionsPanel);
25 | }
26 | }
--------------------------------------------------------------------------------
/src/test/java/org/semux/message/CLIMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.message;
8 |
9 | import static org.junit.Assert.assertNotNull;
10 |
11 | import java.util.MissingResourceException;
12 |
13 | import org.junit.Test;
14 |
15 | public class CLIMessageTest {
16 |
17 | @Test
18 | public void testExists() {
19 | assertNotNull(CliMessages.get("Address"));
20 | }
21 |
22 | @Test(expected = MissingResourceException.class)
23 | public void testNotExists() {
24 | assertNotNull(CliMessages.get("NotExist"));
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/message/GUIMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.message;
8 |
9 | import static org.junit.Assert.assertNotNull;
10 |
11 | import java.util.MissingResourceException;
12 |
13 | import org.junit.Test;
14 |
15 | public class GUIMessageTest {
16 |
17 | @Test
18 | public void testExists() {
19 | assertNotNull(GuiMessages.get("Address"));
20 | }
21 |
22 | @Test(expected = MissingResourceException.class)
23 | public void testNotExists() {
24 | assertNotNull(GuiMessages.get("NotExist"));
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/CapabilityTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net;
8 |
9 | import static org.junit.Assert.assertEquals;
10 | import static org.junit.Assert.assertFalse;
11 | import static org.junit.Assert.assertTrue;
12 |
13 | import org.junit.Test;
14 |
15 | public class CapabilityTest {
16 |
17 | @Test
18 | public void testIsSupported() {
19 | assertFalse(CapabilityTreeSet.emptyList().isSupported(Capability.SEMUX));
20 | assertFalse(CapabilityTreeSet.of("SEMUX").isSupported(Capability.FAST_SYNC));
21 | assertTrue(CapabilityTreeSet.of("SEMUX").isSupported(Capability.SEMUX));
22 | assertTrue(CapabilityTreeSet.of(Capability.SEMUX).isSupported(Capability.SEMUX));
23 | assertEquals(CapabilityTreeSet.of(Capability.SEMUX), CapabilityTreeSet.of(Capability.SEMUX));
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/FrameTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net;
8 |
9 | import static org.assertj.core.api.Assertions.assertThat;
10 |
11 | import org.junit.Test;
12 |
13 | import io.netty.buffer.ByteBuf;
14 | import io.netty.buffer.Unpooled;
15 |
16 | public class FrameTest {
17 |
18 | @Test
19 | public void testReadAndWrite() {
20 | short version = 0x1122;
21 | byte compressType = 0x33;
22 | byte packetType = 0x44;
23 | int packetId = 0x55667788;
24 | int packetSize = 0x99aabbcc;
25 | int bodySize = 0xddeeff00;
26 | Frame frame = new Frame(version, compressType, packetType, packetId, packetSize, bodySize, null);
27 |
28 | ByteBuf buf = Unpooled.copiedBuffer(new byte[Frame.HEADER_SIZE]);
29 |
30 | buf.writerIndex(0);
31 | frame.writeHeader(buf);
32 |
33 | buf.readerIndex(0);
34 | Frame frame2 = Frame.readHeader(buf);
35 |
36 | assertThat(frame2).isEqualToComparingFieldByFieldRecursively(frame);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/filter/SemuxIpFilterTestBase.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.filter;
8 |
9 | import java.io.File;
10 |
11 | abstract class SemuxIpFilterTestBase {
12 |
13 | /**
14 | * Get a testing ipfilter.json from resource bundle
15 | *
16 | * @param fileName
17 | * @return
18 | */
19 | protected static File getFile(String fileName) {
20 | return new File(SemuxIpFilterLoaderTest.class.getResource("/ipfilter/" + fileName).getFile());
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/MessageCodeTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg;
8 |
9 | import static org.junit.Assert.assertEquals;
10 | import static org.junit.Assert.assertNull;
11 |
12 | import org.junit.Test;
13 |
14 | public class MessageCodeTest {
15 |
16 | @Test
17 | public void testConverting() {
18 | for (MessageCode code : MessageCode.values()) {
19 | assertEquals(code, MessageCode.of(code.getCode()));
20 | assertEquals(code, MessageCode.of(code.toByte()));
21 | }
22 | }
23 |
24 | @Test
25 | public void testNegativeByte() {
26 | byte b = (byte) 0xff;
27 | assertNull(MessageCode.of(b));
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/MessageFactoryTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg;
8 |
9 | import static org.junit.Assert.assertNull;
10 |
11 | import org.junit.Test;
12 |
13 | public class MessageFactoryTest {
14 |
15 | @Test
16 | public void testNonExist() throws MessageException {
17 | MessageFactory factory = new MessageFactory();
18 | assertNull(factory.create((byte) 0xff, new byte[1]));
19 | }
20 |
21 | @Test(expected = MessageException.class)
22 | public void testWrongCodec() throws MessageException {
23 | MessageFactory factory = new MessageFactory();
24 | factory.create((byte) 0x01, new byte[1]);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/ReasonCodeTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg;
8 |
9 | import static org.junit.Assert.assertEquals;
10 | import static org.junit.Assert.assertNull;
11 |
12 | import org.junit.Test;
13 |
14 | public class ReasonCodeTest {
15 |
16 | @Test
17 | public void testConverting() {
18 | for (ReasonCode code : ReasonCode.values()) {
19 | assertEquals(code, ReasonCode.of(code.getCode()));
20 | assertEquals(code, ReasonCode.of(code.toByte()));
21 | }
22 | }
23 |
24 | @Test
25 | public void testNegativeByte() {
26 | byte b = (byte) 0xff;
27 | assertNull(MessageCode.of(b));
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/consensus/GetBlockHeaderMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import static org.assertj.core.api.Assertions.assertThat;
10 |
11 | import org.junit.Test;
12 | import org.semux.net.msg.MessageCode;
13 |
14 | public class GetBlockHeaderMessageTest {
15 |
16 | @Test
17 | public void testSerialization() {
18 | long number = 1;
19 |
20 | GetBlockHeaderMessage m = new GetBlockHeaderMessage(number);
21 | assertThat(m.getCode()).isEqualTo(MessageCode.GET_BLOCK_HEADER);
22 | assertThat(m.getResponseMessageClass()).isEqualTo(BlockHeaderMessage.class);
23 |
24 | GetBlockHeaderMessage m2 = new GetBlockHeaderMessage(m.getBody());
25 | assertThat(m2.getCode()).isEqualTo(MessageCode.GET_BLOCK_HEADER);
26 | assertThat(m2.getResponseMessageClass()).isEqualTo(BlockHeaderMessage.class);
27 | assertThat(m2.getNumber()).isEqualTo(number);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/consensus/NewHeightMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import static org.junit.Assert.assertEquals;
10 |
11 | import org.junit.Test;
12 |
13 | public class NewHeightMessageTest {
14 | @Test
15 | public void testSerialization() {
16 | int height = 1;
17 |
18 | NewHeightMessage msg = new NewHeightMessage(height);
19 | NewHeightMessage msg2 = new NewHeightMessage(msg.getBody());
20 | assertEquals(height, msg2.getHeight());
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/consensus/NewViewMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import static org.junit.Assert.assertEquals;
10 | import static org.junit.Assert.assertTrue;
11 |
12 | import java.util.Collections;
13 |
14 | import org.junit.Test;
15 | import org.semux.consensus.Proof;
16 |
17 | public class NewViewMessageTest {
18 | @Test
19 | public void testSerialization() {
20 | int height = 1;
21 | int view = 1;
22 | Proof proof = new Proof(height, view, Collections.emptyList());
23 |
24 | NewViewMessage msg = new NewViewMessage(proof);
25 | NewViewMessage msg2 = new NewViewMessage(msg.getBody());
26 | assertEquals(height, msg2.getHeight());
27 | assertEquals(view, msg2.getView());
28 | assertTrue(msg2.getProof().getVotes().isEmpty());
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/consensus/VoteMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.consensus;
8 |
9 | import static org.junit.Assert.assertArrayEquals;
10 | import static org.junit.Assert.assertEquals;
11 | import static org.junit.Assert.assertTrue;
12 |
13 | import org.junit.Test;
14 | import org.semux.consensus.Vote;
15 | import org.semux.consensus.VoteType;
16 | import org.semux.crypto.Key;
17 |
18 | public class VoteMessageTest {
19 | @Test
20 | public void testSerialization() {
21 | Key key = new Key();
22 | VoteType type = VoteType.COMMIT;
23 | long height = 1;
24 | int view = 2;
25 | Vote vote = Vote.newReject(type, height, view);
26 | vote.sign(key);
27 | assertTrue(vote.validate());
28 |
29 | VoteMessage msg = new VoteMessage(vote);
30 | VoteMessage msg2 = new VoteMessage(msg.getBody());
31 | Vote vote2 = msg2.getVote();
32 | assertEquals(type, vote2.getType());
33 | assertEquals(height, vote2.getHeight());
34 | assertEquals(view, vote2.getView());
35 | assertTrue(vote2.validate());
36 |
37 | assertArrayEquals(key.toAddress(), vote2.getSignature().getAddress());
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/p2p/NodesMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p;
8 |
9 | import static org.junit.Assert.assertArrayEquals;
10 | import static org.junit.Assert.assertFalse;
11 | import static org.semux.net.msg.p2p.NodesMessage.MAX_NODES;
12 |
13 | import java.net.InetSocketAddress;
14 | import java.util.ArrayList;
15 | import java.util.List;
16 |
17 | import org.junit.Test;
18 | import org.semux.net.NodeManager.Node;
19 |
20 | public class NodesMessageTest {
21 |
22 | @Test
23 | public void testCodec() {
24 | List nodes = new ArrayList<>();
25 | nodes.add(new Node(new InetSocketAddress("127.0.0.1", 5160)));
26 | nodes.add(new Node(new InetSocketAddress("127.0.0.1", 5161)));
27 | NodesMessage nodesMessage = new NodesMessage(new NodesMessage(nodes).getBody());
28 | assertArrayEquals(nodes.toArray(), nodesMessage.getNodes().toArray());
29 | }
30 |
31 | @Test
32 | public void testOverflow() {
33 | List list = new ArrayList<>();
34 | for (int i = 0; i < MAX_NODES + 1; i++) {
35 | list.add(new Node("127.0.0.1", 1234));
36 | }
37 | NodesMessage maliciousMessage = new NodesMessage(list);
38 |
39 | assertFalse(maliciousMessage.validate());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/p2p/TransactionMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p;
8 |
9 | import static org.hamcrest.Matchers.equalTo;
10 | import static org.junit.Assert.assertThat;
11 |
12 | import org.junit.Test;
13 | import org.semux.Network;
14 | import org.semux.core.Amount;
15 | import org.semux.core.Transaction;
16 | import org.semux.core.TransactionType;
17 | import org.semux.crypto.Key;
18 | import org.semux.util.Bytes;
19 | import org.semux.util.TimeUtil;
20 |
21 | public class TransactionMessageTest {
22 | @Test
23 | public void testSerialization() {
24 | Network network = Network.DEVNET;
25 | TransactionType type = TransactionType.TRANSFER;
26 | byte[] to = Bytes.random(20);
27 | Amount value = Amount.of(2);
28 | Amount fee = Amount.of(50_000_000L);
29 | long nonce = 1;
30 | long timestamp = TimeUtil.currentTimeMillis();
31 | byte[] data = Bytes.of("data");
32 |
33 | Transaction tx = new Transaction(network, type, to, value, fee, nonce, timestamp, data);
34 | tx.sign(new Key());
35 |
36 | TransactionMessage msg = new TransactionMessage(tx);
37 | TransactionMessage msg2 = new TransactionMessage(msg.getBody());
38 | assertThat(msg2.getTransaction().getHash(), equalTo(tx.getHash()));
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/p2p/handshake/v1/HelloMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p.handshake.v1;
8 |
9 | import static org.junit.Assert.assertEquals;
10 | import static org.junit.Assert.assertTrue;
11 |
12 | import org.junit.Test;
13 | import org.semux.config.Config;
14 | import org.semux.config.Constants;
15 | import org.semux.config.UnitTestnetConfig;
16 | import org.semux.crypto.Key;
17 |
18 | public class HelloMessageTest {
19 |
20 | @Test
21 | public void testCodec() {
22 | Config config = new UnitTestnetConfig(Constants.DEFAULT_ROOT_DIR);
23 |
24 | Key key = new Key();
25 | String peerId = key.toAddressString();
26 | HelloMessage msg = new HelloMessage(config.network(), config.networkVersion(), peerId, "127.0.0.1", 5161,
27 | config.getClientId(), 2, key);
28 | assertTrue(msg.validate(config));
29 | assertEquals(key.toAddressString(), msg.getPeer().getPeerId());
30 |
31 | msg = new HelloMessage(msg.getBody());
32 | assertTrue(msg.validate(config));
33 | assertEquals(key.toAddressString(), msg.getPeer().getPeerId());
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/p2p/handshake/v1/WorldMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p.handshake.v1;
8 |
9 | import static org.junit.Assert.assertEquals;
10 | import static org.junit.Assert.assertTrue;
11 |
12 | import org.junit.Test;
13 | import org.semux.config.Config;
14 | import org.semux.config.Constants;
15 | import org.semux.config.UnitTestnetConfig;
16 | import org.semux.crypto.Key;
17 |
18 | public class WorldMessageTest {
19 |
20 | @Test
21 | public void testCodec() {
22 | Config config = new UnitTestnetConfig(Constants.DEFAULT_ROOT_DIR);
23 |
24 | Key key = new Key();
25 | String peerId = key.toAddressString();
26 | WorldMessage msg = new WorldMessage(config.network(), config.networkVersion(), peerId, "127.0.0.1", 5161,
27 | config.getClientId(), 2, key);
28 | assertTrue(msg.validate(config));
29 | assertEquals(key.toAddressString(), msg.getPeer().getPeerId());
30 |
31 | msg = new WorldMessage(msg.getBody());
32 | assertTrue(msg.validate(config));
33 | assertEquals(key.toAddressString(), msg.getPeer().getPeerId());
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/net/msg/p2p/handshake/v2/InitMessageTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.net.msg.p2p.handshake.v2;
8 |
9 | import static org.junit.Assert.assertArrayEquals;
10 | import static org.junit.Assert.assertEquals;
11 |
12 | import org.junit.Test;
13 | import org.semux.util.Bytes;
14 | import org.semux.util.TimeUtil;
15 |
16 | public class InitMessageTest {
17 |
18 | @Test
19 | public void testCodec() {
20 | byte[] secret = Bytes.random(32);
21 | long timestamp = TimeUtil.currentTimeMillis();
22 |
23 | InitMessage msg = new InitMessage(secret, timestamp);
24 | assertArrayEquals(secret, msg.getSecret());
25 | assertEquals(timestamp, msg.getTimestamp());
26 |
27 | msg = new InitMessage(msg.getBody());
28 | assertArrayEquals(secret, msg.getSecret());
29 | assertEquals(timestamp, msg.getTimestamp());
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/rules/TemporaryDatabaseRule.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.rules;
8 |
9 | import java.io.File;
10 | import java.nio.file.Path;
11 | import java.util.EnumMap;
12 |
13 | import org.junit.rules.TemporaryFolder;
14 | import org.semux.config.Constants;
15 | import org.semux.db.Database;
16 | import org.semux.db.DatabaseFactory;
17 | import org.semux.db.DatabaseName;
18 | import org.semux.db.LeveldbDatabase;
19 |
20 | public class TemporaryDatabaseRule extends TemporaryFolder implements DatabaseFactory {
21 |
22 | private final EnumMap databases = new EnumMap<>(DatabaseName.class);
23 |
24 | @Override
25 | public void before() throws Throwable {
26 | create();
27 | }
28 |
29 | @Override
30 | public void after() {
31 | close();
32 | delete();
33 | }
34 |
35 | @Override
36 | public Database getDB(DatabaseName name) {
37 | return databases.computeIfAbsent(name, k -> {
38 | File file = new File(getRoot(), k.toString().toLowerCase());
39 | return new LeveldbDatabase(file);
40 | });
41 | }
42 |
43 | @Override
44 | public void close() {
45 | for (Database db : databases.values()) {
46 | db.close();
47 | }
48 | }
49 |
50 | @Override
51 | public Path getDataDir() {
52 | return super.getRoot().toPath();
53 | }
54 | }
--------------------------------------------------------------------------------
/src/test/java/org/semux/util/ArrayUtilTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import static org.junit.Assert.assertEquals;
10 | import static org.junit.Assert.assertFalse;
11 |
12 | import java.util.Arrays;
13 | import java.util.TreeSet;
14 |
15 | import org.junit.Test;
16 |
17 | public class ArrayUtilTest {
18 |
19 | @Test
20 | public void testPermutation() {
21 | int n = 100;
22 | int[] arr = ArrayUtil.permutation(n);
23 |
24 | TreeSet set = new TreeSet<>();
25 | for (int i : arr) {
26 | set.add(i);
27 | }
28 |
29 | assertEquals(0, set.first().intValue());
30 | assertEquals(n - 1, set.last().intValue());
31 | }
32 |
33 | @Test
34 | public void testShuffle() {
35 | int n = 100;
36 |
37 | int[] arr = new int[n];
38 | for (int i = 0; i < n; i++) {
39 | arr[i] = i;
40 | }
41 |
42 | int[] arr2 = arr.clone();
43 | ArrayUtil.shuffle(arr2);
44 | assertFalse(Arrays.equals(arr, arr2));
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/util/BasicAuthTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import static org.junit.Assert.assertEquals;
10 | import static org.junit.Assert.assertNull;
11 |
12 | import org.apache.commons.lang3.tuple.Pair;
13 | import org.junit.Test;
14 |
15 | public class BasicAuthTest {
16 |
17 | @Test
18 | public void testAuth() {
19 | String username = "name";
20 | String password = "password";
21 | String auth = BasicAuth.generateAuth(username, password);
22 |
23 | Pair p = BasicAuth.parseAuth(auth);
24 | assertEquals(username, p.getKey());
25 | assertEquals(password, p.getValue());
26 | }
27 |
28 | @Test
29 | public void testInvalid() {
30 | assertNull(BasicAuth.parseAuth("invalid_auth_string"));
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/util/ByteArrayTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import static org.hamcrest.Matchers.equalTo;
10 | import static org.junit.Assert.assertEquals;
11 | import static org.junit.Assert.assertFalse;
12 | import static org.junit.Assert.assertThat;
13 | import static org.junit.Assert.assertTrue;
14 |
15 | import java.io.IOException;
16 | import java.util.Arrays;
17 | import java.util.HashMap;
18 |
19 | import org.junit.Test;
20 | import org.semux.crypto.Hex;
21 |
22 | public class ByteArrayTest {
23 |
24 | @Test
25 | public void testInHashMap() {
26 | byte[] b1 = Bytes.random(20);
27 | byte[] b2 = Bytes.random(20);
28 | byte[] b3 = Arrays.copyOf(b1, b1.length);
29 |
30 | HashMap map = new HashMap<>();
31 | map.put(ByteArray.of(b1), true);
32 |
33 | assertFalse(map.containsKey(ByteArray.of(b2)));
34 | assertTrue(map.containsKey(ByteArray.of(b3)));
35 | }
36 |
37 | @Test
38 | public void testToString() {
39 | byte[] b = Bytes.random(20);
40 | assertEquals(Hex.encode(b), ByteArray.of(b).toString());
41 | }
42 |
43 | @Test
44 | public void testByteArrayKeyDeserializer() throws IOException {
45 | byte[] x = Bytes.random(3);
46 | ByteArray.ByteArrayKeyDeserializer d = new ByteArray.ByteArrayKeyDeserializer();
47 | Object y = d.deserializeKey(Hex.encode0x(x), null);
48 | assertTrue(y instanceof ByteArray);
49 | assertThat(ByteArray.of(x), equalTo(y));
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/util/CommandParserTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import java.util.List;
10 |
11 | import org.junit.Assert;
12 | import org.junit.Test;
13 |
14 | public class CommandParserTest {
15 |
16 | @Test
17 | public void testDelimiting() {
18 | String input = "foo and \"bar stuff\" \"stuff \\\" with quotes \\\" \" more \"\"";
19 | List parsed = CommandParser.parseInput(input);
20 |
21 | Assert.assertEquals(6, parsed.size());
22 | Assert.assertEquals("foo", parsed.get(0));
23 | Assert.assertEquals("and", parsed.get(1));
24 | Assert.assertEquals("bar stuff", parsed.get(2));
25 | Assert.assertEquals("stuff \" with quotes \" ", parsed.get(3));
26 | Assert.assertEquals("more", parsed.get(4));
27 | Assert.assertEquals("", parsed.get(5));
28 | }
29 |
30 | @Test
31 | public void testBasic() {
32 | String input = "getBlockByNumber 1";
33 | List parsed = CommandParser.parseInput(input);
34 | Assert.assertEquals(2, parsed.size());
35 | Assert.assertEquals("getBlockByNumber", parsed.get(0));
36 | Assert.assertEquals("1", parsed.get(1));
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/util/FileUtilTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import static org.junit.Assert.assertFalse;
10 |
11 | import java.io.File;
12 | import java.io.IOException;
13 |
14 | import org.junit.AfterClass;
15 | import org.junit.BeforeClass;
16 | import org.junit.Test;
17 |
18 | public class FileUtilTest {
19 |
20 | private static final File dir = new File("test");
21 |
22 | @BeforeClass
23 | public static void setUp() throws IOException {
24 | dir.mkdirs();
25 | new File(dir, "file").createNewFile();
26 | }
27 |
28 | @Test
29 | public void testRecursiveDelete() {
30 | FileUtil.recursiveDelete(dir);
31 | assertFalse(dir.exists());
32 | }
33 |
34 | @AfterClass
35 | public static void tearDown() {
36 | dir.delete();
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/util/SimpleEncoderTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import static org.hamcrest.Matchers.equalTo;
10 | import static org.junit.Assert.assertThat;
11 |
12 | import org.junit.Test;
13 |
14 | public class SimpleEncoderTest {
15 |
16 | @Test
17 | public void testToAppend() {
18 | byte[] append = Bytes.of("hello");
19 |
20 | SimpleEncoder enc = new SimpleEncoder(append);
21 | enc.writeString("s");
22 |
23 | assertThat(enc.toBytes(), equalTo(Bytes.merge(append, Bytes.of((byte) 1), Bytes.of("s"))));
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/util/StringUtilTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import static org.junit.Assert.assertFalse;
10 | import static org.junit.Assert.assertTrue;
11 |
12 | import org.junit.Test;
13 |
14 | public class StringUtilTest {
15 |
16 | @Test
17 | public void testIsNullOrEmpty() {
18 | assertTrue(StringUtil.isNullOrEmpty(null));
19 | assertTrue(StringUtil.isNullOrEmpty(""));
20 | assertFalse(StringUtil.isNullOrEmpty("abc"));
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/test/java/org/semux/util/TimeUtilTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-2020 The Semux Developers
3 | *
4 | * Distributed under the MIT software license, see the accompanying file
5 | * LICENSE or https://opensource.org/licenses/mit-license.php
6 | */
7 | package org.semux.util;
8 |
9 | import static org.junit.Assert.assertEquals;
10 |
11 | import java.time.Duration;
12 | import java.util.Arrays;
13 | import java.util.Collection;
14 |
15 | import org.junit.Assert;
16 | import org.junit.Test;
17 | import org.junit.runner.RunWith;
18 | import org.junit.runners.Parameterized;
19 |
20 | @RunWith(Parameterized.class)
21 | public class TimeUtilTest {
22 |
23 | @Parameterized.Parameters
24 | public static Collection