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 07786703..e30df9c5 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java @@ -26,9 +26,7 @@ import java.io.IOException; import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; import static org.ethereum.config.SystemProperties.CONFIG; import static org.ethereum.core.Denomination.SZABO; @@ -74,6 +72,8 @@ public class BlockchainImpl implements Blockchain { // to avoid using minGasPrice=0 from Genesis for the wallet private static final long INITIAL_MIN_GAS_PRICE = 10 * SZABO.longValue(); + private final Set pendingTransactions = Collections.synchronizedSet(new HashSet()); + @Autowired private Repository repository; private Repository track; @@ -228,6 +228,8 @@ public class BlockchainImpl implements Blockchain { // Remove all wallet transactions as they already approved by the net wallet.removeTransactions(block.getTransactionsList()); + // Clear pending transaction from the mem + clearPendingTransactions(block.getTransactionsList()); listener.trace(String.format("Block chain size: [ %d ]", this.getSize())); listener.onBlock(block); @@ -518,6 +520,27 @@ public class BlockchainImpl implements Blockchain { } + public void addPendingTransactions(Set transactions) { + logger.info("Pending transaction list added: size: [{}]", transactions.size()); + + if (listener != null) + listener.onPendingTransactionsReceived(transactions); + pendingTransactions.addAll(transactions); + } + + public void clearPendingTransactions(List receivedTransactions) { + + for (Transaction tx : receivedTransactions) { + logger.info("Clear transaction, hash: [{}]", Hex.toHexString(tx.getHash())); + pendingTransactions.remove(tx); + } + } + + public Set getPendingTransactions() { + return pendingTransactions; + } + + public void setRepository(Repository repository) { this.repository = repository; } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java b/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java index 2b0df093..4acdb198 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Blockchain.java @@ -1,14 +1,12 @@ package org.ethereum.facade; -import org.ethereum.core.Block; -import org.ethereum.core.Chain; -import org.ethereum.core.Genesis; -import org.ethereum.core.TransactionReceipt; +import org.ethereum.core.*; import org.ethereum.net.BlockQueue; import java.math.BigInteger; import java.util.List; +import java.util.Set; public interface Blockchain { @@ -53,4 +51,10 @@ public interface Blockchain { public List getAltChains(); public List getGarbage(); + + public Set getPendingTransactions(); + public void addPendingTransactions(Set transactions); + public void clearPendingTransactions(List receivedTransactions); + + } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java index 859ec47a..e7712e68 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Ethereum.java @@ -133,4 +133,6 @@ public interface Ethereum { public ChannelManager getChannelManager(); + public Set getPendingTransactions(); + } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java index 9ddac091..6d87e40f 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java @@ -243,4 +243,10 @@ public class EthereumImpl implements Ethereum { public ChannelManager getChannelManager() { return channelManager; } + + + @Override + public Set getPendingTransactions() { + return getBlockchain().getPendingTransactions(); + } } 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 40c1a524..4a6551da 100644 --- a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java @@ -71,7 +71,6 @@ public class WorldManager { @Autowired private AdminInfo adminInfo; - private final Set pendingTransactions = Collections.synchronizedSet(new HashSet()); @Autowired private EthereumListener listener; @@ -101,22 +100,6 @@ public class WorldManager { peerDiscovery.stop(); } - public void addPendingTransactions(Set transactions) { - logger.info("Pending transaction list added: size: [{}]", transactions.size()); - - if (listener != null) - listener.onPendingTransactionsReceived(transactions); - pendingTransactions.addAll(transactions); - } - - public void clearPendingTransactions(List receivedTransactions) { - - for (Transaction tx : receivedTransactions) { - logger.info("Clear transaction, hash: [{}]", Hex.toHexString(tx.getHash())); - pendingTransactions.remove(tx); - } - } - public ChannelManager getChannelManager() { return channelManager; } @@ -153,14 +136,11 @@ public class WorldManager { return activePeer; } - public Set getPendingTransactions() { - return pendingTransactions; - } public boolean isBlockchainLoading() { return blockchain.getQueue().size() > 2; } - + public void loadBlockchain() { Block bestBlock = blockStore.getBestBlock(); @@ -176,6 +156,7 @@ public class WorldManager { blockchain.setBestBlock(Genesis.getInstance()); blockchain.setTotalDifficulty(BigInteger.ZERO); + listener.onBlock(Genesis.getInstance()); repository.dumpState(Genesis.getInstance(), 0, 0, null); logger.info("Genesis block loaded"); 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 03c69133..8b80fd0d 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 @@ -168,7 +168,8 @@ public class EthHandler extends SimpleChannelInboundHandler { private void processTransactions(TransactionsMessage msg) { Set txSet = msg.getTransactions(); - worldManager.addPendingTransactions(txSet); + worldManager.getBlockchain(). + addPendingTransactions(txSet); for (Transaction tx : txSet) { worldManager.getWallet().addTransaction(tx); @@ -421,7 +422,8 @@ public class EthHandler extends SimpleChannelInboundHandler { private void sendPendingTransactions() { Set pendingTxs = - worldManager.getPendingTransactions(); + worldManager.getBlockchain() + .getPendingTransactions(); TransactionsMessage msg = new TransactionsMessage(pendingTxs); msgQueue.sendMessage(msg); }