/** * Deserializes a {@link ProtocolWaveletDelta} as an untransformed wavelet * delta. */ public static WaveletDelta deserialize(ProtocolWaveletDelta delta) { List<WaveletOperation> ops = Lists.newArrayList(); for (ProtocolWaveletOperation op : delta.getOperationList()) { WaveletOperationContext context = new WaveletOperationContext( ParticipantId.ofUnsafe(delta.getAuthor()), Constants.NO_TIMESTAMP, 1); ops.add(deserialize(op, context)); } HashedVersion hashedVersion = deserialize(delta.getHashedVersion()); return new WaveletDelta(new ParticipantId(delta.getAuthor()), hashedVersion, ops); }
@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); } }); }
/** * 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(); } }
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;
+ delta.getHashedVersion().getVersion() + " with " + delta.getOperationCount() + " ops");
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."));
/** * 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()); }
/** * 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); }
/** * 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); }