Skip to content

Commit

Permalink
Fix transaction notification in Private Miner sample
Browse files Browse the repository at this point in the history
  • Loading branch information
cupuyc committed Feb 21, 2017
1 parent d32c63f commit dc82dae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
52 changes: 35 additions & 17 deletions src/main/java/com/ethercamp/harmony/service/WalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import lombok.AllArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.ethereum.config.SystemProperties;
import org.ethereum.core.*;
import org.ethereum.crypto.ECKey;
import org.ethereum.db.ByteArrayWrapper;
Expand Down Expand Up @@ -102,6 +103,9 @@ public class WalletService {

EmbeddedDatabase wordsDatabase;

@Autowired
SystemProperties config;

/**
* key - hex address in lower case
* value - address user friendly name
Expand All @@ -111,6 +115,8 @@ public class WalletService {
final Map<String, TransactionInfo> pendingSendTransactions = new ConcurrentHashMap<>();
final Map<String, TransactionInfo> pendingReceiveTransactions = new ConcurrentHashMap<>();

private boolean subscribedForEvents;

@PostConstruct
public void init() {
addresses.clear();
Expand All @@ -122,24 +128,36 @@ public void init() {
fileSystemWalletStore.fromStore().stream()
.forEach(a -> addresses.put(a.address, a.name));

ethereum.addListener(new EthereumListenerAdapter() {
@Override
public void onSyncDone(SyncState state) {
if (state == SyncState.UNSECURE) {
ethereum.addListener(new EthereumListenerAdapter() {
@Override
public void onPendingTransactionsReceived(List<Transaction> list) {
handlePendingTransactionsReceived(list);
}

@Override
public void onBlock(BlockSummary blockSummary) {
handleBlock(blockSummary);
}
});
// workaround issue in ethereumJ-core, where single miner could never got sync done event
if (config.minerStart()) {
subscribeOnce();
} else {
ethereum.addListener(new EthereumListenerAdapter() {
@Override
public void onSyncDone(SyncState state) {
if (state == SyncState.UNSECURE || state == SyncState.COMPLETE) {
subscribeOnce();
}
}
}
});
});
}
}

private void subscribeOnce() {
if (!subscribedForEvents) {
subscribedForEvents = true;
ethereum.addListener(new EthereumListenerAdapter() {
@Override
public void onPendingTransactionsReceived(List<Transaction> list) {
handlePendingTransactionsReceived(list);
}

@Override
public void onBlock(BlockSummary blockSummary) {
handleBlock(blockSummary);
}
});
}
}

public void handleBlock(BlockSummary blockSummary) {
Expand Down
8 changes: 2 additions & 6 deletions src/main/resources/private.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ mine {
# when 'sync.enabled' is true the mining starts when the sync is complete
# else the mining will start immediately, taking the best block from database
# (or genesis if no blocks exist yet)
start = false
start = true

# Coinbase address for storing block mining reward
coinbase = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"
Expand All @@ -68,16 +68,12 @@ mine {

# number of CPU threads the miner will mine on
# 0 disables CPU mining
cpuMineThreads = 2
cpuMineThreads = 1

# there two options for CPU mining 'light' and 'full'
# 'light' requires only 16M of RAM but is much slower
# 'full' requires 1G of RAM and possibly ~7min for the DataSet generation
# but is much faster during mining
fullDataSet = true

# start mining with specific nonce (might be usefult for testing)
# null for random start nonce
startNonce = null
}

0 comments on commit dc82dae

Please sign in to comment.