From 8089f6e57cdbf1e9131f20079f8fa791f9f0f20d Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Mon, 29 Dec 2014 10:34:50 +0100 Subject: [PATCH 1/3] Polish whitespace and imports - Replace leading tabs with spaces - Add space before opening curly brace - Optimize imports using shared .idea/codeStyleSettings.xml - Do not align assignments on equals sign - Remove unnecessary extra newlines --- ethereumj-core/build.gradle | 2 +- .../antlr4/org/ethereum/serpent/Serpent.g4 | 4 +- .../org/ethereum/core/BlockchainImpl.java | 18 +++-- .../java/org/ethereum/net/BlockQueue.java | 22 +++---- .../java/org/ethereum/net/MessageQueue.java | 2 +- .../java/org/ethereum/net/eth/EthHandler.java | 9 +-- .../ethereum/net/message/StaticMessages.java | 65 ++++++++++--------- .../java/org/ethereum/net/p2p/P2pHandler.java | 19 ++++-- .../ethereum/net/server/ChannelManager.java | 4 +- 9 files changed, 77 insertions(+), 68 deletions(-) diff --git a/ethereumj-core/build.gradle b/ethereumj-core/build.gradle index 49d39de0..040f2b3d 100644 --- a/ethereumj-core/build.gradle +++ b/ethereumj-core/build.gradle @@ -70,7 +70,7 @@ javadoc { options.author = true options.header = project.name options.addStringOption('Xdoclint:all,-missing', '-quiet') - options.encoding ="UTF-8" + options.encoding = "UTF-8" options.links( "http://docs.oracle.com/javase/8/docs/api/", "http://netty.io/4.0/api/" diff --git a/ethereumj-core/src/main/antlr4/org/ethereum/serpent/Serpent.g4 b/ethereumj-core/src/main/antlr4/org/ethereum/serpent/Serpent.g4 index b7d25525..f0312f97 100644 --- a/ethereumj-core/src/main/antlr4/org/ethereum/serpent/Serpent.g4 +++ b/ethereumj-core/src/main/antlr4/org/ethereum/serpent/Serpent.g4 @@ -8,8 +8,8 @@ INDENT, DEDENT } @lexer::header { import com.yuvalshavit.antlr4.DenterHelper; } - - + + @lexer::members { private final DenterHelper denter = new DenterHelper(NL, SerpentParser.INDENT, SerpentParser.DEDENT) { @Override diff --git a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java index 17496ef9..1f753772 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java @@ -9,9 +9,12 @@ import org.ethereum.net.BlockQueue; import org.ethereum.net.server.ChannelManager; import org.ethereum.util.AdvancedDeviceUtils; import org.ethereum.vm.ProgramInvokeFactory; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.spongycastle.util.encoders.Hex; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.FileSystemUtils; @@ -20,7 +23,9 @@ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; + import java.math.BigInteger; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -162,14 +167,15 @@ public class BlockchainImpl implements Blockchain { } // cut on the chain got lastBlock + 1 > n - if (block.getNumber() > bestBlock.getNumber() + 1){ + if (block.getNumber() > bestBlock.getNumber() + 1) { channelManager.ethSync(); } + if (!hasParentOnTheChain(block) && block.getNumber() > bestBlock.getNumber()) { - if (!hasParentOnTheChain(block) && block.getNumber() > bestBlock.getNumber()){ + if (1 == 1) + return; // todo: temporary cancel the rollback - if (1==1)return; // todo: temporary cancel the rollback logger.info("*** Blockchain will rollback and resynchronise now "); long rollbackIdx = bestBlock.getNumber() - 30; @@ -233,7 +239,6 @@ public class BlockchainImpl implements Blockchain { logger.info("Sync done"); syncDoneCalled = true; - listener.onSyncDone(); } } @@ -416,9 +421,8 @@ public class BlockchainImpl implements Blockchain { if (logger.isDebugEnabled()) logger.debug("block added to the blockChain: index: [{}]", block.getNumber()); if (block.getNumber() % 100 == 0) - logger.info("*** Last block added [ #{} ]", block.getNumber()); - - } + logger.info("*** Last block added [ #{} ]", block.getNumber()); + } public boolean hasParentOnTheChain(Block block) { diff --git a/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java b/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java index 970990f4..1a0050ab 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java @@ -65,18 +65,18 @@ public class BlockQueue { @Autowired Blockchain blockchain; - public BlockQueue() { - timer.scheduleAtFixedRate(new TimerTask() { - public void run() { - nudgeQueue(); - } - }, 10, 10); - } + public BlockQueue() { + timer.scheduleAtFixedRate(new TimerTask() { + public void run() { + nudgeQueue(); + } + }, 10, 10); + } - /** - * Processing the queue adding blocks to the chain. - */ - private void nudgeQueue() { + /** + * Processing the queue adding blocks to the chain. + */ + private void nudgeQueue() { try { if (blockReceivedQueue.isEmpty()) return; diff --git a/ethereumj-core/src/main/java/org/ethereum/net/MessageQueue.java b/ethereumj-core/src/main/java/org/ethereum/net/MessageQueue.java index a2e20b6e..a017b851 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/MessageQueue.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/MessageQueue.java @@ -77,7 +77,7 @@ public class MessageQueue { ctx.close(); } - public void receivedMessage(Message msg) throws InterruptedException { + public void receivedMessage(Message msg) throws InterruptedException { worldManager.getListener().trace("[Recv: " + msg + "]"); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/eth/EthHandler.java b/ethereumj-core/src/main/java/org/ethereum/net/eth/EthHandler.java index 64ce9dd3..e7ebe221 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/eth/EthHandler.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/eth/EthHandler.java @@ -164,8 +164,6 @@ public class EthHandler extends SimpleChannelInboundHandler { } } - - private void processTransactions(TransactionsMessage msg) { Set txSet = msg.getTransactions(); @@ -249,9 +247,6 @@ public class EthHandler extends SimpleChannelInboundHandler { private void processBlockHashes(BlockHashesMessage blockHashesMessage) { List receivedHashes = blockHashesMessage.getBlockHashes(); - -// receivedHashes.forEach(hash -> System.out.println(Hex.toHexString(hash))); - BlockQueue chainQueue = blockchain.getQueue(); // result is empty, peer has no more hashes @@ -285,7 +280,7 @@ public class EthHandler extends SimpleChannelInboundHandler { List blockList = blocksMessage.getBlocks(); - if (!blockList.isEmpty()){ + if (!blockList.isEmpty()) { Block block = blockList.get(blockList.size()-1); if (block.getNumber() > lastBlock.getNumber()) lastBlock = blockList.get(blockList.size()-1); @@ -513,7 +508,7 @@ public class EthHandler extends SimpleChannelInboundHandler { blockchain.getQueue().addHash(hash); } - public void doSync(){ + public void doSync() { logger.info("Sync force activated, block: {}", lastBlock); syncStatus = SyncSatus.HASH_RETRIEVING; setBestHash(lastBlock.getHash()); 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 14036091..60f626e8 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 @@ -5,7 +5,12 @@ import org.ethereum.crypto.HashUtil; import org.ethereum.net.client.Capability; import org.ethereum.net.eth.EthHandler; import org.ethereum.net.eth.GetTransactionsMessage; -import org.ethereum.net.p2p.*; +import org.ethereum.net.p2p.DisconnectMessage; +import org.ethereum.net.p2p.GetPeersMessage; +import org.ethereum.net.p2p.HelloMessage; +import org.ethereum.net.p2p.P2pHandler; +import org.ethereum.net.p2p.PingMessage; +import org.ethereum.net.p2p.PongMessage; import org.ethereum.net.shh.ShhHandler; import org.spongycastle.util.encoders.Hex; @@ -22,38 +27,38 @@ import java.util.List; */ public class StaticMessages { - public static final String PEER_ID = Hex.toHexString(HashUtil.randomPeerId()); - - public final static PingMessage PING_MESSAGE = new PingMessage(); - public final static PongMessage PONG_MESSAGE = new PongMessage(); - public final static HelloMessage HELLO_MESSAGE = generateHelloMessage(); - public final static GetPeersMessage GET_PEERS_MESSAGE = new GetPeersMessage(); - public final static GetTransactionsMessage GET_TRANSACTIONS_MESSAGE = new GetTransactionsMessage(); - public final static DisconnectMessage DISCONNECT_MESSAGE = new DisconnectMessage(ReasonCode.REQUESTED); + public static final String PEER_ID = Hex.toHexString(HashUtil.randomPeerId()); - public static final byte[] SYNC_TOKEN = Hex.decode("22400891"); + public final static PingMessage PING_MESSAGE = new PingMessage(); + public final static PongMessage PONG_MESSAGE = new PongMessage(); + public final static HelloMessage HELLO_MESSAGE = generateHelloMessage(); + public final static GetPeersMessage GET_PEERS_MESSAGE = new GetPeersMessage(); + public final static GetTransactionsMessage GET_TRANSACTIONS_MESSAGE = new GetTransactionsMessage(); + public final static DisconnectMessage DISCONNECT_MESSAGE = new DisconnectMessage(ReasonCode.REQUESTED); - private static HelloMessage generateHelloMessage() { - String helloAnnouncement = buildHelloAnnouncement(); - byte p2pVersion = P2pHandler.VERSION; - List capabilities = Arrays.asList( - new Capability(Capability.ETH, EthHandler.VERSION), - new Capability(Capability.SHH, ShhHandler.VERSION)); - int listenPort = SystemProperties.CONFIG.listenPort(); + public static final byte[] SYNC_TOKEN = Hex.decode("22400891"); - return new HelloMessage(p2pVersion, helloAnnouncement, - capabilities, listenPort, PEER_ID); - } + private static HelloMessage generateHelloMessage() { + String helloAnnouncement = buildHelloAnnouncement(); + byte p2pVersion = P2pHandler.VERSION; + List capabilities = Arrays.asList( + new Capability(Capability.ETH, EthHandler.VERSION), + new Capability(Capability.SHH, ShhHandler.VERSION)); + int listenPort = SystemProperties.CONFIG.listenPort(); - private static String buildHelloAnnouncement() { - String version = SystemProperties.CONFIG.projectVersion(); - String system = System.getProperty("os.name"); - if (system.contains(" ")) - system = system.substring(0, system.indexOf(" ")); - if (System.getProperty("java.vm.vendor").contains("Android")) - system = "Android"; - String phrase = SystemProperties.CONFIG.helloPhrase(); + return new HelloMessage(p2pVersion, helloAnnouncement, + capabilities, listenPort, PEER_ID); + } - return String.format("Ethereum(J)/v%s/%s/%s/Java", version, phrase, system); - } + private static String buildHelloAnnouncement() { + String version = SystemProperties.CONFIG.projectVersion(); + String system = System.getProperty("os.name"); + if (system.contains(" ")) + system = system.substring(0, system.indexOf(" ")); + if (System.getProperty("java.vm.vendor").contains("Android")) + system = "Android"; + String phrase = SystemProperties.CONFIG.helloPhrase(); + + return String.format("Ethereum(J)/v%s/%s/%s/Java", version, phrase, system); + } } diff --git a/ethereumj-core/src/main/java/org/ethereum/net/p2p/P2pHandler.java b/ethereumj-core/src/main/java/org/ethereum/net/p2p/P2pHandler.java index 85d97fa3..beb7fbd4 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/p2p/P2pHandler.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/p2p/P2pHandler.java @@ -1,7 +1,5 @@ package org.ethereum.net.p2p; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.SimpleChannelInboundHandler; import org.ethereum.core.Block; import org.ethereum.core.Transaction; import org.ethereum.manager.WorldManager; @@ -16,19 +14,30 @@ import org.ethereum.net.message.StaticMessages; import org.ethereum.net.peerdiscovery.PeerInfo; import org.ethereum.net.shh.ShhHandler; import org.ethereum.net.shh.ShhMessageCodes; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.util.*; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; import static org.ethereum.net.message.StaticMessages.*; - /** * Process the basic protocol messages between every peer on the network. * @@ -277,8 +286,6 @@ public class P2pHandler extends SimpleChannelInboundHandler { return handshakeHelloMessage; } - - private void startTimers() { // sample for pinging in background diff --git a/ethereumj-core/src/main/java/org/ethereum/net/server/ChannelManager.java b/ethereumj-core/src/main/java/org/ethereum/net/server/ChannelManager.java index a3eae9f3..00d34619 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/server/ChannelManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/server/ChannelManager.java @@ -108,9 +108,7 @@ public class ChannelManager { } public void reconnect(){ - for (Channel channel : channels){ - channel.p2pHandler.sendDisconnect(); - } + channels.forEach(c -> c.p2pHandler.sendDisconnect()); } public void ethSync() { From bd2e4bd89eaab67e566de3f6a811bb8aeddc0cdc Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Mon, 29 Dec 2014 10:35:21 +0100 Subject: [PATCH 2/3] Rename Denomination#{FINNY => FINNEY} --- .../src/main/java/org/ethereum/core/Denomination.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Denomination.java b/ethereumj-core/src/main/java/org/ethereum/core/Denomination.java index 184b55ee..c90eb850 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Denomination.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Denomination.java @@ -9,7 +9,7 @@ public enum Denomination { BABBAGE(newBigInt(6)), SHANNON(newBigInt(9)), SZABO(newBigInt(12)), - FINNY(newBigInt(15)), + FINNEY(newBigInt(15)), ETHER(newBigInt(18)), EINSTEIN(newBigInt(21)), DOUGLAS(newBigInt(42)); @@ -42,8 +42,8 @@ public enum Denomination { else if(value.compareTo(ETHER.value()) == 1 || value.compareTo(ETHER.value()) == 0) { return Float.toString(value.divide(ETHER.value()).floatValue()) + " ETHER"; } - else if(value.compareTo(FINNY.value()) == 1 || value.compareTo(FINNY.value()) == 0) { - return Float.toString(value.divide(FINNY.value()).floatValue()) + " FINNY"; + else if(value.compareTo(FINNEY.value()) == 1 || value.compareTo(FINNEY.value()) == 0) { + return Float.toString(value.divide(FINNEY.value()).floatValue()) + " FINNEY"; } else if(value.compareTo(SZABO.value()) == 1 || value.compareTo(SZABO.value()) == 0) { return Float.toString(value.divide(SZABO.value()).floatValue()) + " SZABO"; From 25ff32446fa9f6c2b8e7b2d2724ccd3a67cfe6c7 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Mon, 29 Dec 2014 10:37:57 +0100 Subject: [PATCH 3/3] Remove denomination values absent in white paper A recent revision of the Ethereum white paper removes the denominations labeled 'lovelace', 'babbage' and 'shannon' [1]. This commit reflects the change to the whitepaper by removing the corresponding labels from the Denomination enum (as an aside, note the discrepancy between 'lovelace' in the whitepaper and 'ada' in our implementation). go-ethereum still contains these values, but cpp-ethereum does not. This commit assumes that the whitepaper and cpp implementation represent the current consensus on explicitly named denominations. By the same rationale, the 'douglas' and 'einstein' labels have been removed as well. These values never appeared in the whitepaper or cpp implementations. They were introduced to the go implementation in ethereum/go-ethereum@e7d9bcd, but because the whitepaper and cpp implementation are silent on these values, they are omitted here. [1]: http://git.io/xJJf7g --- .../java/org/ethereum/core/Denomination.java | 24 ++----------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Denomination.java b/ethereumj-core/src/main/java/org/ethereum/core/Denomination.java index c90eb850..a8173b7c 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Denomination.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Denomination.java @@ -5,14 +5,9 @@ import java.math.BigInteger; public enum Denomination { WEI(newBigInt(0)), - ADA(newBigInt(3)), - BABBAGE(newBigInt(6)), - SHANNON(newBigInt(9)), SZABO(newBigInt(12)), FINNEY(newBigInt(15)), - ETHER(newBigInt(18)), - EINSTEIN(newBigInt(21)), - DOUGLAS(newBigInt(42)); + ETHER(newBigInt(18)); private BigInteger amount; @@ -33,13 +28,7 @@ public enum Denomination { } public static String toFriendlyString(BigInteger value) { - if(value.compareTo(DOUGLAS.value()) == 1 || value.compareTo(DOUGLAS.value()) == 0) { - return Float.toString(value.divide(DOUGLAS.value()).floatValue()) + " DOUGLAS"; - } - else if(value.compareTo(EINSTEIN.value()) == 1 || value.compareTo(EINSTEIN.value()) == 0) { - return Float.toString(value.divide(EINSTEIN.value()).floatValue()) + " EINSTEIN"; - } - else if(value.compareTo(ETHER.value()) == 1 || value.compareTo(ETHER.value()) == 0) { + if (value.compareTo(ETHER.value()) == 1 || value.compareTo(ETHER.value()) == 0) { return Float.toString(value.divide(ETHER.value()).floatValue()) + " ETHER"; } else if(value.compareTo(FINNEY.value()) == 1 || value.compareTo(FINNEY.value()) == 0) { @@ -48,15 +37,6 @@ public enum Denomination { else if(value.compareTo(SZABO.value()) == 1 || value.compareTo(SZABO.value()) == 0) { return Float.toString(value.divide(SZABO.value()).floatValue()) + " SZABO"; } - else if(value.compareTo(SHANNON.value()) == 1 || value.compareTo(SHANNON.value()) == 0) { - return Float.toString(value.divide(SHANNON.value()).floatValue()) + " SHANNON"; - } - else if(value.compareTo(BABBAGE.value()) == 1 || value.compareTo(BABBAGE.value()) == 0) { - return Float.toString(value.divide(BABBAGE.value()).floatValue()) + " BABBAGE"; - } - else if(value.compareTo(ADA.value()) == 1 || value.compareTo(ADA.value()) == 0) { - return Float.toString(value.divide(ADA.value()).floatValue()) + " ADA"; - } else return Float.toString(value.divide(WEI.value()).floatValue()) + " WEI"; }