Skip to content

Commit 54fe08b

Browse files
committed
fix loadblock
1 parent 4403dad commit 54fe08b

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class Client{
3333
client.serializer().register(GetQuery.class);
3434

3535
Collection<Address> cluster = Arrays.asList(
36-
new Address("192.168.0.100", 5000)
36+
new Address("192.168.0.107", 5000)
3737
);
3838
CompletableFuture<CopycatClient> future = client.connect(cluster);
3939
future.join();
@@ -104,7 +104,6 @@ public static void putMessage1(Message message) {
104104
client.submit(new PutCommand(block_key, message.getMessage()));
105105
System.out.println("Block: consensus success");
106106
f = false;
107-
break;
108107
}
109108
} catch (NullPointerException e) {
110109
e.printStackTrace();
@@ -189,27 +188,19 @@ public static void getMessage(Peer peer,String key) {
189188
}
190189
public static void loadBlock(Peer peer){
191190
int i = 2;
192-
boolean f = true;
193-
while(f){
191+
boolean a = true;
192+
while(a){
194193
String block_key = "block" + i;
195194
Object block = client.submit(new GetQuery(block_key)).join();
196-
System.out.println(block.toString());
197-
try {
198-
if (!(block == null)) {
199-
/*System.out.println("Consensus " + block_key + " is: " +
200-
block);*/
201-
peer.addReceiveBlock(String.valueOf
202-
(block));
203-
f =true;
204-
i = i+1;
205-
}else {
206-
f = false;
207-
break;
208-
}
209-
} catch (NullPointerException e) {
210-
e.printStackTrace();
211-
System.out.println("object == null");
212-
break;
195+
if (!(block == null)) {
196+
peer.addReceiveBlock(String.valueOf
197+
(block));
198+
System.out.println(block.toString());
199+
a = true;
200+
i ++;
201+
}
202+
if (null == block){
203+
a = false;
213204
}
214205
}
215206
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public void addBlock(List<Transaction> transactions) {
304304

305305
}
306306

307-
public void receiveBlock(Block block, UTXOSet utxoSet) {
307+
public void receiveBlock(Block block, UTXOSet utxoSet, Peer peer) {
308308

309309
byte[] lastHashKey = LAST_HASH;
310310
byte[] lastHash = blockDB.get(lastHashKey);
@@ -323,7 +323,7 @@ public void receiveBlock(Block block, UTXOSet utxoSet) {
323323
.toByteArray();
324324

325325
// update lastHash
326-
Tron.getPeer().getBlockchain().getBlockDB().put(lastHashKey, ch);
326+
peer.getBlockchain().getBlockDB().put(lastHashKey, ch);
327327

328328
this.lastHash = ch;
329329
currentHash = ch;

src/main/java/org/tron/peer/Peer.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void addReceiveBlock(String message) {
5959
try {
6060
block = TronBlock.Block.parseFrom(ByteArray.fromHexString(message
6161
));
62-
blockchain.receiveBlock(block, utxoSet);
62+
blockchain.receiveBlock(block, utxoSet, this);
6363
} catch (InvalidProtocolBufferException e) {
6464
e.printStackTrace();
6565
}
@@ -76,23 +76,30 @@ private void init() {
7676
initWallet();
7777
initBlockchian();
7878
initUTXOSet();
79+
initLoadBlock();
7980
}
8081

82+
private void initLoadBlock(){
83+
if (this.type.equals(Peer.PEER_NORMAL)){
84+
Client.loadBlock(this);
85+
}
86+
}
8187
private void initBlockchian() {
8288
new ConsensusCommand().server();
8389
if (Blockchain.dbExists()) {
8490
blockchain = new Blockchain();
8591
} else {
8692
//blockchain = new Blockchain(ByteArray.toHexString(wallet.getAddress()));
8793
if (this.type.equals(Peer.PEER_SERVER)){
94+
System.out.println("peer-server");
8895
blockchain = new Blockchain(ByteArray.toHexString(wallet
8996
.getAddress()), this.type);
9097
}
9198
if (this.type.equals(Peer.PEER_NORMAL)){
92-
//System.out.println("BlockChain loadding ...");
99+
System.out.println("BlockChain loadding ...");
93100
blockchain = new Blockchain(ByteArray.toHexString(wallet
94101
.getAddress()), this.type);
95-
Client.loadBlock(this);
102+
96103
}
97104
}
98105
}

0 commit comments

Comments
 (0)