@Override public void submitRequest(ParticipantId loggedInUser, WaveletName waveletName, ProtocolWaveletDelta delta, @Nullable String channelId, SubmitRequestListener listener) { super.submitRequest(loggedInUser, waveletName, delta, channelId, listener); Map<WaveletId, WaveletData> wavelets = waves.get(waveletName.waveId); if (wavelets == null) { wavelets = Maps.newHashMap(); waves.put(waveletName.waveId, wavelets); } WaveletData wavelet = wavelets.get(waveletName.waveletId); if (wavelet == null) { long dummyCreationTime = System.currentTimeMillis(); wavelet = WaveletDataUtil.createEmptyWavelet( waveletName, ParticipantId.ofUnsafe(delta.getAuthor()), HashedVersion.unsigned(0), dummyCreationTime); wavelets.put(waveletName.waveletId, wavelet); } // Add the delta to the history and update the wavelet's version. HashedVersion resultingVersion = updateAndGetVersion(waveletName, delta.getOperationCount()); TransformedWaveletDelta versionedDelta = CoreWaveletOperationSerializer.deserialize(delta, resultingVersion, APP_TIMESTAMP); deltas.put(waveletName, versionedDelta); // Confirm submit success. doSubmitSuccess(waveletName, resultingVersion, APP_TIMESTAMP); // Send an update echoing the submitted delta. Note: the document state is // ignored. waveletUpdate(wavelet, DeltaSequence.of(versionedDelta)); // Send a corresponding update of the index wave. }
ProtocolWaveletDelta delta; try { delta = ProtocolWaveletDelta.parseFrom(deltaBytes); } catch (InvalidProtocolBufferException ex) { throw new InvalidRequestException("Parse delta", operation, ex); currentVersion = waveletSnapshot.snapshot.getVersion(); if (currentVersion == delta.getHashedVersion().getVersion()) { if (importedFromVersion == -1) { importedFromVersion = currentVersion; + delta.getHashedVersion().getVersion() + ", but current version is " + currentVersion + "." + "Possibly wavelet is modified during import."); return;
/** * Deserializes a {@link ProtocolWaveletDelta} as a transformed wavelet delta. */ public static TransformedWaveletDelta deserialize(ProtocolWaveletDelta delta, HashedVersion resultingVersion, long applicationTimestamp) { ParticipantId author = ParticipantId.ofUnsafe(delta.getAuthor()); int count = delta.getOperationCount(); Preconditions.checkArgument(count > 0, "Cannot deserialize an empty delta"); List<WaveletOperation> ops = Lists.newArrayListWithCapacity(count); if (count > 1) { WaveletOperationContext context = new WaveletOperationContext(author, applicationTimestamp, 1); for (int i = 0; i < count - 1; i++) { ProtocolWaveletOperation op = delta.getOperation(i); ops.add(deserialize(op, context)); } } WaveletOperationContext context = new WaveletOperationContext(author, applicationTimestamp, 1, resultingVersion); ops.add(deserialize(delta.getOperation(count - 1), context)); return new TransformedWaveletDelta(author, resultingVersion, applicationTimestamp, ops); }
ByteStringMessage.parseProtocolWaveletDelta(signedDelta.getDelta()).getMessage(); Preconditions.checkArgument(protocolDelta.getOperationCount() > 0, "empty delta");
Preconditions.checkArgument(delta.getOperationCount() > 0, "empty delta"); LOG.info("Submit to " + waveletName + " by " + delta.getAuthor() + " @ " + delta.getHashedVersion().getVersion() + " with " + delta.getOperationCount() + " ops"); if (!wavelet.checkAccessPermission(ParticipantId.of(delta.getAuthor()))) { resultListener.onFailure(FederationErrors.badRequest( delta.getAuthor() + " is not a participant of " + waveletName)); return;
public void testFailedLocalWaveletRequest() throws Exception { ProtocolSignedDelta removeDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature1) .setDelta(removeParticipantProtoDelta(localWavelet).toByteString()) .build(); try { .setDelta(addParticipantProtoDelta(localWavelet).toByteString()) .build(); ProtocolSignedDelta addAgainDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature2) .setDelta(ProtocolWaveletDelta.newBuilder(addParticipantProtoDelta(localWavelet)) .setHashedVersion(serialize(localWavelet.getCurrentVersion())) .build().toByteString()) .build(); localWavelet.submitRequest(localWaveletName, addAgainDelta); ProtocolSignedDelta rollbackDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature1) .setDelta(ProtocolWaveletDelta.newBuilder(doubleRemoveParticipantProtoDelta(localWavelet)) .setHashedVersion(serialize(localWavelet.getCurrentVersion())) .build().toByteString()) .build(); try {
public void testFailedLocalWaveletRequest() throws Exception { ProtocolSignedDelta removeDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature1) .setDelta(removeParticipantProtoDelta(localWavelet).toByteString()) .build(); try { .setDelta(addParticipantProtoDelta(localWavelet).toByteString()) .build(); ProtocolSignedDelta addAgainDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature2) .setDelta(ProtocolWaveletDelta.newBuilder(addParticipantProtoDelta(localWavelet)) .setHashedVersion(serialize(localWavelet.getCurrentVersion())) .build().toByteString()) .build(); localWavelet.submitRequest(localWaveletName, addAgainDelta); ProtocolSignedDelta rollbackDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature1) .setDelta(ProtocolWaveletDelta.newBuilder(doubleRemoveParticipantProtoDelta(localWavelet)) .setHashedVersion(serialize(localWavelet.getCurrentVersion())) .build().toByteString()) .build(); try {
if (delta.getHashedVersion().getVersion() == 0) { LOG.warning("Remote user tried to submit delta at version 0 - disallowed. " + signedDelta); listener.onFailure(FederationErrors.badRequest("Remote users may not create wavelets."));
public void testSuccessfulLocalRequest() throws Exception { ProtocolSignedDelta addDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature1) .setDelta(addParticipantProtoDelta(localWavelet).toByteString()) .build(); localWavelet.submitRequest(localWaveletName, addDelta); assertEquals(localWavelet.getCurrentVersion().getVersion(), 2); assertTrue(localWavelet.isDeltaSigner( localWavelet.getCurrentVersion(), fakeSigner1)); assertFalse(localWavelet.isDeltaSigner( localWavelet.getCurrentVersion(), fakeSigner2)); HashedVersion oldVersion = localWavelet.getCurrentVersion(); ProtocolSignedDelta removeDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature2) .setDelta(ProtocolWaveletDelta.newBuilder(removeParticipantProtoDelta(localWavelet)) .setHashedVersion(serialize(localWavelet.getCurrentVersion())).build().toByteString()) .build(); localWavelet.submitRequest(localWaveletName, removeDelta); assertEquals(localWavelet.getCurrentVersion().getVersion(), 4); assertTrue(localWavelet.isDeltaSigner(oldVersion, fakeSigner1)); assertFalse(localWavelet.isDeltaSigner(oldVersion, fakeSigner2)); assertTrue(localWavelet.isDeltaSigner( localWavelet.getCurrentVersion(), fakeSigner2)); assertFalse(localWavelet.isDeltaSigner( localWavelet.getCurrentVersion(), fakeSigner1)); }
@Override public void submitRequest(ParticipantId loggedInUser, final WaveletName waveletName, final ProtocolWaveletDelta delta, final String channelId, final SubmitRequestListener listener) { final ParticipantId author = new ParticipantId(delta.getAuthor()); if (!author.equals(loggedInUser)) { listener.onFailure("Author field on delta must match logged in user"); return; } waveletInfo.getUserManager(author).submitRequest(channelId, waveletName); waveletProvider.submitRequest(waveletName, delta, new SubmitRequestListener() { @Override public void onSuccess(int operationsApplied, HashedVersion hashedVersionAfterApplication, long applicationTimestamp) { listener.onSuccess(operationsApplied, hashedVersionAfterApplication, applicationTimestamp); waveletInfo.getUserManager(author).submitResponse(channelId, waveletName, hashedVersionAfterApplication); } @Override public void onFailure(String error) { listener.onFailure(error); waveletInfo.getUserManager(author).submitResponse(channelId, waveletName, null); } }); }
@Override public void submitRequest(WaveletName waveletName, ProtocolWaveletDelta delta, final SubmitRequestListener listener) { Preconditions.checkState(initialized, "Wave server not yet initialized"); if (delta.getOperationCount() == 0) { listener.onFailure("Empty delta at version " + delta.getHashedVersion().getVersion()); return; } // The serialised version of this delta happens now. This should be the only place, ever! ProtocolSignedDelta signedDelta = certificateManager.signDelta(ByteStringMessage.serializeMessage(delta)); submitDelta(waveletName, delta, signedDelta, new SubmitResultListener() { @Override public void onFailure(FederationError errorMessage) { listener.onFailure(errorMessage.getErrorMessage()); } @Override public void onSuccess(int operationsApplied, ProtocolHashedVersion hashedVersionAfterApplication, long applicationTimestamp) { listener.onSuccess(operationsApplied, CoreWaveletOperationSerializer.deserialize(hashedVersionAfterApplication), applicationTimestamp); } }); }
public void testSuccessfulLocalRequest() throws Exception { ProtocolSignedDelta addDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature1) .setDelta(addParticipantProtoDelta(localWavelet).toByteString()) .build(); localWavelet.submitRequest(localWaveletName, addDelta); assertEquals(localWavelet.getCurrentVersion().getVersion(), 2); assertTrue(localWavelet.isDeltaSigner( localWavelet.getCurrentVersion(), fakeSigner1)); assertFalse(localWavelet.isDeltaSigner( localWavelet.getCurrentVersion(), fakeSigner2)); HashedVersion oldVersion = localWavelet.getCurrentVersion(); ProtocolSignedDelta removeDelta = ProtocolSignedDelta.newBuilder() .addSignature(fakeSignature2) .setDelta(ProtocolWaveletDelta.newBuilder(removeParticipantProtoDelta(localWavelet)) .setHashedVersion(serialize(localWavelet.getCurrentVersion())).build().toByteString()) .build(); localWavelet.submitRequest(localWaveletName, removeDelta); assertEquals(localWavelet.getCurrentVersion().getVersion(), 4); assertTrue(localWavelet.isDeltaSigner(oldVersion, fakeSigner1)); assertFalse(localWavelet.isDeltaSigner(oldVersion, fakeSigner2)); assertTrue(localWavelet.isDeltaSigner( localWavelet.getCurrentVersion(), fakeSigner2)); assertFalse(localWavelet.isDeltaSigner( localWavelet.getCurrentVersion(), fakeSigner1)); }
/** * Sets correct version hash to delta. * * @param delta the source delta. * @param waveletSnapshot to append delta. * @param waveletName name of wavelet. * @return the delta to import. * @throws InvalidParticipantAddress deserialize of participant error. */ ProtocolWaveletDelta setVersionHash(ProtocolWaveletDelta delta, CommittedWaveletSnapshot waveletSnapshot, WaveletName waveletName) throws InvalidParticipantAddress { ProtocolWaveletDelta.Builder newDelta = ProtocolWaveletDelta.newBuilder(delta); if (waveletSnapshot == null) { ProtocolHashedVersion ver = ProtocolHashedVersion.newBuilder(delta.getHashedVersion()). setHistoryHash(ByteString.copyFrom(HASH_FACTORY.createVersionZero(waveletName).getHistoryHash())). build(); newDelta.setHashedVersion(ver); } else { ProtocolHashedVersion ver = ProtocolHashedVersion.newBuilder(delta.getHashedVersion()). setHistoryHash(ByteString.copyFrom(waveletSnapshot.snapshot.getHashedVersion().getHistoryHash())). build(); newDelta.setHashedVersion(ver); } return newDelta.build(); } }
/** * Inspects the given applied delta to determine the {@code HashedVersion} it * was applied at. * This may require looking at the contained {@code ProtocolWaveletDelta}. * * @param appliedDeltaBytes to inspect * @return hashed version the delta was applied at * @throws InvalidProtocolBufferException if the contained * {@code ProtocolWaveletDelta} is invalid * (is only inspected if the applied delta has the hashed version set) */ public static HashedVersion getHashedVersionAppliedAt( ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDeltaBytes) throws InvalidProtocolBufferException { ProtocolAppliedWaveletDelta appliedDelta = appliedDeltaBytes.getMessage(); return CoreWaveletOperationSerializer.deserialize( // If the delta was transformed, the version it was actually applied at is specified // in the top-level message, otherwise we take if from the original signed delta. appliedDelta.hasHashedVersionAppliedAt() ? appliedDelta.getHashedVersionAppliedAt() : ProtocolWaveletDelta.parseFrom(appliedDelta.getSignedOriginalDelta().getDelta()) .getHashedVersion()); }
private void submitDeltaToNewWavelet(WaveletName name, ParticipantId user, WaveletOperation... ops) { HashedVersion version = V0_HASH_FACTORY.createVersionZero(name); WaveletDelta delta = new WaveletDelta(user, version, ImmutableList.copyOf(ops)); ProtocolWaveletDelta protoDelta = CoreWaveletOperationSerializer.serialize(delta); // Submitting the request will require the certificate manager to sign the delta. We'll just // leave it unsigned. ProtocolSignedDelta signedProtoDelta = ProtocolSignedDelta.newBuilder().setDelta(protoDelta.toByteString()).build(); waveServer.submitRequest(name, protoDelta, new SubmitRequestListener() { @Override public void onSuccess(int operationsApplied, HashedVersion hashedVersionAfterApplication, long applicationTimestamp) { // Wavelet was submitted. } @Override public void onFailure(String errorMessage) { fail("Could not submit callback"); } }); } }
/** * Assert that an operation is unchanged when serialised then deserialised. * * @param op operation to check */ private static void assertReversible(WaveletOperation op) { // Test both (de)serialising a single operation... assertEquals(op, CoreWaveletOperationSerializer.deserialize( CoreWaveletOperationSerializer.serialize(op), OP_CONTEXT)); List<WaveletOperation> ops = ImmutableList.of(op, op, op); ParticipantId author = new ParticipantId("kalman@google.com"); HashedVersion hashedVersion = HashedVersion.unsigned(0); WaveletDelta delta = new WaveletDelta(author, hashedVersion, ops); ProtocolWaveletDelta serialized = CoreWaveletOperationSerializer.serialize(delta); WaveletDelta deserialized = CoreWaveletOperationSerializer.deserialize(serialized); assertEquals(hashedVersion.getVersion(), serialized.getHashedVersion().getVersion()); assertTrue(Arrays.equals(hashedVersion.getHistoryHash(), serialized.getHashedVersion().getHistoryHash().toByteArray())); assertDeepEquals(delta, deserialized); }
private void submitDeltaToNewWavelet(WaveletName name, ParticipantId user, WaveletOperation... ops) { HashedVersion version = V0_HASH_FACTORY.createVersionZero(name); WaveletDelta delta = new WaveletDelta(user, version, ImmutableList.copyOf(ops)); ProtocolWaveletDelta protoDelta = CoreWaveletOperationSerializer.serialize(delta); // Submitting the request will require the certificate manager to sign the delta. We'll just // leave it unsigned. ProtocolSignedDelta signedProtoDelta = ProtocolSignedDelta.newBuilder().setDelta(protoDelta.toByteString()).build(); waveServer.submitRequest(name, protoDelta, new SubmitRequestListener() { @Override public void onSuccess(int operationsApplied, HashedVersion hashedVersionAfterApplication, long applicationTimestamp) { // Wavelet was submitted. } @Override public void onFailure(String errorMessage) { fail("Could not submit callback"); } }); } }
/** * Assert that an operation is unchanged when serialised then deserialised. * * @param op operation to check */ private static void assertReversible(WaveletOperation op) { // Test both (de)serialising a single operation... assertEquals(op, CoreWaveletOperationSerializer.deserialize( CoreWaveletOperationSerializer.serialize(op), OP_CONTEXT)); List<WaveletOperation> ops = ImmutableList.of(op, op, op); ParticipantId author = new ParticipantId("kalman@google.com"); HashedVersion hashedVersion = HashedVersion.unsigned(0); WaveletDelta delta = new WaveletDelta(author, hashedVersion, ops); ProtocolWaveletDelta serialized = CoreWaveletOperationSerializer.serialize(delta); WaveletDelta deserialized = CoreWaveletOperationSerializer.deserialize(serialized); assertEquals(hashedVersion.getVersion(), serialized.getHashedVersion().getVersion()); assertTrue(Arrays.equals(hashedVersion.getHistoryHash(), serialized.getHashedVersion().getHistoryHash().toByteArray())); assertDeepEquals(delta, deserialized); }
/** * Serializes a transformed delta as a {@link ProtocolWaveletDelta}. * * The target version of the result does not have an accompanying hash; * server delta hashes are redundant in the client/server protocol. * * @param delta to serialize * @return serialized protocol buffer wavelet delta */ public static ProtocolWaveletDelta serialize(TransformedWaveletDelta delta) { ProtocolWaveletDelta.Builder protobufDelta = ProtocolWaveletDelta.newBuilder(); for (WaveletOperation waveletOp : delta) { protobufDelta.addOperation(serialize(waveletOp)); } protobufDelta.setAuthor(delta.getAuthor().getAddress()); protobufDelta.setHashedVersion(serialize(HashedVersion.unsigned(delta.getAppliedAtVersion()))); return protobufDelta.build(); }
public void testSignature_missingSignerInfo() throws Exception { ProtocolWaveletDelta delta = ProtocolWaveletDelta.newBuilder() .setHashedVersion(getProtocolHashedVersion()) .setAuthor("bob@example.com") .build(); ByteStringMessage<ProtocolWaveletDelta> canonicalDelta = ByteStringMessage.serializeMessage(delta); manager = new CertificateManagerImpl(config, getSigner(), getVerifier(store, false), store); ProtocolSignedDelta signedDelta = manager.signDelta(canonicalDelta); try { manager.verifyDelta(signedDelta); fail("expected UnknownSignerException, but didn't get it"); } catch (UnknownSignerException e) { // expected } catch (Exception e) { fail("expected UnknownSignerExeception, but got " + e); } }