Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions src/main/java/model/codec/EntityType.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public class EntityType {
public static final String TYPE_VALIDATED_TRANSACTION = "type-lightchain-validated-transaction";
public static final String TYPE_VALIDATED_BLOCK = "type-lightchain-validated-block";
public static final String TYPE_ECDSA_SIGNATURE = "type-lightchain-ecdsa-signature";
public static final String TYPE_ACCOUNT = "type-lightchain-account";

}
24 changes: 23 additions & 1 deletion src/main/java/model/lightchain/Account.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package model.lightchain;

import model.Entity;
import model.codec.EntityType;
import model.crypto.PublicKey;

/**
* Represents a LightChain account which is the constituent of the SnapShot.
*/
public class Account {
public class Account extends Entity {
/**
* Unique identifier of the account.
*/
Expand Down Expand Up @@ -72,4 +74,24 @@ public Identifier getLastBlockId() {
public int getStake() {
return this.stake;
}

/**
* Type of this entity.
*
* @return type of this entity.
*/
@Override
public String type() {
return EntityType.TYPE_ACCOUNT;
}

/**
* Identifier of this account.
*
* @return identifier of this Account
*/
@Override
public Identifier id() {
return this.identifier;
}
}
13 changes: 13 additions & 0 deletions src/main/java/modules/ads/merkletree/MerkleTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,17 @@ private void buildMerkleTree() {
public int size() {
return this.size;
}

/**
* returns all entities in the merkle tree.
*
* @return all entities in the merkle tree.
*/
public ArrayList<Entity> all() {
ArrayList<Entity> entities = new ArrayList<>();
for (Entity e : entityHashTable.values()) {
entities.add(e);
}
return entities;
}
}
85 changes: 85 additions & 0 deletions src/main/java/state/merkle/MerkleSnapshot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package state.merkle;

import java.util.ArrayList;

import model.Entity;
import model.lightchain.Account;
import model.lightchain.Identifier;
import modules.ads.merkletree.MerkleTree;

/**
* Implements a simplified merkle tree based model of the protocol snapshot at a given block.
*/
public class MerkleSnapshot implements state.Snapshot {
private final Identifier rootBlockId;
private final long rootBlockHeight;
private MerkleTree merkleTree;

/**
* Constructor of MerkleSnapShot.
*
* @param rootBlockId root block id representing this snapshot.
* @param rootBlockHeight root block height of this snapshot.
*/
public MerkleSnapshot(Identifier rootBlockId, long rootBlockHeight) {
this.rootBlockId = rootBlockId;
this.rootBlockHeight = rootBlockHeight;
this.merkleTree = new MerkleTree();
}

/**
* The identifier of finalized block that this snapshot represents.
*
* @return the identifier of finalized block that this snapshot represents.
*/
@Override
public Identifier getReferenceBlockId() {
return rootBlockId;
}

/**
* The height of the reference block that this snapshot represents.
*
* @return height of the reference block that this snapshot represents.
*/
@Override
public long getReferenceBlockHeight() {
return rootBlockHeight;
}

/**
* Fetches account corresponding to an identifier at the given snapshot.
*
* @param identifier identifier of an account of interest.
* @return account corresponding to the given identifier at this snapshot, or null if such an account
* does not exist.
*/
@Override
public Account getAccount(Identifier identifier) {
return (Account) merkleTree.get(identifier).getEntity();
}

/**
* Adds an account to the snapshot.
*
* @param identifier Identifier of the account to add.
* @param account Account to add.
*/
public void addAccount(Identifier identifier, Account account) {
this.merkleTree.put(account);
}

/**
* The list of accounts in this snapshot.
*
* @return the list of accounts in this snapshot.
*/
@Override
public ArrayList<Account> all() {
ArrayList<Account> accounts = new ArrayList<>();
for (Entity entity : merkleTree.all()) {
accounts.add((Account) entity);
}
return accounts;
}
}
Loading