Skip to content

Commit

Permalink
refactor(api): simplify activation of init policy states
Browse files Browse the repository at this point in the history
  • Loading branch information
hbriese committed Nov 29, 2023
1 parent e8c6866 commit 8342340
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
30 changes: 8 additions & 22 deletions api/src/features/accounts/accounts.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ACCOUNTS_QUEUE, AccountActivationEvent } from './accounts.queue';
import { tryOrIgnoreAsync } from 'lib';
import { DatabaseService } from '../database/database.service';
import e from '~/edgeql-js';
import { and } from '../database/database.util';

@Injectable()
@Processor(ACCOUNTS_QUEUE.name)
Expand Down Expand Up @@ -39,27 +38,14 @@ export class AccountsProcessor {
return;
}

await this.db.transaction(async (db) => {
await e
.select({
account: e.update(e.Account, () => ({
filter_single: { address: account },
set: {
isActive: true,
},
})),
policyState: e.update(e.PolicyState, (ps) => ({
filter: and(
e.op(ps.policy.account.address, '=', account),
e.op(ps.isAccountInitState, '=', true),
),
set: {
activationBlock: receipt.blockNumber,
},
})),
})
.run(db);
});
await this.db.query(
e.update(e.Account, () => ({
filter_single: { address: account },
set: {
isActive: true,
},
})),
);

await this.accounts.publishAccount({ account, event: AccountEvent.update });
}
Expand Down
24 changes: 14 additions & 10 deletions api/src/features/policies/policies.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,27 @@ export class PoliciesEventsProcessor {
private async markStateAsActive(chain: Chain, log: Log, key: PolicyKey) {
// TODO: filter state by state hash (part of event log) to ensure correct state is activated
// It's possible that two policies are activated in the same proposal; it's not prohibited by a constraint.
await e
.update(e.PolicyState, (ps) => ({
await this.db.query(
e.update(e.PolicyState, (ps) => ({
filter: and(
e.op(ps.policy, '=', selectPolicy({ account: asUAddress(log.address, chain), key })),
e.op(
ps.proposal,
'=',
e.select(e.Transaction, () => ({
filter_single: { hash: asHex(log.transactionHash) },
proposal: { id: true },
})).proposal,
e.op(
ps.proposal,
'?=', // Returns {false} rather than {} if one doesn't exist
e.select(e.Transaction, () => ({
filter_single: { hash: asHex(log.transactionHash) },
proposal: { id: true },
})).proposal,
),
'or',
ps.isAccountInitState,
),
),
set: {
activationBlock: log.blockNumber,
},
}))
.run(this.db.client);
})),
);
}
}

0 comments on commit 8342340

Please sign in to comment.