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

Commit 11c9553

Browse files
authored
Merge pull request #2014 from OpenBazaar/drwasho/eth-saveMsg21
feat: Save outbound order payment, refund, and dispute messages
2 parents 740068a + 1fd7a0d commit 11c9553

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

core/disputes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (n *OpenBazaarNode) OpenDispute(orderID string, contract *pb.RicardianContr
110110
contract.Signatures = append(contract.Signatures, rc.Signatures[0])
111111

112112
// Send to moderator
113-
err = n.SendDisputeOpen(order.Payment.Moderator, nil, rc)
113+
err = n.SendDisputeOpen(order.Payment.Moderator, nil, rc, orderID)
114114
if err != nil {
115115
return err
116116
}
@@ -131,7 +131,7 @@ func (n *OpenBazaarNode) OpenDispute(orderID string, contract *pb.RicardianContr
131131
return nil
132132
}
133133
}
134-
err = n.SendDisputeOpen(counterparty, &counterkey, rc)
134+
err = n.SendDisputeOpen(counterparty, &counterkey, rc, orderID)
135135
if err != nil {
136136
return err
137137
}
@@ -764,11 +764,11 @@ func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPer
764764
return err
765765
}
766766

767-
err = n.SendDisputeClose(buyerID, &buyerKey, rc)
767+
err = n.SendDisputeClose(buyerID, &buyerKey, rc, orderID)
768768
if err != nil {
769769
return err
770770
}
771-
err = n.SendDisputeClose(vendorID, &vendorKey, rc)
771+
err = n.SendDisputeClose(vendorID, &vendorKey, rc, orderID)
772772
if err != nil {
773773
return err
774774
}

core/net.go

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,26 @@ func (n *OpenBazaarNode) SendRefund(peerID string, refundMessage *pb.RicardianCo
443443
log.Errorf("failed to marshal the contract: %v", err)
444444
return err
445445
}
446+
// Create the REFUND message
446447
m := pb.Message{
447448
MessageType: pb.Message_REFUND,
448449
Payload: a,
449450
}
451+
452+
// Save REFUND message to the database for this order for resending if necessary
453+
orderID0 := refundMessage.Refund.OrderID
454+
if orderID0 == "" {
455+
log.Errorf("failed fetching orderID")
456+
} else {
457+
err = n.Datastore.Messages().Put(
458+
fmt.Sprintf("%s-%d", orderID0, int(pb.Message_REFUND)),
459+
orderID0, pb.Message_REFUND, peerID, repo.Message{Msg: m},
460+
"", 0, []byte{})
461+
if err != nil {
462+
log.Errorf("failed putting message (%s-%d): %v", orderID0, int(pb.Message_REFUND), err)
463+
}
464+
}
465+
450466
k, err := libp2p.UnmarshalPublicKey(refundMessage.GetBuyerOrder().GetBuyerID().GetPubkeys().Identity)
451467
if err != nil {
452468
log.Errorf("failed to unmarshal publicKey: %v", err)
@@ -508,16 +524,33 @@ func (n *OpenBazaarNode) SendOrderCompletion(peerID string, k *libp2p.PubKey, co
508524
}
509525

510526
// SendDisputeOpen - send open dispute msg to peer
511-
func (n *OpenBazaarNode) SendDisputeOpen(peerID string, k *libp2p.PubKey, disputeMessage *pb.RicardianContract) error {
527+
func (n *OpenBazaarNode) SendDisputeOpen(peerID string, k *libp2p.PubKey, disputeMessage *pb.RicardianContract, orderID string) error {
512528
a, err := ptypes.MarshalAny(disputeMessage)
513529
if err != nil {
514530
log.Errorf("failed to marshal the contract: %v", err)
515531
return err
516532
}
533+
534+
// Create the DISPUTE_OPEN message
517535
m := pb.Message{
518536
MessageType: pb.Message_DISPUTE_OPEN,
519537
Payload: a,
520538
}
539+
540+
// Save DISPUTE_OPEN message to the database for this order for resending if necessary
541+
orderID0 := orderID
542+
if orderID0 == "" {
543+
log.Errorf("failed fetching orderID")
544+
} else {
545+
err = n.Datastore.Messages().Put(
546+
fmt.Sprintf("%s-%d", orderID0, int(pb.Message_DISPUTE_OPEN)),
547+
orderID0, pb.Message_DISPUTE_OPEN, peerID, repo.Message{Msg: m},
548+
"", 0, []byte{})
549+
if err != nil {
550+
log.Errorf("failed putting message (%s-%d): %v", orderID0, int(pb.Message_DISPUTE_OPEN), err)
551+
}
552+
}
553+
521554
return n.sendMessage(peerID, k, m)
522555
}
523556

@@ -528,24 +561,58 @@ func (n *OpenBazaarNode) SendDisputeUpdate(peerID string, updateMessage *pb.Disp
528561
log.Errorf("failed to marshal the contract: %v", err)
529562
return err
530563
}
564+
565+
// Create the DISPUTE_UPDATE message
531566
m := pb.Message{
532567
MessageType: pb.Message_DISPUTE_UPDATE,
533568
Payload: a,
534569
}
570+
571+
// Save DISPUTE_UPDATE message to the database for this order for resending if necessary
572+
orderID0 := updateMessage.OrderId
573+
if orderID0 == "" {
574+
log.Errorf("failed fetching orderID")
575+
} else {
576+
err = n.Datastore.Messages().Put(
577+
fmt.Sprintf("%s-%d", orderID0, int(pb.Message_DISPUTE_UPDATE)),
578+
orderID0, pb.Message_DISPUTE_UPDATE, peerID, repo.Message{Msg: m},
579+
"", 0, []byte{})
580+
if err != nil {
581+
log.Errorf("failed putting message (%s-%d): %v", orderID0, int(pb.Message_DISPUTE_UPDATE), err)
582+
}
583+
}
584+
535585
return n.sendMessage(peerID, nil, m)
536586
}
537587

538588
// SendDisputeClose - send dispute closed msg to peer
539-
func (n *OpenBazaarNode) SendDisputeClose(peerID string, k *libp2p.PubKey, resolutionMessage *pb.RicardianContract) error {
589+
func (n *OpenBazaarNode) SendDisputeClose(peerID string, k *libp2p.PubKey, resolutionMessage *pb.RicardianContract, orderID string) error {
540590
a, err := ptypes.MarshalAny(resolutionMessage)
541591
if err != nil {
542592
log.Errorf("failed to marshal the contract: %v", err)
543593
return err
544594
}
595+
596+
// Create the DISPUTE_CLOSE message
545597
m := pb.Message{
546598
MessageType: pb.Message_DISPUTE_CLOSE,
547599
Payload: a,
548600
}
601+
602+
// Save DISPUTE_CLOSE message to the database for this order for resending if necessary
603+
orderID0 := orderID
604+
if orderID0 == "" {
605+
log.Errorf("failed fetching orderID")
606+
} else {
607+
err = n.Datastore.Messages().Put(
608+
fmt.Sprintf("%s-%d", orderID0, int(pb.Message_DISPUTE_CLOSE)),
609+
orderID0, pb.Message_DISPUTE_CLOSE, peerID, repo.Message{Msg: m},
610+
"", 0, []byte{})
611+
if err != nil {
612+
log.Errorf("failed putting message (%s-%d): %v", orderID0, int(pb.Message_DISPUTE_CLOSE), err)
613+
}
614+
}
615+
549616
return n.sendMessage(peerID, k, m)
550617
}
551618

@@ -804,6 +871,8 @@ func (n *OpenBazaarNode) SendOrderPayment(spend *SpendResponse) error {
804871
if err != nil {
805872
return err
806873
}
874+
875+
// Create the ORDER_PAYMENT message
807876
m := pb.Message{
808877
MessageType: pb.Message_ORDER_PAYMENT,
809878
Payload: a,
@@ -813,6 +882,21 @@ func (n *OpenBazaarNode) SendOrderPayment(spend *SpendResponse) error {
813882
if err != nil {
814883
return err
815884
}
885+
886+
// Save ORDER_PAYMENT message to the database for this order for resending if necessary
887+
orderID0 := msg.OrderID
888+
if orderID0 == "" {
889+
log.Errorf("failed fetching orderID")
890+
} else {
891+
err = n.Datastore.Messages().Put(
892+
fmt.Sprintf("%s-%d", orderID0, int(pb.Message_ORDER_PAYMENT)),
893+
orderID0, pb.Message_ORDER_PAYMENT, spend.PeerID, repo.Message{Msg: m},
894+
"", 0, []byte{})
895+
if err != nil {
896+
log.Errorf("failed putting message (%s-%d): %v", orderID0, int(pb.Message_ORDER_PAYMENT), err)
897+
}
898+
}
899+
816900
ctx, cancel := context.WithTimeout(context.Background(), n.OfflineMessageFailoverTimeout)
817901
err = n.Service.SendMessage(ctx, p, &m)
818902
cancel()

0 commit comments

Comments
 (0)