Skip to content

Commit

Permalink
add consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
xxo1shine committed Aug 19, 2019
1 parent 127cd78 commit 6157b15
Show file tree
Hide file tree
Showing 70 changed files with 2,724 additions and 1,387 deletions.

Large diffs are not rendered by default.

117 changes: 117 additions & 0 deletions chainbase/src/main/java/org/tron/common/utils/BIUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/

package org.tron.common.utils;

import java.math.BigInteger;

public class BIUtil {

/**
* @param valueA - not null
* @param valueB - not null
* @return true - if the valueA is less than valueB is zero
*/
public static boolean isLessThan(BigInteger valueA, BigInteger valueB) {
return valueA.compareTo(valueB) < 0;
}

/**
* @param value - not null
* @return true - if the param is zero
*/
public static boolean isZero(BigInteger value) {
return value.compareTo(BigInteger.ZERO) == 0;
}

/**
* @param valueA - not null
* @param valueB - not null
* @return true - if the valueA is equal to valueB is zero
*/
public static boolean isEqual(BigInteger valueA, BigInteger valueB) {
return valueA.compareTo(valueB) == 0;
}

/**
* @param valueA - not null
* @param valueB - not null
* @return true - if the valueA is not equal to valueB is zero
*/
public static boolean isNotEqual(BigInteger valueA, BigInteger valueB) {
return !isEqual(valueA, valueB);
}

/**
* @param valueA - not null
* @param valueB - not null
* @return true - if the valueA is more than valueB is zero
*/
public static boolean isMoreThan(BigInteger valueA, BigInteger valueB) {
return valueA.compareTo(valueB) > 0;
}


/**
* @param valueA - not null
* @param valueB - not null
* @return sum - valueA + valueB
*/
public static BigInteger sum(BigInteger valueA, BigInteger valueB) {
return valueA.add(valueB);
}


/**
* @param data = not null
* @return new positive BigInteger
*/
public static BigInteger toBI(byte[] data) {
return new BigInteger(1, data);
}

/**
* @param data = not null
* @return new positive BigInteger
*/
public static BigInteger toBI(long data) {
return BigInteger.valueOf(data);
}


public static boolean isPositive(BigInteger value) {
return value.signum() > 0;
}

public static boolean isNotCovers(BigInteger covers, BigInteger value) {
return covers.compareTo(value) < 0;
}

public static BigInteger max(BigInteger first, BigInteger second) {
return first.compareTo(second) < 0 ? second : first;
}

/**
* Returns a result of safe addition of two {@code int} values {@code Integer.MAX_VALUE} is
* returned if overflow occurs
*/
public static int addSafely(int a, int b) {
long res = (long) a + (long) b;
return res > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.tron.core.store;

import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.tron.common.utils.ByteArray;
import org.tron.core.capsule.BytesCapsule;
import org.tron.core.db.TronStoreWithRevoking;

@Slf4j(topic = "DB")
@Component
public class WitnessScheduleStore extends TronStoreWithRevoking<BytesCapsule> {

private static final byte[] ACTIVE_WITNESSES = "active_witnesses".getBytes();
private static final byte[] CURRENT_SHUFFLED_WITNESSES = "current_shuffled_witnesses".getBytes();

private static final int ADDRESS_BYTE_ARRAY_LENGTH = 21;

@Autowired
private WitnessScheduleStore(@Value("witness_schedule") String dbName) {
super(dbName);
}

private void saveData(byte[] species, List<ByteString> witnessesAddressList) {
byte[] ba = new byte[witnessesAddressList.size() * ADDRESS_BYTE_ARRAY_LENGTH];
int i = 0;
for (ByteString address : witnessesAddressList) {
System.arraycopy(address.toByteArray(), 0,
ba, i * ADDRESS_BYTE_ARRAY_LENGTH, ADDRESS_BYTE_ARRAY_LENGTH);
i++;
}

this.put(species, new BytesCapsule(ba));
}

private List<ByteString> getData(byte[] species) {
List<ByteString> witnessesAddressList = new ArrayList<>();
return Optional.ofNullable(getUnchecked(species))
.map(BytesCapsule::getData)
.map(ba -> {
int len = ba.length / ADDRESS_BYTE_ARRAY_LENGTH;
for (int i = 0; i < len; ++i) {
byte[] b = new byte[ADDRESS_BYTE_ARRAY_LENGTH];
System.arraycopy(ba, i * ADDRESS_BYTE_ARRAY_LENGTH, b, 0, ADDRESS_BYTE_ARRAY_LENGTH);
witnessesAddressList.add(ByteString.copyFrom(b));
}
logger.debug("getWitnesses:" + ByteArray.toStr(species) + witnessesAddressList);
return witnessesAddressList;
}).orElseThrow(
() -> new IllegalArgumentException(
"not found " + ByteArray.toStr(species) + "Witnesses"));
}

public void saveActiveWitnesses(List<ByteString> witnessesAddressList) {
saveData(ACTIVE_WITNESSES, witnessesAddressList);
}

public List<ByteString> getActiveWitnesses() {
return getData(ACTIVE_WITNESSES);
}

public void saveCurrentShuffledWitnesses(List<ByteString> witnessesAddressList) {
saveData(CURRENT_SHUFFLED_WITNESSES, witnessesAddressList);
}

public List<ByteString> getCurrentShuffledWitnesses() {
return getData(CURRENT_SHUFFLED_WITNESSES);
}
}
197 changes: 197 additions & 0 deletions consensus/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
buildscript {
repositories {
mavenCentral()
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.2'
}
}

group = "com.github.tronprotocol"
ext.githubProjectName = "consensus"

version = project.properties["release.version"]

apply plugin: "java-library"
//apply plugin: "checkstyle"
apply plugin: "jacoco"
apply plugin: "maven"
apply plugin: "maven-publish"
//apply plugin: "me.champeau.gradle.jmh"

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

// Dependency versions
// ---------------------------------------

def junitVersion = "4.12"
def mockitoVersion = "2.1.0"
def jmhLibVersion = "1.20"
def testNgVersion = "6.11"
def guavaVersion = "24.1-jre"
def jacocoVersion = "0.8.0"
def leveldbVersion = "1.8"
def logbackVersion = "1.2.3"
def jansiVersion = "1.16"
def lombokVersion = "1.18.2"
def slf4jVersion = "1.7.25"
// --------------------------------------

repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}

dependencies {
compile project(":chainbase")
compile project(":protocol")
testImplementation "junit:junit:$junitVersion"
testImplementation "org.mockito:mockito-core:$mockitoVersion"

testImplementation "org.testng:testng:$testNgVersion"

compile "com.google.guava:guava:$guavaVersion"
compile "org.fusesource.leveldbjni:leveldbjni-all:$leveldbVersion"
compile "org.fusesource.jansi:jansi:$jansiVersion"
compile "org.slf4j:slf4j-api:$slf4jVersion"
compile "org.slf4j:jcl-over-slf4j:$slf4jVersion"
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
compile "org.projectlombok:lombok:$lombokVersion"
compile group: 'org.rocksdb', name: 'rocksdbjni', version: '5.15.10'
compile "com.madgag.spongycastle:core:1.58.0.0"
compile "org.apache.commons:commons-lang3:3.4"
compile 'com.github.tronprotocol:zksnark-java-sdk:master-SNAPSHOT'
}

//javadoc {
// failOnError = false
// exclude "**/test/**"
// options {
// windowTitle = "Chainbase Javadoc ${project.version}"
// }
// // Clear the following options to make the docs consistent with the old format
// options.addStringOption("top").value = ""
// options.addStringOption("doctitle").value = ""
// options.addStringOption("header").value = ""
// options.stylesheetFile = new File(projectDir, "gradle/stylesheet.css");
//
// options.links(
// "https://docs.oracle.com/javase/7/docs/api/"
// )
//
// if (JavaVersion.current().isJava7()) {
// // "./gradle/stylesheet.css" only supports Java 7
// options.addStringOption("stylesheetfile", rootProject.file("./gradle/stylesheet.css").toString())
// }
//}

task sourcesJar(type: Jar, dependsOn: classes) {
classifier = "sources"
from sourceSets.main.allSource
}

//task javadocJar(type: Jar, dependsOn: javadoc) {
// classifier = "javadoc"
// from javadoc.destinationDir
//}

artifacts {
archives jar
archives sourcesJar
// archives javadocJar
}


//jmh {
// jmhVersion = jmhLibVersion
// humanOutputFile = null
// includeTests = false
// jvmArgs = ["-Djmh.ignoreLock=true"]
// jvmArgsAppend = ["-Djmh.separateClasspathJAR=true"]
//
// if (project.hasProperty("jmh")) {
// include = ".*" + project.jmh + ".*"
// println("JMH: " + include);
// }
//
//}

//plugins.withType(EclipsePlugin) {
// project.eclipse.classpath.plusConfigurations += [ configurations.jmh ]
//}

test {

testLogging {
// showing skipped occasionally should prevent CI timeout due to lack of standard output
events=["skipped", "failed"] // "started", "passed"
// showStandardStreams = true
exceptionFormat="full"

debug.events = ["skipped", "failed"]
debug.exceptionFormat="full"

info.events = ["failed", "skipped"]
info.exceptionFormat="full"

warn.events = ["failed", "skipped"]
warn.exceptionFormat="full"
}

maxHeapSize = "1200m"

if (System.getenv("CI") == null) {
maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
}
}

task testng(type: Test) {
useTestNG()
testLogging {
events=["skipped", "failed"]
exceptionFormat="full"

debug.events = ["skipped", "failed"]
debug.exceptionFormat="full"

info.events = ["failed", "skipped"]
info.exceptionFormat="full"

warn.events = ["failed", "skipped"]
warn.exceptionFormat="full"
}
}

check.dependsOn testng

jacoco {
toolVersion = jacocoVersion // See http://www.eclemma.org/jacoco/.
}

jacocoTestReport {
reports {
xml.enabled = true
html.enabled = true
}

afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it,
exclude: ["io/reactivex/tck/**"])
})
}
}

build.dependsOn jacocoTestReport

//checkstyle {
// configFile file("checkstyle.xml")
// ignoreFailures = true
// toolVersion ="6.19"
//}

//apply from: file("gradle/javadoc_cleanup.gradle")
Loading

0 comments on commit 6157b15

Please sign in to comment.