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

Commit

Permalink
chore(BUX-322): save BUMP in parent tx
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Nov 27, 2023
1 parent a69c347 commit 1064cb6
Showing 1 changed file with 45 additions and 20 deletions.
65 changes: 45 additions & 20 deletions paymail_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"database/sql"
"encoding/hex"
"fmt"
"github.com/libsv/go-bt/v2"
"github.com/bitcoin-sv/go-paymail/beef"
"reflect"
"time"

Expand Down Expand Up @@ -182,7 +182,7 @@ func (p *PaymailDefaultServiceProvider) RecordTransaction(ctx context.Context,
}, nil
}

saveBEEFTxInputs(ctx, p.client, p2pTx, rts)
go saveBEEFTxInputs(ctx, p.client, p2pTx, rts)

// Return the response from the p2p request
return &paymail.P2PTransactionPayload{
Expand Down Expand Up @@ -329,25 +329,55 @@ func saveBEEFTxInputs(ctx context.Context, c ClientInterface, p2pTx *paymail.P2P
}

for _, input := range inputsToAdd {
err := saveBeefTransactionInput(ctx, c, input)
bump, err := getBump(p2pTx, input)
if err != nil {
c.Logger().Error(ctx, "error in saveBEEFTxInputs", err)
c.Logger().Error(ctx, fmt.Errorf("error in saveBEEFTxInputs: %w for beef: %v", err, p2pTx.DecodedBeef).Error())
}
err = saveBeefTransactionInput(ctx, c, input, bump)
if err != nil {
c.Logger().Error(ctx, fmt.Errorf("error in saveBEEFTxInputs: %w for beef: %v", err, p2pTx.DecodedBeef).Error())
}
}

}

func getInputsWhichAreNotInDb(ctx context.Context, c ClientInterface, p2pTx *paymail.P2PTransaction) ([]*bt.Tx, error) {
func getBump(p2pTx *paymail.P2PTransaction, input *beef.TxData) (*BUMP, error) {
if input.BumpIndex.Length() > len(p2pTx.DecodedBeef.BUMPs) {
return nil, fmt.Errorf("error in getBump: bump index exceeds bumps length")
}
bump := p2pTx.DecodedBeef.BUMPs[input.BumpIndex.Length()]
paths := make([][]BUMPLeaf, 0)

for _, path := range bump.Path {
pathLeaves := make([]BUMPLeaf, 0)
for _, leaf := range path {
l := BUMPLeaf{
Offset: leaf.Offset,

Check failure on line 355 in paymail_service_provider.go

View workflow job for this annotation

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

leaf.Offset undefined (type beef.BUMPLeaf has no field or method Offset)

Check failure on line 355 in paymail_service_provider.go

View workflow job for this annotation

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

leaf.Offset undefined (type beef.BUMPLeaf has no field or method Offset)
Hash: leaf.Hash,
TxID: leaf.TxId,

Check failure on line 357 in paymail_service_provider.go

View workflow job for this annotation

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

leaf.TxId undefined (type beef.BUMPLeaf has no field or method TxId)

Check failure on line 357 in paymail_service_provider.go

View workflow job for this annotation

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

leaf.TxId undefined (type beef.BUMPLeaf has no field or method TxId)
Duplicate: leaf.Duplicate,

Check failure on line 358 in paymail_service_provider.go

View workflow job for this annotation

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

leaf.Duplicate undefined (type beef.BUMPLeaf has no field or method Duplicate)

Check failure on line 358 in paymail_service_provider.go

View workflow job for this annotation

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

leaf.Duplicate undefined (type beef.BUMPLeaf has no field or method Duplicate)
}
pathLeaves = append(pathLeaves, l)
}
paths = append(paths, pathLeaves)
}
return &BUMP{
BlockHeight: bump.BlockHeight,
Path: paths,
}, nil
}

func getInputsWhichAreNotInDb(ctx context.Context, c ClientInterface, p2pTx *paymail.P2PTransaction) ([]*beef.TxData, error) {
var txIDs []string
for _, tx := range p2pTx.DecodedBeef.Transactions {
txIDs = append(txIDs, tx.GetTxID())
}
dbTxs, err := c.GetTransactionsByIDs(ctx, txIDs)
dbTxs, err := c.GetTransactionsByIDs(context.Background(), txIDs)
if err != nil {
return nil, fmt.Errorf("error during getting txs from db: %s", err)
return nil, fmt.Errorf("error during getting txs from db: %w", err)
}

txs := make([]*bt.Tx, 0)
txs := make([]*beef.TxData, 0)

if len(dbTxs) == len(txIDs) {
return txs, nil
Expand All @@ -362,26 +392,21 @@ func getInputsWhichAreNotInDb(ctx context.Context, c ClientInterface, p2pTx *pay
}
}
if !found {
txs = append(txs, input.Transaction)
txs = append(txs, input)
}
}

return txs, nil
}

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())
}
func saveBeefTransactionInput(ctx context.Context, c ClientInterface, input *beef.TxData, bump *BUMP) error {
newOpts := c.DefaultModelOptions(New())
inputTx := newTransaction(input.Transaction.String(), newOpts...)

if inputTx != nil {
return nil
if bump != nil {
inputTx.BUMP = *bump
}

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

sync := newSyncTransaction(
inputTx.GetID(),
inputTx.Client().DefaultSyncConfig(),
Expand All @@ -393,7 +418,7 @@ func saveBeefTransactionInput(ctx context.Context, c ClientInterface, input *bt.

inputTx.syncTransaction = sync

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

0 comments on commit 1064cb6

Please sign in to comment.