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));
}
Aggregations