@@ -55,6 +55,26 @@ func (l *TransactionListener) getOrderDetails(orderID string, address btc.Addres
55
55
return contract , state , funded , records , err
56
56
}
57
57
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
+
58
78
func (l * TransactionListener ) OnTransactionReceived (cb wallet.TransactionCallback ) {
59
79
log .Info ("Transaction received" , cb .Txid , cb .Height )
60
80
@@ -75,11 +95,13 @@ func (l *TransactionListener) OnTransactionReceived(cb wallet.TransactionCallbac
75
95
//contract, state, funded, records, err := l.db.Sales().GetByPaymentAddress(output.Address)
76
96
if err == nil && state != pb .OrderState_PROCESSING_ERROR {
77
97
l .processSalePayment (cb .Txid , output , contract , state , funded , records )
98
+ l .cleanupOrderState (true , cb .Txid , output , contract , state , funded , records )
78
99
continue
79
100
}
80
101
contract , state , funded , records , err = l .getOrderDetails (output .OrderID , output .Address , false )
81
102
if err == nil {
82
103
l .processPurchasePayment (cb .Txid , output , contract , state , funded , records )
104
+ l .cleanupOrderState (false , cb .Txid , output , contract , state , funded , records )
83
105
continue
84
106
}
85
107
}
0 commit comments