From 3953dc97c1b78498ff62adae523f527f43304fc0 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Sat, 5 Jul 2014 21:41:00 +0200 Subject: [PATCH] Genesis only created/applied when DB is empty + logging instead of printStackTrace() --- .../java/org/ethereum/core/Blockchain.java | 5 ++-- .../main/java/org/ethereum/db/Repository.java | 2 +- .../org/ethereum/gui/ConsoleTokenMaker.java | 7 +++-- .../java/org/ethereum/gui/SerpentEditor.java | 12 ++++++--- .../main/java/org/ethereum/gui/ToolBar.java | 2 -- .../org/ethereum/manager/WorldManager.java | 4 +-- .../net/client/EthereumProtocolHandler.java | 4 +-- .../ethereum/net/message/StaticMessages.java | 2 -- .../net/message/TransactionsMessage.java | 6 ++++- .../EthereumPeerTasterHandler.java | 2 +- .../net/peerdiscovery/PeerTaster.java | 2 +- .../main/java/org/ethereum/vm/Program.java | 10 +++---- .../src/main/java/org/ethereum/vm/VM.java | 7 +++-- .../src/main/resources/log4j.properties | 22 +++++++++------- .../src/main/resources/system.properties | 16 ++++++------ .../src/test/resources/log4j.properties | 26 ++++++++++--------- .../src/test/resources/system.properties | 16 ++++++------ 17 files changed, 79 insertions(+), 66 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java index 3e45e6f5..94bca447 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Blockchain.java @@ -2,7 +2,6 @@ package org.ethereum.core; import org.ethereum.db.DatabaseImpl; import org.ethereum.manager.WorldManager; -import org.ethereum.net.message.StaticMessages; import org.ethereum.net.submit.WalletTransaction; import org.ethereum.util.ByteUtil; import org.iq80.leveldb.DBIterator; @@ -99,7 +98,7 @@ public class Blockchain { // if it is the first block to add // check that the parent is the genesis if (index.isEmpty() - && !Arrays.equals(StaticMessages.GENESIS_HASH, + && !Arrays.equals(Genesis.getInstance().getHash(), firstBlockToAdd.getParentHash())) { return; } @@ -184,7 +183,7 @@ public class Blockchain { public byte[] getLatestBlockHash() { if (index.isEmpty()) - return StaticMessages.GENESIS_HASH; + return Genesis.getInstance().getHash(); else return getLastBlock().getHash(); } diff --git a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java index c757c493..3bcc9afc 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/Repository.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/Repository.java @@ -308,7 +308,7 @@ public class Repository { } bw.close(); } catch (IOException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } } } diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/ConsoleTokenMaker.java b/ethereumj-core/src/main/java/org/ethereum/gui/ConsoleTokenMaker.java index 6131edad..86512c4d 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/ConsoleTokenMaker.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/ConsoleTokenMaker.java @@ -4,6 +4,8 @@ package org.ethereum.gui; import javax.swing.text.Segment; import org.fife.ui.rsyntaxtextarea.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** @@ -12,6 +14,8 @@ import org.fife.ui.rsyntaxtextarea.*; * Created on: 24/04/14 11:52 */ public class ConsoleTokenMaker extends AbstractTokenMaker { + + private Logger logger = LoggerFactory.getLogger("gui"); protected final String operators = "+-*/%!=<>^&|?:"; @@ -75,8 +79,7 @@ public class ConsoleTokenMaker extends AbstractTokenMaker { break; default: - new Exception("Unknown tokenType: '" + tokenType + "'"). - printStackTrace(); + logger.error("Unknown tokenType: '" + tokenType + "'"); tokenType = Token.IDENTIFIER; break; diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java b/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java index 5b55cb02..cb30eb75 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/SerpentEditor.java @@ -3,6 +3,8 @@ package org.ethereum.gui; import org.ethereum.serpent.SerpentCompiler; import org.fife.ui.rsyntaxtextarea.*; import org.fife.ui.rtextarea.RTextScrollPane; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.swing.*; @@ -23,6 +25,8 @@ import static org.ethereum.config.SystemProperties.CONFIG; */ public class SerpentEditor extends JFrame { + private Logger logger = LoggerFactory.getLogger("gui"); + private String codeSample = "\n\n\n" + "" + "if !contract.storage[msg.data[0]]:\n" + @@ -181,7 +185,7 @@ public class SerpentEditor extends JFrame { asmResult = SerpentCompiler.compile(codeArea.getText()); } } catch (Throwable th) { - th.printStackTrace(); + logger.error(th.getMessage(), th); splitPanel.setDividerLocation(0.8); result.setVisible(true); @@ -218,7 +222,7 @@ public class SerpentEditor extends JFrame { machineCode = SerpentCompiler.encodeMachineCodeForVMRun(machineCode, null); } } catch (Throwable th) { - th.printStackTrace(); + logger.error(th.getMessage(), th); splitPanel.setDividerLocation(0.7); result.setVisible(true); result.setText(th.getMessage()); @@ -266,7 +270,7 @@ public class SerpentEditor extends JFrame { String content = new Scanner(file).useDelimiter("\\Z").next(); codeArea.setText(content); } catch (FileNotFoundException e1) { - e1.printStackTrace(); + logger.error(e1.getMessage(), e1); } catch (java.util.NoSuchElementException e2) { // don't worry it's just the file is empty codeArea.setText(""); @@ -334,7 +338,7 @@ public class SerpentEditor extends JFrame { out.write(codeArea.getText()); out.close(); } catch (IOException e1) { - e1.printStackTrace(); + logger.error(e1.getMessage(), e1); } } }); diff --git a/ethereumj-core/src/main/java/org/ethereum/gui/ToolBar.java b/ethereumj-core/src/main/java/org/ethereum/gui/ToolBar.java index ee3550f3..02a22ca0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/gui/ToolBar.java +++ b/ethereumj-core/src/main/java/org/ethereum/gui/ToolBar.java @@ -226,9 +226,7 @@ public class ToolBar extends JFrame { cp.add(chainToggle); cp.add(walletToggle); - WorldManager.getInstance().getWallet(); WorldManager.getInstance().loadBlockChain(); - MainData.instance.toString(); } diff --git a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java index 6a4c65a4..7e8fef01 100644 --- a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java @@ -205,7 +205,7 @@ public class WorldManager { .createProgramInvoke(tx, lastBlock, trackRepository); if (logger.isInfoEnabled()) - logger.info("running the init for contract: addres={}", + logger.info("running the init for contract: address={}", Hex.toHexString(tx.getContractAddress())); VM vm = new VM(); @@ -224,7 +224,7 @@ public class WorldManager { Block lastBlock = blockchain.getLastBlock(); if (logger.isInfoEnabled()) - logger.info("calling for existing contract: addres={}", + logger.info("calling for existing contract: address={}", Hex.toHexString(tx.getReceiveAddress())); ProgramInvoke programInvoke = ProgramInvokeFactory diff --git a/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java b/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java index 9e9ad304..54463143 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/client/EthereumProtocolHandler.java @@ -314,7 +314,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { this.tearDown = true; logger.info("Lost connection to the server"); - cause.printStackTrace(); + logger.error(cause.getMessage(), cause); ctx.close().sync(); timer.cancel(); } @@ -359,7 +359,7 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter { private void sendGetChain(ChannelHandlerContext ctx) { - byte[] hash = WorldManager.getInstance().getBlockChain().getLatestBlockHash(); + byte[] hash = WorldManager.getInstance().getBlockChain().getLastBlock().getHash(); GetChainMessage chainMessage = new GetChainMessage((byte)100, hash); chainMessage.toString(); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/message/StaticMessages.java b/ethereumj-core/src/main/java/org/ethereum/net/message/StaticMessages.java index b83d45d8..be2ec441 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/message/StaticMessages.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/message/StaticMessages.java @@ -1,6 +1,5 @@ package org.ethereum.net.message; -import org.ethereum.core.Genesis; import org.ethereum.crypto.HashUtil; import org.spongycastle.util.encoders.Hex; @@ -17,7 +16,6 @@ public class StaticMessages { public static final byte[] GET_TRANSACTIONS = Hex.decode("2240089100000002C116"); public static final byte[] DISCONNECT_08 = Hex.decode("2240089100000003C20108"); - public static final byte[] GENESIS_HASH = Genesis.getInstance().getHash(); public static final byte[] MAGIC_PACKET = Hex.decode("22400891"); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/message/TransactionsMessage.java b/ethereumj-core/src/main/java/org/ethereum/net/message/TransactionsMessage.java index 1213f33f..a66cb4f7 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/message/TransactionsMessage.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/message/TransactionsMessage.java @@ -7,6 +7,8 @@ import org.ethereum.net.Command; import org.ethereum.util.RLP; import org.ethereum.util.RLPItem; import org.ethereum.util.RLPList; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -19,6 +21,8 @@ import java.util.List; * Created on: 06/04/14 14:56 */ public class TransactionsMessage extends Message { + + private Logger logger = LoggerFactory.getLogger("wire"); private List transactions = new ArrayList(); @@ -36,7 +40,7 @@ public class TransactionsMessage extends Message { try { baos.write(txPayload); } catch (IOException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } } diff --git a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/EthereumPeerTasterHandler.java b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/EthereumPeerTasterHandler.java index 7fae1b32..8ae22492 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/EthereumPeerTasterHandler.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/EthereumPeerTasterHandler.java @@ -161,7 +161,7 @@ public class EthereumPeerTasterHandler extends ChannelInboundHandlerAdapter { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { this.tearDown = true; logger.info("Lost connection to the server"); - cause.printStackTrace(); + logger.error(cause.getMessage(), cause); timer.cancel(); timer.purge(); timer = null; diff --git a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerTaster.java b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerTaster.java index 71d97e87..83a1c65b 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerTaster.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/peerdiscovery/PeerTaster.java @@ -56,7 +56,7 @@ public class PeerTaster { try { workerGroup.shutdownGracefully().sync(); } catch (InterruptedException e) { - e.printStackTrace(); + logger.error(e.getMessage(), e); } } } diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java index 3fca7dc6..2958b382 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java @@ -21,7 +21,8 @@ import java.util.Stack; public class Program { private Logger logger = LoggerFactory.getLogger("VM"); - private Logger gasLogger = null; + private Logger gasLogger = LoggerFactory.getLogger("gas"); + private int invokeHash; private ProgramListener listener; Stack stack = new Stack(); @@ -39,8 +40,7 @@ public class Program { public Program(byte[] ops, ProgramInvoke invokeData) { - gasLogger = LoggerFactory.getLogger("gas - " + invokeData.hashCode()); - + this.invokeHash = invokeData.hashCode(); result.setRepository(invokeData.getRepository()); if (ops == null) throw new RuntimeException("program can not run with ops: null"); @@ -408,7 +408,7 @@ public class Program { } public void spendGas(int gasValue, String cause) { - gasLogger.info("Spent for cause: [ {} ], gas: [ {} ]", cause, gasValue); + gasLogger.info("[{}] Spent for cause: [ {} ], gas: [ {} ]", invokeHash, cause, gasValue); long afterSpend = invokeData.getGas().longValue() - gasValue - result.getGasUsed(); if (afterSpend < 0) @@ -417,7 +417,7 @@ public class Program { } public void refundGas(int gasValue, String cause) { - gasLogger.info("Refund for cause: [ {} ], gas: [ {} ]", cause, gasValue); + gasLogger.info("[{}] Refund for cause: [ {} ], gas: [ {} ]", invokeHash, cause, gasValue); result.refundGas(gasValue); } diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java index 7f4d37e5..23839741 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java @@ -1,6 +1,7 @@ package org.ethereum.vm; import org.ethereum.crypto.HashUtil; +import org.ethereum.vm.Program.OutOfGasException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -556,8 +557,10 @@ public class VM { program.fullTrace(); } catch (RuntimeException e) { program.stop(); - logger.info("VM halted: ", e); - + if(e instanceof OutOfGasException) + logger.warn("OutOfGasException occurred", e); + else + logger.error("VM halted", e); throw e; } } diff --git a/ethereumj-core/src/main/resources/log4j.properties b/ethereumj-core/src/main/resources/log4j.properties index a49c861a..8dff5800 100644 --- a/ethereumj-core/src/main/resources/log4j.properties +++ b/ethereumj-core/src/main/resources/log4j.properties @@ -14,16 +14,18 @@ log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolic log4j.appender.file.RollingPolicy.FileNamePattern=./logs/ethereum_%d{yyyy-MM-dd}_h%d{HH}.log # filter noisy classes -log4j.logger.org.ethereum.net = INFO -log4j.logger.peerdiscovery = FATAL -log4j.logger.java.nio = WARN -log4j.logger.io.netty = FATAL -log4j.logger.org.ethereum.core = FATAL -log4j.logger.wire = DEBUG -log4j.logger.VM = DEBUG +log4j.logger.org.ethereum.core = ERROR +log4j.logger.org.ethereum.net = ERROR +log4j.logger.org.ethereum.db = ERROR +log4j.logger.peerdiscovery = ERROR +log4j.logger.java.nio = ERROR +log4j.logger.io.netty = ERROR +log4j.logger.wire = ERROR +log4j.logger.VM = ERROR log4j.logger.main = INFO -log4j.logger.state = DEBUG +log4j.logger.state = ERROR +log4j.logger.repository = ERROR log4j.logger.blockchain = DEBUG -log4j.logger.ui = DEBUG -log4j.logger.gas = DEBUG +log4j.logger.ui = ERROR +log4j.logger.gas = ERROR diff --git a/ethereumj-core/src/main/resources/system.properties b/ethereumj-core/src/main/resources/system.properties index a115d5fb..ed9e6998 100644 --- a/ethereumj-core/src/main/resources/system.properties +++ b/ethereumj-core/src/main/resources/system.properties @@ -5,19 +5,19 @@ server.acceptConnections = false # one default access point to start # discover the network e.g. ip: [54.201.28.117] port: [30303] # Peer Server Zero: peer discovery -peer.discovery.ip = 54.72.69.180 -peer.discovery.port = 30303 +#peer.discovery.ip = 54.72.69.180 +#peer.discovery.port = 30303 # Peer Server One: peer discovery -#peer.discovery.ip = 54.204.10.41 -#peer.discovery.port = 30303 +peer.discovery.ip = 54.204.10.41 +peer.discovery.port = 30303 # active peer ip and port # that is the peer through # we get the chain: [54.201.28.117] port: [30303] # ZeroGox -#peer.active.ip = 54.204.10.41 -#peer.active.port = 30303 +peer.active.ip = 54.204.10.41 +peer.active.port = 30303 # Some dude in Canada #peer.active.ip = 131.104.247.135 @@ -28,8 +28,8 @@ peer.discovery.port = 30303 #peer.active.port = 30303 # PoC-5 testnet -peer.active.ip = 54.72.69.180 -peer.active.port = 30303 +#peer.active.ip = 54.72.69.180 +#peer.active.port = 30303 #peer.active.ip = 151.64.223.120 #peer.active.port = 30304 diff --git a/ethereumj-core/src/test/resources/log4j.properties b/ethereumj-core/src/test/resources/log4j.properties index 2d237af3..d7ec7675 100644 --- a/ethereumj-core/src/test/resources/log4j.properties +++ b/ethereumj-core/src/test/resources/log4j.properties @@ -8,16 +8,18 @@ log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern= %d{HH:mm:ss} [%c{1}] %m%n # filter noisy classes -log4j.logger.org.ethereum.net = FATAL -log4j.logger.peerdiscovery = FATAL -log4j.logger.java.nio = FATAL -log4j.logger.io.netty = FATAL -log4j.logger.org.ethereum.core = FATAL -log4j.logger.wire = FATAL -log4j.logger.VM = DEBUG -log4j.logger.main = DEBUG -log4j.logger.state = FATAL -log4j.logger.blockchain = FATAL -log4j.logger.ui = FATAL -log4j.logger.gas = DEBUG +log4j.logger.org.ethereum.core = ERROR +log4j.logger.org.ethereum.net = ERROR +log4j.logger.org.ethereum.db = ERROR +log4j.logger.peerdiscovery = ERROR +log4j.logger.java.nio = ERROR +log4j.logger.io.netty = ERROR +log4j.logger.wire = ERROR +log4j.logger.VM = OFF +log4j.logger.main = INFO +log4j.logger.state = ERROR +log4j.logger.repository = ERROR +log4j.logger.blockchain = DEBUG +log4j.logger.ui = ERROR +log4j.logger.gas = ERROR diff --git a/ethereumj-core/src/test/resources/system.properties b/ethereumj-core/src/test/resources/system.properties index 11eb10a8..64f774cd 100644 --- a/ethereumj-core/src/test/resources/system.properties +++ b/ethereumj-core/src/test/resources/system.properties @@ -5,23 +5,23 @@ server.acceptConnections = false # one default access point to start # discover the network e.g. ip: [54.201.28.117] port: [30303] # Peer Server Zero: peer discovery -peer.discovery.ip = 54.201.28.117 -peer.discovery.port = 30303 +#peer.discovery.ip = 54.201.28.117 +#peer.discovery.port = 30303 # Peer Server One: peer discovery -#peer.discovery.ip = 54.204.10.41 -#peer.discovery.port = 30303 +peer.discovery.ip = 54.204.10.41 +peer.discovery.port = 30303 # active peer ip and port # that is the peer through # we get the chain: [54.201.28.117] port: [30303] # ZeroGox -#peer.active.ip = 54.204.10.41 -#peer.active.port = 30303 +peer.active.ip = 54.204.10.41 +peer.active.port = 30303 # RomanJ -peer.active.ip = 54.211.14.10 -peer.active.port = 50505 +#peer.active.ip = 54.211.14.10 +#peer.active.port = 50505 # PoC-5 testnet #peer.active.ip = 54.72.69.180