forked from Qortal/qortal
more refactoring
This commit is contained in:
parent
749baf8843
commit
0f16b1588c
@ -4,7 +4,7 @@ import java.math.BigDecimal;
|
|||||||
|
|
||||||
import qora.account.PublicKeyAccount;
|
import qora.account.PublicKeyAccount;
|
||||||
|
|
||||||
public class Block implements IBlockData {
|
public class Block implements BlockData {
|
||||||
private int version;
|
private int version;
|
||||||
private byte[] reference;
|
private byte[] reference;
|
||||||
private int transactionCount;
|
private int transactionCount;
|
||||||
|
@ -2,7 +2,7 @@ package data.block;
|
|||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
public interface IBlockData {
|
public interface BlockData {
|
||||||
public int getVersion();
|
public int getVersion();
|
||||||
public byte[] getReference();
|
public byte[] getReference();
|
||||||
public int getTransactionCount();
|
public int getTransactionCount();
|
@ -1,9 +0,0 @@
|
|||||||
package data.repository;
|
|
||||||
|
|
||||||
import data.block.IBlockData;
|
|
||||||
|
|
||||||
public interface IRepository {
|
|
||||||
// XXX use NoDataFoundException?
|
|
||||||
IBlockData getBlockBySignature(byte[] signature) throws Exception;
|
|
||||||
IBlockData getBlockByHeight(int height) throws Exception;
|
|
||||||
}
|
|
@ -11,6 +11,8 @@ import static java.util.stream.Collectors.toMap;
|
|||||||
public abstract class Transaction {
|
public abstract class Transaction {
|
||||||
|
|
||||||
// Transaction types
|
// Transaction types
|
||||||
|
// TODO Transaction types are semantic and should go into the business logic layer.
|
||||||
|
// No need to know the meaning of the integer value in data layer
|
||||||
public enum TransactionType {
|
public enum TransactionType {
|
||||||
GENESIS(1), PAYMENT(2), REGISTER_NAME(3), UPDATE_NAME(4), SELL_NAME(5), CANCEL_SELL_NAME(6), BUY_NAME(7), CREATE_POLL(8), VOTE_ON_POLL(9), ARBITRARY(
|
GENESIS(1), PAYMENT(2), REGISTER_NAME(3), UPDATE_NAME(4), SELL_NAME(5), CANCEL_SELL_NAME(6), BUY_NAME(7), CREATE_POLL(8), VOTE_ON_POLL(9), ARBITRARY(
|
||||||
10), ISSUE_ASSET(11), TRANSFER_ASSET(12), CREATE_ASSET_ORDER(13), CANCEL_ASSET_ORDER(14), MULTIPAYMENT(15), DEPLOY_AT(16), MESSAGE(17);
|
10), ISSUE_ASSET(11), TRANSFER_ASSET(12), CREATE_ASSET_ORDER(13), CANCEL_ASSET_ORDER(14), MULTIPAYMENT(15), DEPLOY_AT(16), MESSAGE(17);
|
||||||
@ -30,6 +32,7 @@ public abstract class Transaction {
|
|||||||
|
|
||||||
// Properties shared with all transaction types
|
// Properties shared with all transaction types
|
||||||
protected TransactionType type;
|
protected TransactionType type;
|
||||||
|
// TODO PublicKeyAccount is a separate data entity, so here should only be a key to reference it
|
||||||
protected PublicKeyAccount creator;
|
protected PublicKeyAccount creator;
|
||||||
protected long timestamp;
|
protected long timestamp;
|
||||||
protected byte[] reference;
|
protected byte[] reference;
|
||||||
|
@ -6,19 +6,19 @@ import java.sql.SQLException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import data.block.IBlockData;
|
import data.block.BlockData;
|
||||||
import data.repository.HSQLDBRepository;
|
|
||||||
import data.repository.IRepository;
|
|
||||||
import database.DB;
|
import database.DB;
|
||||||
import database.NoDataFoundException;
|
import database.NoDataFoundException;
|
||||||
import qora.account.PublicKeyAccount;
|
import qora.account.PublicKeyAccount;
|
||||||
import qora.transaction.Transaction;
|
import qora.transaction.Transaction;
|
||||||
import qora.transaction.TransactionFactory;
|
import qora.transaction.TransactionFactory;
|
||||||
|
import repository.BlockRepository;
|
||||||
|
import repository.hsqldb.HSQLDBBlockRepository;
|
||||||
|
|
||||||
public class BlockFactory {
|
public class BlockFactory {
|
||||||
|
|
||||||
// XXX repository should be pushed here from the root entry, no need to know the repository type
|
// XXX repository should be pushed here from the root entry, no need to know the repository type
|
||||||
private static IRepository repository = new HSQLDBRepository();
|
private static BlockRepository repository = new HSQLDBBlockRepository();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load Block from DB using block signature.
|
* Load Block from DB using block signature.
|
||||||
@ -52,7 +52,7 @@ public class BlockFactory {
|
|||||||
return GenesisBlock.getInstance();
|
return GenesisBlock.getInstance();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
IBlockData data = repository.getBlockByHeight(height);
|
BlockData data = repository.fromHeight(height);
|
||||||
|
|
||||||
// TODO fill this list from TransactionFactory
|
// TODO fill this list from TransactionFactory
|
||||||
List<Transaction> transactions = new ArrayList<Transaction>();
|
List<Transaction> transactions = new ArrayList<Transaction>();
|
||||||
|
8
src/repository/BlockRepository.java
Normal file
8
src/repository/BlockRepository.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package repository;
|
||||||
|
|
||||||
|
import data.block.BlockData;
|
||||||
|
|
||||||
|
public interface BlockRepository {
|
||||||
|
BlockData fromSignature(byte[] signature) throws DataException;
|
||||||
|
BlockData fromHeight(int height) throws DataException;
|
||||||
|
}
|
22
src/repository/DataException.java
Normal file
22
src/repository/DataException.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package repository;
|
||||||
|
|
||||||
|
public class DataException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -3963965667288257605L;
|
||||||
|
|
||||||
|
public DataException() {}
|
||||||
|
|
||||||
|
public DataException(String message)
|
||||||
|
{
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,9 +3,13 @@ package repository;
|
|||||||
public abstract class Repository {
|
public abstract class Repository {
|
||||||
|
|
||||||
protected TransactionRepository transactionRepository;
|
protected TransactionRepository transactionRepository;
|
||||||
|
protected BlockRepository blockRepository;
|
||||||
|
|
||||||
public TransactionRepository getTransactionRepository() {
|
public TransactionRepository getTransactionRepository() {
|
||||||
return this.transactionRepository;
|
return this.transactionRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BlockRepository getBlockRepository() {
|
||||||
|
return this.blockRepository;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,4 +12,8 @@ public abstract class RepositoryManager {
|
|||||||
return repository.transactionRepository;
|
return repository.transactionRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static BlockRepository getBlockRepository() {
|
||||||
|
return repository.blockRepository;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package data.repository;
|
package repository.hsqldb;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
@ -7,11 +7,13 @@ import java.sql.SQLException;
|
|||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
import data.block.Block;
|
import data.block.Block;
|
||||||
import data.block.IBlockData;
|
import data.block.BlockData;
|
||||||
import database.DB;
|
import database.DB;
|
||||||
import qora.account.PublicKeyAccount;
|
import qora.account.PublicKeyAccount;
|
||||||
|
import repository.BlockRepository;
|
||||||
|
import repository.DataException;
|
||||||
|
|
||||||
public class HSQLDBRepository implements IRepository
|
public class HSQLDBBlockRepository implements BlockRepository
|
||||||
{
|
{
|
||||||
protected static final int TRANSACTIONS_SIGNATURE_LENGTH = 64;
|
protected static final int TRANSACTIONS_SIGNATURE_LENGTH = 64;
|
||||||
protected static final int GENERATOR_SIGNATURE_LENGTH = 64;
|
protected static final int GENERATOR_SIGNATURE_LENGTH = 64;
|
||||||
@ -20,19 +22,29 @@ public class HSQLDBRepository implements IRepository
|
|||||||
private static final String BLOCK_DB_COLUMNS = "version, reference, transaction_count, total_fees, "
|
private static final String BLOCK_DB_COLUMNS = "version, reference, transaction_count, total_fees, "
|
||||||
+ "transactions_signature, height, generation, generating_balance, generator, generator_signature, AT_data, AT_fees";
|
+ "transactions_signature, height, generation, generating_balance, generator, generator_signature, AT_data, AT_fees";
|
||||||
|
|
||||||
public IBlockData getBlockBySignature(byte[] signature) throws SQLException
|
public BlockData fromSignature(byte[] signature) throws DataException
|
||||||
{
|
{
|
||||||
ResultSet rs = DB.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE signature = ?", signature);
|
ResultSet rs;
|
||||||
|
try {
|
||||||
|
rs = DB.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE signature = ?", signature);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DataException("Error loading data from DB", e);
|
||||||
|
}
|
||||||
return getBlockFromResultSet(rs);
|
return getBlockFromResultSet(rs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IBlockData getBlockByHeight(int height) throws SQLException
|
public BlockData fromHeight(int height) throws DataException
|
||||||
{
|
{
|
||||||
ResultSet rs = DB.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE height = ?", height);
|
ResultSet rs;
|
||||||
|
try {
|
||||||
|
rs = DB.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE height = ?", height);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DataException("Error loading data from DB", e);
|
||||||
|
}
|
||||||
return getBlockFromResultSet(rs);
|
return getBlockFromResultSet(rs);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IBlockData getBlockFromResultSet(ResultSet rs) throws SQLException {
|
private BlockData getBlockFromResultSet(ResultSet rs) throws DataException {
|
||||||
int version = rs.getInt(1);
|
int version = rs.getInt(1);
|
||||||
byte[] reference = DB.getResultSetBytes(rs.getBinaryStream(2), REFERENCE_LENGTH);
|
byte[] reference = DB.getResultSetBytes(rs.getBinaryStream(2), REFERENCE_LENGTH);
|
||||||
int transactionCount = rs.getInt(3);
|
int transactionCount = rs.getInt(3);
|
@ -10,7 +10,7 @@ import data.transaction.GenesisTransaction;
|
|||||||
import data.transaction.Transaction;
|
import data.transaction.Transaction;
|
||||||
import database.DB;
|
import database.DB;
|
||||||
|
|
||||||
public class HSQLDBGenesisTransaction extends HSQLDBTransaction {
|
public class HSQLDBGenesisTransactionRepository extends HSQLDBTransactionRepository {
|
||||||
|
|
||||||
Transaction fromBase(byte[] signature, byte[] reference, PublicKeyAccount creator, long timestamp, BigDecimal fee) {
|
Transaction fromBase(byte[] signature, byte[] reference, PublicKeyAccount creator, long timestamp, BigDecimal fee) {
|
||||||
try {
|
try {
|
@ -5,7 +5,7 @@ import repository.Repository;
|
|||||||
public class HSQLDBRepository extends Repository {
|
public class HSQLDBRepository extends Repository {
|
||||||
|
|
||||||
public HSQLDBRepository() {
|
public HSQLDBRepository() {
|
||||||
this.transactionRepository = new HSQLDBTransaction();
|
this.transactionRepository = new HSQLDBTransactionRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,12 @@ import database.DB;
|
|||||||
import qora.block.Block;
|
import qora.block.Block;
|
||||||
import repository.TransactionRepository;
|
import repository.TransactionRepository;
|
||||||
|
|
||||||
public class HSQLDBTransaction implements TransactionRepository {
|
public class HSQLDBTransactionRepository implements TransactionRepository {
|
||||||
|
|
||||||
private HSQLDBGenesisTransaction genesisTransactionRepository;
|
private HSQLDBGenesisTransactionRepository genesisTransactionRepository;
|
||||||
|
|
||||||
public HSQLDBTransaction() {
|
public HSQLDBTransactionRepository() {
|
||||||
genesisTransactionRepository = new HSQLDBGenesisTransaction();
|
genesisTransactionRepository = new HSQLDBGenesisTransactionRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Transaction fromSignature(byte[] signature) {
|
public Transaction fromSignature(byte[] signature) {
|
Loading…
x
Reference in New Issue
Block a user