/
DogecoinWrapper.java
executable file
·256 lines (224 loc) · 10.4 KB
/
DogecoinWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright (C) 2017 RSK Labs Ltd.
* Copyright (C) 2018 Coinfabrik and Oscar Guindzberg.
*/
package org.dogethereum.agents.core.dogecoin;
import lombok.extern.slf4j.Slf4j;
import org.bitcoinj.core.*;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.script.Script;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;
import org.bitcoinj.wallet.Wallet;
import org.dogethereum.agents.constants.SystemProperties;
import org.dogethereum.agents.util.AgentUtils;
import org.dogethereum.agents.constants.AgentConstants;
import org.dogethereum.agents.util.FileUtils;
import org.dogethereum.agents.util.OperatorPublicKeyHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Component
@Slf4j(topic = "DogecoinWrapper")
public class DogecoinWrapper {
private OperatorPublicKeyHandler operatorPublicKeyHandler;
SystemProperties config;
private WalletAppKit kit;
private Context dogeContext;
private AgentConstants agentConstants;
private File dataDirectory;
private Map<Sha256Hash, List<Proof>> dogeTxToRelayToEthProofsMap = new ConcurrentHashMap<>();
private File dogeTxToRelayToEthProofsFile;
@Autowired
public DogecoinWrapper(OperatorPublicKeyHandler operatorPublicKeyHandler) throws Exception {
this.operatorPublicKeyHandler = operatorPublicKeyHandler;
this.config = SystemProperties.CONFIG;
this.agentConstants = config.getAgentConstants();
this.dogeContext = new Context(agentConstants.getDogeParams());
this.dataDirectory = new File(config.dataDirectory() + "/DogecoinWrapper");
this.dogeTxToRelayToEthProofsFile = new File(dataDirectory.getAbsolutePath() + "/DogeTxToRelayToEthProofs.ser");
restoreProofsFromFile();
setup();
start();
}
public void setup() {
kit = new WalletAppKit(dogeContext, Script.ScriptType.P2PKH, null, dataDirectory, "dogethereumAgentLibdohj") {
@Override
protected void onSetupCompleted() {
Context.propagate(dogeContext);
if (config.isDogeLockTxRelayEnabled() || config.isOperatorEnabled()) {
// When we receive a block that includes a tx that sends funds to eth via peg, store the PartialMerkleTree
vPeerGroup.addBlocksDownloadedEventListener((peer, block, filteredBlock, blocksLeft) -> {
if (filteredBlock != null) {
// filteredBlock may be null if we are downloading just headers before fastCatchupTimeSecs
Context.propagate(dogeContext);
onBlock(filteredBlock);
}
});
vWallet.addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) -> coinsReceivedOrSent(tx));
vWallet.addCoinsSentEventListener((wallet, tx, prevBalance, newBalance) -> coinsReceivedOrSent(tx));
}
vPeerGroup.setDownloadTxDependencies(0);
}
private void coinsReceivedOrSent(Transaction tx) {
Context.propagate(dogeContext);
if (AgentUtils.isLockTx(tx, vWallet, agentConstants, operatorPublicKeyHandler) || AgentUtils.isUnlockTx(tx, vWallet, operatorPublicKeyHandler)) {
onTransaction(tx);
}
}
@Override
protected Wallet createWallet() {
Wallet wallet = super.createWallet();
if (config.isDogeLockTxRelayEnabled() || config.isOperatorEnabled()) {
LegacyAddress address = operatorPublicKeyHandler.getAddress();
// Be notified when we receive doge so we call registerTransaction()
wallet.addWatchedAddress(address, operatorPublicKeyHandler.getAddressCreationTime());
}
return wallet;
}
@Override
protected BlockStore provideBlockStore(File file) throws BlockStoreException {
return new AltcoinLevelDBBlockStore(dogeContext, getChainFile());
}
@Override
protected boolean chainFileDelete(File chainFile) {
return FileUtils.recursiveDelete(chainFile.getAbsolutePath());
}
@Override
protected File getChainFile() {
return new File(directory, "chain");
}
@Override
protected boolean chainFileExists(File chainFile) {
return chainFile.exists();
}
};
// TODO: Make the dogecoin peer list configurable
// if (!peerAddresses.isEmpty()) {
// kit.setPeerNodes(peerAddresses.toArray(new PeerAddress[]{}));
//}
kit.connectToLocalHost();
InputStream checkpoints = DogecoinWrapper.class.getResourceAsStream("/" + dogeContext.getParams().getId() + ".checkpoints");
if (checkpoints != null) {
kit.setCheckpoints(checkpoints);
}
}
public void start() {
Context.propagate(dogeContext);
kit.startAsync().awaitRunning();
}
public void stop() {
Context.propagate(dogeContext);
kit.stopAsync().awaitTerminated();
}
public int getBestChainHeight() {
return kit.chain().getBestChainHeight();
}
public StoredBlock getChainHead() {
return kit.chain().getChainHead();
}
public StoredBlock getBlock(Sha256Hash hash) throws BlockStoreException {
return kit.store().get(hash);
}
public StoredBlock getStoredBlockAtHeight(int height) throws BlockStoreException {
return AgentUtils.getStoredBlockAtHeight(kit.store(), height);
}
public Set<Transaction> getTransactions(int minconfirmations, boolean includeLock, boolean includeUnlock) {
Set<Transaction> txs = new HashSet<>();
for (Transaction tx : kit.wallet().getTransactions(false)) {
if (tx.getConfidence().getConfidenceType().equals(TransactionConfidence.ConfidenceType.BUILDING) &&
tx.getConfidence().getDepthInBlocks() >= minconfirmations) {
if (AgentUtils.isLockTx(tx, kit.wallet(), agentConstants, operatorPublicKeyHandler) && includeLock ||
AgentUtils.isUnlockTx(tx, kit.wallet(), operatorPublicKeyHandler) && includeUnlock) {
txs.add(tx);
}
}
}
return txs;
}
public Map<Sha256Hash, List<Proof>> getTransactionsToSendToEth() {
return dogeTxToRelayToEthProofsMap;
}
public void onBlock(FilteredBlock filteredBlock) {
synchronized (this) {
log.debug("onBlock {}", filteredBlock.getHash());
List<Sha256Hash> hashes = new ArrayList<>();
PartialMerkleTree tree = filteredBlock.getPartialMerkleTree();
tree.getTxnHashAndMerkleRoot(hashes);
for (Sha256Hash txToSendToEth : dogeTxToRelayToEthProofsMap.keySet()) {
if (hashes.contains(txToSendToEth)) {
List<Proof> proofs = dogeTxToRelayToEthProofsMap.get(txToSendToEth);
boolean alreadyIncluded = false;
for (Proof proof : proofs) {
if (proof.getBlockHash().equals(filteredBlock.getHash())) {
alreadyIncluded = true;
}
}
if (!alreadyIncluded) {
Proof proof = new Proof(filteredBlock.getHash(), tree);
proofs.add(proof);
log.info("New proof for tx " + txToSendToEth + " in block " + filteredBlock.getHash());
try {
flushProofs();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
} else {
log.info("Proof for tx " + txToSendToEth + " in block " + filteredBlock.getHash() + " already stored");
}
}
}
}
}
public void onTransaction(Transaction tx) {
log.debug("onTransaction {}", tx.getTxId());
synchronized (this) {
dogeTxToRelayToEthProofsMap.put(tx.getTxId(), new ArrayList<Proof>());
try {
flushProofs();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
@PreDestroy
public void tearDown() throws BlockStoreException, IOException {
log.info("DogecoinWrapper tearDown starting...");
stop();
synchronized (this) {
flushProofs();
}
log.info("DogecoinWrapper tearDown finished.");
}
private void restoreProofsFromFile() throws IOException, ClassNotFoundException {
if (dogeTxToRelayToEthProofsFile.exists()) {
synchronized (this) {
try (
FileInputStream txsToSendToEthFileIs = new FileInputStream(dogeTxToRelayToEthProofsFile);
ObjectInputStream txsToSendToEthObjectIs = new ObjectInputStream(txsToSendToEthFileIs);
) {
this.dogeTxToRelayToEthProofsMap = (Map<Sha256Hash, List<Proof>> ) txsToSendToEthObjectIs.readObject();
}
}
}
}
private void flushProofs() throws IOException {
if (!dataDirectory.exists()) {
if (!dataDirectory.mkdirs()) {
throw new IOException("Could not create directory " + dataDirectory.getAbsolutePath());
}
}
try (
FileOutputStream txsToSendToEthFileOs = new FileOutputStream(dogeTxToRelayToEthProofsFile);
ObjectOutputStream txsToSendToEthObjectOs = new ObjectOutputStream(txsToSendToEthFileOs);
) {
txsToSendToEthObjectOs.writeObject(this.dogeTxToRelayToEthProofsMap);
}
}
public void broadcastDogecoinTransaction(Transaction tx) {
kit.peerGroup().broadcastTransaction(tx);
}
}