qortal/src/qora/block/BlockFactory.java
Kc 749baf8843 *** NOT FINAL, NOT TESTED ***
added data access class for block
added HSQLDB repository with reader methods for block data
started usage of repository and data objects in BlockFactory
2018-06-08 21:31:31 +02:00

78 lines
2.0 KiB
Java

package qora.block;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import data.block.IBlockData;
import data.repository.HSQLDBRepository;
import data.repository.IRepository;
import database.DB;
import database.NoDataFoundException;
import qora.account.PublicKeyAccount;
import qora.transaction.Transaction;
import qora.transaction.TransactionFactory;
public class BlockFactory {
// XXX repository should be pushed here from the root entry, no need to know the repository type
private static IRepository repository = new HSQLDBRepository();
/**
* Load Block from DB using block signature.
*
* @param signature
* @return ? extends Block, or null if not found
* @throws SQLException
*/
public static Block fromSignature(byte[] signature) throws SQLException {
Block block = Block.fromSignature(signature);
if (block == null)
return null;
// Can we promote to a GenesisBlock?
if (GenesisBlock.isGenesisBlock(block))
return GenesisBlock.getInstance();
// Standard block
return block;
}
/**
* Load Block from DB using block height
*
* @param height
* @return ? extends Block, or null if not found
* @throws SQLException
*/
public static Block fromHeight(int height) {
if (height == 1)
return GenesisBlock.getInstance();
try {
IBlockData data = repository.getBlockByHeight(height);
// TODO fill this list from TransactionFactory
List<Transaction> transactions = new ArrayList<Transaction>();
// TODO fetch account for data.getGeneratorPublicKey()
PublicKeyAccount generator = null;
return new Block(data.getVersion(), data.getReference(), data.getTimestamp(), data.getGeneratingBalance(),
generator,data.getGeneratorSignature(),data.getTransactionsSignature(),
data.getAtBytes(), data.getAtFees(), transactions);
} catch (Exception e) { // XXX move NoDataFoundException to repository domain and use it here?
return null;
}
}
// Navigation
// Converters
// Processing
}