Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
eed80f8
Abdullah/stubnetwork extended test (#20)
akucukoduk16 Apr 12, 2022
ca8e558
Merge branch 'master' into abdullah/mapdb-transactions-and-blocks
Apr 13, 2022
b3f6f3e
TansactionsMapDb and tests are implemented.
Apr 14, 2022
f4b67c5
Merge branch 'master' into abdullah/mapdb-transactions-and-blocks
Apr 14, 2022
edc8bf7
BlocksMapDb is implemented
Apr 14, 2022
b289d60
BlocksMapDb is implemented but adding problem exist
Apr 15, 2022
fe69f97
Distributed is implemented
Apr 15, 2022
7b0b8a3
Proof of id remains same for transaction
Apr 15, 2022
aa21128
Change structure
Apr 15, 2022
3c8c098
Change structure
Apr 15, 2022
9fa93cb
Merge branch 'master' into abdullah/mapdb-transactions-and-blocks
Apr 21, 2022
97a11d5
Transactions and Blocks mapdb done.
Apr 21, 2022
433128c
Style modifications
Apr 21, 2022
c385d55
Style modifications
Apr 21, 2022
edd814f
Style modifications
Apr 21, 2022
0054cc1
Style modifications
Apr 21, 2022
800e427
Style modifications
Apr 21, 2022
18f49a6
Style modifications
Apr 21, 2022
24d3727
Style modifications
Apr 21, 2022
6c1d8cd
Style modifications
Apr 21, 2022
2fdbedd
Merge branch 'master' into abdullah/distributed
Apr 21, 2022
187a261
Merge branch 'abdullah/mapdb-transactions-and-blocks' into abdullah/d…
Apr 21, 2022
3c9ec34
Merge branch 'master' into abdullah/distributed and some tests implem…
Apr 21, 2022
fc0dd87
resolves decoding issue of Signature, JsonEncoder is updated
onacitarhan17 Apr 25, 2022
8db2bdd
Merge branch 'master' into abdullah/distributed
Apr 28, 2022
e01c676
AddTest is implemented
Apr 28, 2022
ee23170
DistributedTest is implemented
Apr 28, 2022
9cae3f3
Style modification
Apr 28, 2022
7ee60b3
Style modification
Apr 28, 2022
ca57dbe
Style modification
Apr 28, 2022
1918961
Merge remote-tracking branch 'origin/abdullah/distributed' into abdul…
Apr 28, 2022
cc2d6aa
Style modification
Apr 28, 2022
ba335fd
Style modification
Apr 28, 2022
99d5c99
concurrently addition tests is fixed
Apr 28, 2022
7d20b7b
apply revision for tests
May 22, 2022
9d294ca
apply revision for tests
May 22, 2022
e3848ed
Merge branch 'master' into abdullah/distributed
May 22, 2022
e188aaf
exception handling fixed
May 22, 2022
5406ee1
sytle fixed
May 23, 2022
0d88845
Merge branch 'master' into abdullah/distributed
May 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/main/java/model/codec/EncodedEntity.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
package model.codec;

import java.io.Serializable;
import java.util.Arrays;

/**
* Represents an encapsulation around the byte representation of an entity accompanied by its original type.
*/
public class EncodedEntity {
public class EncodedEntity implements Serializable {
private final byte[] bytes;
private final String type;

// EncodedEntity(id.getBytes() || byte(i), "assignment")
/**
* Creates encoded entity.
*
* @param bytes bytes of entity.
* @param type types of entity.
*/
public EncodedEntity(byte[] bytes, String type) {
this.bytes = bytes.clone();
this.type = type;
}

/**
* Hashcode of entity.
*
* @return hashcode of encodedentity.
*/
@Override
public int hashCode() {
return Arrays.hashCode(this.bytes);
}

/**
* Check if objects are equal.
*
* @param o encodedentity.
* @return true if equals.
*/
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof EncodedEntity)) {
return false;
}
EncodedEntity that = (EncodedEntity) o;
return Arrays.equals(this.bytes, that.bytes);
}

public byte[] getBytes() {
return bytes.clone();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/model/crypto/Sha3256Hash.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package model.crypto;

import java.io.Serializable;
import java.util.Arrays;

import model.lightchain.Identifier;
Expand All @@ -8,7 +9,7 @@
* Represents SHA3-256 data type which extends abstract Hash data type for
* the cryptographic hash function used in LightChain.
*/
public class Sha3256Hash extends Hash {
public class Sha3256Hash extends Hash implements Serializable {
public static final int Size = 32;
private final byte[] hashBytes;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/model/crypto/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ public Identifier getSignerId() {
public byte[] getBytes() {
return bytes.clone();
}

}
10 changes: 5 additions & 5 deletions src/main/java/storage/Distributed.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;

import model.Entity;
import model.lightchain.Block;
import model.exceptions.CodecException;
import model.lightchain.Identifier;

/**
Expand All @@ -26,7 +26,7 @@ public interface Distributed {
* @return true if entity did not exist on the database, false if entity is already in
* database.
*/
boolean add(Entity e);
boolean add(Entity e) throws CodecException;

/**
* Removes entity with given identifier.
Expand All @@ -35,20 +35,20 @@ public interface Distributed {
* @return true if entity exists on database and removed successfully, false if entity does not exist on
* database.
*/
boolean remove(Entity e);
boolean remove(Entity e) throws CodecException;

/**
* Returns the entity with given identifier.
*
* @param e identifier of the entity.
* @return the entity itself if exists and null otherwise.
*/
Block get(Identifier e);
Entity get(Identifier e) throws CodecException;

/**
* Returns all entities stored in database.
*
* @return all stored entities in database.
*/
ArrayList<Entity> all();
ArrayList<Entity> all() throws CodecException;
}
151 changes: 151 additions & 0 deletions src/main/java/storage/mapdb/DistributedMapDb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package storage.mapdb;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import model.Entity;
import model.codec.EncodedEntity;
import model.exceptions.CodecException;
import model.lightchain.Identifier;
import modules.codec.JsonEncoder;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import org.mapdb.HTreeMap;
import org.mapdb.Serializer;

/**
* Distributed databese that store encoded entities.
*/
public class DistributedMapDb implements storage.Distributed {

private final DB db;
private final ReentrantReadWriteLock lock;
private static final String MAP_NAME = "distributed_map";
private final HTreeMap distributedMap;

/**
* Creates DistributedMapDb.
*/
public DistributedMapDb(String filePath) {
this.db = DBMaker.fileDB(filePath).make();
this.lock = new ReentrantReadWriteLock();
distributedMap = this.db.hashMap(MAP_NAME)
.keySerializer(Serializer.BYTE_ARRAY)
.createOrOpen();
}

/**
* Checks existence of entity on the database.
*
* @param entityId Identifier of entity.
* @return true if a entity with that identifier exists, false otherwise.
*/
@Override
public boolean has(Identifier entityId) {
boolean hasBoolean;
try {
lock.readLock().lock();
hasBoolean = distributedMap.containsKey(entityId.getBytes());
} finally {
lock.readLock().unlock();
}
return hasBoolean;
}

/**
* Adds entity to the database.
*
* @param e given entity to be added.
* @return true if entity did not exist on the database, false if entity is already in
* database.
*/
@Override
public boolean add(Entity e) throws CodecException {
JsonEncoder encoder = new JsonEncoder();
boolean addBoolean;
try {
lock.writeLock().lock();
addBoolean = distributedMap.putIfAbsentBoolean(e.id().getBytes(), encoder.encode(e));
} catch (CodecException ex) {
throw new CodecException("could not encode the entity", ex);
} finally {
lock.writeLock().unlock();
}
return addBoolean;
}

/**
* Removes entity with given identifier.
*
* @param e identifier of the entity.
* @return true if entity exists on database and removed successfully, false if entity does not exist on
* database.
*/
@Override
public boolean remove(Entity e) throws CodecException {
JsonEncoder encoder = new JsonEncoder();
boolean removeBoolean;
try {
lock.writeLock().lock();
removeBoolean = distributedMap.remove(e.id().getBytes(), encoder.encode(e));
} catch (CodecException exception) {
throw new CodecException("could not encode entity", exception);
} finally {
lock.writeLock().unlock();
}
return removeBoolean;
}

/**
* Returns the entity with given identifier.
*
* @param entityId identifier of the entity.
* @return the entity itself if exists and null otherwise.
*/
@Override
public Entity get(Identifier entityId) throws CodecException {

Entity decodedEntity;

try {
JsonEncoder encoder = new JsonEncoder();
lock.readLock().lock();
EncodedEntity encodedEntity = (EncodedEntity) distributedMap.get(entityId.getBytes());
if (encodedEntity == null) {
return null;
}
decodedEntity = encoder.decode(encodedEntity);
} catch (CodecException e) {
throw new CodecException("could not found the class", e);
} finally {
lock.readLock().unlock();
}
return decodedEntity;
}

/**
* Returns all entities stored in database.
*
* @return all stored entities in database.
*/
@Override
public ArrayList<Entity> all() throws CodecException {
JsonEncoder encoder = new JsonEncoder();
ArrayList<Entity> allEntities = new ArrayList<>();
for (Object encodedEntity : distributedMap.values()) {
try {
allEntities.add(encoder.decode((EncodedEntity) encodedEntity));
} catch (CodecException e) {
throw new CodecException("could not found the class", e);
}
}
return allEntities;
}

/**
* It closes the database.
*/
public void closeDb() {
db.close();
}
}
2 changes: 1 addition & 1 deletion src/test/java/modules/JsonEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class JsonEncoderTest {
@Test
public void testEncodingRoundTrip() throws CodecException {
JsonEncoder encoder = new JsonEncoder();
EntityFixture entity = new EntityFixture();
Entity entity = new EntityFixture();
Entity entityChanged = encoder.decode(encoder.encode(entity));
Assertions.assertEquals(entity, entityChanged);
}
Expand Down
Loading