'Stores device keys for a device. Returns whether there was a change
or the keys were already in the database.'

def set_e2e_device_keys(self, user_id, device_id, time_now, device_keys):

    def _set_e2e_device_keys_txn(txn):
        old_key_json = self._simple_select_one_onecol_txn(txn, table='e2e_device_keys_json', keyvalues={'user_id': user_id, 'device_id': device_id}, retcol='key_json', allow_none=True)
        new_key_json = encode_canonical_json(device_keys)
        if (old_key_json == new_key_json):
            return False
        self._simple_upsert_txn(txn, table='e2e_device_keys_json', keyvalues={'user_id': user_id, 'device_id': device_id}, values={'ts_added_ms': time_now, 'key_json': new_key_json})
        return True
    return self.runInteraction('set_e2e_device_keys', _set_e2e_device_keys_txn)
