2016-01-19 20:50:58 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Christian Basler
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-10-31 07:49:03 +01:00
|
|
|
package ch.dissem.apps.abit.service;
|
|
|
|
|
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Binder;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
|
2016-01-23 23:30:51 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.Queue;
|
2015-10-31 07:49:03 +01:00
|
|
|
|
|
|
|
import ch.dissem.apps.abit.notification.ProofOfWorkNotification;
|
|
|
|
import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine;
|
|
|
|
import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
|
|
|
|
|
|
|
|
import static ch.dissem.apps.abit.notification.ProofOfWorkNotification.ONGOING_NOTIFICATION_ID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Proof of Work Service makes sure POW is done in a foreground process, so it shouldn't be
|
|
|
|
* killed by the system before the nonce is found.
|
|
|
|
*/
|
|
|
|
public class ProofOfWorkService extends Service {
|
|
|
|
// Object to use as a thread-safe lock
|
2016-04-27 17:30:05 +02:00
|
|
|
private static ProofOfWorkEngine engine = new MultiThreadedPOWEngine();
|
2016-01-23 23:30:51 +01:00
|
|
|
private static boolean calculating;
|
|
|
|
private static final Queue<PowItem> queue = new LinkedList<>();
|
2016-10-16 23:16:38 +02:00
|
|
|
private ProofOfWorkNotification notification;
|
2015-10-31 07:49:03 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2016-01-23 23:30:51 +01:00
|
|
|
notification = new ProofOfWorkNotification(this);
|
2015-10-31 07:49:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
2016-01-23 23:30:51 +01:00
|
|
|
return new PowBinder(this);
|
2015-10-31 07:49:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static class PowBinder extends Binder {
|
2016-01-23 23:30:51 +01:00
|
|
|
private final ProofOfWorkService service;
|
2016-10-16 23:16:38 +02:00
|
|
|
private final ProofOfWorkNotification notification;
|
2015-10-31 07:49:03 +01:00
|
|
|
|
2016-01-23 23:30:51 +01:00
|
|
|
private PowBinder(ProofOfWorkService service) {
|
|
|
|
this.service = service;
|
2016-10-16 23:16:38 +02:00
|
|
|
this.notification = service.notification;
|
2015-10-31 07:49:03 +01:00
|
|
|
}
|
|
|
|
|
2016-10-16 23:16:38 +02:00
|
|
|
void process(PowItem item) {
|
2016-01-23 23:30:51 +01:00
|
|
|
synchronized (queue) {
|
|
|
|
service.startService(new Intent(service, ProofOfWorkService.class));
|
|
|
|
service.startForeground(ONGOING_NOTIFICATION_ID,
|
2016-10-16 23:16:38 +02:00
|
|
|
notification.getNotification());
|
2016-01-23 23:30:51 +01:00
|
|
|
if (!calculating) {
|
|
|
|
calculating = true;
|
|
|
|
service.calculateNonce(item);
|
|
|
|
} else {
|
|
|
|
queue.add(item);
|
|
|
|
notification.update(queue.size()).show();
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 07:49:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-23 23:30:51 +01:00
|
|
|
static class PowItem {
|
|
|
|
private final byte[] initialHash;
|
|
|
|
private final byte[] targetValue;
|
|
|
|
private final ProofOfWorkEngine.Callback callback;
|
|
|
|
|
|
|
|
PowItem(byte[] initialHash, byte[] targetValue, ProofOfWorkEngine.Callback callback) {
|
|
|
|
this.initialHash = initialHash;
|
|
|
|
this.targetValue = targetValue;
|
|
|
|
this.callback = callback;
|
2015-10-31 07:49:03 +01:00
|
|
|
}
|
2016-01-23 23:30:51 +01:00
|
|
|
}
|
2015-10-31 07:49:03 +01:00
|
|
|
|
2016-01-23 23:30:51 +01:00
|
|
|
private void calculateNonce(final PowItem item) {
|
2016-10-16 23:16:38 +02:00
|
|
|
engine.calculateNonce(item.initialHash, item.targetValue, (initialHash, nonce) -> {
|
|
|
|
try {
|
|
|
|
item.callback.onNonceCalculated(initialHash, nonce);
|
|
|
|
} finally {
|
|
|
|
PowItem next;
|
|
|
|
synchronized (queue) {
|
|
|
|
next = queue.poll();
|
|
|
|
if (next == null) {
|
|
|
|
calculating = false;
|
|
|
|
stopForeground(true);
|
|
|
|
stopSelf();
|
|
|
|
} else {
|
|
|
|
notification.update(queue.size()).show();
|
2015-10-31 07:49:03 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-16 23:16:38 +02:00
|
|
|
if (next != null) {
|
|
|
|
calculateNonce(next);
|
|
|
|
}
|
2016-01-23 23:30:51 +01:00
|
|
|
}
|
|
|
|
});
|
2015-10-31 07:49:03 +01:00
|
|
|
}
|
2016-10-16 23:16:38 +02:00
|
|
|
}
|