Include conflicted current profile version in SetProfileResponse

This commit is contained in:
Chris Eager authored and Chris Eager committed 2026-10-14 11:21:05 -04:00
1 parent edfefba78f
commit 458eadf0a0
8 files changed
+44 -15

No files matched your search

@@ -21,6 +21,7 @@ import java.util.stream.Collectors;
import org.signal.chat.common.S3UploadForm;
import org.signal.chat.errors.FailedPrecondition;
import org.signal.chat.errors.NotFound;
import org.signal.chat.profile.ExpectedVersionConflict;
import org.signal.chat.profile.GetAvatarCredentialRequest;
import org.signal.chat.profile.GetAvatarCredentialResponse;
import org.signal.chat.profile.GetProfileRequest;
@@ -115,9 +116,11 @@ public class ProfileGrpcService extends SimpleProfileGrpc.ProfileImplBase {
final boolean currentVersionMatchesExpected = Arrays.equals(account.getCurrentProfileVersion().orElse(new byte[0]), expectedCurrentVersion);
if (!currentVersionMatchesExpected) {
return SetProfileResponse.newBuilder().setExpectedVersionWriteConflict(FailedPrecondition.newBuilder()
.setDescription("current and expected profile versions must match")
.build()).build();
final ExpectedVersionConflict conflict = account.getCurrentProfileVersion()
.map(v -> ExpectedVersionConflict.newBuilder().setCurrentProfileVersion(ByteString.copyFrom(v)).build())
.orElseGet(ExpectedVersionConflict::getDefaultInstance);
return SetProfileResponse.newBuilder().setExpectedVersionWriteConflict(conflict)
.build();
}
final byte[] version = request.getVersion().toByteArray();
@@ -190,12 +193,13 @@ public class ProfileGrpcService extends SimpleProfileGrpc.ProfileImplBase {
a.setBadges(clock, updatedBadges);
});
} catch (final WriteConflictException _) {
return SetProfileResponse.newBuilder()
.setExpectedVersionWriteConflict(FailedPrecondition.newBuilder()
.setDescription("current and expected version mismatch")
.build())
.build();
} catch (final WriteConflictException e) {
final ExpectedVersionConflict conflict = e.getConflictedValue()
.map(v -> ExpectedVersionConflict.newBuilder().setCurrentProfileVersion(ByteString.copyFrom(v)).build())
.orElseGet(ExpectedVersionConflict::getDefaultInstance);
return SetProfileResponse.newBuilder().setExpectedVersionWriteConflict(conflict).build();
}
if (request.getV1Request().getAvatarChange() != AvatarChange.AVATAR_CHANGE_UNCHANGED && avatarData.currentAvatar().isPresent()) {
@@ -1345,7 +1345,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
});
if (!maybeUpdatedAccount.getCurrentProfileVersion().map(v -> Arrays.equals(v, newVersion)).orElse(false)) {
throw new WriteConflictException();
throw new WriteConflictException(maybeUpdatedAccount.getCurrentProfileVersion().orElse(null));
}
return maybeUpdatedAccount;
@@ -128,7 +128,7 @@ public class IssuedReceiptsManager {
try {
dynamoDbClient.updateItem(updateItemRequest);
} catch (final ConditionalCheckFailedException _) {
throw new WriteConflictException();
throw new WriteConflictException(null);
}
}
@@ -162,7 +162,7 @@ public class ProfilesV2 {
if (cancellationCode.isPresent()) {
// any other conditional check failure is an expected data hash mismatch, which is a write conflict
Metrics.counter(WRITE_CONFLICTS_COUNTER_NAME, "code", cancellationCode.get()).increment();
throw new WriteConflictException();
throw new WriteConflictException(null);
}
}
@@ -5,7 +5,21 @@
package org.whispersystems.textsecuregcm.storage;
import javax.annotation.Nullable;
import org.whispersystems.textsecuregcm.util.NoStackTraceException;
import java.util.Optional;
public class WriteConflictException extends NoStackTraceException {
@Nullable
private final byte[] conflictedValue;
public WriteConflictException(@Nullable final byte[] conflictedValue) {
this.conflictedValue = conflictedValue;
}
/// The conflicted key, if available and relevant
public Optional<byte[]> getConflictedValue() {
return Optional.ofNullable(conflictedValue);
}
}
@@ -175,6 +175,13 @@ message SetProfileResult {
// next: 1
}
message ExpectedVersionConflict {
// In certain, highly unlikely, cases, a new profile version might not be
// distributed among all devices. These devices wouldn't be able to decrypt the
// profile, but the value can be used to set a new version.
bytes current_profile_version = 2;
}
message SetProfileResponse {
oneof response {
SetProfileResult result = 1;
@@ -187,7 +194,7 @@ message SetProfileResponse {
PaymentsForbiddenInRegion payments_forbidden_in_region = 3 [(tag.reason) = "payments_forbidden_in_region"];
// The current version did not match the request's expectation, indicating
// another device on the account may have created a new version the caller does not know about.
errors.FailedPrecondition expected_version_write_conflict = 4 [(tag.reason) = "expected_version_write_conflict"];
ExpectedVersionConflict expected_version_write_conflict = 4 [(tag.reason) = "expected_version_write_conflict"];
// next: 5
}
@@ -6,6 +6,7 @@
package org.whispersystems.textsecuregcm.grpc;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -339,7 +340,9 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
final byte[] validPaymentAddress = new byte[582];
final byte[] validPhoneNumberSharing = new byte[29];
doThrow(WriteConflictException.class)
final byte[] conflictedVersion = TestRandomUtil.nextBytes(32);
doThrow(new WriteConflictException(conflictedVersion))
.when(accountsManager).updateCurrentProfileVersion(any(), any(), any(), any());
final SetProfileRequest request = SetProfileRequest.newBuilder()
@@ -358,6 +361,7 @@ public class ProfileGrpcServiceTest extends SimpleBaseGrpcTest<ProfileGrpcServic
final SetProfileResponse response = authenticatedServiceStub().setProfile(request);
assertTrue(response.hasExpectedVersionWriteConflict());
assertArrayEquals(conflictedVersion, response.getExpectedVersionWriteConflict().getCurrentProfileVersion().toByteArray());
verify(profilesManager).set(any(), any(), any(), any());
verify(accountsManager).updateCurrentProfileVersion(any(), any(), any(), any());
}
@@ -104,7 +104,7 @@ class LoginPurchaseManagerTest {
void generateReceiptAlreadyRedeemed() throws Exception {
when(paymentProcessor.claimOneTimePurchase(PURCHASE_ID))
.thenReturn(new PaymentDetails(PURCHASE_ID, ReceiptLevel.LOGIN, PURCHASED_AT));
doThrow(new WriteConflictException())
doThrow(new WriteConflictException(null))
.when(issuedReceiptsManager).recordOneTimeIssuance(any(), any(), any(), any());
assertThatExceptionOfType(PurchaseReceiptAlreadyRedeemedException.class).isThrownBy(() ->
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest()));