Skip to content

opt(common): GenesisBlock timestamp valid message error #6295

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions common/src/main/java/org/tron/common/args/GenesisBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,17 @@ public void setAssets(final List<Account> assets) {
*/
public void setTimestamp(final String timestamp) {
this.timestamp = timestamp;

if (this.timestamp == null) {
this.timestamp = DEFAULT_TIMESTAMP;
}

try {
long l = Long.parseLong(this.timestamp);
if (l < 0) {
} else {

Choose a reason for hiding this comment

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

This changed the original logic. What if the passed timestamp = ""? The changed logic will not catch this error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the code for catch still exists

The core issue to be addressed in this pull request (PR) is that when a user enters a negative number, an error message is displayed: " Timestamp must be a Long type.", which is incorrect.
It should be changed to: "Timestamp must be greater than or equal to 0.";

Then I optimized the code a bit further. When this.timestamp == null, DEFAULT_TIMESTAMP will be set, and I believe there's no need to execute the following verification logic

Choose a reason for hiding this comment

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

I mean the function params timestamp = "" not null, how the new logic catch it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image
The complete code length is like this, and it seems that browsing GitHub's code cannot clearly see the modified code

try {
long l = Long.parseLong(this.timestamp);
if (l < 0) {
throw new IllegalArgumentException("Timestamp(" + timestamp + ") must be greater than or equal to 0.");
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Timestamp(" + timestamp + ") must be a Long type.");
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Timestamp(" + timestamp + ") must be a Long type.");
}
}

Expand Down
Loading