Skip to content

Commit

Permalink
Fixing and beatify small things
Browse files Browse the repository at this point in the history
  • Loading branch information
PanagiotisDrakatos committed Oct 21, 2023
1 parent 4a78744 commit ec74cfb
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.Adrestus.core;

import com.google.common.reflect.TypeToken;
import io.distributedLedger.*;
import org.junit.jupiter.api.Test;

Expand All @@ -8,6 +9,8 @@ public class DeleteDBTest {

@Test
public void delete_test() {
IDatabase<String, LevelDBTransactionWrapper<Transaction>> transaction_database = new DatabaseFactory(String.class, Transaction.class, new TypeToken<LevelDBTransactionWrapper<Transaction>>() {
}.getType()).getDatabase(DatabaseType.LEVEL_DB);
IDatabase<String, TransactionBlock> transaction_block1 = new DatabaseFactory(String.class, TransactionBlock.class).getDatabase(DatabaseType.ROCKS_DB, DatabaseInstance.ZONE_0_TRANSACTION_BLOCK);
IDatabase<String, TransactionBlock> transaction_block2 = new DatabaseFactory(String.class, TransactionBlock.class).getDatabase(DatabaseType.ROCKS_DB, DatabaseInstance.ZONE_1_TRANSACTION_BLOCK);
IDatabase<String, TransactionBlock> transaction_block3 = new DatabaseFactory(String.class, TransactionBlock.class).getDatabase(DatabaseType.ROCKS_DB, DatabaseInstance.ZONE_2_TRANSACTION_BLOCK);
Expand All @@ -34,5 +37,6 @@ public void delete_test() {


commit.delete_db();
transaction_database.delete_db();
}
}
32 changes: 32 additions & 0 deletions adrestus-core/src/test/java/io/Adrestus/core/RPCExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,38 @@ public void myCdownload2() throws Exception {
assertEquals(transactionBlock, blocks.get(0));


client.close();
example.close();
example = null;
database.delete_db();
} catch (Exception e) {
System.out.println("Exception caught: " + e.toString());
}
}
@Test
public void myCadownload2() throws Exception {
try {
IDatabase<String, TransactionBlock> database = new DatabaseFactory(String.class, TransactionBlock.class).getDatabase(DatabaseType.ROCKS_DB, DatabaseInstance.ZONE_1_TRANSACTION_BLOCK);
IDatabase<String, TransactionBlock> database2 = new DatabaseFactory(String.class, TransactionBlock.class).getDatabase(DatabaseType.ROCKS_DB, DatabaseInstance.ZONE_2_TRANSACTION_BLOCK);
TransactionBlock transactionBlock = new TransactionBlock();
TransactionBlock transactionBlock2 = new TransactionBlock();
String hash = HashUtil.sha256_bytetoString(encode.encode(transactionBlock));
transactionBlock.setHash(hash);
transactionBlock2.setHash("1");
database.save(transactionBlock.getHash(), transactionBlock);
database2.save(transactionBlock2.getHash(), transactionBlock2);

RpcAdrestusServer<AbstractBlock> example = new RpcAdrestusServer<AbstractBlock>(new TransactionBlock(), DatabaseInstance.ZONE_2_TRANSACTION_BLOCK, "localhost", 8095, eventloop);
new Thread(example).start();
RpcAdrestusClient<AbstractBlock> client = new RpcAdrestusClient<AbstractBlock>(new TransactionBlock(), "localhost", 8095, eventloop);
client.connect();
List<AbstractBlock> blocks = client.getBlocksList("1");
if (blocks.isEmpty()) {
System.out.println("error");
}
assertEquals(transactionBlock2, blocks.get(0));


client.close();
example.close();
example = null;
Expand Down
42 changes: 42 additions & 0 deletions adrestus-core/src/test/java/io/Adrestus/core/RocksDBBlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,48 @@ public void find_between_range() {
assertEquals("hash1", firstKey.get());
database.delete_db();
}
@Test
public void find_between_range2() {
IDatabase<String, TransactionBlock> database = new DatabaseFactory(String.class, TransactionBlock.class).getDatabase(DatabaseType.ROCKS_DB, DatabaseInstance.ZONE_1_TRANSACTION_BLOCK);
String hash = "Hash";
TransactionBlock transactionBlock1 = new TransactionBlock();
transactionBlock1.setHeight(1);
transactionBlock1.setHash("hash1");

TransactionBlock transactionBlock2 = new TransactionBlock();
transactionBlock2.setHeight(2);
transactionBlock2.setHash("hash2");

TransactionBlock transactionBlock3 = new TransactionBlock();
transactionBlock3.setHeight(3);
transactionBlock3.setHash("hash3");

TransactionBlock transactionBlock4 = new TransactionBlock();
transactionBlock4.setHeight(4);
transactionBlock4.setHash("hash4");

TransactionBlock transactionBlock5 = new TransactionBlock();
transactionBlock5.setHeight(5);
transactionBlock5.setHash("hash5");

TransactionBlock transactionBlock6 = new TransactionBlock();
transactionBlock6.setHeight(6);
transactionBlock6.setHash("hash6");

Map<String, TransactionBlock> map = new HashMap<>();
map.put("hash1", transactionBlock1);
map.put("hash2", transactionBlock2);
map.put("hash3", transactionBlock3);
map.put("hash4", transactionBlock4);
map.put("hash5", transactionBlock5);
map.put("hash6", transactionBlock6);

database.saveAll(map);

Map<String, TransactionBlock> map_returned = database.findBetweenRange("hash145");
assertEquals(0, map_returned.size());
database.delete_db();
}

@Test
public void save_all_tree() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ public Map<K, V> findBetweenRange(K key) {
do {
byte[] serializedKey = iterator.key();
byte[] serializedValue = iterator.value();
final byte[] res = rocksDB.get(serializedKey);
if(res==null){
return (Map<K, V>) hashmap;
}
hashmap.put(keyMapper.decode(serializedKey), valueMapper.decode(serializedValue));
iterator.next();
} while (iterator.isValid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ public void myBfind_between_range() {

database.delete_db();
}
@Test
public void myBfind_between_range2() {
IDatabase<String, String> database = new DatabaseFactory(String.class, String.class).getDatabase(DatabaseType.LEVEL_DB);
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");

database.saveAll(map);
assertEquals(4, database.findDBsize());
Map<String, String> res = database.findBetweenRange("key234");
assertEquals(0, res.entrySet().size());

database.delete_db();
}

@Test
public void myAdelete() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public Service(Class<T> typeParameterClass, PatriciaTreeInstance instance) {

@Override
public List<T> download(String hash) throws Exception {
ZoneDatabaseFactory.getZoneInstance(0);
Map<String, T> map;
if (hash.equals(""))
map = database.seekFromStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ private static void setup() throws InterruptedException {
TreeFactory.getMemoryTree(0).store(address10, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(0).store(address11, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(0).store(address12, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(0).store("ADR-GBIV-HG2J-27P5-BNVN-MLN6-DL5V-M3YZ-PKEJ-CFFG-FK4L", new PatriciaTreeNode(1000, 0));
TreeFactory.getMemoryTree(0).store("ADR-GBZX-XXCW-LWJC-J7RZ-Q6BJ-RFBA-J5WU-NBAG-4RL7-7G6Z",new PatriciaTreeNode(1000,0));
TreeFactory.getMemoryTree(0).store("ADR-GD3G-DK4I-DKM2-IQSB-KBWL-HWRV-BBQA-MUAS-MGXA-5QPP",new PatriciaTreeNode(1000,0));

TreeFactory.getMemoryTree(1).store(address1, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(1).store(address2, new PatriciaTreeNode(3000, 0));
Expand All @@ -173,6 +176,9 @@ private static void setup() throws InterruptedException {
TreeFactory.getMemoryTree(1).store(address10, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(1).store(address11, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(1).store(address12, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(1).store("ADR-GBIV-HG2J-27P5-BNVN-MLN6-DL5V-M3YZ-PKEJ-CFFG-FK4L", new PatriciaTreeNode(1000, 0));
TreeFactory.getMemoryTree(1).store("ADR-GBZX-XXCW-LWJC-J7RZ-Q6BJ-RFBA-J5WU-NBAG-4RL7-7G6Z",new PatriciaTreeNode(2000,0));
TreeFactory.getMemoryTree(1).store("ADR-GD3G-DK4I-DKM2-IQSB-KBWL-HWRV-BBQA-MUAS-MGXA-5QPP",new PatriciaTreeNode(2000,0));


TreeFactory.getMemoryTree(2).store(address1, new PatriciaTreeNode(3000, 0));
Expand All @@ -187,6 +193,9 @@ private static void setup() throws InterruptedException {
TreeFactory.getMemoryTree(2).store(address10, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(2).store(address11, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(2).store(address12, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(2).store("ADR-GBIV-HG2J-27P5-BNVN-MLN6-DL5V-M3YZ-PKEJ-CFFG-FK4L", new PatriciaTreeNode(1000, 0));
TreeFactory.getMemoryTree(2).store("ADR-GBZX-XXCW-LWJC-J7RZ-Q6BJ-RFBA-J5WU-NBAG-4RL7-7G6Z",new PatriciaTreeNode(3000,0));
TreeFactory.getMemoryTree(2).store("ADR-GD3G-DK4I-DKM2-IQSB-KBWL-HWRV-BBQA-MUAS-MGXA-5QPP",new PatriciaTreeNode(3000,0));


TreeFactory.getMemoryTree(3).store(address1, new PatriciaTreeNode(3000, 0));
Expand All @@ -201,6 +210,9 @@ private static void setup() throws InterruptedException {
TreeFactory.getMemoryTree(3).store(address10, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(3).store(address11, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(3).store(address12, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(3).store("ADR-GBIV-HG2J-27P5-BNVN-MLN6-DL5V-M3YZ-PKEJ-CFFG-FK4L", new PatriciaTreeNode(1000, 0));
TreeFactory.getMemoryTree(3).store("ADR-GBZX-XXCW-LWJC-J7RZ-Q6BJ-RFBA-J5WU-NBAG-4RL7-7G6Z",new PatriciaTreeNode(4000,0));
TreeFactory.getMemoryTree(3).store("ADR-GD3G-DK4I-DKM2-IQSB-KBWL-HWRV-BBQA-MUAS-MGXA-5QPP",new PatriciaTreeNode(4000,0));

TransactionBlock TransactionBlockZone2 = new TransactionBlock();
TransactionBlockZone2.setHeight(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ public static void setup() throws Exception {
TreeFactory.getMemoryTree(0).store(address10, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(0).store(address11, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(0).store(address12, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(0).store("ADR-GBIV-HG2J-27P5-BNVN-MLN6-DL5V-M3YZ-PKEJ-CFFG-FK4L", new PatriciaTreeNode(1000, 0));
TreeFactory.getMemoryTree(0).store("ADR-GBZX-XXCW-LWJC-J7RZ-Q6BJ-RFBA-J5WU-NBAG-4RL7-7G6Z",new PatriciaTreeNode(1000,0));
TreeFactory.getMemoryTree(0).store("ADR-GD3G-DK4I-DKM2-IQSB-KBWL-HWRV-BBQA-MUAS-MGXA-5QPP",new PatriciaTreeNode(1000,0));

TreeFactory.getMemoryTree(1).store(address1, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(1).store(address2, new PatriciaTreeNode(3000, 0));
Expand All @@ -241,7 +244,9 @@ public static void setup() throws Exception {
TreeFactory.getMemoryTree(1).store(address10, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(1).store(address11, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(1).store(address12, new PatriciaTreeNode(3000, 0));

TreeFactory.getMemoryTree(1).store("ADR-GBIV-HG2J-27P5-BNVN-MLN6-DL5V-M3YZ-PKEJ-CFFG-FK4L", new PatriciaTreeNode(1000, 0));
TreeFactory.getMemoryTree(1).store("ADR-GBZX-XXCW-LWJC-J7RZ-Q6BJ-RFBA-J5WU-NBAG-4RL7-7G6Z",new PatriciaTreeNode(2000,0));
TreeFactory.getMemoryTree(1).store("ADR-GD3G-DK4I-DKM2-IQSB-KBWL-HWRV-BBQA-MUAS-MGXA-5QPP",new PatriciaTreeNode(2000,0));

TreeFactory.getMemoryTree(2).store(address1, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(2).store(address2, new PatriciaTreeNode(3000, 0));
Expand All @@ -255,7 +260,9 @@ public static void setup() throws Exception {
TreeFactory.getMemoryTree(2).store(address10, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(2).store(address11, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(2).store(address12, new PatriciaTreeNode(3000, 0));

TreeFactory.getMemoryTree(2).store("ADR-GBIV-HG2J-27P5-BNVN-MLN6-DL5V-M3YZ-PKEJ-CFFG-FK4L", new PatriciaTreeNode(1000, 0));
TreeFactory.getMemoryTree(2).store("ADR-GBZX-XXCW-LWJC-J7RZ-Q6BJ-RFBA-J5WU-NBAG-4RL7-7G6Z",new PatriciaTreeNode(3000,0));
TreeFactory.getMemoryTree(2).store("ADR-GD3G-DK4I-DKM2-IQSB-KBWL-HWRV-BBQA-MUAS-MGXA-5QPP",new PatriciaTreeNode(3000,0));

TreeFactory.getMemoryTree(3).store(address1, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(3).store(address2, new PatriciaTreeNode(3000, 0));
Expand All @@ -269,6 +276,9 @@ public static void setup() throws Exception {
TreeFactory.getMemoryTree(3).store(address10, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(3).store(address11, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(3).store(address12, new PatriciaTreeNode(3000, 0));
TreeFactory.getMemoryTree(3).store("ADR-GBIV-HG2J-27P5-BNVN-MLN6-DL5V-M3YZ-PKEJ-CFFG-FK4L", new PatriciaTreeNode(1000, 0));
TreeFactory.getMemoryTree(3).store("ADR-GBZX-XXCW-LWJC-J7RZ-Q6BJ-RFBA-J5WU-NBAG-4RL7-7G6Z",new PatriciaTreeNode(4000,0));
TreeFactory.getMemoryTree(3).store("ADR-GD3G-DK4I-DKM2-IQSB-KBWL-HWRV-BBQA-MUAS-MGXA-5QPP",new PatriciaTreeNode(4000,0));

kad1 = new KademliaData(new SecurityAuditProofs(address1, vk1, ecKeyPair1.getPublicKey(), signatureData1), new NettyConnectionInfo("192.168.1.106", KademliaConfiguration.PORT));
kad2 = new KademliaData(new SecurityAuditProofs(address2, vk2, ecKeyPair2.getPublicKey(), signatureData2), new NettyConnectionInfo("192.168.1.113", KademliaConfiguration.PORT));
Expand Down Expand Up @@ -457,7 +467,7 @@ public void test() throws IOException, InterruptedException {
}


CountDownLatch latch = new CountDownLatch(20);
CountDownLatch latch = new CountDownLatch(50);
ConsensusState c = new ConsensusState(latch);
c.getTransaction_block_timer().scheduleAtFixedRate(new ConsensusState.TransactionBlockConsensusTask(), ConsensusConfiguration.CONSENSUS_TIMER, ConsensusConfiguration.CONSENSUS_TIMER);
//c.getCommittee_block_timer().scheduleAtFixedRate(new ConsensusState.CommitteeBlockConsensusTask(), ConsensusConfiguration.CONSENSUS_COMMITTEE_TIMER, ConsensusConfiguration.CONSENSUS_COMMITTEE_TIMER);
Expand Down

0 comments on commit ec74cfb

Please sign in to comment.