[FEATURE] Clean up the `server.go` file for the rpc
Describe the feature you'd like
Currently the cmd/rpc/server.go file for the rpc lacks the hygiene standards of the rest of the application.
- Add a comment (nearly) per line of code
- Look for opportunities to separate the RPC payload structures
- Fix the
memory leakTODO by re-architecting the code
// TODO likely a memory leak here from un-discarded stores
func setupStateMachine(height uint64, w http.ResponseWriter) (*fsm.StateMachine, bool) {
state, err := app.FSM.TimeMachine(height)
if err != nil {
write(w, ErrTimeMachine(err), http.StatusInternalServerError)
return nil, false
}
return state, true
}
Motivation
Currently the cmd/rpc/server.go file for the rpc lacks the hygiene standards of the rest of the application. As it's the interface file, many client-side application builders will look here first to understand the API. Currently it is not a good representation of the cleanliness of the code.
Additional context
- Affects wallet and block explorer so be sure to fix breaking changes
- After completion of this issue the RPC should be finalized and ready for specification.
Required Tags
- Priority: High
- Module: RPC
@pablocampogo must finish #34 before this issue may be completed
In addition to the improvements identified in the issue description:
- Code Comments & Code Quality Practices
- Remote Procedure Call (RPC) Payload Structures
- Resource Management Issues in TimeMachine
Additional Observations Noted or Discussed:
- All RPC server functionality is contained within a single monolithic source file.
- Route definitions are currently stored as string literals, which affects the clarity and re-usability of route configurations.
- Reliance on global variables for critical data elements such as database connections, controllers, and configuration settings.
- RPC server instantiates an RPC client to query itself resulting in tight coupling between the server and client types.
- Database access requires consistent usage of
Discardwithstore.NewStoreWithDBas well as theTimeMachine. WhileDiscardis being correctly called afterStoreaccess, applying the same solution forTimeMachinemay be beneficial here. - The CLI tool utilizes
StringToCommittees, but there exists a duplicate and similarly named functionstringToCommitteeswithin this package, potentially leading to confusion or future mistakes.
Proposed PR Order:
Phase One:
- Implement changes that do not alter logic or program flow.
- Refactor route configurations for improved clarity.
- Refactor code to eliminate global variables.
- Separate query & admin request handlers, along with their supporting functions, into distinct files, specifically
admin_handlers.goandquery_handlers.go. - Add comments to affected sections of the code base.
- Refactor the startup logic to enhance clarity and maintainability.
The newly introduced Server type will encapsulate what were previously
global variables. Consequently, many functions have been converted into
methods associated with the Server type due to their reliance on these
global variables.
These modifications are intended to enhance the discoverability, usability and clarity of canopy components and features.
Phase Two:
Resolve the issue of database transaction resource leaks.
- Option 1: Ensure
Discardis properly called wherever necessary. - Option 2: Introduce a new type that acts as a proxy or decorator for database access. This abstraction layer would alleviate the RPC server from handling database access intricacies and could facilitate additional features such as caching and data transformations/aggregations.
Phase Three:
RPC/Tx request and response type refactor:
- Investigate using struct embeds to split
txRequesttype similarly to the other request types. - Determine where to store types
Future Considerations:
- Refactor to remove support/utility functions from the Server type, leaving only HTTP handlers or other directly relevant functions. This is to avoid distracting the reader with code that is less relevant to the core functionality.
- Eliminate the use of the RPC client by the RPC server within the
pollChainInfoprocess. - Decouple client and server implementations and place them into separate packages.
- Move poll code to a separate type or package
This all looks great to me and seems like a reasonable balance between modularization, clarity, and bug fixes