mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-11-02 13:37:24 +00:00
Support attaching multiple wallets to a single BlockChain. Patch from Jan Møller. Resolves issue 39.
This commit is contained in:
@@ -65,7 +65,7 @@ public class BlockChain {
|
|||||||
protected StoredBlock chainHead;
|
protected StoredBlock chainHead;
|
||||||
|
|
||||||
protected final NetworkParameters params;
|
protected final NetworkParameters params;
|
||||||
protected final Wallet wallet;
|
protected final List<Wallet> wallets;
|
||||||
|
|
||||||
// Holds blocks that we have received but can't plug into the chain yet, eg because they were created whilst we
|
// Holds blocks that we have received but can't plug into the chain yet, eg because they were created whilst we
|
||||||
// were downloading the block chain.
|
// were downloading the block chain.
|
||||||
@@ -80,6 +80,14 @@ public class BlockChain {
|
|||||||
* {@link com.google.bitcoin.store.BoundedOverheadBlockStore} if you'd like to ensure fast startup the next time you run the program.
|
* {@link com.google.bitcoin.store.BoundedOverheadBlockStore} if you'd like to ensure fast startup the next time you run the program.
|
||||||
*/
|
*/
|
||||||
public BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore) {
|
public BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore) {
|
||||||
|
this(params, new ArrayList<Wallet>(), blockStore);
|
||||||
|
addWallet(wallet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a BlockChain connected to the given list of wallets and a store.
|
||||||
|
*/
|
||||||
|
public BlockChain(NetworkParameters params, List<Wallet> wallets, BlockStore blockStore){
|
||||||
try {
|
try {
|
||||||
this.blockStore = blockStore;
|
this.blockStore = blockStore;
|
||||||
chainHead = blockStore.getChainHead();
|
chainHead = blockStore.getChainHead();
|
||||||
@@ -87,9 +95,17 @@ public class BlockChain {
|
|||||||
} catch (BlockStoreException e) {
|
} catch (BlockStoreException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.params = params;
|
this.params = params;
|
||||||
this.wallet = wallet;
|
this.wallets = new ArrayList<Wallet>(wallets);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a wallet to the BlockChain. Note that the wallet will be unaffected by any blocks received while it
|
||||||
|
* was not part of this BlockChain. This method is useful if the wallet has just been created, and its keys
|
||||||
|
* have never been in use, or if the wallet has been loaded along with the BlockChain
|
||||||
|
*/
|
||||||
|
public synchronized void addWallet(Wallet wallet) {
|
||||||
|
wallets.add(wallet);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -214,10 +230,12 @@ public class BlockChain {
|
|||||||
// Then build a list of all blocks in the old part of the chain and the new part.
|
// Then build a list of all blocks in the old part of the chain and the new part.
|
||||||
List<StoredBlock> oldBlocks = getPartialChain(chainHead, splitPoint);
|
List<StoredBlock> oldBlocks = getPartialChain(chainHead, splitPoint);
|
||||||
List<StoredBlock> newBlocks = getPartialChain(newChainHead, splitPoint);
|
List<StoredBlock> newBlocks = getPartialChain(newChainHead, splitPoint);
|
||||||
// Now inform the wallet. This is necessary so the set of currently active transactions (that we can spend)
|
// Now inform the wallets. This is necessary so the set of currently active transactions (that we can spend)
|
||||||
// can be updated to take into account the re-organize. We might also have received new coins we didn't have
|
// can be updated to take into account the re-organize. We might also have received new coins we didn't have
|
||||||
// before and our previous spends might have been undone.
|
// before and our previous spends might have been undone.
|
||||||
|
for (Wallet wallet : wallets) {
|
||||||
wallet.reorganize(oldBlocks, newBlocks);
|
wallet.reorganize(oldBlocks, newBlocks);
|
||||||
|
}
|
||||||
// Update the pointer to the best known block.
|
// Update the pointer to the best known block.
|
||||||
setChainHead(newChainHead);
|
setChainHead(newChainHead);
|
||||||
}
|
}
|
||||||
@@ -384,6 +402,7 @@ public class BlockChain {
|
|||||||
|
|
||||||
private void scanTransaction(StoredBlock block, Transaction tx, NewBlockType blockType)
|
private void scanTransaction(StoredBlock block, Transaction tx, NewBlockType blockType)
|
||||||
throws ScriptException, VerificationException {
|
throws ScriptException, VerificationException {
|
||||||
|
for (Wallet wallet : wallets) {
|
||||||
boolean shouldReceive = false;
|
boolean shouldReceive = false;
|
||||||
for (TransactionOutput output : tx.outputs) {
|
for (TransactionOutput output : tx.outputs) {
|
||||||
// TODO: Handle more types of outputs, not just regular to address outputs.
|
// TODO: Handle more types of outputs, not just regular to address outputs.
|
||||||
@@ -408,6 +427,7 @@ public class BlockChain {
|
|||||||
if (shouldReceive)
|
if (shouldReceive)
|
||||||
wallet.receive(tx, block, blockType);
|
wallet.receive(tx, block, blockType);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the block at the head of the current best chain. This is the block which represents the greatest
|
* Returns the block at the head of the current best chain. This is the block which represents the greatest
|
||||||
|
|||||||
Reference in New Issue
Block a user