-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added a checkstyle and filled it with rules Signed-off-by: Georgi Georgiev <[email protected]> * updated checkstyle rules and location Signed-off-by: Georgi Georgiev <[email protected]> * Add nabu jar * Add initial Kademlia bootstrap logic * Replace existing Nabu jar with Nabu jar that has dependencies built into it * Add network module * Add network module * Update nabu * Refactor * Remove finished TOTO * Refactor long line * Remove unused import * Refactor time period to constant * Update cli logic * Add comma separator * Rename constant * Remove unused variable * Refactor network module * Change bootnode * Remove westend-local * Refactor DB * Add loading local genesis only from json * Add javadocs * Remove unused import * Refactor --------- Signed-off-by: Georgi Georgiev <[email protected]> Co-authored-by: Georgi Georgiev <[email protected]> Co-authored-by: Boris Velkovski <[email protected]> Co-authored-by: vikinatora <[email protected]>
- Loading branch information
1 parent
0d10265
commit d385fb7
Showing
19 changed files
with
381 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ out/ | |
### Rocks DB ### | ||
/rocks-db | ||
/test | ||
|
||
### Local Chain Spec | ||
/genesis/westend-local.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
public enum Chain { | ||
POLKADOT("polkadot"), | ||
KUSAMA("kusama"), | ||
LOCAL("local"), | ||
WESTEND("westend"); | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.limechain.network; | ||
|
||
import com.limechain.chain.Chain; | ||
import com.limechain.chain.ChainService; | ||
import com.limechain.config.HostConfig; | ||
import com.limechain.network.kad.KademliaService; | ||
import io.ipfs.multihash.Multihash; | ||
import io.libp2p.core.Host; | ||
import io.libp2p.protocol.Ping; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.java.Log; | ||
import org.peergos.HostBuilder; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.logging.Level; | ||
|
||
/** | ||
* A Singleton Network class that handles all peer connections and Kademlia | ||
*/ | ||
@Component | ||
@Log | ||
@Getter | ||
@Setter | ||
public class Network { | ||
/** | ||
* Interval between periodic peer searches | ||
*/ | ||
private static final int TEN_SECONDS_IN_MS = 10000; | ||
private static final int HOST_PORT = 1001; | ||
private static Network network; | ||
public static KademliaService kademliaService; | ||
private HostBuilder hostBuilder; | ||
private Host host; | ||
|
||
/** | ||
* Initializes a host for the peer connection, | ||
* Initializes the Kademlia service | ||
* Manages if nodes running locally are going to be allowed | ||
* Connects Kademlia to boot nodes | ||
* | ||
* @param chainService chain specification information containing boot nodes | ||
* @param hostConfig host configuration containing current network | ||
*/ | ||
private Network(ChainService chainService, HostConfig hostConfig) { | ||
boolean isLocalEnabled = hostConfig.getChain() == Chain.LOCAL; | ||
hostBuilder = (new HostBuilder()).generateIdentity().listenLocalhost(HOST_PORT); | ||
Multihash hostId = Multihash.deserialize(hostBuilder.getPeerId().getBytes()); | ||
kademliaService = new KademliaService("/dot/kad", hostId, isLocalEnabled); | ||
hostBuilder.addProtocols(List.of(new Ping(), kademliaService.getDht())); | ||
|
||
host = hostBuilder.build(); | ||
kademliaService.setHost(host); | ||
|
||
kademliaService.connectBootNodes(chainService.getGenesis().getBootNodes()); | ||
} | ||
|
||
/** | ||
* @return Network class instance | ||
*/ | ||
public static Network getInstance() { | ||
if (network != null) { | ||
return network; | ||
} | ||
throw new AssertionError("Network not initialized."); | ||
} | ||
|
||
/** | ||
* Initializes singleton Network instance | ||
* This is used two times on startup | ||
* | ||
* @return Network instance saved in class or if not found returns new Network instance | ||
*/ | ||
public static Network initialize(ChainService chainService, HostConfig hostConfig) { | ||
if (network != null) { | ||
log.log(Level.WARNING, "Network module already initialized."); | ||
return network; | ||
} | ||
network = new Network(chainService, hostConfig); | ||
log.log(Level.INFO, "Initialized network module!"); | ||
return network; | ||
} | ||
|
||
/** | ||
* Periodically searched for new peers | ||
*/ | ||
@Scheduled(fixedDelay = TEN_SECONDS_IN_MS) | ||
public void findPeers() { | ||
log.log(Level.INFO, "Searching for nodes..."); | ||
kademliaService.findNewPeers(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/limechain/network/kad/KademliaService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.limechain.network.kad; | ||
|
||
import io.ipfs.multiaddr.MultiAddress; | ||
import io.ipfs.multihash.Multihash; | ||
import io.libp2p.core.Host; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.java.Log; | ||
import org.peergos.protocol.dht.Kademlia; | ||
import org.peergos.protocol.dht.KademliaEngine; | ||
import org.peergos.protocol.dht.RamProviderStore; | ||
import org.peergos.protocol.dht.RamRecordStore; | ||
|
||
import java.net.ConnectException; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.logging.Level; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Service used for operating the Kademlia distributed hash table. | ||
*/ | ||
@Log | ||
@Getter | ||
@Setter | ||
public class KademliaService { | ||
public static final int REPLICATION = 20; | ||
public static final int ALPHA = 3; | ||
private Kademlia dht; | ||
private Host host; | ||
|
||
public KademliaService(String protocolId, Multihash hostId, boolean localDht) { | ||
this.initialize(protocolId, hostId, localDht); | ||
} | ||
|
||
/** | ||
* Initializes Kademlia dht with replication=20 and alpha=3 | ||
* | ||
* @param protocolId | ||
* @param hostId | ||
* @param localEnabled | ||
* @return Kademlia dht | ||
*/ | ||
private void initialize(String protocolId, Multihash hostId, boolean localEnabled) { | ||
dht = new Kademlia(new KademliaEngine(hostId, new RamProviderStore(), new RamRecordStore()), | ||
protocolId, REPLICATION, ALPHA, localEnabled); | ||
} | ||
|
||
/** | ||
* Connects to boot nodes to the Kademlia dht | ||
* | ||
* @param bootNodes boot nodes set in ChainService | ||
*/ | ||
public void connectBootNodes(String[] bootNodes) { | ||
var bootstrapMultiAddress = List.of(bootNodes).stream() | ||
.map(MultiAddress::new) | ||
.collect(Collectors.toList()); | ||
int successfulBootNodes = dht.bootstrapRoutingTable(host, bootstrapMultiAddress, addr -> !addr.contains("wss")); | ||
log.log(Level.INFO, "Successfully connected to " + successfulBootNodes + " boot nodes"); | ||
} | ||
|
||
/** | ||
* Populates Kademlia dht with peers closest in distance to a random id | ||
*/ | ||
public void findNewPeers() { | ||
byte[] hash = new byte[32]; | ||
(new Random()).nextBytes(hash); | ||
Multihash randomPeerId = new Multihash(Multihash.Type.sha2_256, hash); | ||
dht.findClosestPeers(randomPeerId, REPLICATION, host); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.