Skip to content
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

Add dependency injection, refactor static classes #45

Merged
merged 2 commits into from
Jan 10, 2018
Merged

Add dependency injection, refactor static classes #45

merged 2 commits into from
Jan 10, 2018

Conversation

Rovak
Copy link
Contributor

@Rovak Rovak commented Jan 9, 2018

What does this PR do?

  • Added dependency injection using Guice
  • Changed dependency on commands from peer to application
  • Made Client non-static
  • Made Server non-static
  • Changed some classes to accept dependencies through constructor
  • Added shutdown method to application so services can cleanly shutdown instead of a forced terminate using System.exit

Why are these changes required?

  • Static classes are harder to test
  • Dependency injection makes it easier to wire classes together
  • There should be cleanup logic to properly shutdown connections, leave clusters etc

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Rovak added 2 commits January 9, 2018 19:18
added dependency injection using Guice
changed dependency off commands from peer to application
made Client non-static
made Server non-static
changed some classes to accept dependencies through constructor

@Override
public void stop() {
server.leave();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not 100% sure if this is the right way to dispose the server


public PeerBuilder() {
Server.serverRun();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this because starting a server should not be done within a factory class. Starting the server is now being done in org.tron.example.Tron.run()

@@ -57,6 +59,8 @@
private byte[] lastHash;
private byte[] currentHash;

private Client client;

/**
* create new blockchain
*
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if it is possible to inject the database when initializing the Blockchain for the first time from this constructor:
public Blockchain(String address, String type) {

Doing so would avoid making the Blockchain responsible for setting the database connection, which to me isn't an intuitive responsibility of this class.

Doing so would mean the logic for deciding whether to load an existing or create a new blockchain would have to change to some other way.

I wonder if if(blockDB.getData(LAST_HASH) != null) { ... } is sufficient for this purpose?

Copy link
Contributor

Choose a reason for hiding this comment

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

Initialize the database when the program starts.

Solution A:

// Initialize on the outside
DB blockDB = new DB(); 
// Incoming through formal parameters
Blockchain blockchain = new Blockchain(blockDB); 

@sasaxie sasaxie merged commit b75048d into tronprotocol:develop Jan 10, 2018
@Rovak Rovak deleted the app-di branch January 10, 2018 07:58
zhaohong pushed a commit that referenced this pull request Feb 5, 2018
 XmThLtmnNLD/Insalk6y8GlKPwfVllFPqFPLUNgwkuQgBPZ2/dm975bMPJVVNSxK
 VIECNCdVCDTtalpHu/ieMM/a9d1bvhZIao4JwGnrGHcEkVli5l8OsIzkz61fgy7G
 jZRmEq8HZuscQritqUsvxSju6tYKSVjzCsGUyD/9T6tC2GrFxdrC/Iw+5jzd+JEa
 keG1pyXQLNVMqj1lhsF09m/M3G6540/uZLCBYLERleZScrMoyJT9WqNd86oRvvav
 eveTMObaQkCUNG+SCdQ7bnWT73v34JGxzdr2fMEfnAkP9PBHDD+cZEjxg0iVLpc=
 =5qRR
 -----END PGP SIGNATURE-----
 

Merge pull request #45 from Rovak/app-di

Add dependency injection, refactor static classes
renchenchang pushed a commit that referenced this pull request May 11, 2018
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