hashgraph / hedera-services

Crypto, token, consensus, file, and smart contract services for the Hedera public ledger
Apache License 2.0
313 stars 137 forks source link

Rewrite CustomFractionalFeeAssessor.reclaim() to use safe math operations #15376

Open netopyr opened 2 months ago

netopyr commented 2 months ago

🆕🐥 First Timers Only

This issue is reserved for people who have never contributed to Hedera or any open source project in general. We know that creating a pull request (PR) is a major barrier for new contributors. The goal of this issue and all other issues labeled by 'Good First Issue' is to help you make your first contribution to Hedera.

👾 Description of the issue

The method CustomFractionalFeeAssessor.reclaim() sums up values from a Map:

    private long reclaim(final long amount, @NonNull final Map<AccountID, Long> credits) {
        var availableToReclaim = 0L;
        for (final var entry : credits.entrySet()) {
            availableToReclaim += entry.getValue();
            validateTrue(availableToReclaim >= 0, CUSTOM_FEE_OUTSIDE_NUMERIC_RANGE);
        }
        ...

To ensure there is no overflow, it checks after each add, if the value has become negative. As all summands are positive, this can only happen after an overflow in which case an Exception is thrown.

While technically correct, this implementation is confusing. Adding some comments would help, but changing the code to make the intent more obvious is even better.

Proposed Solution:

Reimplement the above functionality using Math.addExact() which throws an ArithmeticException if an overflow occurs. Feel free to use the Stream-API instead of an explicit loop.

📋 Step by step guide to do a contribution

If you have never contributed to an open source project at GitHub, the following step-by-step guide will introduce you to the workflow. More information and concrete samples for shell commands for each step can be found in our CONTRIBUTING.md file. A more detailed general documentation of the GitHub PR workflow can be found here.

🎉 Contribute to Hacktoberfest

Solve this issue as part of the Hacktoberfest event and get a chance to receive cool goodies like a T-Shirt. 🎽

🤔 Additional Information

If you have any questions, just ask us directly in this issue by adding a comment. You can join our community chat at Discord. A general manual about open-source contributions can be found here.

Ayush-Baranwal commented 2 months ago

hi @netopyr I would like to contribute to this issue.

SanikaBhalerao1345 commented 1 month ago

hii @netopyr, being a beginner i would like to contribute to this issue

boooby19 commented 1 month ago

Hello ! I am new to open-source projects. I am working as junior dev in c#/React, also have 3 year experience with Java. Can I work on this issue? Thanks ! :)

netopyr commented 1 month ago

@SanikaBhalerao1345 please go ahead. If you have any questions, do not hesitate to ask.

@boooby19 I assigned to you the other issue you were interested in.

Ndacyayisenga-droid commented 3 weeks ago

@boooby19 are you still interested in this issue??. Otherwise I can take it up