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

Commit

Permalink
feat(BUX-358): create SyncTx for "raw recorded" txs
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain authored and wregulski committed Dec 8, 2023
1 parent b65f0a8 commit d39525c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
60 changes: 22 additions & 38 deletions action_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (c *Client) RecordTransaction(ctx context.Context, xPubKey, txHex, draftID
func (c *Client) RecordRawTransaction(ctx context.Context, txHex string,
opts ...ModelOps,
) (*Transaction, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "record_raw_transaction")

return c.recordTxHex(ctx, txHex, opts...)
Expand All @@ -58,36 +57,9 @@ func (c *Client) RecordRawTransaction(ctx context.Context, txHex string,
func recordMonitoredTransaction(ctx context.Context, client ClientInterface, txHex string,
opts ...ModelOps,
) (*Transaction, error) {
// Check for existing NewRelic transaction
ctx = client.GetOrStartTxn(ctx, "record_monitored_transaction")

transaction, err := client.recordTxHex(ctx, txHex, opts...)
if err != nil {
return nil, err
}

if transaction.BlockHash == "" {
// Create the sync transaction model
sync := newSyncTransaction(
transaction.GetID(),
transaction.Client().DefaultSyncConfig(),
transaction.GetOptions(true)...,
)
sync.BroadcastStatus = SyncStatusSkipped
sync.P2PStatus = SyncStatusSkipped

// Use the same metadata
sync.Metadata = transaction.Metadata

// If all the options are skipped, do not make a new model (ignore the record)
if !sync.isSkipped() {
if err = sync.Save(ctx); err != nil {
return nil, err
}
}
}

return transaction, nil
return client.recordTxHex(ctx, txHex, opts...)
}

func (c *Client) recordTxHex(ctx context.Context, txHex string, opts ...ModelOps) (*Transaction, error) {
Expand All @@ -110,6 +82,16 @@ func (c *Client) recordTxHex(ctx context.Context, txHex string, opts ...ModelOps
return nil, err
}

monitor := c.options.chainstate.Monitor()

if monitor != nil {
// do not register transactions we have nothing to do with
allowUnknown := monitor.AllowUnknownTransactions()
if transaction.XpubInIDs == nil && transaction.XpubOutIDs == nil && !allowUnknown {
return nil, ErrTransactionUnknown
}
}

// Logic moved from BeforeCreating hook - should be refactorized in next iteration

// If we are external and the user disabled incoming transaction checking, check outputs
Expand All @@ -134,22 +116,24 @@ func (c *Client) recordTxHex(ctx context.Context, txHex string, opts ...ModelOps

// /Logic moved from BeforeCreating hook - should be refactorized in next iteration

monitor := c.options.chainstate.Monitor()
if !transaction.isMined() {
// Create the sync transaction model
sync := newSyncTransaction(
transaction.GetID(),
transaction.Client().DefaultSyncConfig(),
transaction.GetOptions(true)...,
)
sync.BroadcastStatus = SyncStatusSkipped
sync.P2PStatus = SyncStatusSkipped

if monitor != nil {
// do not register transactions we have nothing to do with
allowUnknown := monitor.AllowUnknownTransactions()
if transaction.XpubInIDs == nil && transaction.XpubOutIDs == nil && !allowUnknown {
return nil, ErrTransactionUnknown
}
sync.Metadata = transaction.Metadata
transaction.syncTransaction = sync
}

// save the transaction model
if err = transaction.Save(ctx); err != nil {
return nil, err
}

// Return the response
return transaction, nil
}

Expand Down
4 changes: 4 additions & 0 deletions model_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ func (m *Transaction) setBUMP(txInfo *chainstate.TransactionInfo) {
m.BUMP = bump
}

func (m *Transaction) isMined() bool {
return m.BlockHash != ""
}

// IsXpubAssociated will check if this key is associated to this transaction
func (m *Transaction) IsXpubAssociated(rawXpubKey string) bool {
// Hash the raw key
Expand Down

0 comments on commit d39525c

Please sign in to comment.