Small adjustments:

+  Block chain the index holds hashes not the full block RLP (bug fix)
+ Ethereum facade some well know functionality
This commit is contained in:
romanman 2014-07-28 00:59:02 +03:00
parent 9197c8df36
commit 96c3f75049
5 changed files with 66 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package org.ethereum.core;
import org.ethereum.db.DatabaseImpl;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.ethereum.util.AdvancedDeviceUtils;
import org.ethereum.util.ByteUtil;
@ -82,7 +83,7 @@ public class Blockchain {
}
public Block getByNumber(long blockNr) {
return new Block(index.get(blockNr));
return new Block(chainDb.get(ByteUtil.longToBytes(blockNr)));
}
public void applyBlock(Block block) {
@ -115,7 +116,7 @@ public class Blockchain {
String blockStateRootHash = Hex.toHexString(block.getStateRoot());
String worldStateRootHash = Hex.toHexString(WorldManager.getInstance().getRepository().getWorldState().getRootHash());
if(!blockStateRootHash.equals(worldStateRootHash)){
// logger.warn("WARNING: STATE CONFLICT! block: {} worldstate {} mismatch", blockNum, worldStateRootHash);
logger.warn("WARNING: STATE CONFLICT! block: {} worldstate {} mismatch", block.getNumber(), worldStateRootHash);
// Last fail on WARNING: STATE CONFLICT! block: 1157 worldstate b1d9a978451ef04c1639011d9516473d51c608dbd25906c89be791707008d2de mismatch
// System.exit(-1);
}
@ -130,6 +131,11 @@ public class Blockchain {
logger.info("*** Block chain size: [ {} ]", this.getSize());
EthereumListener listener = WorldManager.getInstance().getListener();
if (listener != null)
listener.trace(String.format("Block chain size: [ %d ]", this.getSize()));
/*
if (lastBlock.getNumber() >= 30) {
@ -153,7 +159,7 @@ public class Blockchain {
}
this.chainDb.put(ByteUtil.longToBytes(block.getNumber()), block.getEncoded());
this.index.put(block.getNumber(), block.getEncoded());
this.index.put(block.getNumber(), block.getHash());
WorldManager.getInstance().getWallet().processBlock(block);
this.setLastBlock(block);
@ -188,7 +194,7 @@ public class Blockchain {
logger.debug("Displaying blocks stored in DB sorted on blocknumber");
for (iterator.seekToFirst(); iterator.hasNext();) {
this.lastBlock = new Block(iterator.next().getValue());
this.index.put(lastBlock.getNumber(), lastBlock.getEncoded());
this.index.put(lastBlock.getNumber(), lastBlock.getHash());
logger.debug("Block #{} -> {}", lastBlock.getNumber(), lastBlock.toFlatString());
}
}

View File

@ -1,5 +1,6 @@
package org.ethereum.facade;
import org.ethereum.core.Block;
import org.ethereum.listener.EthereumListener;
import org.ethereum.net.client.PeerData;
@ -16,7 +17,16 @@ public interface Ethereum {
public PeerData findPeer(PeerData peerData);
public PeerData findPeer();
public void stopPeerDiscover();
public void connect(InetAddress addr, int port);
public void connect(String ip, int port);
public Block getBlockByIndex(long index);
public long getBlockChainSize();
public void addListener(EthereumListener listener);
}

View File

@ -1,6 +1,6 @@
package org.ethereum.facade;
import org.ethereum.config.SystemProperties;
import org.ethereum.core.Block;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.client.ClientPeer;
@ -31,6 +31,11 @@ public class EthereumImpl implements Ethereum {
public PeerData findPeer(PeerData peerData){
logger.info("Looking for online peer");
EthereumListener listener = WorldManager.getInstance().getListener();
if (listener != null)
listener.trace("Looking for online peer");
WorldManager.getInstance().startPeerDiscovery();
List<PeerData> peers = WorldManager.getInstance().getPeers();
boolean found = false;
@ -44,6 +49,10 @@ public class EthereumImpl implements Ethereum {
if (peer.isOnline() && !peer.equals(peerData)){
logger.info("Found peer: {}", peer.toString());
if (listener != null)
listener.trace(String.format("Found online peer: [ %s ]", peer.toString()));
return peer;
}
++i;
@ -56,6 +65,11 @@ public class EthereumImpl implements Ethereum {
return findPeer(null);
}
@Override
public void stopPeerDiscover(){
WorldManager.getInstance().stopPeerDiscover();
}
@Override
public void connect(InetAddress addr, int port){
connect(addr.getHostName(), port);
@ -69,6 +83,17 @@ public class EthereumImpl implements Ethereum {
port);
}
@Override
public Block getBlockByIndex(long index){
Block block = WorldManager.getInstance().getBlockChain().getByNumber(index);
return block;
}
@Override
public long getBlockChainSize(){
return WorldManager.getInstance().getBlockChain().getSize();
}
@Override
public void addListener(EthereumListener listener) {
WorldManager.getInstance().addListener(listener);

View File

@ -407,6 +407,15 @@ public class WorldManager {
peerDiscovery.start();
};
public void stopPeerDiscover(){
if (listener != null)
listener.trace("Stopping peer discovery");
if (peerDiscovery.isStarted())
peerDiscovery.stop();
}
public BlockQueue getBlockQueue() {
return blockQueue;
}
@ -415,4 +424,8 @@ public class WorldManager {
blockchain.close();
repository.close();
}
public EthereumListener getListener() {
return listener;
}
}

View File

@ -8,6 +8,7 @@ import io.netty.channel.FixedRecvByteBufAllocator;
import org.ethereum.config.SystemProperties;
import org.ethereum.core.Block;
import org.ethereum.core.Transaction;
import org.ethereum.listener.EthereumListener;
import org.ethereum.manager.WorldManager;
import org.ethereum.net.Command;
import org.ethereum.net.MessageQueue;
@ -119,6 +120,11 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
HelloMessage helloMessage = new HelloMessage(rlpList);
logger.info(helloMessage.toString());
if (peerListener != null) peerListener.console(helloMessage.toString());
EthereumListener listener = WorldManager.getInstance().getListener();
if (listener != null)
listener.trace(String.format("Got handshake: [ %s ]", helloMessage.toString()));
}
// got DISCONNECT
if (Command.fromInt(command) == DISCONNECT) {
@ -228,6 +234,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
}, 3000, secToAskForChain * 1000);
}
if (blockList.isEmpty()) return;
WorldManager.getInstance().getBlockQueue().addBlocks(blockList);
if (peerListener != null) peerListener.console(blocksMessage.toString());