Skip to content

Commit ffaf8a6

Browse files
fix
1 parent fc2ea22 commit ffaf8a6

File tree

19 files changed

+461
-131
lines changed

19 files changed

+461
-131
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ dependencies {
7474
compile group: 'io.atomix.copycat', name: 'copycat-client', version: '1.1.4'
7575
compile group: 'io.atomix.catalyst', name: 'catalyst-netty', version: '1.1.1'
7676
compile group: 'net.jcip', name: 'jcip-annotations', version: '1.0'
77+
78+
// https://mvnrepository.com/artifact/com.alibaba/fastjson
79+
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.44'
7780
}
7881

7982
tasks.matching { it instanceof Test }.all {

src/main/java/org/tron/command/Cli.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,17 @@ public void run(Peer peer) {
4545
new ConsensusCommand().server();
4646
break;
4747
case "getmessage":
48-
new ConsensusCommand().getClient(cmdParameters);
48+
new ConsensusCommand().getClient(peer,cmdParameters);
4949
break;
5050
case "putmessage":
5151
new ConsensusCommand().putClient(cmdParameters);
5252
break;
5353
case "put":
5454
new ConsensusCommand().execute(peer, cmdParameters);
5555
break;
56+
case "loadblock":
57+
new ConsensusCommand().loadBlock(peer);
58+
break;
5659
case "help":
5760
default:
5861
new HelpCommand().execute(peer, cmdParameters);

src/main/java/org/tron/command/ConsensusCommand.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,20 @@ public void putClient(String[] args) {
2626
Client.putMessage(args);
2727
}
2828

29-
public void getClient(String[] args) {
29+
public void getClient(Peer peer,String[] args) {
3030
//Client.getMessage(args[0]);
3131
if (Tron.getPeer().getType().equals(Peer.PEER_SERVER)) {
32-
Client.getMessage(MessageType.TRANSACTION);
33-
Client.getMessage(MessageType.BLOCK);
32+
Client.getMessage(peer,MessageType.TRANSACTION);
33+
Client.getMessage(peer,MessageType.BLOCK);
3434
}else{
35-
Client.getMessage(MessageType.BLOCK);
35+
Client.getMessage(peer,MessageType.BLOCK);
3636
}
3737

3838
}
39+
public void loadBlock(Peer peer){
40+
System.out.println("BlockChain loadding ...");
41+
Client.loadBlock(peer);
42+
}
3943

4044
public void usage() {
4145
System.out.println("");

src/main/java/org/tron/consensus/client/Client.java

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.tron.overlay.message.Message;
1010
import org.tron.overlay.message.Type;
1111
import org.tron.peer.Peer;
12-
1312
import java.util.Arrays;
1413
import java.util.Collection;
1514
import java.util.concurrent.CompletableFuture;
@@ -30,12 +29,22 @@ public class Client{
3029
client.serializer().register(GetQuery.class);
3130

3231
Collection<Address> cluster = Arrays.asList(
33-
new Address("192.168.0.109", 5000)
34-
32+
new Address("192.168.0.107", 5000)
3533
);
36-
3734
CompletableFuture<CopycatClient> future = client.connect(cluster);
3835
future.join();
36+
/*InetAddress localhost = null;
37+
try {
38+
localhost = InetAddress.getLocalHost();
39+
Collection<Address> cluster = Arrays.asList(
40+
new Address(localhost.getHostAddress(), 5000)
41+
);
42+
43+
CompletableFuture<CopycatClient> future = client.connect(cluster);
44+
future.join();
45+
} catch (UnknownHostException e) {
46+
e.printStackTrace();
47+
}*/
3948
}
4049

4150
public static CopycatClient getClient() {
@@ -74,25 +83,47 @@ public static void putMessage1(Message message) {
7483
System.out.println("block:" + message.getType().toString()
7584
+ "; type: " + message.getMessage().getClass().getSimpleName
7685
() + "; message:" + message.getMessage());*/
77-
client.submit(new PutCommand("block", message.getMessage()));
78-
System.out.println("Block: consensus success");
86+
87+
//client.submit(new PutCommand("block", message.getMessage()));
88+
//System.out.println("Block: consensus success");
89+
90+
int i = 1;
91+
boolean f = true;
92+
while(f){
93+
String block_key = "block" + i;
94+
Object block = client.submit(new GetQuery(block_key)).join();
95+
try {
96+
if (!(block == null)) {
97+
f =true;
98+
i = i+1;
99+
}else {
100+
client.submit(new PutCommand(block_key, message.getMessage()));
101+
System.out.println("Block: consensus success");
102+
f = false;
103+
break;
104+
}
105+
} catch (NullPointerException e) {
106+
e.printStackTrace();
107+
System.out.println("object == null");
108+
}
109+
}
79110
}
80111
}
81112

82-
public static void getMessage(String key) {
83-
84-
Peer peerConsensus = Peer.getInstance("server");
113+
public static void getMessage(Peer peer,String key) {
85114
final String[] preMessage = {null};
86115
final String[] preTime = {null};
87116
if (key.equals("transaction")) {
88117
Thread thread = new Thread(() -> {
89118
while(true){
90119
Object time = client.submit(new GetQuery("time")).join();
91120
if(!time.toString().equals(preTime[0])) {
92-
client.submit(new GetQuery(key)).thenAccept(result -> {
121+
client.submit(new GetQuery(key)).thenAccept(transaction
122+
-> {
93123
//System.out.println("Consensus " + key + " is: " + result);
94124
//System.out.println("type: " + result.getClass().getSimpleName());
95-
peerConsensus.addReceiveTransaction(String.valueOf(result));
125+
peer.addReceiveTransaction(String
126+
.valueOf(transaction));
96127
});
97128
preTime[0] = time.toString();
98129
}else {
@@ -111,12 +142,33 @@ public static void getMessage(String key) {
111142
if (key.equals("block")) {
112143
Thread thread = new Thread(() -> {
113144
while(true){
114-
client.submit(new GetQuery(key)).thenAccept(result -> {
145+
int i = 1;
146+
boolean f = true;
147+
String block_key;
148+
while(f){
149+
block_key = "block" + i;
150+
Object block = client.submit(new GetQuery(block_key)).join();
151+
try {
152+
if (!(block == null)) {
153+
f =true;
154+
i = i+1;
155+
}else {
156+
f = false;
157+
}
158+
} catch (NullPointerException e) {
159+
e.printStackTrace();
160+
}
161+
}
162+
i = i-1;
163+
String finalBlock_key = "block" + i;
164+
165+
client.submit(new GetQuery(finalBlock_key)).thenAccept(block -> {
115166
/*System.out.println("Consensus " + key + " is: " +
116-
result);*/
117-
if (!String.valueOf(result).equals(preMessage[0])) {
118-
peerConsensus.addReceiveBlock(String.valueOf(result));
119-
preMessage[0] = String.valueOf(result);
167+
block);*/
168+
if (!String.valueOf(block).equals(preMessage[0])) {
169+
peer.addReceiveBlock(String.valueOf
170+
(block));
171+
preMessage[0] = String.valueOf(block);
120172
}else {
121173
preMessage[0] = preMessage[0];
122174
}
@@ -131,4 +183,30 @@ public static void getMessage(String key) {
131183
thread.start();
132184
}
133185
}
186+
public static void loadBlock(Peer peer){
187+
int i = 2;
188+
boolean f = true;
189+
while(f){
190+
String block_key = "block" + i;
191+
Object block = client.submit(new GetQuery(block_key)).join();
192+
System.out.println(block.toString());
193+
try {
194+
if (!(block == null)) {
195+
/*System.out.println("Consensus " + block_key + " is: " +
196+
block);*/
197+
peer.addReceiveBlock(String.valueOf
198+
(block));
199+
f =true;
200+
i = i+1;
201+
}else {
202+
f = false;
203+
break;
204+
}
205+
} catch (NullPointerException e) {
206+
e.printStackTrace();
207+
System.out.println("object == null");
208+
break;
209+
}
210+
}
211+
}
134212
}

src/main/java/org/tron/consensus/server/Server.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public static void serverRun() {
2323
System.out.println("Server localhost: " + localhost.getHostAddress
2424
());
2525

26-
Address address = new Address(localhost.getHostAddress(), 5000);
26+
//Address address = new Address(localhost.getHostAddress(), 5000);
27+
Address address = new Address("192.168.0.107", 5000);
2728

2829
CopycatServer server = CopycatServer.builder(address)
2930
.withStateMachine(MapstateMachine::new)
@@ -43,7 +44,7 @@ public static void serverRun() {
4344
future.join();
4445

4546
//Collection<Address> cluster = Collections.singleton(new Address
46-
// ("192.16.50.129", 5000));
47+
// ("192.168.0.100", 5000));
4748
//server.join(cluster).join();
4849

4950
System.out.println("Server xxd: " + server.cluster().members());
@@ -55,7 +56,7 @@ public static void serverRun() {
5556
System.out.println("Server state: " + state);
5657
}
5758
});
58-
server.context();
59+
//server.context();
5960

6061
} catch (UnknownHostException e) {
6162
e.printStackTrace();

src/main/java/org/tron/core/BlockUtils.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ public static Block newGenesisBlock(Transaction coinbase) {
8585
return genesisBlock.build();
8686
}
8787

88+
public static Block newGenesisBlock(List<Transaction> transactions) {
89+
90+
Block.Builder genesisBlock = Block.newBuilder();
91+
92+
for (Transaction tx : transactions) {
93+
genesisBlock.addTransactions(tx);
94+
}
95+
96+
BlockHeader.Builder builder = BlockHeader.newBuilder();
97+
builder.setDifficulty(ByteString.copyFrom(ByteArray.fromHexString
98+
("2001")));
99+
100+
genesisBlock.setBlockHeader(builder.build());
101+
102+
builder.setHash(ByteString.copyFrom(sha3(prepareData
103+
(genesisBlock.build()))));
104+
105+
genesisBlock.setBlockHeader(builder.build());
106+
107+
return genesisBlock.build();
108+
}
109+
88110
/**
89111
* get prepare data of the block
90112
*
@@ -96,7 +118,6 @@ public static byte[] prepareData(Block block) {
96118

97119
BlockHeader.Builder blockHeader = tmp.getBlockHeaderBuilder();
98120
blockHeader.clearHash();
99-
blockHeader.clearIsPoW();
100121
blockHeader.clearNonce();
101122

102123
tmp.setBlockHeader(blockHeader.build());

src/main/java/org/tron/core/Blockchain.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.tron.core;
22

3+
import com.alibaba.fastjson.JSON;
4+
import com.google.common.io.ByteStreams;
35
import com.google.protobuf.ByteString;
46
import com.google.protobuf.InvalidProtocolBufferException;
57
import org.slf4j.Logger;
@@ -20,10 +22,10 @@
2022
import org.tron.utils.ByteArray;
2123

2224
import java.io.File;
25+
import java.io.IOException;
26+
import java.io.InputStream;
2327
import java.nio.file.Paths;
24-
import java.util.Arrays;
25-
import java.util.HashMap;
26-
import java.util.List;
28+
import java.util.*;
2729

2830
import static org.tron.core.Constant.BLOCK_DB_NAME;
2931
import static org.tron.core.Constant.LAST_HASH;
@@ -43,17 +45,39 @@ public class Blockchain {
4345
*
4446
* @param address wallet address
4547
*/
46-
public Blockchain(String address) {
48+
public Blockchain(String address, String type) {
4749
if (dbExists()) {
4850
logger.info("blockchain already exists.");
4951
System.exit(0);
5052
}
5153

5254
blockDB = new LevelDbDataSource(BLOCK_DB_NAME);
5355
blockDB.init();
54-
Transaction coinbase = TransactionUtils.newCoinbaseTransaction
55-
(address, genesisCoinbaseData);
56-
Block genesisBlock = BlockUtils.newGenesisBlock(coinbase);
56+
57+
InputStream is = getClass().getClassLoader().getResourceAsStream("genesis.json");
58+
String json = null;
59+
try {
60+
json = new String(ByteStreams.toByteArray(is));
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
}
64+
65+
GenesisBlockLoader genesisBlockLoader = JSON.parseObject(json, GenesisBlockLoader.class);
66+
67+
Iterator iterator = genesisBlockLoader.getTransaction().entrySet().iterator();
68+
69+
List<Transaction> transactions = new ArrayList<>();
70+
71+
while (iterator.hasNext()) {
72+
Map.Entry entry = (Map.Entry) iterator.next();
73+
String key = (String) entry.getKey();
74+
Integer value = (Integer) entry.getValue();
75+
76+
Transaction transaction = TransactionUtils.newCoinbaseTransaction(key, genesisCoinbaseData, value);
77+
transactions.add(transaction);
78+
}
79+
80+
Block genesisBlock = BlockUtils.newGenesisBlock(transactions);
5781

5882
this.lastHash = genesisBlock.getBlockHeader().getHash().toByteArray();
5983
this.currentHash = this.lastHash;
@@ -63,9 +87,13 @@ public Blockchain(String address) {
6387
byte[] lastHash = genesisBlock.getBlockHeader()
6488
.getHash()
6589
.toByteArray();
66-
6790
blockDB.put(LAST_HASH, lastHash);
68-
91+
// put message to consensus 2018/1/2
92+
if (type.equals(Peer.PEER_SERVER)) {
93+
String value = ByteArray.toHexString(genesisBlock.toByteArray());
94+
Message message = new Message(value, Type.BLOCK);
95+
Client.putMessage1(message); // consensus: put message GenesisBlock
96+
}
6997
logger.info("new blockchain");
7098
}
7199

0 commit comments

Comments
 (0)