/** * Builds a transformed delta from an applied delta and its transformed ops. */ public static TransformedWaveletDelta buildTransformedDelta( ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDeltaBytes, WaveletDelta transformed) throws InvalidProtocolBufferException { ProtocolAppliedWaveletDelta appliedDelta = appliedDeltaBytes.getMessage(); Preconditions.checkArgument( getHashedVersionAppliedAt(appliedDeltaBytes).equals(transformed.getTargetVersion())); Preconditions.checkArgument(appliedDelta.getOperationsApplied() == transformed.size()); HashedVersion resultingVersion = HASH_FACTORY.create(appliedDeltaBytes.getByteArray(), transformed.getTargetVersion(), appliedDelta.getOperationsApplied()); return TransformedWaveletDelta.cloneOperations(resultingVersion, appliedDelta.getApplicationTimestamp(), transformed); }
ProtocolSignedDelta toVerify = appliedDelta.getMessage().getSignedOriginalDelta(); HashedVersion deltaEndVersion; try {
public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println( "Usage: java [options] " + ParseRawDelta.class.getName() + " <base64 encoded raw delta>"); System.exit(1); } else { System.out.println("Parsing base64 string: " + args[0]); byte[] bytes = CharBase64.decode(args[0]); ProtocolAppliedWaveletDelta parsed = ProtocolAppliedWaveletDelta.parseFrom(bytes); System.out.println(parsed.toString()); } } }
certificateManager.verifyDelta(appliedDelta.getMessage().getSignedOriginalDelta()); } catch (SignatureException e) { LOG.warning("Verification failure for " + domain + " incoming " + getWaveletName(), e); appliedDelta.getMessage().getSignedOriginalDelta().getDelta()); LOG.info("actual delta: " + actualDelta); } catch (InvalidProtocolBufferException e) { long opsApplied = applicationResult.getResultingVersion().getVersion() - expectedVersion.getVersion(); if (opsApplied != appliedDelta.getMessage().getOperationsApplied()) { throw new OperationException("Operations applied here do not match the authoritative" + " server claim (got " + opsApplied + ", expected " + appliedDelta.getMessage().getOperationsApplied() + ".");
appliedDelta.getMessage().getSignedOriginalDelta().getDelta()); WaveletDelta delta = CoreWaveletOperationSerializer.deserialize(protocolDelta.getMessage()); if (appliedDelta.getMessage().hasHashedVersionAppliedAt()) { LOG.warning("Hashes are the same but applied delta has hashed_version_applied_at");
/** * Creates an applied delta message from a signed protocol delta and * application metadata. * * @param signedDelta the original delta * @param appliedAtVersion version at which the delta applied * @param operationsApplied number of ops in the delta * @param applicationTimestamp timestamp of application */ public static ByteStringMessage<ProtocolAppliedWaveletDelta> buildAppliedDelta( ProtocolSignedDelta signedDelta, HashedVersion appliedAtVersion, int operationsApplied, long applicationTimestamp) { ProtocolAppliedWaveletDelta.Builder appliedDeltaBuilder = ProtocolAppliedWaveletDelta .newBuilder() .setSignedOriginalDelta(signedDelta) .setOperationsApplied(operationsApplied) .setApplicationTimestamp(applicationTimestamp); // TODO(soren): Only set hashedVersionAppliedAt when different from the // signed delta's target version if we rev the protocol. appliedDeltaBuilder.setHashedVersionAppliedAt( CoreWaveletOperationSerializer.serialize(appliedAtVersion)); return ByteStringMessage.serializeMessage(appliedDeltaBuilder.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()); }
protected WaveletDeltaRecord createRecord() { HashedVersion targetVersion = HashedVersion.of(0, new byte[] {3, 2, 1}); HashedVersion resultingVersion = HashedVersion.of(2, new byte[] {1, 2, 3}); List<WaveletOperation> ops = ImmutableList.of(UTIL.noOp(), UTIL.addParticipant(TestingConstants.OTHER_PARTICIPANT)); TransformedWaveletDelta transformed = TransformedWaveletDelta.cloneOperations( TestingConstants.PARTICIPANT, resultingVersion, 1234567890, ops); ProtocolWaveletDelta serializedDelta = CoreWaveletOperationSerializer.serialize(transformed); ProtocolSignature signature = ProtocolSignature.newBuilder().setSignatureAlgorithm(SignatureAlgorithm.SHA1_RSA) .setSignatureBytes(ByteString.copyFrom(new byte[] {1, 2, 3})).setSignerId( ByteString.copyFromUtf8("somebody")).build(); ProtocolSignedDelta signedDelta = ProtocolSignedDelta.newBuilder().setDelta( ByteStringMessage.serializeMessage(serializedDelta).getByteString()).addAllSignature( ImmutableList.of(signature)).build(); ProtocolAppliedWaveletDelta delta = ProtocolAppliedWaveletDelta.newBuilder().setApplicationTimestamp(1234567890) .setHashedVersionAppliedAt(CoreWaveletOperationSerializer.serialize(targetVersion)) .setSignedOriginalDelta(signedDelta).setOperationsApplied(2).build(); return new WaveletDeltaRecord(targetVersion, ByteStringMessage.serializeMessage(delta), transformed); }
protected WaveletDeltaRecord createRecord() { HashedVersion targetVersion = HashedVersion.of(0, new byte[] {3, 2, 1}); HashedVersion resultingVersion = HashedVersion.of(2, new byte[] {1, 2, 3}); List<WaveletOperation> ops = ImmutableList.of(UTIL.noOp(), UTIL.addParticipant(TestingConstants.OTHER_PARTICIPANT)); TransformedWaveletDelta transformed = TransformedWaveletDelta.cloneOperations( TestingConstants.PARTICIPANT, resultingVersion, 1234567890, ops); ProtocolWaveletDelta serializedDelta = CoreWaveletOperationSerializer.serialize(transformed); ProtocolSignature signature = ProtocolSignature.newBuilder().setSignatureAlgorithm(SignatureAlgorithm.SHA1_RSA) .setSignatureBytes(ByteString.copyFrom(new byte[] {1, 2, 3})).setSignerId( ByteString.copyFromUtf8("somebody")).build(); ProtocolSignedDelta signedDelta = ProtocolSignedDelta.newBuilder().setDelta( ByteStringMessage.serializeMessage(serializedDelta).getByteString()).addAllSignature( ImmutableList.of(signature)).build(); ProtocolAppliedWaveletDelta delta = ProtocolAppliedWaveletDelta.newBuilder().setApplicationTimestamp(1234567890) .setHashedVersionAppliedAt(CoreWaveletOperationSerializer.serialize(targetVersion)) .setSignedOriginalDelta(signedDelta).setOperationsApplied(2).build(); return new WaveletDeltaRecord(targetVersion, ByteStringMessage.serializeMessage(delta), transformed); }
@Override public boolean isDeltaSigner(HashedVersion version, ByteString signerId) { ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDelta = lookupAppliedDeltaByEndVersion(version); if (appliedDelta == null) { return false; } ProtocolSignedDelta signedDelta = appliedDelta.getMessage().getSignedOriginalDelta(); for (ProtocolSignature signature : signedDelta.getSignatureList()) { if (signature.getSignerId().equals(signerId)) return true; } return false; } }
/** * Calculates the hashed version after an applied delta is applied. */ public static HashedVersion calculateResultingHashedVersion( ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDelta) throws InvalidProtocolBufferException { return HASH_FACTORY.create( appliedDelta.getByteArray(), getHashedVersionAppliedAt(appliedDelta), appliedDelta.getMessage().getOperationsApplied()); }
/** * Parses a {@link ProtocolAppliedWaveletDelta}. Convenience method, equivalent to * {@code from(ProtocolAppliedWaveletDelta.getDefaultInstanceForType(), byteString)}. */ public static ByteStringMessage<ProtocolAppliedWaveletDelta> parseProtocolAppliedWaveletDelta( ByteString byteString) throws InvalidProtocolBufferException { return new ByteStringMessage<ProtocolAppliedWaveletDelta>( ProtocolAppliedWaveletDelta.parseFrom(byteString), byteString); }