Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 1677a62

Browse files
authored
Merge pull request #2045 from OpenBazaar/brian.cleanupOrderState
Cleanup Order State
2 parents 666c83a + d537582 commit 1677a62

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

wallet/listeners/transaction_listener.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ func (l *TransactionListener) getOrderDetails(orderID string, address btc.Addres
5555
return contract, state, funded, records, err
5656
}
5757

58+
// cleanupOrderState - scan each order to ensure the state in the db matches the state of the contract stored
59+
func (l *TransactionListener) cleanupOrderState(isSale bool, txid string, output wallet.TransactionOutput, contract *pb.RicardianContract, state pb.OrderState, funded bool, records []*wallet.TransactionRecord) {
60+
61+
orderId, err := calcOrderId(contract.BuyerOrder)
62+
if err != nil {
63+
return
64+
}
65+
log.Debugf("Cleaning up order state for: #%s\n", orderId)
66+
67+
if contract.DisputeResolution != nil && state != pb.OrderState_DECIDED && state != pb.OrderState_RESOLVED {
68+
log.Infof("Out of sync order. Found %s and should either DECIDED be %s\n", state, pb.OrderState_RESOLVED)
69+
if isSale {
70+
l.db.Sales().Put(orderId, *contract, pb.OrderState_RESOLVED, false)
71+
} else {
72+
l.db.Purchases().Put(orderId, *contract, pb.OrderState_RESOLVED, false)
73+
}
74+
75+
}
76+
}
77+
5878
func (l *TransactionListener) OnTransactionReceived(cb wallet.TransactionCallback) {
5979
log.Info("Transaction received", cb.Txid, cb.Height)
6080

@@ -75,11 +95,13 @@ func (l *TransactionListener) OnTransactionReceived(cb wallet.TransactionCallbac
7595
//contract, state, funded, records, err := l.db.Sales().GetByPaymentAddress(output.Address)
7696
if err == nil && state != pb.OrderState_PROCESSING_ERROR {
7797
l.processSalePayment(cb.Txid, output, contract, state, funded, records)
98+
l.cleanupOrderState(true, cb.Txid, output, contract, state, funded, records)
7899
continue
79100
}
80101
contract, state, funded, records, err = l.getOrderDetails(output.OrderID, output.Address, false)
81102
if err == nil {
82103
l.processPurchasePayment(cb.Txid, output, contract, state, funded, records)
104+
l.cleanupOrderState(false, cb.Txid, output, contract, state, funded, records)
83105
continue
84106
}
85107
}

0 commit comments

Comments
 (0)