Skip to content

Commit

Permalink
fix(core-database): don't swallow errors in buildWallets
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian committed Apr 16, 2019
1 parent d012ce8 commit 4460324
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 33 deletions.
Expand Up @@ -10,8 +10,8 @@ export class DatabaseConnectionStub implements Database.IConnection {
public walletsRepository: Database.IWalletsRepository;
public options: any;

public buildWallets(): Promise<boolean> {
return undefined;
public buildWallets(): Promise<void> {
return null;
}

public commitQueuedQueries(): any {}
Expand Down
23 changes: 9 additions & 14 deletions packages/core-database-postgres/src/integrity-verifier.ts
Expand Up @@ -10,7 +10,7 @@ export class IntegrityVerifier {

constructor(private readonly query: QueryExecutor, private readonly walletManager: Database.IWalletManager) {}

public async run(): Promise<boolean> {
public async run(): Promise<void> {
this.logger.info("Integrity Verification - Step 1 of 8: Received Transactions");
await this.buildReceivedTransactions();

Expand Down Expand Up @@ -40,7 +40,7 @@ export class IntegrityVerifier {
);
this.logger.info(`Number of registered delegates: ${Object.keys(this.walletManager.allByUsername()).length}`);

return this.verifyWalletsConsistency();
this.verifyWalletsConsistency();
}

private async buildReceivedTransactions(): Promise<void> {
Expand Down Expand Up @@ -169,28 +169,23 @@ export class IntegrityVerifier {
}

/**
* Verify the consistency of the wallets table by comparing all records against
* the in memory wallets.
* Verify the consistency of the wallets table by comparing all records against the in memory wallets.
*
* NOTE: This is faster than rebuilding the entire table from scratch each time.
* @returns {Boolean}
*/
private async verifyWalletsConsistency(): Promise<boolean> {
let detectedInconsistency = false;

private verifyWalletsConsistency(): void {
for (const wallet of this.walletManager.allByAddress()) {
if (wallet.balance.isLessThan(0) && !this.isGenesis(wallet)) {
detectedInconsistency = true;
this.logger.warn(`Wallet '${wallet.address}' has a negative balance of '${wallet.balance}'`);
break;

// @TODO: throw here
}

if (wallet.voteBalance.isLessThan(0)) {
detectedInconsistency = true;
this.logger.warn(`Wallet ${wallet.address} has a negative vote balance of '${wallet.voteBalance}'`);
break;

// @TODO: throw here
}
}

return !detectedInconsistency;
}
}
12 changes: 2 additions & 10 deletions packages/core-database-postgres/src/postgres-connection.ts
Expand Up @@ -110,16 +110,8 @@ export class PostgresConnection implements Database.IConnection {
this.logger.debug("Disconnected from database");
}

public async buildWallets(): Promise<boolean> {
try {
return await new IntegrityVerifier(this.query, this.walletManager).run();
} catch (error) {
this.logger.error(error.stack);

app.forceExit("Failed to build wallets. This indicates a problem with the database.");

return false;
}
public async buildWallets(): Promise<void> {
await new IntegrityVerifier(this.query, this.walletManager).run();
}

public async commitQueuedQueries(): Promise<void> {
Expand Down
7 changes: 1 addition & 6 deletions packages/core-database/src/database-service.ts
Expand Up @@ -122,15 +122,10 @@ export class DatabaseService implements Database.IDatabaseService {
}
}

// @TODO: make this throw an error
public async buildWallets(): Promise<void> {
this.walletManager.reset();

try {
await this.connection.buildWallets();
} catch (err) {
this.logger.error(err.stack);
}
await this.connection.buildWallets();
}

public async commitQueuedQueries(): Promise<void> {
Expand Down
Expand Up @@ -18,7 +18,7 @@ export interface IConnection {

disconnect(): Promise<void>;

buildWallets(): Promise<boolean>;
buildWallets(): Promise<void>;

saveBlock(block: Interfaces.IBlock): Promise<void>;

Expand Down

0 comments on commit 4460324

Please sign in to comment.