Fix incorrect count of forged blocks

This commit is contained in:
catbref 2019-06-04 16:16:26 +01:00
parent b7f53afe68
commit c03a5c3d0e

View File

@ -56,7 +56,7 @@ public class HSQLDBBlockRepository implements BlockRepository {
@Override @Override
public BlockData fromSignature(byte[] signature) throws DataException { public BlockData fromSignature(byte[] signature) throws DataException {
try (ResultSet resultSet = this.repository.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE signature = ?", signature)) { try (ResultSet resultSet = this.repository.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE signature = ? LIMIT 1", signature)) {
return getBlockFromResultSet(resultSet); return getBlockFromResultSet(resultSet);
} catch (SQLException e) { } catch (SQLException e) {
throw new DataException("Error fetching block by signature from repository", e); throw new DataException("Error fetching block by signature from repository", e);
@ -65,7 +65,7 @@ public class HSQLDBBlockRepository implements BlockRepository {
@Override @Override
public BlockData fromReference(byte[] reference) throws DataException { public BlockData fromReference(byte[] reference) throws DataException {
try (ResultSet resultSet = this.repository.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE reference = ?", reference)) { try (ResultSet resultSet = this.repository.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE reference = ? LIMIT 1", reference)) {
return getBlockFromResultSet(resultSet); return getBlockFromResultSet(resultSet);
} catch (SQLException e) { } catch (SQLException e) {
throw new DataException("Error fetching block by reference from repository", e); throw new DataException("Error fetching block by reference from repository", e);
@ -74,7 +74,7 @@ public class HSQLDBBlockRepository implements BlockRepository {
@Override @Override
public BlockData fromHeight(int height) throws DataException { public BlockData fromHeight(int height) throws DataException {
try (ResultSet resultSet = this.repository.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE height = ?", height)) { try (ResultSet resultSet = this.repository.checkedExecute("SELECT " + BLOCK_DB_COLUMNS + " FROM Blocks WHERE height = ? LIMIT 1", height)) {
return getBlockFromResultSet(resultSet); return getBlockFromResultSet(resultSet);
} catch (SQLException e) { } catch (SQLException e) {
throw new DataException("Error fetching block by height from repository", e); throw new DataException("Error fetching block by height from repository", e);
@ -83,7 +83,7 @@ public class HSQLDBBlockRepository implements BlockRepository {
@Override @Override
public int getHeightFromSignature(byte[] signature) throws DataException { public int getHeightFromSignature(byte[] signature) throws DataException {
try (ResultSet resultSet = this.repository.checkedExecute("SELECT height FROM Blocks WHERE signature = ?", signature)) { try (ResultSet resultSet = this.repository.checkedExecute("SELECT height FROM Blocks WHERE signature = ? LIMIT 1", signature)) {
if (resultSet == null) if (resultSet == null)
return 0; return 0;
@ -157,15 +157,25 @@ public class HSQLDBBlockRepository implements BlockRepository {
@Override @Override
public int countForgedBlocks(byte[] publicKey) throws DataException { public int countForgedBlocks(byte[] publicKey) throws DataException {
String subquerySql = "SELECT proxy_public_key FROM ProxyForgers WHERE forger = ?"; String directSql = "SELECT COUNT(*) FROM Blocks WHERE generator = ?";
String sql = "SELECT COUNT(*) FROM Blocks WHERE generator IN (?, (" + subquerySql + ")) LIMIT 1"; String proxySql = "SELECT COUNT(*) FROM ProxyForgers JOIN Blocks ON generator = proxy_public_key WHERE forger = ?";
try (ResultSet resultSet = this.repository.checkedExecute(sql, publicKey, publicKey)) { int totalCount = 0;
return resultSet.getInt(1);
try (ResultSet resultSet = this.repository.checkedExecute(directSql, publicKey)) {
totalCount += resultSet.getInt(1);
} catch (SQLException e) { } catch (SQLException e) {
throw new DataException("Unable to fetch forged blocks count from repository", e); throw new DataException("Unable to fetch forged blocks count from repository", e);
} }
try (ResultSet resultSet = this.repository.checkedExecute(proxySql, publicKey)) {
totalCount += resultSet.getInt(1);
} catch (SQLException e) {
throw new DataException("Unable to fetch forged blocks count from repository", e);
}
return totalCount;
} }
@Override @Override