Fix pending tx functionality + expose the correct interface

This commit is contained in:
Roman Mandeleil 2015-02-02 22:02:57 +02:00
parent 163036c5ea
commit 277ca6af7a
6 changed files with 48 additions and 30 deletions

View File

@ -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<Transaction> pendingTransactions = Collections.synchronizedSet(new HashSet<Transaction>());
@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<Transaction> transactions) {
logger.info("Pending transaction list added: size: [{}]", transactions.size());
if (listener != null)
listener.onPendingTransactionsReceived(transactions);
pendingTransactions.addAll(transactions);
}
public void clearPendingTransactions(List<Transaction> receivedTransactions) {
for (Transaction tx : receivedTransactions) {
logger.info("Clear transaction, hash: [{}]", Hex.toHexString(tx.getHash()));
pendingTransactions.remove(tx);
}
}
public Set<Transaction> getPendingTransactions() {
return pendingTransactions;
}
public void setRepository(Repository repository) {
this.repository = repository;
}

View File

@ -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<Chain> getAltChains();
public List<Block> getGarbage();
public Set<Transaction> getPendingTransactions();
public void addPendingTransactions(Set<Transaction> transactions);
public void clearPendingTransactions(List<Transaction> receivedTransactions);
}

View File

@ -133,4 +133,6 @@ public interface Ethereum {
public ChannelManager getChannelManager();
public Set<Transaction> getPendingTransactions();
}

View File

@ -243,4 +243,10 @@ public class EthereumImpl implements Ethereum {
public ChannelManager getChannelManager() {
return channelManager;
}
@Override
public Set<Transaction> getPendingTransactions() {
return getBlockchain().getPendingTransactions();
}
}

View File

@ -71,7 +71,6 @@ public class WorldManager {
@Autowired
private AdminInfo adminInfo;
private final Set<Transaction> pendingTransactions = Collections.synchronizedSet(new HashSet<Transaction>());
@Autowired
private EthereumListener listener;
@ -101,22 +100,6 @@ public class WorldManager {
peerDiscovery.stop();
}
public void addPendingTransactions(Set<Transaction> transactions) {
logger.info("Pending transaction list added: size: [{}]", transactions.size());
if (listener != null)
listener.onPendingTransactionsReceived(transactions);
pendingTransactions.addAll(transactions);
}
public void clearPendingTransactions(List<Transaction> 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<Transaction> 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");

View File

@ -168,7 +168,8 @@ public class EthHandler extends SimpleChannelInboundHandler<EthMessage> {
private void processTransactions(TransactionsMessage msg) {
Set<Transaction> 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<EthMessage> {
private void sendPendingTransactions() {
Set<Transaction> pendingTxs =
worldManager.getPendingTransactions();
worldManager.getBlockchain()
.getPendingTransactions();
TransactionsMessage msg = new TransactionsMessage(pendingTxs);
msgQueue.sendMessage(msg);
}