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

Infinite recursion warning #667 #668

Conversation

albertopeam
Copy link
Contributor

I have read the issue and it seems that the function in Utilities line 149 was commented but the code inside it was not copied to the function that now causes the recursion

Solution: I only forwarded the call from formatToEthereumUnits to formatToPrecision, which was the same as before introducing the recursion.

Could you take a look? I ran the unit tests but they crashed as it seems that I need a node running locally and I didn't read the doc yet. If you forward me to the doc I will install the env and I will ran the tests properly before submit the PR

@albertopeam
Copy link
Contributor Author

nevermind, I found the doc https://github.com/web3swift-team/web3swift#development

@yaroslavyaroslav yaroslavyaroslav requested review from JeneaVranceanu and removed request for JeneaVranceanu November 15, 2022 04:23
@yaroslavyaroslav
Copy link
Collaborator

@albertopeam just started the ci/cd pipeline that will check it also

@albertopeam
Copy link
Contributor Author

albertopeam commented Nov 15, 2022

I have checked locally and the local test suite runs(I also saw the CI @yaroslavyaroslav, thanks for sharing), unfortunately this function that triggers the recursion doesn't have an associated test.
My intention was to create a test to demonstrate the fix is working but I have seen that the function is internal and no caller exists in the project right now so, I have a question, is needed this function? should I change to public func? I would say that it can be removed unless some future plan to use it

function signature

static func formatToEthereumUnits(_ bigNumber: BigInt, toUnits: Utilities.Units = .eth, decimals: Int = 4, decimalSeparator: String = ".") -> String?

@yaroslavyaroslav
Copy link
Collaborator

I think that we've discussion within Discord just right about that. @janndriessen @JeneaVranceanu folks are more involved in this one, so I guess they could say more.

@JeneaVranceanu
Copy link
Collaborator

JeneaVranceanu commented Nov 15, 2022

@albertopeam
Thanks for taking a look at this issue.
Here are a few responses to your questions:

should I change to public func?

No, as that is not a scope of this issue and there were no requests to make it public so let's leave it internal.

I have seen that the function is internal and no caller exists in the project right now so, I have a question, is needed this function?

Looks like it indeed can be dropped as eventually the formatToPrecision is called and the main and only difference between formatToPrecision and formatToEthereumUnits looks to be the way the number of desired decimals is declared: numberDecimals: Int and toUnits: Utilities.Units respectively.

But I'd like to keep the toUnits: Utilities.Units because not all people know that ETH is divisible up to 18 decimal places. Though, that is something people learn quite fast.
So what I suggest is update the Units enumeration to the following representation:

extension Utilities {
    /// Various units used in Ethereum ecosystem
    public enum Units {
        case eth
        case wei
        case Kwei
        case Mwei
        case Gwei
        case Microether
        case Finney
        case custom(Int)

        public var decimals: Int {
            get {
                switch self {
                case .eth:
                    return 18
                case .wei:
                    return 0
                case .Kwei:
                    return 3
                case .Mwei:
                    return 6
                case .Gwei:
                    return 9
                case .Microether:
                    return 12
                case .Finney:
                    return 15
                case .custom(let value):
                    return value
                }
            }
        }
    }
}

Update: I'd even use this example and update names + add new cases -> https://github.com/ethereum/eth-utils/blob/master/eth_utils/units.py

@albertopeam albertopeam marked this pull request as ready for review November 15, 2022 21:02
@albertopeam
Copy link
Contributor Author

albertopeam commented Nov 15, 2022

I have updated the PR with the comments received, if I misunderstood something feel free to comment :)
@JeneaVranceanu @yaroslavyaroslav, could you take a look?

@yaroslavyaroslav yaroslavyaroslav requested review from janndriessen and JeneaVranceanu and removed request for janndriessen November 17, 2022 12:56
@yaroslavyaroslav
Copy link
Collaborator

I'm just really not into this. But folks have both expertise and rights to handle that appropriately.

janndriessen
janndriessen previously approved these changes Nov 17, 2022
Copy link
Collaborator

@janndriessen janndriessen left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the changes. 🚀

@JeneaVranceanu
Copy link
Collaborator

Waiting for CI and merging.

@JeneaVranceanu
Copy link
Collaborator

JeneaVranceanu commented Nov 17, 2022

@janndriessen I pushed a few commits to this branch. Please, take a look at the as they are small and re-approve this PR if you have no questions.
Thanks!

@janndriessen
Copy link
Collaborator

@janndriessen I pushed a few commits to this branch. Please, take a look at the as they are small and re-approve this PR if you have no questions. Thanks!

good catch with the type!

Copy link
Collaborator

@janndriessen janndriessen left a comment

Choose a reason for hiding this comment

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

🚢 it.

@janndriessen janndriessen merged commit a3fecba into web3swift-team:develop Nov 17, 2022
@albertopeam albertopeam deleted the bugfix/infinite-recursion-warning branch November 17, 2022 21:50
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.

None yet

4 participants