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 0d32a780..af0076f0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java @@ -516,10 +516,7 @@ public class BlockchainImpl implements Blockchain { // CREATE NEW CONTRACT ADDRESS AND ADD TX VALUE if(isContractCreation) { - if (isValueTx) // adding to balance also creates the account - trackTx.addBalance(receiverAddress, new BigInteger(1, tx.getValue())); - else - trackTx.createAccount(receiverAddress); + trackTx.addBalance(receiverAddress, BigInteger.ZERO); // also creates account if(stateLogger.isDebugEnabled()) stateLogger.debug("new contract created address={}", diff --git a/ethereumj-core/src/main/java/org/ethereum/mine/Miner.java b/ethereumj-core/src/main/java/org/ethereum/mine/Miner.java index 3806ea53..06a5fe08 100644 --- a/ethereumj-core/src/main/java/org/ethereum/mine/Miner.java +++ b/ethereumj-core/src/main/java/org/ethereum/mine/Miner.java @@ -85,6 +85,10 @@ public class Miner { while(ByteUtil.increment(testNonce) && !stop) { + if (testNonce[31] == 0 && testNonce[30] == 0){ + System.out.println("mining: " + new BigInteger(1, testNonce)); + } + if (testNonce[31] == 0) sleep(); concat = Arrays.concatenate(hash, testNonce); 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 5c7666bd..42b4f8a7 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/BlockQueue.java @@ -148,6 +148,13 @@ public class BlockQueue { } } + public void returnHashes(List hashes){ + + ListIterator iterator = hashes.listIterator(hashes.size()); + while(iterator.hasPrevious()) + blockHashQueue.addLast((byte[])iterator.previous()); + } + public void addNewBlockHash(byte[] hash){ blockHashQueue.addFirst(hash); } @@ -158,6 +165,7 @@ public class BlockQueue { * @return A list of hashes for which blocks need to be retrieved. */ public List getHashes() { + List hashes = new ArrayList<>(); while (!blockHashQueue.isEmpty() && hashes.size() < CONFIG.maxBlocksAsk()) { hashes.add(blockHashQueue.removeLast()); @@ -205,6 +213,10 @@ public class BlockQueue { return blockReceivedQueue.size(); } + public boolean isHashesEmpty(){ + return blockHashQueue.size() == 0; + } + public void clear(){ this.blockHashQueue.clear(); this.blockReceivedQueue.clear(); 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 b5ad9a75..6cac10e1 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 @@ -67,6 +67,7 @@ public class EthHandler extends SimpleChannelInboundHandler { @Autowired private WorldManager worldManager; + private List sentHashes; public EthHandler(){ this.peerDiscoveryMode = false; @@ -232,7 +233,7 @@ public class EthHandler extends SimpleChannelInboundHandler { // or peer doesn't have the best hash anymore if (receivedHashes.isEmpty() || !this.peerId.equals(hashRetrievalLock)) { - startGetBlockTimer(); // start getting blocks from hash queue + sendGetBlocks(); // start getting blocks from hash queue return; } @@ -247,7 +248,7 @@ public class EthHandler extends SimpleChannelInboundHandler { logger.trace("Catch up with the hashes until: {[]}", foundHash); // if known hash is found, ignore the rest - startGetBlockTimer(); // start getting blocks from hash queue + sendGetBlocks(); // start getting blocks from hash queue return; } } @@ -260,18 +261,26 @@ public class EthHandler extends SimpleChannelInboundHandler { List blockList = blocksMessage.getBlocks(); - if (blockList.isEmpty()) return; - blockchain.getQueue().addBlocks(blockList); - blockchain.getQueue().logHashQueueSize(); + // check if you got less blocks than you asked + if (blockList.size() < sentHashes.size()){ + for (int i = 0; i < blockList.size(); ++i) + sentHashes.remove(0); - // If we got less blocks then we could get, - // it the correct indication that we are in sync we - // the chain from here there will be NEW_BLOCK only - // message expectation - if (blockList.size() < CONFIG.maxBlocksAsk()) { + logger.info("Got less blocks: [{}], return [{}] hashes to the queue", + blockList.size(), sentHashes.size()); + blockchain.getQueue().returnHashes(sentHashes); + } + + if (blockchain.getQueue().isHashesEmpty()) { logger.info(" The peer sync process fully complete"); syncStatus = SyncSatus.SYNC_DONE; - stopGetBlocksTimer(); + blockchain.getQueue().addBlocks(blockList); + blockchain.getQueue().logHashQueueSize(); + } else{ + if (blockList.isEmpty()) return; + blockchain.getQueue().addBlocks(blockList); + blockchain.getQueue().logHashQueueSize(); + sendGetBlocks(); } } @@ -350,6 +359,11 @@ public class EthHandler extends SimpleChannelInboundHandler { msgQueue.sendMessage(msg); } + public void sendNewBlock(Block block){ + NewBlockMessage msg = new NewBlockMessage(block, block.getDifficulty()); + msgQueue.sendMessage(msg); + } + private void sendGetTransactions() { msgQueue.sendMessage(GET_TRANSACTIONS_MESSAGE); } @@ -366,9 +380,12 @@ public class EthHandler extends SimpleChannelInboundHandler { if (queue.size() > CONFIG.maxBlocksQueued()) return; // retrieve list of block hashes from queue + // save them locally in case the remote peer + // will return less blocks than requested. List hashes = queue.getHashes(); + this.sentHashes = hashes; + if (hashes.isEmpty()) { - stopGetBlocksTimer(); return; } @@ -413,20 +430,20 @@ public class EthHandler extends SimpleChannelInboundHandler { }, 2000, 10000); } - public void startGetBlockTimer() { - syncStatus = SyncSatus.BLOCK_RETRIEVING; - getBlocksTimer = new Timer("GetBlocksTimer"); - getBlocksTimer.scheduleAtFixedRate(new TimerTask() { - public void run() { - BlockQueue blockQueue = blockchain.getQueue(); - if (blockQueue.size() > CONFIG.maxBlocksQueued()) { - logger.trace("Blocks queue too big temporary postpone blocks request"); - return; - } - sendGetBlocks(); - } - }, 300, 10); - } +// public void startGetBlockTimer() { +// syncStatus = SyncSatus.BLOCK_RETRIEVING; +// getBlocksTimer = new Timer("GetBlocksTimer"); +// getBlocksTimer.scheduleAtFixedRate(new TimerTask() { +// public void run() { +// BlockQueue blockQueue = blockchain.getQueue(); +// if (blockQueue.size() > CONFIG.maxBlocksQueued()) { +// logger.trace("Blocks queue too big temporary postpone blocks request"); +// return; +// } +// sendGetBlocks(); +// } +// }, 300, 10); +// } private void stopGetBlocksTimer() { getBlocksTimer.cancel(); diff --git a/ethereumj-core/src/main/java/org/ethereum/net/server/Channel.java b/ethereumj-core/src/main/java/org/ethereum/net/server/Channel.java index a3000569..3eefc757 100644 --- a/ethereumj-core/src/main/java/org/ethereum/net/server/Channel.java +++ b/ethereumj-core/src/main/java/org/ethereum/net/server/Channel.java @@ -61,10 +61,10 @@ public class Channel { ethHandler.sendTransaction(tx); } - public void sendBlock(Block block){ + public void sendNewBlock(Block block){ // 1. check by best block send or not to send - // ethHandler.sendBlock(block); + ethHandler.sendNewBlock(block); } 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 405a06d4..3c71d477 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 @@ -45,8 +45,11 @@ public class ChannelManager { } - public void sendBlock(){ + public void sendNewBlock(Block block){ // 1. Go over all channels and send the block + for (Channel channel : channels){ + channel.sendNewBlock(block); + } } public void addChannel(Channel channel){