Originally, we always try to re-decrypt to-device messages. This causes two problems. First, olm events cannot be decrypted twice, so if at the first attempt, the decryption succeeds, but we could not verify the event, it will be lost forever. Second, the number of undecryptable events grows larger and larger, and tryDecryptEvents() will take more time as the session proceeds.
We do two things in this diff: One is to add a failedTimes counter to undecryptable to-device events. Once it fails to decrypt and/or verify too many times, we will no longer try to decrypt them again. The other one is that, before performing the actual decryption, we check the error info we already have in the event, which can contain the decrypted raw event in case it is decrypted but unable to verify. We will use the original decrypted event, bypass actual decryption and go straight to verification. This way we correctly keep olm events that we could not verify in the past when the sender's identity key finally comes.
Type: fix