generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SPV-1026): removed broadcast cronjob and updated transaction str…
…ategies (#692)
- Loading branch information
1 parent
b8e53bc
commit bf555b1
Showing
19 changed files
with
181 additions
and
812 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package engine | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/bitcoin-sv/go-paymail" | ||
) | ||
|
||
// processP2PTransaction will process the sync transaction record, or save the failure | ||
func processP2PTransaction(ctx context.Context, tx *Transaction) error { | ||
// Successfully capture any panics, convert to readable string and log the error | ||
defer recoverAndLog(tx.Client().Logger()) | ||
|
||
syncTx := tx.syncTransaction | ||
// Create the lock and set the release for after the function completes | ||
unlock, err := newWriteLock( | ||
ctx, fmt.Sprintf(lockKeyProcessP2PTx, syncTx.GetID()), syncTx.Client().Cachestore(), | ||
) | ||
defer unlock() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// No draft? | ||
if len(tx.DraftID) == 0 { | ||
syncTx.addSyncResult(ctx, syncActionP2P, "all", "no draft found, cannot complete p2p") | ||
|
||
return nil // TODO: why nil here?? | ||
} | ||
|
||
// Notify any P2P paymail providers associated to the transaction | ||
var results []*SyncResult | ||
if results, err = _notifyPaymailProviders(ctx, tx); err != nil { | ||
syncTx.addSyncResult(ctx, syncActionP2P, "", err.Error()) | ||
return err | ||
} | ||
|
||
// Update if we have some results | ||
if len(results) > 0 { | ||
syncTx.Results.Results = append(syncTx.Results.Results, results...) | ||
} | ||
|
||
// Update sync status to be ready now | ||
if syncTx.SyncStatus == SyncStatusPending { | ||
syncTx.SyncStatus = SyncStatusReady | ||
} | ||
|
||
if err = syncTx.Save(ctx); err != nil { | ||
syncTx.addSyncResult(ctx, syncActionP2P, "internal", err.Error()) | ||
return err | ||
} | ||
|
||
// Done! | ||
return nil | ||
} | ||
|
||
// _notifyPaymailProviders will notify any associated Paymail providers | ||
func _notifyPaymailProviders(ctx context.Context, transaction *Transaction) ([]*SyncResult, error) { | ||
pm := transaction.Client().PaymailClient() | ||
outputs := transaction.draftTransaction.Configuration.Outputs | ||
|
||
notifiedReceivers := make([]string, 0) | ||
results := make([]*SyncResult, len(outputs)) | ||
|
||
var payload *paymail.P2PTransactionPayload | ||
var err error | ||
|
||
for _, out := range outputs { | ||
p4 := out.PaymailP4 | ||
|
||
if p4 == nil || p4.ResolutionType != ResolutionTypeP2P { | ||
continue | ||
} | ||
|
||
receiver := fmt.Sprintf("%s@%s", p4.Alias, p4.Domain) | ||
if contains(notifiedReceivers, func(x string) bool { return x == receiver }) { | ||
continue // no need to send the same transaction to the same receiver second time | ||
} | ||
|
||
if payload, err = finalizeP2PTransaction( | ||
ctx, | ||
pm, | ||
p4, | ||
transaction, | ||
); err != nil { | ||
return nil, err | ||
} | ||
|
||
notifiedReceivers = append(notifiedReceivers, receiver) | ||
results = append(results, &SyncResult{ | ||
Action: syncActionP2P, | ||
ExecutedAt: time.Now().UTC(), | ||
Provider: p4.ReceiveEndpoint, | ||
StatusMessage: "success: " + payload.TxID, | ||
}) | ||
|
||
} | ||
return results, nil | ||
} |
Oops, something went wrong.