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

Feature/remote content hash #296

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from

Conversation

Deco354
Copy link

@Deco354 Deco354 commented Oct 25, 2020

Description

Adds a hash to RemotePost so we can identify when a remote posts content has been updated.

A lot of this logic was already present in AbstractPost so the non specialized parts of the hash have been extracted so any client of WordPressKit can generate a hash. It's also been adapted to be faster seeing as this hash will not be uploaded nor need to meet cryptographic standards but will be ran for every post the server sends and will block the user if it runs slow. AbstractPost will still be able to use the slower better collision proof version of this hash.

Fixes

Part of Conflict Resolution Dialog.

At present we use dateModified to identify if a remote post has changed which is not fully reliable.

Testing Details

Running the tests and analyzing that they do indeed test that this is functional should suffice.

  • Please check here if your pull request includes additional test coverage.

@diegoreymendez diegoreymendez requested review from diegoreymendez and removed request for diegoreymendez October 28, 2020 23:57
Copy link
Contributor

@diegoreymendez diegoreymendez left a comment

Choose a reason for hiding this comment

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

Adding some initial feedback.

return [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
}

+ (NSString *)sha256StringFromData:(NSData *)data {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this method is poorly named (I know it existed already) because it doesn't really do anything in terms of SHA256. As it is right now, this is more like hexStringFromData.

/// and at present are treated as genuine updates as they are triggered by the user changing their posts content.
- (NSString *)contentHash
{
NSString *hashedContents = [NSString stringWithFormat:@"%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@%@/%@/%@",
Copy link
Contributor

Choose a reason for hiding this comment

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

Considering we have support to hash each component in the methods below, and then combine the hash of each component (through combineHashes), is there a reason behind the choice of using a big string concatenating all of the post elements?

Copy link

@palringo-dec palringo-dec Oct 30, 2020

Choose a reason for hiding this comment

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

Performance mostly.

Hashing every property will likely minimize collisions but it took ~ 0.1s when I performance tested this. I fear this could result in a significant delay when dealing with many posts.

Property hash performance

image

For an AbstractPost upload I don't think this is a problem as a post upload isn't an action that blocks the user. However for downloading a RemotePost update this could result in real user delays as the non-arrival of this update will block them from continued use of the app.

ConcatenatedStringHash Performance

After concatenating the strings we're down to < 0.01s

image

I'm still a little bit concerned that this has the potential to be a bit too slow if we're going to be checking large numbers of posts each update. The swift algorithm I wrote earlier this month just performs a bitwise shift operation, this would be much faster than the others if it prove necessary.

Let me know your thoughts, perhaps I've missed something somewhere.
How are posts currently downloaded when in large numbers? Do we download them in batches?

This doesn't actually do any sha256 hashing
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.

3 participants