Search in sources :

Example 1 with SPVBlockStore

use of org.bitcoinj.store.SPVBlockStore in project bitcoin-wallet by bitcoin-wallet.

the class BlockchainService method onCreate.

@Override
public void onCreate() {
    serviceCreatedAt = System.currentTimeMillis();
    log.debug(".onCreate()");
    super.onCreate();
    nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    application = (WalletApplication) getApplication();
    config = application.getConfiguration();
    final Wallet wallet = application.getWallet();
    peerConnectivityListener = new PeerConnectivityListener();
    broadcastPeerState(0);
    blockChainFile = new File(getDir("blockstore", Context.MODE_PRIVATE), Constants.Files.BLOCKCHAIN_FILENAME);
    final boolean blockChainFileExists = blockChainFile.exists();
    if (!blockChainFileExists) {
        log.info("blockchain does not exist, resetting wallet");
        wallet.reset();
    }
    try {
        blockStore = new SPVBlockStore(Constants.NETWORK_PARAMETERS, blockChainFile);
        // detect corruptions as early as possible
        blockStore.getChainHead();
        final long earliestKeyCreationTime = wallet.getEarliestKeyCreationTime();
        if (!blockChainFileExists && earliestKeyCreationTime > 0) {
            try {
                final Stopwatch watch = Stopwatch.createStarted();
                final InputStream checkpointsInputStream = getAssets().open(Constants.Files.CHECKPOINTS_FILENAME);
                CheckpointManager.checkpoint(Constants.NETWORK_PARAMETERS, checkpointsInputStream, blockStore, earliestKeyCreationTime);
                watch.stop();
                log.info("checkpoints loaded from '{}', took {}", Constants.Files.CHECKPOINTS_FILENAME, watch);
            } catch (final IOException x) {
                log.error("problem reading checkpoints, continuing without", x);
            }
        }
    } catch (final BlockStoreException x) {
        blockChainFile.delete();
        final String msg = "blockstore cannot be created";
        log.error(msg, x);
        throw new Error(msg, x);
    }
    try {
        blockChain = new BlockChain(Constants.NETWORK_PARAMETERS, wallet, blockStore);
    } catch (final BlockStoreException x) {
        throw new Error("blockchain cannot be created", x);
    }
    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
    intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
    // implicitly start PeerGroup
    registerReceiver(connectivityReceiver, intentFilter);
    application.getWallet().addCoinsReceivedEventListener(Threading.SAME_THREAD, walletEventListener);
    application.getWallet().addCoinsSentEventListener(Threading.SAME_THREAD, walletEventListener);
    application.getWallet().addChangeEventListener(Threading.SAME_THREAD, walletEventListener);
    registerReceiver(tickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}
Also used : IntentFilter(android.content.IntentFilter) BlockStoreException(org.bitcoinj.store.BlockStoreException) BlockChain(org.bitcoinj.core.BlockChain) Wallet(org.bitcoinj.wallet.Wallet) InputStream(java.io.InputStream) SPVBlockStore(org.bitcoinj.store.SPVBlockStore) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) PowerManager(android.os.PowerManager) File(java.io.File)

Aggregations

IntentFilter (android.content.IntentFilter)1 PowerManager (android.os.PowerManager)1 Stopwatch (com.google.common.base.Stopwatch)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BlockChain (org.bitcoinj.core.BlockChain)1 BlockStoreException (org.bitcoinj.store.BlockStoreException)1 SPVBlockStore (org.bitcoinj.store.SPVBlockStore)1 Wallet (org.bitcoinj.wallet.Wallet)1