Skip to content

Event Refactor Phase 1#440

Closed
jmjatlanta wants to merge 9 commits intoGLEECBTC:devfrom
jmjatlanta:jmj_dev_event_tests
Closed

Event Refactor Phase 1#440
jmjatlanta wants to merge 9 commits intoGLEECBTC:devfrom
jmjatlanta:jmj_dev_event_tests

Conversation

@jmjatlanta
Copy link
Copy Markdown

@jmjatlanta jmjatlanta commented May 18, 2021

Note: This is a re-do of pr #427, but based on the dev branch.

I would like to use this PR to open a dialog into integrating heavier use of C++ features into the codebase. Comments are encouraged. But please send any comments about what I should or should not be working on directly to me instead of commenting here.

Status as of 2021-05-10: I believe the code sufficiently demonstrates concepts that could be used in further refactoring. I believe the code to be bug-free. , but not complete, as both old and new ideologies coexist for discussion and testing purposes. More test cases are needed for edge cases that I have as yet not learned about.

Points for discussion:

  1. Implementations in header files: Much of the code I have seen contains little implementation in .cpp files and most in .h header files. The disadvantages of this approach are (1) increased compile times and (2) delicate dependencies in linking libraries or executables. Moving the implementations to .cpp files does have disadvantages as well, such as objects compiled with different parameters can cause different (sometimes hard to diagnose) errors, but it is my opinion that the benefits outweigh the dangers.
  2. Use the standard library where it makes sense to avoid platform-specific code. Two changes highlight the benefits here. Mutexes (std::mutex insted of pthread's version), and lists (std::list vs ptr-to-ptr lists).
  3. Remove use of pointers where possible (i.e. pass references if possible, const references are even better), use reference counted pointers instead of raw pointers. The std::list vs ptr-to-ptr is an example here as well.
  4. Declare necessary variables when you need them, not at the beginning of the function. This often makes the code easier to read.
  5. Use exceptions where it makes sense. If the code is littered with if statements for error checking, consider using try/catch. This can reduce the code you need to write, as well as provide a performance boost in some instances if done correctly.
  6. Use namespaces. Instead of starting each method name with komdo_, consider using namespace komodo. This reduces name collisions within the codebase (we know what an "event" is in komdo, and don't have to worry about puting "komodo_" in front of it, or a UI event having a name collision with it). This also often makes using your code as a library less cumbersome.
  7. Use bool if you only need true/false instead of other integrals. This makes your intent clear.
  8. Comment things that may be unclear. I personally use doxygen style comments on top of my functions. I hope everyone can understand my code by reading it, but sometimes it is very inconvenient to do so. Of course the evil side of comments is they are often stale or inaccurate. But the intent often remains.
  9. Push implementations to where they belong. A "notary event" needs to be serialized. It is probably best that the code to do so reside close to the definition of what a notary event is. Every serialized "event" has a header, so this is probably a good time to use a hierarchy of "notary_event is an event, and knows how to serialize itself". Now reading or writing it to disk only requires code to tell a notary event to serialize/deserialize itself. The implementation code stays centralized and out of the way.
  10. Refactor a little at a time. Knowing then what is known now, everyone would love to rewrite pretty much everything we've ever written. Avoid the urge. Bite-sized subsystems can be tackled. Eating the whole elephant will take time. The majority of the komodo_events.h file breaks point 9 above. I'd love to get rid of it, as well as big chunks of komodo.h for the same reason. But that would make testing and comparing the existing implementation with the refactor too much to take on in this round. So they will remain until they are no longer needed.
  11. Header files should be independent. Include all the #include files necessary for the .h. This makes chasing down dependencies less of a problem at the expense of a little compile time. Use #pragma once to help with this.

The above points are simply opinions. I am not here to start wars. If the team decides that any (or all) of these are a bad idea, I'll happily work without using them. Explaining why they are a bad idea helps everyone, but is not required.

Reading suggestions and References:

@jmjatlanta jmjatlanta changed the title Event Refactor Phase 1 #439 Event Refactor Phase 1 May 18, 2021
@jmjatlanta jmjatlanta marked this pull request as ready for review May 18, 2021 14:31
@jmjatlanta
Copy link
Copy Markdown
Author

jmjatlanta commented May 18, 2021

There are merging issues. Please do not review just yet. Resolved (trash in my build environment)

ca333
ca333 previously approved these changes Jul 30, 2021
Copy link
Copy Markdown

@ca333 ca333 left a comment

Choose a reason for hiding this comment

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

elegant refactoring

@DeckerSU
Copy link
Copy Markdown

DeckerSU commented Aug 5, 2021

@jmjatlanta may be i'm tired or missed something, but let's re-check 'P' (pubkey record) case. In old (non-refactored) sources the actions we do in that case were:

  1. Read the num of pubkeys num= filedata[fpos++] and make sure num <= 64.
  2. Fill the pubkeys array uint8_t pubkeys[64][33] via memread and if all is fine, mean, we actually read all needed num records -> call komodo_eventadd_pubkeys.
  3. In which we will fill the komodo_event_pubkeys struct variable P and add event via komodo_eventadd and also call komodo_notarysinit (!) with read pubkeys.

But new modern refactored implementation seems missed steps in (3), in ctor event_pubkeys::event_pubkeys(...) we read pubkeys array with mem_nread, mean, complete step (2) and after fill events list with new event via sp->add_event(...). But where the other steps from (3) and komodo_notarysinit call? I'm tired, blind, or it's really absent?

And also where is komodo_eventadd call in new implementation and filling Komodo_events member of struct komodo_state, or we decided to don't use it at all?

Copy link
Copy Markdown
Collaborator

@dimxy dimxy left a comment

Choose a reason for hiding this comment

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

do we need both memread and the new mem_read functions?

@jmjatlanta
Copy link
Copy Markdown
Author

jmjatlanta commented Aug 6, 2021

3. In which we will fill the `komodo_event_pubkeys` struct variable `P` and add event via `komodo_eventadd` and also call `komodo_notarysinit` (!) with read pubkeys.

But new modern refactored implementation seems missed steps in (3), in ctor event_pubkeys::event_pubkeys(...) we read pubkeys array with mem_nread, mean, complete step (2) and after fill events list with new event via sp->add_event(...). But where the other steps from (3) and komodo_notarysinit call? I'm tired, blind, or it's really absent?

And also where is komodo_eventadd call in new implementation and filling Komodo_events member of struct komodo_state, or we decided to don't use it at all?

The intent here was to fill a std::list of events instead of the ptr-to-ptr linked list of Komodo_events. In reviewing the code, it looks like what was written is half-baked. Both events and Komodo_events still exist. I do not recall why this is, but I suspect I PR'd the wrong branch, missed some commits, or went totally A.D.D. and came back thinking I was further along than I was.

In short, DO NOT MERGE THIS or waste time reviewing until I sort out where my train went off the rails. My apologies.

Update: My branch jmj_dev_event_tests_2 seems to be a mix of a few things I was working on at the time, and seems to be further along. So my guess is I blended several things together in my head, and thought this PR was based on my newer branch. I will evaluate jmj_dev_event_tests_2 to see where it is at and if it should replace this one.

@DeckerSU
Copy link
Copy Markdown

DeckerSU commented Aug 6, 2021

In short, DO NOT MERGE THIS or waste time reviewing until I sort out where my train went off the rails. My apologies.

All is Ok, thx for your hard work. I just thought that if PR is done, it represents some complete state.

@jmjatlanta jmjatlanta mentioned this pull request Aug 10, 2021
@jmjatlanta
Copy link
Copy Markdown
Author

Replaced by #476

@jmjatlanta jmjatlanta closed this Aug 10, 2021
who-biz pushed a commit to who-biz/komodo that referenced this pull request Aug 30, 2022
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.

4 participants