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): small fixes; update go-paymail ref
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Nov 28, 2023
1 parent fcb1d74 commit d6fa8d0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 48 additions & 50 deletions paymail_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"database/sql"
"encoding/hex"
"fmt"
"github.com/bitcoin-sv/go-paymail/beef"
"reflect"
"time"

"github.com/bitcoin-sv/go-paymail"
"github.com/bitcoin-sv/go-paymail/beef"
"github.com/bitcoin-sv/go-paymail/server"
"github.com/bitcoin-sv/go-paymail/spv"
"github.com/bitcoinschema/go-bitcoin/v2"
Expand Down Expand Up @@ -175,15 +175,12 @@ 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 p2pTx.DecodedBeef != nil {
if reflect.TypeOf(rts) == reflect.TypeOf(&externalIncomingTx{}) {
go saveBEEFTxInputs(ctx, p.client, p2pTx.DecodedBeef)
}
}

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

// Return the response from the p2p request
return &paymail.P2PTransactionPayload{
Note: p2pTx.MetaData.Note,
Expand Down Expand Up @@ -318,59 +315,32 @@ func deriveKey(rawXPubKey string, num uint32) (k *derivedPubKey, err error) {
return
}

func saveBEEFTxInputs(ctx context.Context, c ClientInterface, p2pTx *paymail.P2PTransaction, rts recordIncomingTxStrategy) {
if reflect.TypeOf(rts) != reflect.TypeOf(&externalIncomingTx{}) {
return
}

inputsToAdd, err := getInputsWhichAreNotInDb(c, p2pTx)
func saveBEEFTxInputs(ctx context.Context, c ClientInterface, dBeef *beef.DecodedBEEF) {
inputsToAdd, err := getInputsWhichAreNotInDb(c, dBeef)
if err != nil {
c.Logger().Error(ctx, "error in saveBEEFTxInputs", err)
}

for _, input := range inputsToAdd {
bump, err := getBump(p2pTx, input)
if err != nil {
c.Logger().Error(ctx, fmt.Errorf("error in saveBEEFTxInputs: %w for beef: %v", err, p2pTx.DecodedBeef).Error())
var bump *BUMP
if input.BumpIndex != nil { // mined
bump, err = getBump(int(*input.BumpIndex), dBeef.BUMPs)
if err != nil {
c.Logger().Error(ctx, fmt.Errorf("error in saveBEEFTxInputs: %w for beef: %v", err, dBeef).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 getBump(p2pTx *paymail.P2PTransaction, input *beef.TxData) (*BUMP, error) {
bumpIndex := uint(input.BumpIndex.Length())
if bumpIndex > uint(len(p2pTx.DecodedBeef.BUMPs)) {
return nil, fmt.Errorf("error in getBump: bump index exceeds bumps length")
}
bump := p2pTx.DecodedBeef.BUMPs[bumpIndex]
paths := make([][]BUMPLeaf, 0)

for _, path := range bump.Path {
pathLeaves := make([]BUMPLeaf, 0)
for _, leaf := range path {
l := BUMPLeaf{
Offset: leaf.Offset,
Hash: leaf.Hash,
TxID: leaf.TxId,
Duplicate: leaf.Duplicate,
}
pathLeaves = append(pathLeaves, l)
c.Logger().Error(ctx, fmt.Errorf("error in saveBEEFTxInputs: %w for beef: %v", err, dBeef).Error())
}
paths = append(paths, pathLeaves)
}
return &BUMP{
BlockHeight: bump.BlockHeight,
Path: paths,
}, nil
}

func getInputsWhichAreNotInDb(c ClientInterface, p2pTx *paymail.P2PTransaction) ([]*beef.TxData, error) {
func getInputsWhichAreNotInDb(c ClientInterface, dBeef *beef.DecodedBEEF) ([]*beef.TxData, error) {
var txIDs []string
for _, tx := range p2pTx.DecodedBeef.Transactions {
for _, tx := range dBeef.Transactions {
txIDs = append(txIDs, tx.GetTxID())
}
dbTxs, err := c.GetTransactionsByIDs(context.Background(), txIDs)
Expand All @@ -384,7 +354,7 @@ func getInputsWhichAreNotInDb(c ClientInterface, p2pTx *paymail.P2PTransaction)
return txs, nil
}

for _, input := range p2pTx.DecodedBeef.Transactions {
for _, input := range dBeef.Transactions {
found := false
for _, dbTx := range dbTxs {
if dbTx.GetID() == input.GetTxID() {
Expand All @@ -400,6 +370,34 @@ func getInputsWhichAreNotInDb(c ClientInterface, p2pTx *paymail.P2PTransaction)
return txs, nil
}

func getBump(bumpIndx int, bumps beef.BUMPs) (*BUMP, error) {
if bumpIndx > len(bumps) {
return nil, fmt.Errorf("error in getBump: bump index exceeds bumps length")
}

bump := bumps[bumpIndx]
paths := make([][]BUMPLeaf, 0)

for _, path := range bump.Path {
pathLeaves := make([]BUMPLeaf, 0)
for _, leaf := range path {
l := BUMPLeaf{
Offset: leaf.Offset,
Hash: leaf.Hash,
TxID: leaf.TxId,
Duplicate: leaf.Duplicate,
}
pathLeaves = append(pathLeaves, l)
}
paths = append(paths, pathLeaves)
}

return &BUMP{
BlockHeight: bump.BlockHeight,
Path: paths,
}, nil
}

func saveBeefTransactionInput(ctx context.Context, c ClientInterface, input *beef.TxData, bump *BUMP) error {
newOpts := c.DefaultModelOptions(New())
inputTx := newTransaction(input.Transaction.String(), newOpts...)
Expand All @@ -422,7 +420,7 @@ func saveBeefTransactionInput(ctx context.Context, c ClientInterface, input *bee

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

0 comments on commit d6fa8d0

Please sign in to comment.