Server POW should work now

This commit is contained in:
2015-12-21 15:31:48 +01:00
parent b0828ec1e5
commit 41f4571bf6
26 changed files with 804 additions and 226 deletions

View File

@ -28,57 +28,7 @@ import static ch.dissem.bitmessage.utils.UnixTime.DAY;
* @author Christian Basler
*/
public class AndroidSecurity extends SpongySecurity {
private final SharedPreferences preferences;
public AndroidSecurity(Context ctx) {
public AndroidSecurity() {
PRNGFixes.apply();
preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
}
@Override
public void doProofOfWork(ObjectMessage object, long nonceTrialsPerByte, long extraBytes, ProofOfWorkEngine.Callback callback) {
if (preferences.getBoolean(PREFERENCE_SERVER_POW, false)) {
object.setNonce(new byte[8]);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
object.write(out);
sendAsBroadcast(getContext().getAddressRepo().getIdentities().get(0), out.toByteArray());
if (object.getPayload() instanceof PlaintextHolder) {
Plaintext plaintext = ((PlaintextHolder) object.getPayload()).getPlaintext();
plaintext.setInventoryVector(object.getInventoryVector());
plaintext.setStatus(SENT);
plaintext.removeLabel(Label.Type.OUTBOX);
plaintext.addLabels(getContext().getMessageRepository().getLabels(Label.Type.SENT));
getContext().getMessageRepository().save(plaintext);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
super.doProofOfWork(object, nonceTrialsPerByte, extraBytes, callback);
}
}
private void sendAsBroadcast(BitmessageAddress identity, byte[] data) throws IOException {
Plaintext msg = new Plaintext.Builder(BROADCAST)
.from(identity)
.message(data)
.build();
Broadcast payload = Factory.getBroadcast(identity, msg);
long expires = UnixTime.now(+2 * DAY);
final ObjectMessage object = new ObjectMessage.Builder()
.stream(identity.getStream())
.expiresTime(expires)
.payload(payload)
.build();
object.sign(identity.getPrivateKey());
payload.encrypt();
object.setNonce(new byte[8]);
getContext().getInventory().storeObject(object);
getContext().getNetworkHandler().offer(object.getInventoryVector());
// TODO: offer to the trusted node only?
// at least make sure it is offered to the trusted node!
}
}

View File

@ -0,0 +1,52 @@
package ch.dissem.apps.abit.adapter;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.util.Arrays;
import ch.dissem.apps.abit.util.Preferences;
import ch.dissem.bitmessage.InternalContext;
import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_SERVER_POW;
/**
* Switches between two {@link ProofOfWorkEngine}s depending on the configuration.
*
* @author Christian Basler
*/
public class SwitchingProofOfWorkEngine implements ProofOfWorkEngine, InternalContext.ContextHolder {
private final Context ctx;
private final String preference;
private final ProofOfWorkEngine option;
private final ProofOfWorkEngine fallback;
public SwitchingProofOfWorkEngine(Context ctx, String preference,
ProofOfWorkEngine option, ProofOfWorkEngine fallback) {
this.ctx = ctx;
this.preference = preference;
this.option = option;
this.fallback = fallback;
}
@Override
public void calculateNonce(byte[] initialHash, byte[] target, Callback callback) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
if (preferences.getBoolean(preference, false)) {
option.calculateNonce(initialHash, target, callback);
} else {
fallback.calculateNonce(initialHash, target, callback);
}
}
@Override
public void setContext(InternalContext context) {
for (ProofOfWorkEngine e : Arrays.asList(option, fallback)) {
if (e instanceof InternalContext.ContextHolder) {
((InternalContext.ContextHolder) e).setContext(context);
}
}
}
}