Skip to content

Commit

Permalink
Rename GV2 "version" to "revision".
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-signal authored and greyson-signal committed May 29, 2020
1 parent c634c24 commit a99c0d4
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 136 deletions.
Expand Up @@ -336,7 +336,7 @@ private void create(@NonNull GroupId groupId,
}
groupId.requireV2();
contentValues.put(V2_MASTER_KEY, groupMasterKey.serialize());
contentValues.put(V2_REVISION, groupState.getVersion());
contentValues.put(V2_REVISION, groupState.getRevision());
contentValues.put(V2_DECRYPTED_GROUP, groupState.toByteArray());
contentValues.put(MEMBERS, serializeV2GroupMembers(groupState));
} else {
Expand Down Expand Up @@ -394,7 +394,7 @@ public void update(@NonNull GroupId.V2 groupId, @NonNull DecryptedGroup decrypte
UUID uuid = Recipient.self().getUuid().get();

contentValues.put(TITLE, title);
contentValues.put(V2_REVISION, decryptedGroup.getVersion());
contentValues.put(V2_REVISION, decryptedGroup.getRevision());
contentValues.put(V2_DECRYPTED_GROUP, decryptedGroup.toByteArray());
contentValues.put(MEMBERS, serializeV2GroupMembers(decryptedGroup));
contentValues.put(ACTIVE, DecryptedGroupUtil.findMemberByUuid(decryptedGroup.getMembersList(), uuid).isPresent() ||
Expand Down
Expand Up @@ -56,7 +56,7 @@ String describeNewGroup(@NonNull DecryptedGroup group) {
return context.getString(R.string.MessageRecord_s_invited_you_to_the_group, describe(selfPending.get().getAddedByUuid()));
}

if (group.getVersion() == 0) {
if (group.getRevision() == 0) {
Optional<DecryptedMember> foundingMember = DecryptedGroupUtil.firstMember(group.getMembersList());
if (foundingMember.isPresent()) {
ByteString foundingMemberUuid = foundingMember.get().getUuid();
Expand Down
Expand Up @@ -157,7 +157,7 @@ public SpannableString getDisplayBody(@NonNull Context context) {
DecryptedGroupV2Context decryptedGroupV2Context = DecryptedGroupV2Context.parseFrom(decoded);
GroupsV2UpdateMessageProducer updateMessageProducer = new GroupsV2UpdateMessageProducer(context, descriptionStrategy, Recipient.self().getUuid().get());

if (decryptedGroupV2Context.hasChange() && decryptedGroupV2Context.getGroupState().getVersion() > 0) {
if (decryptedGroupV2Context.hasChange() && decryptedGroupV2Context.getGroupState().getRevision() > 0) {
DecryptedGroupChange change = decryptedGroupV2Context.getChange();
List<String> strings = updateMessageProducer.describeChange(change);
StringBuilder result = new StringBuilder();
Expand Down
Expand Up @@ -147,13 +147,13 @@ public static void ejectFromGroup(@NonNull Context context, @NonNull GroupId.V2
@WorkerThread
public static void updateGroupFromServer(@NonNull Context context,
@NonNull GroupMasterKey groupMasterKey,
int version,
int revision,
long timestamp,
@Nullable byte[] signedGroupChange)
throws GroupChangeBusyException, IOException, GroupNotAMemberException
{
try (GroupManagerV2.GroupUpdater updater = new GroupManagerV2(context).updater(groupMasterKey)) {
updater.updateLocalToServerVersion(version, timestamp, signedGroupChange);
updater.updateLocalToServerRevision(revision, timestamp, signedGroupChange);
}
}

Expand Down
Expand Up @@ -375,7 +375,7 @@ private GroupManager.GroupActionResult commitChange(@NonNull GroupChange.Actions
final GroupDatabase.GroupRecord groupRecord = groupDatabase.requireGroup(groupId);
final GroupDatabase.V2GroupProperties v2GroupProperties = groupRecord.requireV2GroupProperties();
final int nextRevision = v2GroupProperties.getGroupRevision() + 1;
final GroupChange.Actions changeActions = change.setVersion(nextRevision).build();
final GroupChange.Actions changeActions = change.setRevision(nextRevision).build();
final DecryptedGroupChange decryptedChange;
final DecryptedGroup decryptedGroupState;

Expand Down Expand Up @@ -427,11 +427,11 @@ class GroupUpdater implements Closeable {
}

@WorkerThread
void updateLocalToServerVersion(int version, long timestamp, @Nullable byte[] signedGroupChange)
void updateLocalToServerRevision(int revision, long timestamp, @Nullable byte[] signedGroupChange)
throws IOException, GroupNotAMemberException
{
new GroupsV2StateProcessor(context).forGroup(groupMasterKey)
.updateLocalGroupToRevision(version, timestamp, getDecryptedGroupChange(signedGroupChange));
.updateLocalGroupToRevision(revision, timestamp, getDecryptedGroupChange(signedGroupChange));
}

private DecryptedGroupChange getDecryptedGroupChange(@Nullable byte[] signedGroupChange) {
Expand Down
Expand Up @@ -15,11 +15,11 @@
import org.signal.storageservice.protos.groups.local.DecryptedPendingMember;
import org.signal.zkgroup.groups.GroupMasterKey;
import org.signal.zkgroup.util.UUIDUtil;
import org.thoughtcrime.securesms.database.model.databaseprotos.DecryptedGroupV2Context;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.whispersystems.signalservice.api.util.UuidUtil;
import org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations;
import org.whispersystems.signalservice.api.util.UuidUtil;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
import org.thoughtcrime.securesms.database.model.databaseprotos.DecryptedGroupV2Context;

import java.util.List;
import java.util.UUID;
Expand All @@ -29,19 +29,19 @@ public final class GroupProtoUtil {
private GroupProtoUtil() {
}

public static int findVersionWeWereAdded(@NonNull DecryptedGroup group, @NonNull UUID uuid)
public static int findRevisionWeWereAdded(@NonNull DecryptedGroup group, @NonNull UUID uuid)
throws GroupNotAMemberException
{
ByteString bytes = UuidUtil.toByteString(uuid);
for (DecryptedMember decryptedMember : group.getMembersList()) {
if (decryptedMember.getUuid().equals(bytes)) {
return decryptedMember.getJoinedAtVersion();
return decryptedMember.getJoinedAtRevision();
}
}
for (DecryptedPendingMember decryptedMember : group.getPendingMembersList()) {
if (decryptedMember.getUuid().equals(bytes)) {
// Assume latest, we don't have any information about when pending members were invited
return group.getVersion();
return group.getRevision();
}
}
throw new GroupNotAMemberException();
Expand All @@ -52,10 +52,10 @@ public static DecryptedGroupV2Context createDecryptedGroupV2Context(@NonNull Gro
@Nullable DecryptedGroupChange plainGroupChange,
@Nullable GroupChange signedServerChange)
{
int version = plainGroupChange != null ? plainGroupChange.getVersion() : decryptedGroup.getVersion();
int revision = plainGroupChange != null ? plainGroupChange.getRevision() : decryptedGroup.getRevision();
SignalServiceProtos.GroupContextV2.Builder contextBuilder = SignalServiceProtos.GroupContextV2.newBuilder()
.setMasterKey(ByteString.copyFrom(masterKey.serialize()))
.setRevision(version);
.setRevision(revision);

if (signedServerChange != null) {
contextBuilder.setGroupChange(signedServerChange.toByteString());
Expand Down
Expand Up @@ -31,13 +31,13 @@ final class GlobalGroupState {
return history;
}

int getLatestVersionNumber() {
int getLatestRevisionNumber() {
if (history.isEmpty()) {
if (localState == null) {
throw new AssertionError();
}
return localState.getVersion();
return localState.getRevision();
}
return history.get(history.size() - 1).getGroup().getVersion();
return history.get(history.size() - 1).getGroup().getRevision();
}
}
Expand Up @@ -17,7 +17,7 @@ final class GroupLogEntry {
@Nullable private final DecryptedGroupChange change;

GroupLogEntry(@NonNull DecryptedGroup group, @Nullable DecryptedGroupChange change) {
if (change != null && group.getVersion() != change.getVersion()) {
if (change != null && group.getRevision() != change.getRevision()) {
throw new AssertionError();
}

Expand Down
Expand Up @@ -12,20 +12,20 @@ final class GroupStateMapper {

static final int LATEST = Integer.MAX_VALUE;

private static final Comparator<GroupLogEntry> BY_VERSION = (o1, o2) -> Integer.compare(o1.getGroup().getVersion(), o2.getGroup().getVersion());
private static final Comparator<GroupLogEntry> BY_REVISION = (o1, o2) -> Integer.compare(o1.getGroup().getRevision(), o2.getGroup().getRevision());

private GroupStateMapper() {
}

/**
* Given an input {@link GlobalGroupState} and a {@param maximumVersionToApply}, returns a result
* containing what the new local group state should be, and any remaining version history to apply.
* Given an input {@link GlobalGroupState} and a {@param maximumRevisionToApply}, returns a result
* containing what the new local group state should be, and any remaining revision history to apply.
* <p>
* Function is pure.
* @param maximumVersionToApply Use {@link #LATEST} to apply the very latest.
* @param maximumRevisionToApply Use {@link #LATEST} to apply the very latest.
*/
static @NonNull AdvanceGroupStateResult partiallyAdvanceGroupState(@NonNull GlobalGroupState inputState,
int maximumVersionToApply)
int maximumRevisionToApply)
{
final ArrayList<GroupLogEntry> statesToApplyNow = new ArrayList<>(inputState.getHistory().size());
final ArrayList<GroupLogEntry> statesToApplyLater = new ArrayList<>(inputState.getHistory().size());
Expand All @@ -34,20 +34,20 @@ private GroupStateMapper() {

for (GroupLogEntry entry : inputState.getHistory()) {
if (inputState.getLocalState() != null &&
inputState.getLocalState().getVersion() >= entry.getGroup().getVersion())
inputState.getLocalState().getRevision() >= entry.getGroup().getRevision())
{
continue;
}

if (entry.getGroup().getVersion() > maximumVersionToApply) {
if (entry.getGroup().getRevision() > maximumRevisionToApply) {
statesToApplyLater.add(entry);
} else {
statesToApplyNow.add(entry);
}
}

Collections.sort(statesToApplyNow, BY_VERSION);
Collections.sort(statesToApplyLater, BY_VERSION);
Collections.sort(statesToApplyNow, BY_REVISION);
Collections.sort(statesToApplyLater, BY_REVISION);

if (statesToApplyNow.size() > 0) {
newLocalState = statesToApplyNow.get(statesToApplyNow.size() - 1)
Expand Down
Expand Up @@ -13,7 +13,6 @@
import org.signal.zkgroup.VerificationFailedException;
import org.signal.zkgroup.groups.GroupMasterKey;
import org.signal.zkgroup.groups.GroupSecretParams;
import org.signal.zkgroup.util.UUIDUtil;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.GroupDatabase;
import org.thoughtcrime.securesms.database.MmsDatabase;
Expand Down Expand Up @@ -150,8 +149,8 @@ public GroupUpdateResult updateLocalGroupToRevision(final int revision,

if (signedGroupChange != null &&
localState != null &&
localState.getVersion() + 1 == signedGroupChange.getVersion() &&
revision == signedGroupChange.getVersion())
localState.getRevision() + 1 == signedGroupChange.getRevision() &&
revision == signedGroupChange.getRevision())
{
try {
Log.i(TAG, "Applying P2P group change");
Expand Down Expand Up @@ -188,7 +187,7 @@ public GroupUpdateResult updateLocalGroupToRevision(final int revision,

GlobalGroupState remainingWork = advanceGroupStateResult.getNewGlobalGroupState();
if (remainingWork.getHistory().size() > 0) {
Log.i(TAG, String.format(Locale.US, "There are more versions on the server for this group, not applying at this time, V[%d..%d]", newLocalState.getVersion() + 1, remainingWork.getLatestVersionNumber()));
Log.i(TAG, String.format(Locale.US, "There are more revisions on the server for this group, not applying at this time, V[%d..%d]", newLocalState.getRevision() + 1, remainingWork.getLatestRevisionNumber()));
}

return new GroupUpdateResult(GroupState.GROUP_UPDATED, newLocalState);
Expand All @@ -206,10 +205,10 @@ private void insertGroupLeave() {
.requireV2GroupProperties()
.getDecryptedGroup();

DecryptedGroup simulatedGroupState = DecryptedGroupUtil.removeMember(decryptedGroup, selfUuid, decryptedGroup.getVersion() + 1);
DecryptedGroup simulatedGroupState = DecryptedGroupUtil.removeMember(decryptedGroup, selfUuid, decryptedGroup.getRevision() + 1);
DecryptedGroupChange simulatedGroupChange = DecryptedGroupChange.newBuilder()
.setEditor(UuidUtil.toByteString(UuidUtil.UNKNOWN_UUID))
.setVersion(simulatedGroupState.getVersion())
.setRevision(simulatedGroupState.getRevision())
.addDeleteMembers(UuidUtil.toByteString(selfUuid))
.build();

Expand Down Expand Up @@ -315,18 +314,18 @@ private void persistLearnedProfileKeys(@NonNull GlobalGroupState globalGroupStat
if (latestOnly || !GroupProtoUtil.isMember(selfUuid, latestServerGroup.getMembersList())) {
history = Collections.singletonList(new GroupLogEntry(latestServerGroup, null));
} else {
int versionWeWereAdded = GroupProtoUtil.findVersionWeWereAdded(latestServerGroup, selfUuid);
int logsNeededFrom = localState != null ? Math.max(localState.getVersion(), versionWeWereAdded) : versionWeWereAdded;
int revisionWeWereAdded = GroupProtoUtil.findRevisionWeWereAdded(latestServerGroup, selfUuid);
int logsNeededFrom = localState != null ? Math.max(localState.getRevision(), revisionWeWereAdded) : revisionWeWereAdded;

history = getFullMemberHistory(selfUuid, logsNeededFrom);
}

return new GlobalGroupState(localState, history);
}

private List<GroupLogEntry> getFullMemberHistory(@NonNull UUID selfUuid, int logsNeededFrom) throws IOException {
private List<GroupLogEntry> getFullMemberHistory(@NonNull UUID selfUuid, int logsNeededFromRevision) throws IOException {
try {
Collection<DecryptedGroupHistoryEntry> groupStatesFromRevision = groupsV2Api.getGroupHistory(groupSecretParams, logsNeededFrom, groupsV2Authorization.getAuthorizationForToday(selfUuid, groupSecretParams));
Collection<DecryptedGroupHistoryEntry> groupStatesFromRevision = groupsV2Api.getGroupHistory(groupSecretParams, logsNeededFromRevision, groupsV2Authorization.getAuthorizationForToday(selfUuid, groupSecretParams));
ArrayList<GroupLogEntry> history = new ArrayList<>(groupStatesFromRevision.size());

for (DecryptedGroupHistoryEntry entry : groupStatesFromRevision) {
Expand Down
Expand Up @@ -545,9 +545,9 @@ private static class GroupStateBuilder {

private final DecryptedGroup.Builder builder;

GroupStateBuilder(@NonNull UUID foundingMember, int version) {
GroupStateBuilder(@NonNull UUID foundingMember, int revision) {
builder = DecryptedGroup.newBuilder()
.setVersion(version)
.setRevision(revision)
.addMembers(DecryptedMember.newBuilder()
.setUuid(UuidUtil.toByteString(foundingMember)));
}
Expand Down
Expand Up @@ -14,32 +14,32 @@ public final class GlobalGroupStateTest {
public void cannot_ask_latestVersionNumber_of_empty_state() {
GlobalGroupState emptyState = new GlobalGroupState(null, emptyList());

emptyState.getLatestVersionNumber();
emptyState.getLatestRevisionNumber();
}

@Test
public void latestVersionNumber_of_state_and_empty_list() {
public void latestRevisionNumber_of_state_and_empty_list() {
GlobalGroupState emptyState = new GlobalGroupState(state(10), emptyList());

assertEquals(10, emptyState.getLatestVersionNumber());
assertEquals(10, emptyState.getLatestRevisionNumber());
}

@Test
public void latestVersionNumber_of_state_and_list() {
public void latestRevisionNumber_of_state_and_list() {
GlobalGroupState emptyState = new GlobalGroupState(state(2), asList(logEntry(3), logEntry(4)));

assertEquals(4, emptyState.getLatestVersionNumber());
assertEquals(4, emptyState.getLatestRevisionNumber());
}

private static GroupLogEntry logEntry(int version) {
return new GroupLogEntry(state(version), change(version));
private static GroupLogEntry logEntry(int revision) {
return new GroupLogEntry(state(revision), change(revision));
}

private static DecryptedGroup state(int version) {
return DecryptedGroup.newBuilder().setVersion(version).build();
private static DecryptedGroup state(int revision) {
return DecryptedGroup.newBuilder().setRevision(revision).build();
}

private static DecryptedGroupChange change(int version) {
return DecryptedGroupChange.newBuilder().setVersion(version).build();
private static DecryptedGroupChange change(int revision) {
return DecryptedGroupChange.newBuilder().setRevision(revision).build();
}
}
Expand Up @@ -115,7 +115,7 @@ public void known_group_three_states_to_update_update_latest() {
}

@Test
public void apply_maximum_group_versions() {
public void apply_maximum_group_revisions() {
DecryptedGroup currentState = state(Integer.MAX_VALUE - 2);
GroupLogEntry log1 = logEntry(Integer.MAX_VALUE - 1);
GroupLogEntry log2 = logEntry(Integer.MAX_VALUE);
Expand All @@ -132,15 +132,15 @@ private static void assertNewState(GlobalGroupState expected, GlobalGroupState a
assertThat(actual.getHistory(), is(expected.getHistory()));
}

private static GroupLogEntry logEntry(int version) {
return new GroupLogEntry(state(version), change(version));
private static GroupLogEntry logEntry(int revision) {
return new GroupLogEntry(state(revision), change(revision));
}

private static DecryptedGroup state(int version) {
return DecryptedGroup.newBuilder().setVersion(version).build();
private static DecryptedGroup state(int revision) {
return DecryptedGroup.newBuilder().setRevision(revision).build();
}

private static DecryptedGroupChange change(int version) {
return DecryptedGroupChange.newBuilder().setVersion(version).build();
private static DecryptedGroupChange change(int revision) {
return DecryptedGroupChange.newBuilder().setRevision(revision).build();
}
}
Expand Up @@ -12,7 +12,7 @@ public final class DecryptedGroupHistoryEntry {
private final DecryptedGroupChange change;

DecryptedGroupHistoryEntry(DecryptedGroup group, DecryptedGroupChange change) {
if (group.getVersion() != change.getVersion()) {
if (group.getRevision() != change.getRevision()) {
throw new AssertionError();
}

Expand Down

0 comments on commit a99c0d4

Please sign in to comment.