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

clean up redundant code in eth/rlp/writer.nim #755

Merged
merged 6 commits into from
Nov 6, 2024
Merged

Conversation

chirag-parmar
Copy link
Contributor

@chirag-parmar chirag-parmar commented Oct 24, 2024

Changelog

  • renamed checkedOptionalFields to countOptionalFields and removed unnecessary abstractions(optionalFieldsNum) for counting optional fields
  • removes redundant hasOptional in favor of countOptionalFields.
  • removed macro countFieldsRuntimeImpl in favor of the nimbus style guide.

Rationale

  • Logical comparison on booleans (x == True) is equivalent to that of integers (x > 0) therefore hasOptional was redundant
  • All the counting of optional fields anyways happened in runtime(op of enumerateRlpFields) all that was required was an additional check for emptiness of optional values. Hence the macro countFieldsRuntimeImpl was not required.

@chirag-parmar chirag-parmar marked this pull request as draft October 24, 2024 11:59
@chirag-parmar chirag-parmar force-pushed the clean-up-rlp-writer branch 2 times, most recently from c55169b to 6a73a9d Compare October 25, 2024 04:36
@chirag-parmar chirag-parmar marked this pull request as ready for review October 25, 2024 04:39
@chirag-parmar chirag-parmar changed the title Clean up redundant code Clean up redundant code in eth/rlp/writer.nim Oct 25, 2024
@chirag-parmar chirag-parmar changed the title Clean up redundant code in eth/rlp/writer.nim clean up redundant code in eth/rlp/writer.nim Oct 25, 2024
@chirag-parmar chirag-parmar requested a review from jangko October 25, 2024 05:00
eth/rlp/writer.nim Outdated Show resolved Hide resolved
Copy link
Contributor

@jangko jangko left a comment

Choose a reason for hiding this comment

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

unorthodox but clever.

@arnetheduck
Copy link
Member

since you're here, another "discrepancy" is that https://github.com/status-im/nim-eth/blob/master/eth/rlp/options.nim#L12 exists as a helper for std/options but isn't used - a similar helper exists somewhere in nimbus for Opt and is actually used, but then in rlp itself there's some half-way Opt/Option support also, which is pretty confusing.

We're moving away from std/options broadly (in fact, I don't think we use them any more in rlp), so to give options a proper cleanup, this should also be attended to so that Opt always gets the same treatment (likely, all support for Opt should live in the rlp library proper but @jangko might know better).

@jangko
Copy link
Contributor

jangko commented Oct 25, 2024

Opt/Options often get special treatment in our encoding library. e.g. json-serialization, json-rpc, and rlp. What I mean special is not the encoding process of the field content itself, but prior to that. When d/encoding an object, the library need to decide what to do with the optional fields. So there are two parts regarding optional fields, (1) field content encoding, overrideable, and (2) handling optional fields of an object is coded in library. But I agree rlp/options.nim can be removed.

example in json-serialization:
https://github.com/status-im/nim-json-serialization/blob/6eadb6e939ffa7882ff5437033c11a9464d3385c/json_serialization/writer.nim#L91-L99

example in json-rpc:
https://github.com/status-im/nim-json-rpc/blob/98a5efba4de26ac852d0715656f6b0c52a203a75/json_rpc/private/server_handler_wrapper.nim#L138-L154

@chirag-parmar
Copy link
Contributor Author

@chirag-parmar chirag-parmar force-pushed the clean-up-rlp-writer branch 2 times, most recently from 77fe3b2 to 276e391 Compare October 28, 2024 09:45
@chirag-parmar chirag-parmar marked this pull request as draft October 29, 2024 03:16

export
options, rlp
export
Copy link
Member

Choose a reason for hiding this comment

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

exports go on top, below the import, not sure why they are here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I'll change it.

std/options,
../rlp
import ../rlp
import results
Copy link
Member

Choose a reason for hiding this comment

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

options.nim is so named after the module it provides support for - this file should thus be renamed to results.nim

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah makes sense. I'll change it

Copy link
Contributor Author

@chirag-parmar chirag-parmar Oct 31, 2024

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@arnetheduck I have wasted too much time for this simple issue. Having the same name as the package is causing an error "cannot import itself". I tried to look for the rules of qualification and landed on the above issue. Its significantly old so maybe you know a way to qualify the package nim-results in an import.

Copy link
Member

Choose a reason for hiding this comment

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

pkg/results and ./results

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🙈

@chirag-parmar chirag-parmar marked this pull request as ready for review October 31, 2024 16:14
@jangko jangko merged commit 034b788 into master Nov 6, 2024
18 checks passed
@jangko jangko deleted the clean-up-rlp-writer branch November 6, 2024 04:46
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