Skip to content

Commit

Permalink
Fix expired async transactions (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdeziel authored Jul 13, 2022
1 parent 6ed18ba commit 57238d6
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 195 deletions.
60 changes: 36 additions & 24 deletions lib/python/rvaspy/rvaspy/api_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions pkg/rvasp/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,23 @@ txloop:
continue txloop
}

// Acknowledge the transaction with the originator
if err = s.acknowledgeTransaction(tx); err != nil {
log.Warn().Err(err).Uint("id", tx.ID).Msg("could not acknowledge transaction")
tx.SetState(pb.TransactionState_FAILED)
switch tx.State {
case pb.TransactionState_PENDING_SENT, pb.TransactionState_PENDING_ACKNOWLEDGED:
// We are the beneficiary, so acknowledge the pending transaction with the
// originator
if err = s.acknowledgeTransaction(tx); err != nil {
log.Warn().Err(err).Uint("id", tx.ID).Msg("could not acknowledge transaction")
tx.SetState(pb.TransactionState_FAILED)
}
case pb.TransactionState_PENDING_RECEIVED:
// We are the originator, so send a new transfer to the beneficiary to
// continue the async handshake
if err = s.parent.continueAsync(tx); err != nil {
log.Warn().Err(err).Uint("id", tx.ID).Msg("could not send transaction")
tx.SetState(pb.TransactionState_FAILED)
}
default:
log.Error().Uint("id", tx.ID).Str("state", tx.StateString).Msg("unexpected transaction state")
}

// Save the updated transaction in the database
Expand Down
6 changes: 3 additions & 3 deletions pkg/rvasp/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (d *DB) LookupIdentity(walletAddress string) *gorm.DB {

// LookupPending returns the pending transactions.
func (d *DB) LookupPending() *gorm.DB {
return d.Query().Where("state in (?, ?)", pb.TransactionState_PENDING_SENT, pb.TransactionState_PENDING_ACKNOWLEDGED)
return d.Query().Where("state in (?, ?, ?)", pb.TransactionState_PENDING_SENT, pb.TransactionState_PENDING_RECEIVED, pb.TransactionState_PENDING_ACKNOWLEDGED)
}

// LookupTransaction by envelope ID.
Expand Down Expand Up @@ -152,8 +152,8 @@ func (d *DB) MakeTransaction(originator string, beneficiary string) (*Transactio
Envelope: uuid.New().String(),
Originator: originatorIdentity,
Beneficiary: beneficiaryIdentity,
State: pb.TransactionState_AWAITING,
StateString: pb.TransactionState_AWAITING.String(),
State: pb.TransactionState_AWAITING_REPLY,
StateString: pb.TransactionState_AWAITING_REPLY.String(),
Timestamp: time.Now(),
Vasp: d.vasp,
}, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/rvasp/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ func (s *dbTestSuite) TestLookupPending() {
id := s.db.GetVASP().ID

// Transaction lookups should be limited to the configured VASP
query := regexp.QuoteMeta(`SELECT * FROM "transactions" WHERE vasp_id = $1 AND state in ($2, $3)`)
s.mock.ExpectQuery(query).WithArgs(id, pb.TransactionState_PENDING_SENT, pb.TransactionState_PENDING_ACKNOWLEDGED).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(id))
query := regexp.QuoteMeta(`SELECT * FROM "transactions" WHERE vasp_id = $1 AND state in ($2, $3, $4)`)
s.mock.ExpectQuery(query).WithArgs(id, pb.TransactionState_PENDING_SENT, pb.TransactionState_PENDING_RECEIVED, pb.TransactionState_PENDING_ACKNOWLEDGED).WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(id))

var transaction db.Transaction
tx := s.db.LookupPending().First(&transaction)
Expand Down
Loading

0 comments on commit 57238d6

Please sign in to comment.