Skip to content

Commit

Permalink
Merge pull request #266 from oasisprotocol/xz/fix-allowance-owner-f-k…
Browse files Browse the repository at this point in the history
…ey-constraint

Add account on new allowance owner
  • Loading branch information
aefhm authored Jan 3, 2023
2 parents 3d9514f + ff998d0 commit 77fb0e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ func (m *Main) queueEscrows(batch *storage.QueryBatch, data *storage.StakingData
func (m *Main) queueAllowanceChanges(batch *storage.QueryBatch, data *storage.StakingData) error {
allowanceChangeDeleteQuery := m.qf.ConsensusAllowanceChangeDeleteQuery()
allowanceChangeUpdateQuery := m.qf.ConsensusAllowanceChangeUpdateQuery()
allowanceOwnerUpsertQuery := m.qf.ConsensusAllowanceOwnerUpsertQuery()

for _, allowanceChange := range data.AllowanceChanges {
if allowanceChange.Allowance.IsZero() {
Expand All @@ -738,6 +739,9 @@ func (m *Main) queueAllowanceChanges(batch *storage.QueryBatch, data *storage.St
allowanceChange.Beneficiary.String(),
)
} else {
// A new account with no funds can still submit allowance change transactions.
// Ensure account exists and satisfy `allowances->accounts` foreign key.
batch.Queue(allowanceOwnerUpsertQuery, allowanceChange.Owner.String())
batch.Queue(allowanceChangeUpdateQuery,
allowanceChange.Owner.String(),
allowanceChange.Beneficiary.String(),
Expand Down
7 changes: 7 additions & 0 deletions analyzer/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ func (qf QueryFactory) ConsensusAllowanceChangeDeleteQuery() string {
WHERE owner = $1 AND beneficiary = $2`, qf.chainID)
}

func (qf QueryFactory) ConsensusAllowanceOwnerUpsertQuery() string {
return fmt.Sprintf(`
INSERT INTO %[1]s.accounts (address)
VALUES ($1)
ON CONFLICT (address) DO NOTHING`, qf.chainID)
}

func (qf QueryFactory) ConsensusAllowanceChangeUpdateQuery() string {
return fmt.Sprintf(`
INSERT INTO %s.allowances (owner, beneficiary, allowance)
Expand Down

0 comments on commit 77fb0e5

Please sign in to comment.