Skip to content

Commit

Permalink
add java-tron
Browse files Browse the repository at this point in the history
  • Loading branch information
sasaxie committed Dec 19, 2017
1 parent 49a6323 commit ad8d728
Show file tree
Hide file tree
Showing 140 changed files with 15,997 additions and 4 deletions.
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# IDEA
.idea
*iml
.DS_Store

# gradle
.gradle
build
gradle
gradlew
gradlew.bat


# log
logs

# lombok
out

# levelDB
tron-data
database*

# database
database

# doc
doc
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Copyright (C) [2007] [TRON Foundation], Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Expand Down Expand Up @@ -162,4 +162,4 @@ General Public License ever published by the Free Software Foundation.
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
Library.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
# java-tron
Java implementation of the Tron whitepaper
# What’s TRON?
TRON is a block chain-based decentralized smart protocol and an application development platform. It allows each user to freely publish, store and own contents and data, and in the decentralized autonomous form, decides an incentive mechanism and enables application developers and content creators through digital asset distribution, circulation and transaction, thus forming a decentralized content entertainment ecosystem.

TRON is a product of Web 4.0 and the decentralized internet of next generation.

# Quick Start

**Download and build**

```shell
> git clone https://github.com/tron-network/java-tron.git
> cd java-tron
> gradle build
```

**Import project to IDEA**

1. [File] -> [New] -> [Project from Existing Sources...]
2. Select java-tron/build.gradle
3. Dialog [Import Project from Gradle], confirm [Use auto-import] and [Use gradle wrapper task configuration] have been
selected,then select Gradle JVM(JDK 1.8)and click [OK]

# Testing

- Install Kafka, create two topics (block and transaction)
- Adjust constant **DEFAULT_BOOTSTRAP_SERVERS** in ConsumerProperty.java file and ProducerProperty.java file to your Kafka's host(like:192.168.1.199:9092)
- IDEA: [Edit Configurations...] -> [Program arguments]: **--type server**
- Run Tron (server)
- IDEA: [Edit Configurations...] -> [Program arguments]: **--type normal**
- Run Tron (client)
- Execute `help` command on the client

# License

java-tron is released under the [LGPL-V3 license](LICENSE).

110 changes: 110 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
group 'org.tron'
version '1.0.0'

apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'application'

sourceCompatibility = 1.8
mainClassName = 'org.tron.example.Tron'

repositories {
mavenCentral()
}

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
}
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'

compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.25'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

compile "org.projectlombok:lombok:1.16.18"

compile group: 'commons-codec', name: 'commons-codec', version: '1.11'

compile "com.madgag.spongycastle:core:1.53.0.0"
compile "com.madgag.spongycastle:prov:1.53.0.0"

compile group: 'com.google.guava', name: 'guava', version: '18.0'

compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.4.0'

compile "org.iq80.leveldb:leveldb:0.7"

compile group: 'org.fusesource.leveldbjni', name: 'leveldbjni-all',
version: '1.8'

compile "org.apache.commons:commons-collections4:4.0"

compile group: 'com.typesafe', name: 'config', version: '1.3.2'

compile "com.google.code.findbugs:jsr305:3.0.0"

compile "com.cedarsoftware:java-util:1.8.0"

compile "org.apache.commons:commons-lang3:3.4"

compile(group: 'org.apache.kafka', name: 'kafka_2.12', version: '0.11.0.2') {
exclude module: 'slf4j-log4j12'
}
compile group: 'org.springframework', name: 'spring-context', version: '4.2.0.RELEASE'

compile(group: 'org.apache.kafka', name: 'kafka_2.12', version: '0.11.0.2') {
exclude module: 'slf4j-log4j12'
}

compile "org.apache.commons:commons-collections4:4.0"

compile group: 'com.beust', name: 'jcommander', version: '1.72'

compile group: 'junit', name: 'junit', version: '4.8.1'

compile group: 'io.atomix.copycat', name: 'copycat-server', version: '1.1.4'
compile group: 'io.atomix.copycat', name: 'copycat-client', version: '1.1.4'
compile group: 'io.atomix.catalyst', name: 'catalyst-netty', version: '1.1.1'
}

tasks.matching { it instanceof Test }.all {
testLogging.events = ["failed", "passed", "skipped"]
}

if (project.hasProperty("mainClass")) {
mainClassName = mainClass
}

sourceSets {
src {
main {
proto {
srcDir 'src/main/proto'
}
}
}
}

protobuf.protoc {
artifact = 'com.google.protobuf:protoc:3.4.0'
}

protobuf {
generatedFilesBaseDir = "$projectDir/src"

generateProtoTasks {
all().each { task ->
task.builtins {
java {}
}
}
}
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'java-tron'

28 changes: 28 additions & 0 deletions src/main/java/org/tron/command/AccountCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.tron.command;

import org.tron.peer.Peer;
import org.tron.utils.ByteArray;

public class AccountCommand extends Command {
public AccountCommand() {
}

@Override
public void execute(Peer peer, String[] parameters) {
System.out.println(ByteArray.toHexString(peer.getMyKey().getAddress()));
}

@Override
public void usage() {
System.out.println("");
System.out.println("USAGE [account]:");
System.out.println("Command: account");
System.out.println("Description: Get your account.");
System.out.println("");
}

@Override
public boolean check(String[] parameters) {
return true;
}
}
52 changes: 52 additions & 0 deletions src/main/java/org/tron/command/Cli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.tron.command;

import org.tron.peer.Peer;

import java.util.Arrays;
import java.util.Scanner;

public class Cli {
public Cli() {
}

public void run(Peer peer) {
Scanner in = new Scanner(System.in);

while (true) {
String cmd = in.nextLine();
cmd = cmd.trim();

String[] cmdArray = cmd.split("\\s+");

if (cmdArray.length == 0) {
continue;
}

String[] cmdParameters = Arrays.copyOfRange(cmdArray, 1, cmdArray.length);

switch (cmdArray[0]) {
case "exit":
case "quit":
case "bye":
new ExitCommand().execute(peer, cmdParameters);
break;
case "send":
new SendCommand().execute(peer, cmdParameters);
break;
case "getbalance":
new GetBalanceCommand().execute(peer, cmdParameters);
break;
case "account":
new AccountCommand().execute(peer, cmdParameters);
break;
case "printblockchain":
new PrintBlockchainCommand().execute(peer, cmdParameters);
break;
case "help":
default:
new HelpCommand().execute(peer, cmdParameters);
break;
}
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/tron/command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.tron.command;

import org.tron.peer.Peer;

public abstract class Command {
public abstract void execute(Peer peer, String[] parameters);

public abstract void usage();

public abstract boolean check(String[] parameters);
}
27 changes: 27 additions & 0 deletions src/main/java/org/tron/command/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.tron.command;

import org.tron.peer.Peer;

public class ExitCommand extends Command {
public ExitCommand() {
}

@Override
public void execute(Peer peer, String[] parameters) {
System.exit(0);
}

@Override
public void usage() {
System.out.println("");
System.out.println("USAGE [exit]:");
System.out.println("Command: exit | quit | bye");
System.out.println("Description: Exit the program.");
System.out.println("");
}

@Override
public boolean check(String[] parameters) {
return true;
}
}
39 changes: 39 additions & 0 deletions src/main/java/org/tron/command/GetBalanceCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.tron.command;

import org.tron.peer.Peer;
import org.tron.protos.core.TronTXOutput;

import java.util.ArrayList;

public class GetBalanceCommand extends Command {
public GetBalanceCommand() {
}

@Override
public void execute(Peer peer, String[] parameters) {
byte[] pubKeyHash = peer.getWallet().getEcKey().getPubKey();
ArrayList<TronTXOutput.TXOutput> utxos = peer.getUTXOSet().findUTXO(pubKeyHash);

long balance = 0;

for (TronTXOutput.TXOutput txOutput : utxos) {
balance += txOutput.getValue();
}

System.out.println(balance);
}

@Override
public void usage() {
System.out.println("");
System.out.println("USAGE [getbalance]:");
System.out.println("Command: getbalance");
System.out.println("Description: Get your balance.");
System.out.println("");
}

@Override
public boolean check(String[] parameters) {
return true;
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/tron/command/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.tron.command;

import org.tron.peer.Peer;

public class HelpCommand extends Command {
public HelpCommand() {
}

@Override
public void execute(Peer peer, String[] parameters) {
new ExitCommand().usage();
new SendCommand().usage();
new GetBalanceCommand().usage();
new AccountCommand().usage();
new PrintBlockchainCommand().usage();
}

@Override
public void usage() {
System.out.printf("USAGE [help]");
System.out.println("help");
}

@Override
public boolean check(String[] parameters) {
return true;
}
}
Loading

0 comments on commit ad8d728

Please sign in to comment.