Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make lots of OlmDevice asynchronous #524

Merged
merged 18 commits into from
Aug 17, 2017
Merged

Make lots of OlmDevice asynchronous #524

merged 18 commits into from
Aug 17, 2017

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented Aug 10, 2017

A whole series of commits, each making a different method of OlmDevice asynchronous.

The tests in this code suffer from the races fixed in #523, so the tests are failing until that lands.

Prepare for some refactoring which will add an extra tick to decryption by
adding some `awaitDecryption` calls in the integration tests.
Add an asynchronous `init` method to OlmDevice which initialises the OlmAccount.
* OlmDevice.getSessionIdsForDevice
* OlmDevice.getSessionIdForDevice
* OlmDevice.getSessionInfoForDevice
* OlmDevice.encryptMessage
* OlmDevice.decryptMessage
* OlmDevice.matchesSession
* OlmDevice.getOneTimeKeys
* OlmDevice.markKeysAsPublished
* OlmDevice.generateOneTimeKeys becomes async
* Stash maxOneTimeKeys at init so that maxNumberOfOneTimeKeys can remain sync
* OlmDevice.hasInboundSessionKeys
* OlmDevice.getInboundGroupSessionKey

The latter means that MegolmDecryption.shareKeysWithDevice takes longer before
it sends out the keyshare, so means the unit test needed an update
console.log(client.credentials.userId + " received event",
event);

client.removeListener("event", onEvent);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because then it would be harder to ignore the m.room.member events

@@ -144,7 +148,7 @@ describe("MegolmDecryption", function() {
megolmDecryption.shareKeysWithDevice(keyRequest);

// it's asynchronous, so we have to wait a bit
return awaitEnsureSessions;
return awaitEncryptForDevice;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we waiting for something different now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two steps here:
1: ensure we have olm sessions for each of the devices (this has always been asynchronous as it involves API calls)
2: encrypt the content for each device (which used to be synchronous, but is now async)

In short, we now need to wait for step 2 to happen, whereas previously waiting for step 1 was sufficient

account.free();
}

this.deviceCurve25519Key = e2eKeys.curve25519;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will throw if _initialise_account throws or if there are no identity keys as e2eKeys will be undefined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if _initialise_account throws, we'll never get here.

OlmAccount.identity_keys() is specced to return a JSON-encoded object, so e2eKeys can't be undefined here.

@@ -367,10 +384,10 @@ OlmDevice.prototype.getSessionIdsForDevice = function(theirDeviceIdentityKey) {
*
* @param {string} theirDeviceIdentityKey Curve25519 identity key for the
* remote device
* @return {string?} session id, or null if no established session
* @return {Promise<string?>} session id, or null if no established session
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?string

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done e5565c6

};

/**
* @inheritdoc
*/
MegolmDecryption.prototype.hasKeysForKeyRequest = async function(keyRequest) {
MegolmDecryption.prototype.hasKeysForKeyRequest = function(keyRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling to see any rhyme or reason for which functions need to be async and which don't? How are you determining this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They all need to be asynchronous. (The actual logic was that I wanted anything that touched sessionStore to be async, because I wanted to back the sessionStore with indexeddb, so sessionStore had to get an async interface).

Of course they can be asynchronous without being async. In this case: hasInboundSessionKeys is being changed to return a Promise, so the async here is redundant. I agree it could have stayed.

utils.inherits(Crypto, EventEmitter);

/**
* Initialise the crypto module so that it is ready for use
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should mention that it returns a promise and that it is only initialised once said promise resolves.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done e5565c6

I couldn't repro the failure locally, but this looks like it should fix the
test failures.
Ok, this *really* ought to fix the racy test.
This failed a test, so let's just bump up the timeout a bit more.
@richvdh
Copy link
Member Author

richvdh commented Aug 17, 2017

I've had enough of this stuff. I'm taking the executive decision to merge it, in kegan's absence.

@richvdh richvdh merged commit 2d82a7b into develop Aug 17, 2017
@richvdh richvdh deleted the rav/async_crypto/1 branch August 17, 2017 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants