Skip to content

Conversation

@KillerInk
Copy link
Contributor

@KillerInk KillerInk commented Jul 11, 2025

This PR removes reliance on the GlobalState structure to improve readability, maintainability, and functionality.

Removed the GlobalState parameter from most functions, simplifying function signatures.
Extracted relevant data directly where needed instead of relying on a global state.
These changes aim to streamline the ASIC management code while ensuring all ASIC types are properly supported and making the codebase more modular.

@github-actions
Copy link

github-actions bot commented Jul 11, 2025

Test Results

20 tests  ±0   20 ✅ ±0   0s ⏱️ ±0s
 1 suites ±0    0 💤 ±0 
 1 files   ±0    0 ❌ ±0 

Results for commit 378bd14. ± Comparison against base commit bbddfdc.

♻️ This comment has been updated with latest results.

Copy link
Collaborator

@johnny9 johnny9 left a comment

Choose a reason for hiding this comment

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

NACK

This doesn't address the issue with the dependancies between these components and main and in fact just makes a lot of this harder to eventually test.

What needs to be done is asic.h, for instance, needs to no longer know about global state.

On top of that this is far too large of a change to go in without any additional tests being written.

@KillerInk
Copy link
Contributor Author

NACK

This doesn't address the issue with the dependancies between these components and main and in fact just makes a lot of this harder to eventually test.

What needs to be done is asic.h, for instance, needs to no longer know about global state.

On top of that this is far too large of a change to go in without any additional tests being written.

the thing is that the GLOBAL_STATE is static in main but not accessible without routing it through methods, in fact its static^^
other thing is that everything that is stored in that pointer struct can lead to mem leaks when a new value get set and the old one is not freed.

char * old_extranonce_str = GLOBAL_STATE->extranonce_str;
GLOBAL_STATE->extranonce_str = stratum_api_v1_message.extranonce_str;
GLOBAL_STATE->extranonce_2_len = stratum_api_v1_message.extranonce_2_len;
free(old_extranonce_str);

like this
this makes sure that every struct with extern flag can exists only once.
i can also move the structs into own headers, then they can get included as needed

@johnny9
Copy link
Collaborator

johnny9 commented Jul 12, 2025

the thing is that the GLOBAL_STATE is static in main but not accessible without routing it through methods, in fact its static^^ other thing is that everything that is stored in that pointer struct can lead to mem leaks when a new value get set and the old one is not freed.

Ok, this might be worthwhile and I might agree, but you called the PR splitt modules but there wasn't any change in dependencies. You need to work on the commit and PR clarity here. It changes 1000 lines of code. Lets be clearer on the intent

@johnny9
Copy link
Collaborator

johnny9 commented Jul 12, 2025

other thing is that everything that is stored in that pointer struct can lead to mem leaks when a new value get set and the old one is not freed.

char * old_extranonce_str = GLOBAL_STATE->extranonce_str;
GLOBAL_STATE->extranonce_str = stratum_api_v1_message.extranonce_str;
GLOBAL_STATE->extranonce_2_len = stratum_api_v1_message.extranonce_2_len;
free(old_extranonce_str);

like this

Not everything in the struct will cause memory leaks. Only the alloc'd values like the strings. That is unavoidable. we need to free the dynamically allocated strings regardless of if GLOBAL_STATE holds the pointer or some other module.

@KillerInk
Copy link
Contributor Author

KillerInk commented Jul 12, 2025

other thing is that everything that is stored in that pointer struct can lead to mem leaks when a new value get set and the old one is not freed.

char * old_extranonce_str = GLOBAL_STATE->extranonce_str;
GLOBAL_STATE->extranonce_str = stratum_api_v1_message.extranonce_str;
GLOBAL_STATE->extranonce_2_len = stratum_api_v1_message.extranonce_2_len;
free(old_extranonce_str);

like this

Not everything in the struct will cause memory leaks. Only the alloc'd values like the strings. That is unavoidable. we need to free the dynamically allocated strings regardless of if GLOBAL_STATE holds the pointer or some other module.

i did not say it will, it can. but its now more safe because its using the same memory location and make the free obsolete.
on my side i never noted that leak^^

the thing is that the GLOBAL_STATE is static in main but not accessible without routing it through methods, in fact its static^^ other thing is that everything that is stored in that pointer struct can lead to mem leaks when a new value get set and the old one is not freed.

Ok, this might be worthwhile and I might agree, but you called the PR splitt modules but there wasn't any change in dependencies. You need to work on the commit and PR clarity here. It changes 1000 lines of code. Lets be clearer on the intent

I probably have to, if you have a good title feel free to change it

@KillerInk KillerInk marked this pull request as draft July 12, 2025 08:51
Copy link
Contributor Author

@KillerInk KillerInk left a comment

Choose a reason for hiding this comment

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

i rechecked everything requested

#include "device_config.h"
#include "wifi_module.h"
#include "pool_module.h"
#include "state_module.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

All these includes is a sign that we need to overhaul this class as well. But that's outside the scope of this PR.

// static bm_job ** active_jobs; is required to keep track of the active jobs since the
const double NONCE_SPACE = 4294967296.0; // 2^32

double ASIC_get_asic_job_frequency_ms(float frequency)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting to see this moved here. Kinda makes sense, at least in the current version.

#include "system_module.h"
#include "device_config.h"
#include "pool_module.h"
#include "state_module.h"
Copy link
Collaborator

@mutatrum mutatrum Jul 30, 2025

Choose a reason for hiding this comment

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

Similar commend to http_server, this file might need to be broken up as well. But not for this PR.


typedef struct
{
// The starting time for a certain period of mining activity.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This comment is not correct I think. AFAIK it has something to do with the statistics. Maybe those can be moved into a Statistics_module?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its correct^^

double duration = (double) (esp_timer_get_time() - module->duration_start) / 1000000;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its only used in system.c it dont have to be global

# Conflicts:
#	main/http_server/http_server.c
#	main/http_server/http_server.h
#	main/tasks/power_management_task.c
#	main/tasks/power_management_task.h
#	main/thermal/thermal.h
# Conflicts:
#	components/asic/asic.c
#	components/asic/bm1397.c
#	components/asic/include/bm1366.h
#	components/asic/include/bm1368.h
#	components/asic/include/bm1370.h
#	components/asic/include/bm1397.h
#	main/http_server/http_server.c
#	main/self_test/self_test.c
#	main/tasks/create_jobs_task.c
#	main/tasks/power_management_task.c
@mutatrum
Copy link
Collaborator

mutatrum commented Sep 5, 2025

As an exercise to the reader (me) I tried to only make GLOBAL_STATE static, and having done that, I'm not sure if this is the right direction to go. It makes testing harder, as you can't easily inject mock or test data anymore. To be able to do this, we would need to revert back to accepting parameters again. Furthermore, it makes lazy programming easier, by not having to think about responsibilities of a function, as everything is available everywhere.

My try is in https://github.com/mutatrum/ESP-Miner/tree/static-global-state. Some stuff could be done further, f.e. the BM####_init functions don't need these parameters anymore, as the bm####.c files can access them directly. And that's exactly where it gets tricky IMO.

Having said that, I think the changes on how the modules are separated do make sense. Maybe a better way would be to indeed remove GlobalState, have the modules live in main.c and only pass around the specific modules that functions need. This forces a better encapsulation of responsibilities.

@KillerInk
Copy link
Contributor Author

this is only the top of the iceberg^^
the main problem are the cross references. class a need b and b need a.
dont know but maybe things should get a little bit more modular.
as sample the stratum and asic task.
stratum does one thing with the asic task, its sends a new notification to asic. and asic send the result to stratum
that could get done different with function callbacks. then both classes dont rely on each other and you can easly replace the asic task with a different, if needed, without touching the stratum code or vise versa.
i think most the of the module structs can get removed/replaced by a local usage with a more modular approach.
that could also help to simplyfy and improve testing and reduce code complexity and force a better encapsulation of responsibilities^^

https://github.com/KillerInk/ESP-Miner/blob/385739054fc73e7516e0fc36e39533da44d010de/main/main.c#L91-L92

https://github.com/KillerInk/ESP-Miner/blob/8f43e63d151c35542534d4719d2a6153c4e7ddc5/main/tasks/asic_task.h#L12-L13
https://github.com/KillerInk/ESP-Miner/blob/8f43e63d151c35542534d4719d2a6153c4e7ddc5/main/tasks/asic_task.c#L29-L30
https://github.com/KillerInk/ESP-Miner/blob/8f43e63d151c35542534d4719d2a6153c4e7ddc5/main/tasks/asic_task_common.c#L27-L31

https://github.com/KillerInk/ESP-Miner/blob/8f43e63d151c35542534d4719d2a6153c4e7ddc5/main/tasks/stratum_task.h#L8
https://github.com/KillerInk/ESP-Miner/blob/8f43e63d151c35542534d4719d2a6153c4e7ddc5/main/tasks/stratum_task.c#L33
https://github.com/KillerInk/ESP-Miner/blob/8f43e63d151c35542534d4719d2a6153c4e7ddc5/main/tasks/stratum_task.c#L336

KillerInk and others added 8 commits September 17, 2025 07:25
# Conflicts:
#	components/asic/asic.c
#	components/asic/bm1397.c
#	components/asic/include/bm1366.h
#	components/asic/include/bm1368.h
#	components/asic/include/bm1370.h
#	components/asic/include/bm1397.h
#	main/global_state.h
#	main/http_server/http_server.c
#	main/main.c
#	main/screen.c
#	main/self_test/self_test.c
#	main/system.c
#	main/tasks/create_jobs_task.c
#	main/tasks/power_management_task.c
#	main/tasks/statistics_task.c
#	main/tasks/stratum_task.c
#	main/thermal/thermal.c
#	main/thermal/thermal.h
…dule2

# Conflicts:
#	components/asic/asic.c
#	main/self_test/self_test.c
#	main/tasks/create_jobs_task.c
#	main/tasks/power_management_task.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

question Further information is requested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants