Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
feat(BUX-322): save inputs from BEEF tx
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Nov 27, 2023
1 parent de81ce6 commit 9332600
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
69 changes: 69 additions & 0 deletions beef_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,72 @@ func hydrateTransaction(ctx context.Context, tx *Transaction) error {

return nil
}

func getParentTransactionsForInput(ctx context.Context, client ClientInterface, input *TransactionInput) ([]*bt.Tx, error) {
inputTx, err := client.GetTransactionByID(ctx, input.UtxoPointer.TransactionID)
if err != nil {
return nil, err
}

if inputTx.MerkleProof.TxOrID != "" {
inputBtTx, err := bt.NewTxFromString(inputTx.Hex)
if err != nil {
return nil, fmt.Errorf("cannot convert to bt.Tx from hex (tx.ID: %s). Reason: %w", inputTx.ID, err)
}

return []*bt.Tx{inputBtTx}, nil
}

return nil, fmt.Errorf("transaction is not mined yet (tx.ID: %s)", inputTx.ID) // TODO: handle it in next iterration
}

func saveBeefTransactionInput(ctx context.Context, c ClientInterface, input *bt.Tx) error {
inputTx, err := c.GetTransactionByID(ctx, input.TxID())
if err != nil && err != ErrMissingTransaction {
return fmt.Errorf("error in saveBeefTransactionInput during getting transaction: %s", err.Error())
}

if inputTx != nil {
if inputTx.BUMP.BlockHeight > 0 {
fmt.Println("input exist in db and has BUMP")
return nil
}
fmt.Println("input exist in db and has no BUMP")

// Sync tx if BUMP is empty
err = _syncTxDataFromChain(ctx, inputTx.syncTransaction, inputTx)
if err != nil {
return fmt.Errorf("error in saveBeefTransactionInput during syncing transaction: %s", err.Error())
}
return nil
}

fmt.Println("input not exist in db")

newOpts := c.DefaultModelOptions(New())
inputTx = newTransaction(input.String(), newOpts...)

err = inputTx.Save(ctx)
if err != nil {
return fmt.Errorf("error in saveBeefTransactionInput during saving tx: %s", err.Error())
}

sync := newSyncTransaction(
inputTx.GetID(),
inputTx.Client().DefaultSyncConfig(),
inputTx.GetOptions(true)...,
)
sync.BroadcastStatus = SyncStatusSkipped
sync.P2PStatus = SyncStatusSkipped
sync.SyncStatus = SyncStatusReady

if err = sync.Save(ctx); err != nil {
return fmt.Errorf("error in saveBeefTransactionInput during saving sync tx: %s", err.Error())
}

err = _syncTxDataFromChain(ctx, sync, inputTx)
if err != nil {
return fmt.Errorf("error in saveBeefTransactionInput during syncing transaction: %s", err.Error())
}
return nil
}
17 changes: 17 additions & 0 deletions paymail_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/hex"
"fmt"
"reflect"
"time"

"github.com/bitcoin-sv/go-paymail"
Expand Down Expand Up @@ -173,6 +174,22 @@ func (p *PaymailDefaultServiceProvider) RecordTransaction(ctx context.Context,
return nil, err
}

if p2pTx.DecodedBeef == nil {
return &paymail.P2PTransactionPayload{
Note: p2pTx.MetaData.Note,
TxID: transaction.ID,
}, nil
}

if reflect.TypeOf(rts) == reflect.TypeOf(&externalIncomingTx{}) {
for _, input := range p2pTx.DecodedBeef.InputsTxData {

Check failure on line 185 in paymail_service_provider.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

p2pTx.DecodedBeef.InputsTxData undefined (type *beef.DecodedBEEF has no field or method InputsTxData)

Check failure on line 185 in paymail_service_provider.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

p2pTx.DecodedBeef.InputsTxData undefined (type *beef.DecodedBEEF has no field or method InputsTxData)
err = saveBeefTransactionInput(ctx, p.client, input.Transaction)
if err != nil {
p.client.Logger().Error(ctx, "error in saveBeefTransactionInput", err)
}
}
}

// Return the response from the p2p request
return &paymail.P2PTransactionPayload{
Note: p2pTx.MetaData.Note,
Expand Down

0 comments on commit 9332600

Please sign in to comment.