mirror of https://github.com/qortal/qortal
catbref
6 years ago
committed by
GitHub
12 changed files with 241 additions and 13 deletions
@ -0,0 +1,86 @@
|
||||
package data.block; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
import qora.account.PublicKeyAccount; |
||||
|
||||
public class Block implements BlockData { |
||||
private int version; |
||||
private byte[] reference; |
||||
private int transactionCount; |
||||
private BigDecimal totalFees; |
||||
private byte[] transactionsSignature; |
||||
private int height; |
||||
private long timestamp; |
||||
private BigDecimal generatingBalance; |
||||
private byte[] generatorPublicKey; |
||||
private byte[] generatorSignature; |
||||
private byte[] atBytes; |
||||
private BigDecimal atFees; |
||||
|
||||
public Block(int version, byte[] reference, int transactionCount, BigDecimal totalFees, byte[] transactionsSignature, |
||||
int height, long timestamp, BigDecimal generatingBalance, byte[] generatorPublicKey, byte[] generatorSignature, |
||||
byte[] atBytes, BigDecimal atFees) |
||||
{ |
||||
this.version = version; |
||||
this.reference = reference; |
||||
this.transactionCount = transactionCount; |
||||
this.totalFees = totalFees; |
||||
this.transactionsSignature = transactionsSignature; |
||||
this.height = height; |
||||
this.timestamp = timestamp; |
||||
this.generatingBalance = generatingBalance; |
||||
this.generatorPublicKey = generatorPublicKey; |
||||
this.generatorSignature = generatorSignature; |
||||
this.atBytes = atBytes; |
||||
this.atFees = atFees; |
||||
} |
||||
|
||||
public int getVersion() { |
||||
return version; |
||||
} |
||||
|
||||
public byte[] getReference() { |
||||
return reference; |
||||
} |
||||
|
||||
public int getTransactionCount() { |
||||
return transactionCount; |
||||
} |
||||
|
||||
public BigDecimal getTotalFees() { |
||||
return totalFees; |
||||
} |
||||
|
||||
public byte[] getTransactionsSignature() { |
||||
return transactionsSignature; |
||||
} |
||||
|
||||
public int getHeight() { |
||||
return height; |
||||
} |
||||
|
||||
public long getTimestamp() { |
||||
return timestamp; |
||||
} |
||||
|
||||
public BigDecimal getGeneratingBalance() { |
||||
return generatingBalance; |
||||
} |
||||
|
||||
public byte[] getGeneratorPublicKey() { |
||||
return generatorPublicKey; |
||||
} |
||||
|
||||
public byte[] getGeneratorSignature() { |
||||
return generatorSignature; |
||||
} |
||||
|
||||
public byte[] getAtBytes() { |
||||
return atBytes; |
||||
} |
||||
|
||||
public BigDecimal getAtFees() { |
||||
return atFees; |
||||
} |
||||
} |
@ -0,0 +1,18 @@
|
||||
package data.block; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
public interface BlockData { |
||||
public int getVersion(); |
||||
public byte[] getReference(); |
||||
public int getTransactionCount(); |
||||
public BigDecimal getTotalFees(); |
||||
public byte[] getTransactionsSignature(); |
||||
public int getHeight(); |
||||
public long getTimestamp(); |
||||
public BigDecimal getGeneratingBalance(); |
||||
public byte[] getGeneratorPublicKey(); |
||||
public byte[] getGeneratorSignature(); |
||||
public byte[] getAtBytes(); |
||||
public BigDecimal getAtFees(); |
||||
} |
@ -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; |
||||
} |
@ -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); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,64 @@
|
||||
package repository.hsqldb; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.sql.PreparedStatement; |
||||
import java.sql.ResultSet; |
||||
import java.sql.SQLException; |
||||
import java.sql.Timestamp; |
||||
|
||||
import data.block.Block; |
||||
import data.block.BlockData; |
||||
import database.DB; |
||||
import qora.account.PublicKeyAccount; |
||||
import repository.BlockRepository; |
||||
import repository.DataException; |
||||
|
||||
public class HSQLDBBlockRepository implements BlockRepository |
||||
{ |
||||
protected static final int TRANSACTIONS_SIGNATURE_LENGTH = 64; |
||||
protected static final int GENERATOR_SIGNATURE_LENGTH = 64; |
||||
protected static final int REFERENCE_LENGTH = GENERATOR_SIGNATURE_LENGTH + TRANSACTIONS_SIGNATURE_LENGTH; |
||||
|
||||
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"; |
||||
|
||||
public BlockData fromSignature(byte[] signature) throws DataException |
||||
{ |
||||
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); |
||||
} |
||||
|
||||
public BlockData fromHeight(int height) throws DataException |
||||
{ |
||||
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); |
||||
} |
||||
|
||||
private BlockData getBlockFromResultSet(ResultSet rs) throws DataException { |
||||
int version = rs.getInt(1); |
||||
byte[] reference = DB.getResultSetBytes(rs.getBinaryStream(2), REFERENCE_LENGTH); |
||||
int transactionCount = rs.getInt(3); |
||||
BigDecimal totalFees = rs.getBigDecimal(4); |
||||
byte[] transactionsSignature = DB.getResultSetBytes(rs.getBinaryStream(5), TRANSACTIONS_SIGNATURE_LENGTH); |
||||
int height = rs.getInt(6); |
||||
long timestamp = rs.getTimestamp(7).getTime(); |
||||
BigDecimal generatingBalance = rs.getBigDecimal(8); |
||||
byte[] generatorPublicKey = DB.getResultSetBytes(rs.getBinaryStream(9)); |
||||
byte[] generatorSignature = DB.getResultSetBytes(rs.getBinaryStream(10), GENERATOR_SIGNATURE_LENGTH); |
||||
byte[] atBytes = DB.getResultSetBytes(rs.getBinaryStream(11)); |
||||
BigDecimal atFees = rs.getBigDecimal(12); |
||||
|
||||
return new Block(version, reference, transactionCount, totalFees, transactionsSignature, height, timestamp, |
||||
generatingBalance,generatorPublicKey, generatorSignature, atBytes, atFees); |
||||
} |
||||
} |
Loading…
Reference in new issue