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

qt: added verification of minimal json in the configure name dialog box #537

Merged
merged 1 commit into from
Apr 1, 2024

Conversation

junekomeiji
Copy link

I added checking of JSON minimality in names/applications.cpp alongside the associated unit tests and added them to the configure name dialog box. The dialog box should tell you if you inputted an non-minimal JSON value and warn you against configuring your domain with said invalid value, but should allow you to continue anyway if you were to insist, as well as allow you to minimalise said JSON value before writing it onto the blockchain.

return false;
} else {
std::string minimalJSON = GetMinimalJSON(text);
LogDebug(BCLog::NAMES, "Minimalised JSON string is: %s \n", minimalJSON);
Copy link
Member

Choose a reason for hiding this comment

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

I suspect that this log entry would be better if it were only done when the return value (see next line) is false, otherwise it may be a bit spammy. I'm not certain though; @domob1812 what do you think?

Choose a reason for hiding this comment

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

Yes indeed, I think this should only be logged (if at all) for when it fails the check.

Copy link
Member

Choose a reason for hiding this comment

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

This line should probably be indented now that it's within an if block?

Copy link
Member

Choose a reason for hiding this comment

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

Possible refactor idea: create a bool variable called matches, set it to text == minimalJSON, then you can avoid duplicating the comparison in the if and the return. I'm guessing that compiler optimizations will produce the same ASM either way, but I think it would be a bit more readable this way.

@@ -112,11 +143,17 @@ void ConfigureNameDialog::onDataEdited(const QString &name)
{
ui->dataSize->setText(tr("%1 / %2").arg(name.size()).arg(MAX_VALUE_LENGTH_UI));
ui->dataSize->resize(ui->dataSize->fontMetrics().horizontalAdvance(ui->dataSize->text()), ui->dataSize->height());

Copy link
Member

Choose a reason for hiding this comment

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

Can these added spaces be reverted?

@JeremyRand
Copy link
Member

Nice work Rose! Code review of 1d20b47 looks good, modulo the two tiny things I noted above. Will do a build/test now and report back.

@JeremyRand
Copy link
Member

Manual GUI testing of 1d20b47 is flawless AFAICT. Running the unit tests now....

@JeremyRand
Copy link
Member

Unit tests also pass on 1d20b47.

I think this is mergeable once the above two trivial things are sorted out (the first one of which I'd like Daniel's opinion on before we decide on what to do).

Copy link

@domob1812 domob1812 left a comment

Choose a reason for hiding this comment

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

Thanks for the change, this definitely helps, too! I've just added a few minor things I think you should resolve before merging this.

src/names/applications.cpp Outdated Show resolved Hide resolved
return false;
} else {
std::string minimalJSON = GetMinimalJSON(text);
LogDebug(BCLog::NAMES, "Minimalised JSON string is: %s \n", minimalJSON);

Choose a reason for hiding this comment

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

Yes indeed, I think this should only be logged (if at all) for when it fails the check.

@@ -62,6 +62,8 @@ void ConfigureNameDialog::accept()

returnData = ui->dataEdit->text();
returnTransferTo = ui->transferTo->text();


Choose a reason for hiding this comment

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

Is this an accidental change? Please revert it.

if(reply == QMessageBox::AcceptRole){
QDialog::accept();
} else if(reply == QMessageBox::RejectRole){

Choose a reason for hiding this comment

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

This looks unfinished? Is this still WIP?

Copy link
Author

Choose a reason for hiding this comment

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

It's so it does nothing if a user clicks the Cancel button.

Copy link
Member

Choose a reason for hiding this comment

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

I think we can just leave out the RejectRole part of the if/else chain? If the intent is to make it clear that RejectRole is a possibility, I think a comment would be sufficient for that.

Choose a reason for hiding this comment

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

Yes I would suggest to either leave it out, or if you think it needs to be explicitly explained that we want to do nothing in that case, then leave it in but add a comment to that effect.

ui->labelValidJSON->setText(tr("Valid JSON text."));
if(IsValidJSONOrEmptyString(data))
{
if(IsMinimalJSONOrEmptyString(data))

Choose a reason for hiding this comment

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

I would suggest to move this onto the top level, so it is not nested if's. What I would to to make the code more readable is to have an if-else construct on just one level, with these checks:

  • IsMinimalJSONOrEmptyString, which already checks for validity (i.e. if true, then the value is valid and minimal),
  • Else, IsValidJSONOrEmptyString, which would mean the value is valid but not minimal,
  • Else, the value is invalid JSON.

src/test/name_applications_tests.cpp Show resolved Hide resolved
src/test/name_applications_tests.cpp Show resolved Hide resolved

if(!v.read(text)){
return false;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Per Daniel's comment about the above else usage, I think you can also avoid else here since the if block includes a return.

@@ -62,7 +62,7 @@ void ConfigureNameDialog::accept()

returnData = ui->dataEdit->text();
returnTransferTo = ui->transferTo->text();

Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the spaces that were added on this line?

} else {
ui->labelValidJSON->setText(tr("JSON data inputted is invalid."));
}

Copy link
Member

Choose a reason for hiding this comment

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

I think this blank line can be removed?

@JeremyRand
Copy link
Member

ACK 5d41631. Unit tests pass, manual GUI testing works fine, code review looks good.

@domob1812 If this gets an ACK from you, is it OK if I merge to minimize delays?

Copy link

@domob1812 domob1812 left a comment

Choose a reason for hiding this comment

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

Looks good to me, please just consider the one more comment I added. Otherwise it is ready to squash and merge.


std::string minimalJSON = GetMinimalJSON(text);

bool matches = (text == minimalJSON);

Choose a reason for hiding this comment

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

Both of minimalJSON and matches can be declared as const, and I think it is good style to do so in this case. Also I would rename matches maybe to isMinimal to make the code more readable.

@JeremyRand
Copy link
Member

Code review of a6a991d looks good to me, testing now....

@JeremyRand
Copy link
Member

ACK a6a991d; code review looks good, manual testing shows no issues, unit tests pass.

@domob1812 if this gets your ACK as well, is it OK if I merge it?

Copy link

@domob1812 domob1812 left a comment

Choose a reason for hiding this comment

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

ACK a6a991d. I can merge this myself now.

@domob1812 domob1812 merged commit 6d42a89 into namecoin:master Apr 1, 2024
13 of 16 checks passed
luisriverag pushed a commit to luisriverag/namecoin-core that referenced this pull request Apr 8, 2024
fe7c81e qt: change QLineEdit error background (w0xlt)

Pull request description:

  This PR proposes a small change in QLineEdit when there is an error in the input.

  master |
  --- |
  ![image](https://user-images.githubusercontent.com/94266259/154762427-b816267e-ec70-4a8f-a7fb-1317ebacf1a4.png)

  PR |
  --- |
  ![image](https://user-images.githubusercontent.com/94266259/154761933-15eb3d81-ca81-4498-b8ec-cf1139ae2f8a.png) |

  This also shows good results when combined with other open PRs.

  namecoin#537 |
  --- |
  ![image](https://user-images.githubusercontent.com/94266259/154763411-6266a283-2d8a-4365-b3f2-a5cb545e773e.png)

  namecoin#533 |
  --- |
  ![image](https://user-images.githubusercontent.com/94266259/154765638-f38b13d6-a4f8-4b46-a470-f882668239f3.png) |

ACKs for top commit:
  GBKS:
    ACK fe7c81e
  jarolrod:
    ACK fe7c81e
  shaavan:
    ACK fe7c81e

Tree-SHA512: eccc53f42d11291944ccb96efdbe460cb10af857f1d4fa9b5348ddcb0796c82faf1cdad9040aae7a25c5d8f4007d6284eba868d7af14acf56280f6acae170b91
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