Skip to content

Commit dc82dae

Browse files
committed
Fix transaction notification in Private Miner sample
1 parent d32c63f commit dc82dae

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

src/main/java/com/ethercamp/harmony/service/WalletService.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import lombok.AllArgsConstructor;
2828
import lombok.Value;
2929
import lombok.extern.slf4j.Slf4j;
30+
import org.ethereum.config.SystemProperties;
3031
import org.ethereum.core.*;
3132
import org.ethereum.crypto.ECKey;
3233
import org.ethereum.db.ByteArrayWrapper;
@@ -102,6 +103,9 @@ public class WalletService {
102103

103104
EmbeddedDatabase wordsDatabase;
104105

106+
@Autowired
107+
SystemProperties config;
108+
105109
/**
106110
* key - hex address in lower case
107111
* value - address user friendly name
@@ -111,6 +115,8 @@ public class WalletService {
111115
final Map<String, TransactionInfo> pendingSendTransactions = new ConcurrentHashMap<>();
112116
final Map<String, TransactionInfo> pendingReceiveTransactions = new ConcurrentHashMap<>();
113117

118+
private boolean subscribedForEvents;
119+
114120
@PostConstruct
115121
public void init() {
116122
addresses.clear();
@@ -122,24 +128,36 @@ public void init() {
122128
fileSystemWalletStore.fromStore().stream()
123129
.forEach(a -> addresses.put(a.address, a.name));
124130

125-
ethereum.addListener(new EthereumListenerAdapter() {
126-
@Override
127-
public void onSyncDone(SyncState state) {
128-
if (state == SyncState.UNSECURE) {
129-
ethereum.addListener(new EthereumListenerAdapter() {
130-
@Override
131-
public void onPendingTransactionsReceived(List<Transaction> list) {
132-
handlePendingTransactionsReceived(list);
133-
}
134-
135-
@Override
136-
public void onBlock(BlockSummary blockSummary) {
137-
handleBlock(blockSummary);
138-
}
139-
});
131+
// workaround issue in ethereumJ-core, where single miner could never got sync done event
132+
if (config.minerStart()) {
133+
subscribeOnce();
134+
} else {
135+
ethereum.addListener(new EthereumListenerAdapter() {
136+
@Override
137+
public void onSyncDone(SyncState state) {
138+
if (state == SyncState.UNSECURE || state == SyncState.COMPLETE) {
139+
subscribeOnce();
140+
}
140141
}
141-
}
142-
});
142+
});
143+
}
144+
}
145+
146+
private void subscribeOnce() {
147+
if (!subscribedForEvents) {
148+
subscribedForEvents = true;
149+
ethereum.addListener(new EthereumListenerAdapter() {
150+
@Override
151+
public void onPendingTransactionsReceived(List<Transaction> list) {
152+
handlePendingTransactionsReceived(list);
153+
}
154+
155+
@Override
156+
public void onBlock(BlockSummary blockSummary) {
157+
handleBlock(blockSummary);
158+
}
159+
});
160+
}
143161
}
144162

145163
public void handleBlock(BlockSummary blockSummary) {

src/main/resources/private.conf

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mine {
4848
# when 'sync.enabled' is true the mining starts when the sync is complete
4949
# else the mining will start immediately, taking the best block from database
5050
# (or genesis if no blocks exist yet)
51-
start = false
51+
start = true
5252

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

6969
# number of CPU threads the miner will mine on
7070
# 0 disables CPU mining
71-
cpuMineThreads = 2
71+
cpuMineThreads = 1
7272

7373
# there two options for CPU mining 'light' and 'full'
7474
# 'light' requires only 16M of RAM but is much slower
7575
# 'full' requires 1G of RAM and possibly ~7min for the DataSet generation
7676
# but is much faster during mining
7777
fullDataSet = true
78-
79-
# start mining with specific nonce (might be usefult for testing)
80-
# null for random start nonce
81-
startNonce = null
8278
}
8379

0 commit comments

Comments
 (0)