forked from LimeChain/Fruzhin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make Fruzhin configurable for different chains. (#23)
# Description This PR allows Fruzhin web to connect to polkadot, kusama or westend chains via providing a chain name or a chain spec string as a parameter for the main method. - What does this PR do? Enables configuration for chains different from polkadot. - Why are these changes needed? To cover light client requirements. - How were these changes implemented and what do they affect? The main method now requires a single parameter. It can be one of two: 1: Name of a well known chain (polkadot, kusama, westend). 2: A chain spec string of a well known chain. Based on the provided a ChainService with the chain context is built and provided to the host to use. Fixes LimeChain#531 ## Checklist: - [X] I have read the [contributing guidelines](https://github.com/LimeChain/Fruzhin/blob/dev/CONTRIBUTING.md). - [X] My PR title matches the [Conventional Commits spec](https://www.conventionalcommits.org/). - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [X] All new and existing tests passed.
- Loading branch information
Showing
36 changed files
with
308 additions
and
417 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
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 was deleted.
Oops, something went wrong.
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
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,74 @@ | ||
package com.limechain.config; | ||
|
||
import com.limechain.chain.Chain; | ||
import com.limechain.chain.spec.ChainSpec; | ||
import com.limechain.polkaj.Hash256; | ||
import com.limechain.utils.DivLogger; | ||
import com.limechain.utils.json.ObjectMapper; | ||
import lombok.Getter; | ||
|
||
import java.util.logging.Level; | ||
|
||
// TODO: Cleanup | ||
|
||
/** | ||
* Configuration class used to store any Host specific information | ||
*/ | ||
@Getter | ||
public class ChainService { | ||
|
||
private static final String GENESIS_DIR_FORMAT = "genesis/%s.json"; | ||
private static final String WSS_PROTOCOL = "wss://"; | ||
private static final String HTTPS_PROTOCOL = "https://"; | ||
|
||
/** | ||
* The chain that the light client is communicating with. | ||
*/ | ||
private Chain chain; | ||
/** | ||
* The chain spec of the chain. | ||
*/ | ||
private ChainSpec chainSpec; | ||
|
||
private static final DivLogger log = new DivLogger(); | ||
|
||
public void init(String chainString) { | ||
log.log(Level.INFO, "Loading chain context..."); | ||
|
||
Chain cliChain = Chain.fromString(chainString); | ||
try { | ||
if (cliChain != null) { | ||
chain = cliChain; | ||
chainSpec = ChainSpec.newFromJSON(getGenesisPath()); | ||
} else { | ||
chainSpec = new ObjectMapper(false).mapToClass(chainString, ChainSpec.class); | ||
chain = Chain.fromChainId(chainSpec.getId()); | ||
} | ||
} catch (Exception e) { | ||
System.out.println("Something went wrong while loading chain data. " + e.getMessage()); | ||
} | ||
|
||
log.log(Level.INFO, "✅️Loaded chain context for the " + chain.getValue() + " chain."); | ||
} | ||
|
||
/** | ||
* Gets the genesis file path based on the chain the node is configured | ||
* | ||
* @return genesis(chain spec) file path | ||
*/ | ||
public String getGenesisPath() { | ||
return String.format(GENESIS_DIR_FORMAT, chain.getId()); | ||
} | ||
|
||
public String getWsRpcEndpoint() { | ||
return WSS_PROTOCOL + chain.getRpcEndpoint(); | ||
} | ||
|
||
public String getHttpsEndpoint() { | ||
return HTTPS_PROTOCOL + chain.getRpcEndpoint(); | ||
} | ||
|
||
public Hash256 getGenesisBlockHash() { | ||
return Hash256.from(chain.getGenesisBlockHash()); | ||
} | ||
} |
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,58 @@ | ||
package com.limechain.config; | ||
|
||
import com.limechain.network.Network; | ||
import com.limechain.storage.block.SyncState; | ||
import com.limechain.sync.warpsync.WarpSyncMachine; | ||
import com.limechain.sync.warpsync.WarpSyncState; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Spring configuration class used to instantiate beans. | ||
*/ | ||
public class CommonConfig { | ||
|
||
private static final Map<Class<?>, Object> beans = new HashMap<>(); | ||
|
||
public static void start() { | ||
getBean(SystemInfo.class); | ||
getBean(WarpSyncMachine.class); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected static <T> T getBean(Class<T> beanClass) { | ||
if (beans.containsKey(beanClass)) { | ||
return (T) beans.get(beanClass); | ||
} else { | ||
Object bean; | ||
switch (beanClass.getSimpleName()) { | ||
case "ChainService": | ||
bean = new ChainService(); | ||
break; | ||
case "SyncState": | ||
bean = new SyncState(getBean(ChainService.class)); | ||
break; | ||
case "SystemInfo": | ||
bean = new SystemInfo(getBean(ChainService.class)); | ||
break; | ||
case "Network": | ||
bean = new Network(getBean(ChainService.class)); | ||
break; | ||
case "WarpSyncState": | ||
bean = new WarpSyncState(getBean(SyncState.class), getBean(Network.class)); | ||
break; | ||
case "WarpSyncMachine": | ||
bean = new WarpSyncMachine(getBean(Network.class), | ||
getBean(ChainService.class), | ||
getBean(SyncState.class), | ||
getBean(WarpSyncState.class)); | ||
break; | ||
default: | ||
return null; | ||
} | ||
beans.put(beanClass, bean); | ||
return (T) bean; | ||
} | ||
} | ||
} |
Oops, something went wrong.