refactor: add a single byte array variant of merge, improve tests for merge conflict resolution

This commit is contained in:
0x330a 2022-12-09 11:36:02 +11:00
parent a6ebfd61c5
commit 2df2e8c713
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
3 changed files with 49 additions and 8 deletions

View File

@ -135,15 +135,15 @@ class InstrumentedTests {
assertFalse(newConf.needsDump())
userProfile.setName("Nibbler")
newConf.setName("Raz")
userProfile.setName("Raz")
newConf.setName("Nibbler")
newConf.setPic(UserPic("http://new.example.com/pic", "qwertyuio".encodeToByteArray()))
val conf = userProfile.push()
val conf2 = newConf.push()
val dump1 = userProfile.dump()
val dump2 = userProfile.dump()
userProfile.dump()
userProfile.dump()
assertFalse(conf.config.contentEquals(conf2.config))
@ -154,18 +154,48 @@ class InstrumentedTests {
assertTrue(userProfile.needsPush())
val newSeq1 = userProfile.push()
val newSeq2 = newConf.push()
assertEquals(3, newSeq1.seqNo)
assertEquals(3, newSeq2.seqNo)
// assume newConf push gets rejected as it was last to write and clear previous config by hash on oxenss
newConf.merge(arrayOf(newSeq1.config))
val newSeqMerge = newConf.push()
assertEquals("Nibbler", newConf.getName())
assertEquals("Nibbler", userProfile.getName())
assertEquals(3, newSeqMerge.seqNo)
// userProfile device polls and merges
userProfile.merge(arrayOf(newSeqMerge.config))
val userConfigMerge = userProfile.push()
assertEquals(3, userConfigMerge.seqNo)
assertEquals("Raz", newConf.getName())
assertEquals("Raz", userProfile.getName())
userProfile.free()
newConf.free()
}
@Test
fun merge_resolves_conflicts() {
val kp = keyPair
val a = UserProfile.newInstance(kp.secretKey)
val b = UserProfile.newInstance(kp.secretKey)
a.setName("A")
val (aPush, aSeq) = a.push()
b.setName("B")
// polls and sees invalid state, has to merge
b.merge(aPush)
val (bPush, bSeq) = b.push()
assertEquals("B", b.getName())
assertEquals(1, aSeq)
assertEquals(2, bSeq)
}
@Test
fun jni_setting_getting() {
val userProfile = UserProfile.newInstance(keyPair.secretKey)

View File

@ -194,7 +194,7 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_confirmPushed(JNIEnv *en
}
extern "C"
JNIEXPORT jint JNICALL
Java_network_loki_messenger_libsession_1util_ConfigBase_merge(JNIEnv *env, jobject thiz,
Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3_3B(JNIEnv *env, jobject thiz,
jobjectArray to_merge) {
auto conf = ptrToConfigBase(env, thiz);
size_t number = env->GetArrayLength(to_merge);
@ -205,4 +205,13 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_merge(JNIEnv *env, jobje
configs.push_back(bytes);
}
return conf->merge(configs);
}
extern "C"
JNIEXPORT jint JNICALL
Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3B(JNIEnv *env, jobject thiz,
jbyteArray to_merge) {
auto conf = ptrToConfigBase(env, thiz);
std::vector<session::ustring> configs = {ustring_from_bytes(env, to_merge)};
return conf->merge(configs);
}

View File

@ -18,6 +18,8 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) {
external fun encryptionDomain(): String
external fun confirmPushed(seqNo: Long)
external fun merge(toMerge: Array<ByteArray>): Int
// Singular merge
external fun merge(toMerge: ByteArray): Int
}
class UserProfile(pointer: Long): ConfigBase(pointer) {