3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

Quick hack (unfinished API) to clear out wallet transactions so you can replay the wallet in case of bugs, like forgetting to save the wallet when you need to.

This commit is contained in:
Mike Hearn 2011-12-27 23:10:32 +00:00 committed by Miron Cuperman
parent edb3814b56
commit 5ce1d21e4c

View File

@ -57,11 +57,28 @@ public class ToyWallet {
params = NetworkParameters.prodNet();
}
wallet = getWallet(params);
// Try to read the wallet from storage, create a new one if not possible.
boolean freshWallet = false;
walletFile = new File("toy.wallet");
try {
wallet = Wallet.loadFromFile(walletFile);
} catch (IOException e) {
wallet = new Wallet(params);
wallet.keychain.add(new ECKey());
wallet.saveToFile(walletFile);
freshWallet = true;
}
System.out.println("Send to: " + wallet.keychain.get(0).toAddress(params));
System.out.println(wallet);
chain = new BlockChain(params, wallet, new DiskBlockStore(params, new File("toy.blockchain")));
File blockChainFile = new File("toy.blockchain");
if (!blockChainFile.exists() && !freshWallet) {
// No block chain, but we had a wallet. So empty out the transactions in the wallet so when we rescan
// the blocks there are no problems (wallets don't support replays without being emptied).
wallet.clearTransactions(0);
}
chain = new BlockChain(params, wallet, new DiskBlockStore(params, blockChainFile));
peerGroup = new PeerGroup(params, chain);
if (testnet) {
peerGroup.addAddress(new PeerAddress(InetAddress.getByName("plan99.net"), 18333));
@ -89,6 +106,7 @@ public class ToyWallet {
public void onBlocksDownloaded(Peer peer, Block block, int blocksLeft) {
super.onBlocksDownloaded(peer, block, blocksLeft);
handleNewBlock();
triggerNetworkStatsUpdate(peerGroup.numConnectedPeers());
}
});
@ -134,20 +152,6 @@ public class ToyWallet {
peerGroup.downloadBlockChain();
}
private Wallet getWallet(NetworkParameters params) throws IOException {
// Try to read the wallet from storage, create a new one if not possible.
walletFile = new File("toy.wallet");
Wallet w;
try {
w = Wallet.loadFromFile(walletFile);
} catch (IOException e) {
w = new Wallet(params);
w.keychain.add(new ECKey());
w.saveToFile(walletFile);
}
return w;
}
private void handleNewBlock() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
@ -204,6 +208,12 @@ public class ToyWallet {
// between confidence states.
int txIndex = transactions.indexOf(tx);
fireContentsChanged(this, txIndex, txIndex);
// Confidences may have changed.
try {
wallet.saveToFile(walletFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
fireIntervalAdded(this, transactions.size() - 1, transactions.size() - 1);
@ -233,7 +243,7 @@ public class ToyWallet {
.replaceAll(key, "<i>" + key + "</i>");
setText(value);
setOpaque(true);
setBackground(index % 2 == 1 ? new Color(50, 50, 50) : Color.WHITE);
setBackground(index % 2 == 1 ? new Color(230, 230, 230) : Color.WHITE);
return this;
}
}