Skip to content

Calculator Package Refactoring and Performance Optimizations #1383

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

Merged
merged 18 commits into from
Aug 11, 2025

Conversation

0oM4R
Copy link
Contributor

@0oM4R 0oM4R commented Jul 7, 2025

Description

This PR contains a comprehensive refactoring of the calculator package in the grid-client to improve performance, readability, and maintainability. The changes focus on optimizing contract cost calculations, simplifying pricing logic, and enhancing test coverage.

Changes

  • Parallelized contract cost calculation using goroutines and WaitGroups
  • Simplified unit conversion functions to reduce computational overhead
  • Optimized contract cost calculation with improved node handling
  • applied the stacking discount on public Ip on the rented node, and on the name contract
  • apply the certified factor on unbilled nu

Related Issues

Checklist

  • Tests included
  • Build pass
  • Documentation
  • Code format and docstring

if errors.Is(err, ErrContractDeleted) {
return
}
resultChain <- result{err: err}
Copy link
Contributor

Choose a reason for hiding this comment

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

you can use sync.Mutex for both adding the cost and accumulating the error, you can use multierror for example

@0oM4R 0oM4R marked this pull request as draft July 10, 2025 09:15
@0oM4R 0oM4R marked this pull request as ready for review July 10, 2025 10:50
@0oM4R 0oM4R requested a review from Eslam-Nawara July 10, 2025 10:53
var totalCost int64 = 0
errList := make([]error, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

you can use "github.com/hashicorp/go-multierror" to accumilate the error instead of a slice of errors

Comment on lines 303 to 308
if len(errList) > 0 {
if len(errList) == 1 {
return 0, errList[0]
}
totalCost += cost
return 0, fmt.Errorf("multiple errors occurred: %v", errList)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

you should not skip errors even if you have multiple ones, you need to send them back to the caller, thus using multierr is much easier

@0oM4R 0oM4R requested a review from Eslam-Nawara July 13, 2025 15:19
@0oM4R 0oM4R force-pushed the development_refactor branch from 11a074a to 09f9e03 Compare August 11, 2025 12:58
@rawdaGastan rawdaGastan merged commit 6f2ee2c into development Aug 11, 2025
40 checks passed
@rawdaGastan rawdaGastan deleted the development_refactor branch August 11, 2025 13:02
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