Skip to content

Commit

Permalink
feat: implement reflection (#4)
Browse files Browse the repository at this point in the history
* feat: implement reflection for Reflectable annotated classes. Improve ObjectMapper and implement JsonSerializer.

* feat: add non object json serialization

* refactor: refactor functionalities around json serialization/deserialization

* feat: add functionality to interact with localStorage. (#5)

* feat: add functionality to interact with localStorage.

* refactor: remove deprecated LocalStorage bean.

* refactor: address pr comments

* fix: fix endless while loop in JsonParser
  • Loading branch information
Zurcusa authored Aug 14, 2024
1 parent 227faf1 commit 64b0339
Show file tree
Hide file tree
Showing 20 changed files with 422 additions and 513 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
implementation("org.projectlombok:lombok:1.18.34")

implementation("org.teavm:teavm-jso-apis:0.10.0")

implementation("org.teavm:teavm-core:0.10.0")
}

teavm.js {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/limechain/chain/spec/ChainSpec.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.limechain.chain.spec;

import com.limechain.teavm.annotation.Reflectable;
import com.limechain.utils.json.JsonUtil;
import com.limechain.utils.json.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -13,6 +15,7 @@
*/
@Getter
@Setter
@Reflectable
public class ChainSpec implements Serializable {
private String id;
private String name;
Expand All @@ -29,6 +32,7 @@ public class ChainSpec implements Serializable {
*/
public static ChainSpec newFromJSON(String pathToChainSpecJSON) throws IOException {
ObjectMapper mapper = new ObjectMapper(false);
return mapper.mapToClass(pathToChainSpecJSON, ChainSpec.class);
String jsonChainSpec = JsonUtil.readJsonFromFile(pathToChainSpecJSON);
return mapper.mapToClass(jsonChainSpec, ChainSpec.class);
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/limechain/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.limechain.config.HostConfig;
import com.limechain.network.kad.KademliaService;
import com.limechain.rpc.server.AppBean;
import com.limechain.storage.KVRepository;
import com.limechain.sync.warpsync.WarpSyncState;
import lombok.Getter;
import lombok.extern.java.Log;
Expand Down Expand Up @@ -39,18 +38,16 @@ public class Network {
*
* @param chainService chain specification information containing boot nodes
* @param hostConfig host configuration containing current network
* @param repository database repository
*/
public Network(ChainService chainService, HostConfig hostConfig, KVRepository<String, Object> repository) {
public Network(ChainService chainService, HostConfig hostConfig) {
this.bootNodes = chainService.getChainSpec().getBootNodes();
this.chain = hostConfig.getChain();
// this.connectionManager = ConnectionManager.getInstance();
this.initializeProtocols(chainService, hostConfig, repository);
this.initializeProtocols(chainService, hostConfig);
}

private void initializeProtocols(ChainService chainService,
HostConfig hostConfig,
KVRepository<String, Object> repository) {
HostConfig hostConfig) {

//
// String chainId = chainService.getChainSpec().getProtocolId();
Expand Down Expand Up @@ -141,7 +138,7 @@ public void findPeers() {
}
if (getPeersCount() >= REPLICATION) {
log.log(Level.INFO,
"Connections have reached replication factor(" + REPLICATION + "). " +
"Connections have reached replication factor(" + REPLICATION + "). " +
"No need to search for new ones yet.");
return;
}
Expand Down
31 changes: 9 additions & 22 deletions src/main/java/com/limechain/rpc/server/CommonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import com.limechain.config.HostConfig;
import com.limechain.config.SystemInfo;
import com.limechain.network.Network;
import com.limechain.storage.KVRepository;
import com.limechain.storage.block.SyncState;
import com.limechain.sync.warpsync.WarpSyncMachine;
import com.limechain.sync.warpsync.WarpSyncState;
import com.limechain.utils.DivLogger;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -23,7 +21,6 @@ public class CommonConfig {
public static void start() {
getBean(SystemInfo.class);
getBean(HostConfig.class);
getBean(KVRepository.class);
getBean(WarpSyncMachine.class);
}

Expand All @@ -36,16 +33,12 @@ protected static Object getBean(Class<?> beanClass) {
HostConfig hostConfig = hostConfig();
beans.put(beanClass, hostConfig);
return hostConfig;
case "KVRepository":
KVRepository<String, Object> repository = repository((HostConfig) getBean(HostConfig.class));
beans.put(beanClass, repository);
return repository;
case "ChainService":
ChainService chainService = chainService((HostConfig) getBean(HostConfig.class));
beans.put(beanClass, chainService);
return chainService;
case "SyncState":
SyncState syncState = syncState((KVRepository<String, Object>) getBean(KVRepository.class));
SyncState syncState = syncState();
beans.put(beanClass, syncState);
return syncState;
case "SystemInfo":
Expand All @@ -54,12 +47,12 @@ protected static Object getBean(Class<?> beanClass) {
return systemInfo;
case "Network":
Network network = network((ChainService) getBean(ChainService.class),
(HostConfig) getBean(HostConfig.class), (KVRepository<String, Object>) getBean(KVRepository.class));
(HostConfig) getBean(HostConfig.class));
beans.put(beanClass, network);
return network;
case "WarpSyncState":
WarpSyncState warpSyncState = warpSyncState((Network) getBean(Network.class),
(SyncState) getBean(SyncState.class), (KVRepository<String, Object>) getBean(KVRepository.class));
(SyncState) getBean(SyncState.class));
beans.put(beanClass, warpSyncState);
return warpSyncState;
case "WarpSyncMachine":
Expand All @@ -78,30 +71,24 @@ private static HostConfig hostConfig() {
return new HostConfig();
}

private static KVRepository<String, Object> repository(HostConfig hostConfig) {
return null;//DBInitializer.initialize(hostConfig.getChain());
}

private static ChainService chainService(HostConfig hostConfig) {
return new ChainService(hostConfig);
}

private static SyncState syncState(KVRepository<String, Object> repository) {
return new SyncState(repository);
private static SyncState syncState() {
return new SyncState();
}

private static SystemInfo systemInfo(HostConfig hostConfig, SyncState syncState) {
return new SystemInfo(hostConfig, syncState);
}

private static Network network(ChainService chainService, HostConfig hostConfig,
KVRepository<String, Object> repository) {
return new Network(chainService, hostConfig, repository);
private static Network network(ChainService chainService, HostConfig hostConfig) {
return new Network(chainService, hostConfig);
}

private static WarpSyncState warpSyncState(Network network, SyncState syncState,
KVRepository<String, Object> repository) {
return new WarpSyncState(syncState, network, repository);
private static WarpSyncState warpSyncState(Network network, SyncState syncState) {
return new WarpSyncState(syncState, network);
}

private static WarpSyncMachine warpSyncMachine(Network network, ChainService chainService, SyncState syncState,
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/limechain/storage/DBConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,14 @@ public class DBConstants {
* Key under which the genesis chain spec is stored
*/
public static final String GENESIS_KEY = "genesis";
/**
* Key under which the --latest-- state trie proof is stored
* TODO: Currently only the latest loaded in sync is stored
*/
public static final String RUNTIME_CODE = "runtimeCode";

/**
* Key under which the hash of the latest finalised block header is stored.
*/
public static final String FINALIZED_BLOCK_KEY = "finalised_head";

/**
* Key under which the highest round and set id is stored.
*/
public static final String HIGHEST_ROUND_AND_SET_ID_KEY = "hrs";
//<editor-fold desc="Sync keys">

// SyncState keys
public static final String LAST_FINALIZED_BLOCK_NUMBER = "ss::lastFinalizedBlockNumber";
public static final String LAST_FINALIZED_BLOCK_HASH = "ss::lastFinalizedBlockHash";
public static final String AUTHORITY_SET = "ss::authoritySet";
public static final String LATEST_ROUND = "ss::latestRound";
public static final String SET_ID = "ss::setId";
// SyncState keys

//</editor-fold>
}
47 changes: 0 additions & 47 deletions src/main/java/com/limechain/storage/DBInitializer.java

This file was deleted.

Loading

0 comments on commit 64b0339

Please sign in to comment.