Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sdargutev committed Mar 16, 2018
1 parent 738cc7c commit d2df4cc
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 66 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ src/main/resources/META-INF/
# output directory
/output-directory/

/output_manager/
/output_witness/
31 changes: 17 additions & 14 deletions src/main/java/org/tron/common/overlay/node/GossipLocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

package org.tron.common.overlay.node;

import io.scalecube.cluster.Cluster;
import io.scalecube.cluster.ClusterConfig;
import io.scalecube.cluster.membership.MembershipEvent.Type;
import io.scalecube.transport.Address;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
Expand All @@ -30,6 +26,11 @@
import org.tron.core.config.args.Args;
import org.tron.core.net.peer.PeerConnection;
import org.tron.core.net.peer.PeerConnectionDelegate;
import io.scalecube.cluster.Cluster;
import io.scalecube.cluster.ClusterConfig;
import io.scalecube.cluster.membership.MembershipEvent;
import io.scalecube.cluster.membership.MembershipEvent.Type;
import io.scalecube.transport.Address;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

Expand All @@ -46,7 +47,7 @@ public class GossipLocalNode implements LocalNode {
//public HashMap<Integer, PeerConnection> listPeer = new HashMap<>();

private ExecutorService executors;

private CompositeSubscription subscriptions = new CompositeSubscription();

// public Collection<PeerConnection> getValidPeer() {
Expand Down Expand Up @@ -81,15 +82,7 @@ public void start() {
//liston peer's change
Subscription membershipListener = cluster
.listenMembership()
.subscribe(event -> {
if (event.type() == Type.REMOVED) {
PeerConnection peer = new PeerConnection(this.cluster, event.oldMember());
peerDel.disconnectPeer(peer);
} else {
PeerConnection peer = new PeerConnection(this.cluster, event.newMember());
peerDel.connectPeer(peer);
}
});
.subscribe(event -> onEvent(event));

executors = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(1000), new ThreadPoolExecutor.CallerRunsPolicy());
Expand All @@ -101,6 +94,16 @@ public void start() {
subscriptions.add(messageSubscription);
}

private void onEvent(MembershipEvent event) {
if (event.type() == Type.REMOVED) {
PeerConnection peer = new PeerConnection(this.cluster, event.oldMember());
peerDel.disconnectPeer(peer);
} else {
PeerConnection peer = new PeerConnection(this.cluster, event.newMember());
peerDel.connectPeer(peer);
}
}

/**
* stop gossip node.
*/
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/tron/core/capsule/BlockCapsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

package org.tron.core.capsule;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.List;
Expand All @@ -32,11 +30,15 @@
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.BlockHeader;
import org.tron.protos.Protocol.Transaction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;

public class BlockCapsule implements ProtoCapsule<Block> {

public static class BlockId extends Sha256Hash {

private static final long serialVersionUID = 640048834186654213L;

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -239,7 +241,7 @@ public Sha256Hash calcMerklerRoot() {
.toByteArray()));
}

if (hashNum % 2 == 1) {
if (hashNum % 2 != 0) {
ids.set(k++, ids.get(max));
}
hashNum = k;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/tron/core/config/args/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@

package org.tron.core.config.args;

import com.google.protobuf.ByteString;
import java.io.Serializable;
import org.apache.commons.lang3.StringUtils;
import org.tron.common.utils.ByteArray;
import org.tron.protos.Protocol.AccountType;
import com.google.protobuf.ByteString;

public class Account implements Serializable {

public class Account {
private static final long serialVersionUID = 2674206490063656846L;

private String accountName;

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/tron/core/config/args/Witness.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

package org.tron.core.config.args;

public class Witness {
import java.io.Serializable;

public class Witness implements Serializable {

private static final long serialVersionUID = -7446501098542377380L;

private String address;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void testallKeys() {
dataSource.resetDb();
}

@Test(timeout = 100)
@Test(timeout = 1000)
public void testLockReleased() {
dataSourceTest.initDB();
// normal close
Expand Down
18 changes: 3 additions & 15 deletions src/test/java/org/tron/core/capsule/BlockCapsuleTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.tron.core.capsule;

import com.google.protobuf.ByteString;
import java.io.File;
import org.junit.AfterClass;
import org.junit.Assert;
Expand All @@ -13,6 +12,8 @@
import org.tron.core.Constant;
import org.tron.core.config.Configuration;
import org.tron.core.config.args.Args;
import org.tron.utils.FileUtils;
import com.google.protobuf.ByteString;


public class BlockCapsuleTest {
Expand All @@ -33,20 +34,7 @@ public static void init() {
@AfterClass
public static void removeDb() {
File dbFolder = new File(dbPath);
deleteFolder(dbFolder);
}

private static void deleteFolder(File index) {
if (!index.isDirectory() || index.listFiles().length <= 0) {
index.delete();
return;
}
for (File file : index.listFiles()) {
if (null != file) {
deleteFolder(file);
}
}
index.delete();
FileUtils.deleteFolder(dbFolder);
}

@Test
Expand Down
18 changes: 3 additions & 15 deletions src/test/java/org/tron/core/db/ManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.tron.core.db;

import com.google.protobuf.ByteString;
import java.io.File;
import org.junit.AfterClass;
import org.junit.Assert;
Expand All @@ -17,6 +16,8 @@
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.config.Configuration;
import org.tron.core.config.args.Args;
import org.tron.utils.FileUtils;
import com.google.protobuf.ByteString;


public class ManagerTest {
Expand Down Expand Up @@ -45,20 +46,7 @@ public static void init() {
@AfterClass
public static void removeDb() {
File dbFolder = new File(dbPath);
deleteFolder(dbFolder);
}

private static void deleteFolder(File index) {
if (!index.isDirectory() || index.listFiles().length <= 0) {
index.delete();
return;
}
for (File file : index.listFiles()) {
if (null != file) {
deleteFolder(file);
}
}
index.delete();
FileUtils.deleteFolder(dbFolder);
}

@Test
Expand Down
19 changes: 4 additions & 15 deletions src/test/java/org/tron/program/AccountVoteWitnessTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.tron.program;

import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
import java.io.File;
import java.util.List;
import org.junit.AfterClass;
Expand All @@ -16,6 +14,9 @@
import org.tron.core.config.args.Args;
import org.tron.core.db.Manager;
import org.tron.protos.Protocol.AccountType;
import org.tron.utils.FileUtils;
import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;

public class AccountVoteWitnessTest {

Expand All @@ -35,21 +36,9 @@ public static void init() {
@AfterClass
public static void removeDb() {
File dbFolder = new File(dbPath);
deleteFolder(dbFolder);
FileUtils.deleteFolder(dbFolder);
}

private static void deleteFolder(File index) {
if (!index.isDirectory() || index.listFiles().length <= 0) {
index.delete();
return;
}
for (File file : index.listFiles()) {
if (null != file) {
deleteFolder(file);
}
}
index.delete();
}

@Test
public void testAccountVoteWitness() {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/tron/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.tron.utils;

import java.io.File;

public class FileUtils {
public static void deleteFolder(File index) {
if (!index.isDirectory() || index.listFiles().length <= 0) {
index.delete();
return;
}
for (File file : index.listFiles()) {
if (null != file) {
deleteFolder(file);
}
}
index.delete();
}

}

0 comments on commit d2df4cc

Please sign in to comment.