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

Commit

Permalink
feat(BUX-291): adjust to review
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Nov 13, 2023
1 parent 1dbcc76 commit c6d3008
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 33 deletions.
2 changes: 1 addition & 1 deletion model_incoming_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func processIncomingTransaction(ctx context.Context, logClient zLogger.GormLogge
transaction.NumberOfInputs = uint32(len(transaction.TransactionBase.parsedTx.Inputs))
}

transaction.updateChainInfo(txInfo)
transaction.setChainInfo(txInfo)

// Create status message
onChain := len(transaction.BlockHash) > 0 || transaction.BlockHeight > 0
Expand Down
7 changes: 5 additions & 2 deletions model_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,19 @@ func (m *Transaction) isExternal() bool {
return m.draftTransaction == nil
}

func (m *Transaction) updateChainInfo(txInfo *chainstate.TransactionInfo) {
func (m *Transaction) setChainInfo(txInfo *chainstate.TransactionInfo) {
m.BlockHash = txInfo.BlockHash
m.BlockHeight = uint64(txInfo.BlockHeight)
m.setMerkleRoot(txInfo)
}

func (m *Transaction) setMerkleRoot(txInfo *chainstate.TransactionInfo) {
if txInfo.MerkleProof != nil {
mp := MerkleProof(*txInfo.MerkleProof)
m.MerkleProof = mp

bump := mp.ToBUMP()
bump.BlockHeight = m.BlockHeight
bump.BlockHeight = uint64(txInfo.BlockHeight)
m.BUMP = bump
}
}
Expand Down
57 changes: 29 additions & 28 deletions record_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"
"time"

"github.com/libsv/go-bt"
)

type recordTxStrategy interface {
Expand All @@ -22,36 +20,21 @@ func recordTransaction(ctx context.Context, c ClientInterface, strategy recordTx
unlock := waitForRecordTxWriteLock(ctx, c, strategy.TxID())
defer unlock()

transaction, err := strategy.Execute(ctx, c, opts)
return transaction, err
return strategy.Execute(ctx, c, opts)
}

func getRecordTxStrategy(ctx context.Context, c ClientInterface, xPubKey, txHex, draftID string) (recordTxStrategy, error) {
var rts recordTxStrategy

if draftID != "" {
rts = &outgoingTx{
Hex: txHex,
RelatedDraftID: draftID,
XPubKey: xPubKey,
}
rts = getOutgoingTxRecordStrategy(xPubKey, txHex, draftID)
} else {
tx, err := getTransactionByHex(ctx, c, txHex)
var err error
rts, err = getIncomingTxRecordStrategy(ctx, c, txHex)

if err != nil {
return nil, err
}

if tx != nil {
rts = &internalIncomingTx{
Tx: tx,
BroadcastNow: false,
}
} else {
rts = &externalIncomingTx{
Hex: txHex,
BroadcastNow: false,
}
}
}

if err := rts.Validate(); err != nil {
Expand All @@ -61,17 +44,35 @@ func getRecordTxStrategy(ctx context.Context, c ClientInterface, xPubKey, txHex,
return rts, nil
}

func getTransactionByHex(ctx context.Context, c ClientInterface, hex string) (*Transaction, error) {
btTx, err := bt.NewTxFromString(hex)
func getOutgoingTxRecordStrategy(xPubKey, txHex, draftID string) recordTxStrategy {
return &outgoingTx{
Hex: txHex,
RelatedDraftID: draftID,
XPubKey: xPubKey,
}
}

func getIncomingTxRecordStrategy(ctx context.Context, c ClientInterface, txHex string) (recordTxStrategy, error) {
tx, err := getTransactionByHex(ctx, txHex, c.DefaultModelOptions()...)
if err != nil {
return nil, err
}

transaction, err := getTransactionByID(
ctx, "", btTx.GetTxID(), c.DefaultModelOptions()...,
)
var rts recordTxStrategy

if tx != nil {
rts = &internalIncomingTx{
Tx: tx,
BroadcastNow: false,
}
} else {
rts = &externalIncomingTx{
Hex: txHex,
BroadcastNow: false,
}
}

return transaction, err
return rts, nil
}

func waitForRecordTxWriteLock(ctx context.Context, c ClientInterface, key string) func() {
Expand Down
2 changes: 1 addition & 1 deletion sync_tx_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func _processSyncTransaction(ctx context.Context, syncTx *SyncTransaction, trans
return nil
}

transaction.updateChainInfo(txInfo)
transaction.setChainInfo(txInfo)

// Create status message
message := "transaction was found on-chain by " + chainstate.ProviderBroadcastClient
Expand Down
10 changes: 10 additions & 0 deletions tx_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"

"github.com/libsv/go-bt"
"github.com/mrz1836/go-datastore"
)

Expand Down Expand Up @@ -254,3 +255,12 @@ func getTransactionsToCalculateBUMP(ctx context.Context, queryParams *datastore.
}
return txs, nil
}

func getTransactionByHex(ctx context.Context, hex string, opts ...ModelOps) (*Transaction, error) {
btTx, err := bt.NewTxFromString(hex)
if err != nil {
return nil, err
}

return getTransactionByID(ctx, "", btTx.GetTxID(), opts...)
}
2 changes: 1 addition & 1 deletion tx_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func _processTransaction(ctx context.Context, transaction *Transaction) error {
return err
}

transaction.updateChainInfo(txInfo)
transaction.setChainInfo(txInfo)

return transaction.Save(ctx)
}

0 comments on commit c6d3008

Please sign in to comment.