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

Commit

Permalink
chore(BUX-358): refactorize tx and incoming tx ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain authored and wregulski committed Dec 8, 2023
1 parent c400c1a commit b1173ce
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 124 deletions.
5 changes: 4 additions & 1 deletion action_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,14 @@ func initRevertTransactionData(t *testing.T) (context.Context, ClientInterface,
assert.NotEmpty(t, hex)

newOpts := client.DefaultModelOptions(WithXPub(testXPub), New())
transaction := newTransactionWithDraftID(
transaction, err := newTransactionWithDraftID(
hex, draftTransaction.ID, newOpts...,
)
require.NoError(t, err)

transaction.draftTransaction = draftTransaction
_hydrateOutgoingWithSync(transaction)

err = transaction.processUtxos(ctx)
require.NoError(t, err)

Expand Down
12 changes: 9 additions & 3 deletions beef_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,23 @@ func createProcessedTx(ctx context.Context, t *testing.T, client ClientInterface
append(client.DefaultModelOptions(), New())...,
)

transaction := newTransaction(testCase.hexForProcessedTx, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testCase.hexForProcessedTx, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

transaction.draftTransaction = draftTx
transaction.DraftID = draftTx.ID

assert.NotEmpty(t, transaction)
require.NotEmpty(t, transaction)

return transaction
}

func addAncestor(ctx context.Context, testCase *beefTestCaseAncestor, client ClientInterface, store *MockTransactionStore, t *testing.T) *Transaction {
ancestor := newTransaction(testCase.hex, append(client.DefaultModelOptions(), New())...)
ancestor, err := txFromHex(testCase.hex, append(client.DefaultModelOptions(), New())...)
if err != nil {
ancestor = emptyTx(append(client.DefaultModelOptions(), New())...)
ancestor.Hex = testCase.hex
}

if testCase.isMined {
ancestor.BlockHeight = uint64(testCase.blockHeight)
Expand Down
44 changes: 33 additions & 11 deletions model_draft_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ func TestDraftTransaction_createTransaction(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -321,7 +323,9 @@ func TestDraftTransaction_createTransaction(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -361,7 +365,9 @@ func TestDraftTransaction_createTransaction(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -420,7 +426,9 @@ func TestDraftTransaction_createTransaction(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -477,7 +485,9 @@ func TestDraftTransaction_createTransaction(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -540,7 +550,9 @@ func TestDraftTransaction_createTransaction(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -588,7 +600,9 @@ func TestDraftTransaction_createTransaction(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -648,7 +662,9 @@ func TestDraftTransaction_createTransaction(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -821,7 +837,9 @@ func TestDraftTransaction_createTransaction(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -1472,7 +1490,9 @@ func TestDraftTransaction_SignInputs(t *testing.T) {
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.Save(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -1558,7 +1578,9 @@ func initSimpleTestCase(t *testing.T) (context.Context, ClientInterface, func())
err = utxo.Save(ctx)
require.NoError(t, err)

transaction := newTransaction(testTxHex, append(client.DefaultModelOptions(), New())...)
transaction, err := txFromHex(testTxHex, append(client.DefaultModelOptions(), New())...)
require.NoError(t, err)

err = transaction.processUtxos(ctx)
require.NoError(t, err)

Expand Down
40 changes: 24 additions & 16 deletions model_incoming_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,35 @@ type IncomingTransaction struct {
StatusMessage string `json:"status_message" toml:"status_message" yaml:"status_message" gorm:"<-;type:varchar(512);comment:This is the status message or error" bson:"status_message"`
}

// newIncomingTransaction will start a new model
func newIncomingTransaction(hex string, opts ...ModelOps) (tx *IncomingTransaction) {
// Create the model
tx = &IncomingTransaction{
Model: *NewBaseModel(ModelIncomingTransaction, opts...),
TransactionBase: TransactionBase{
Hex: hex,
},
Status: SyncStatusReady,
func emptyIncomingTx(opts ...ModelOps) *IncomingTransaction {
return &IncomingTransaction{
Model: *NewBaseModel(ModelIncomingTransaction, opts...),
TransactionBase: TransactionBase{},
Status: SyncStatusReady,
}
}

// Attempt to parse
if len(hex) > 0 {
tx.parsedTx, _ = bt.NewTxFromString(hex)
tx.ID = tx.parsedTx.TxID()
// newIncomingTransaction will start a new model
func newIncomingTransaction(hex string, opts ...ModelOps) (*IncomingTransaction, error) {
var btTx *bt.Tx
var err error

if btTx, err = bt.NewTxFromString(hex); err != nil {
return nil, err
}

return
tx := emptyIncomingTx(opts...)
tx.ID = btTx.TxID()
tx.Hex = hex
tx.parsedTx = btTx

return tx, nil
}

// getIncomingTransactionByID will get the incoming transactions to process
func getIncomingTransactionByID(ctx context.Context, id string, opts ...ModelOps) (*IncomingTransaction, error) {
// Construct an empty tx
tx := newIncomingTransaction("", opts...)
tx := emptyIncomingTx(opts...)
tx.ID = id

// Get the record
Expand Down Expand Up @@ -338,7 +343,10 @@ func processIncomingTransaction(ctx context.Context, logClient zLogger.GormLogge

if transaction == nil {
// Create the new transaction model
transaction = newTransactionFromIncomingTransaction(incomingTx)
if transaction, err = newTransactionFromIncomingTransaction(incomingTx); err != nil {
logClient.Error(ctx, fmt.Sprintf("processIncomingTransaction(): newTransactionFromIncomingTransaction() for %s failed. Reason: %s", incomingTx.ID, err))
return err
}

if err = transaction.processUtxos(ctx); err != nil {
logClient.Error(ctx, fmt.Sprintf("processIncomingTransaction(): processUtxos() for %s failed. Reason: %s", incomingTx.ID, err))
Expand Down
2 changes: 1 addition & 1 deletion model_incoming_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestIncomingTransaction_GetModelName(t *testing.T) {
t.Parallel()

bTx := newIncomingTransaction(testTxHex, New())
bTx, _ := newIncomingTransaction(testTxHex, New())
assert.Equal(t, ModelIncomingTransaction.String(), bTx.GetModelName())
}

Expand Down
12 changes: 9 additions & 3 deletions model_sync_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ func Test_areParentsBroadcast(t *testing.T) {

opts := []ModelOps{WithClient(client)}

tx := newTransaction(testTxHex, append(opts, New())...)
tx, err := txFromHex(testTxHex, append(opts, New())...)
require.NoError(t, err)

txErr := tx.Save(ctx)
require.NoError(t, txErr)

tx2 := newTransaction(testTx2Hex, append(opts, New())...)
tx2, err := txFromHex(testTx2Hex, append(opts, New())...)
require.NoError(t, err)

txErr = tx2.Save(ctx)
require.NoError(t, txErr)

tx3 := newTransaction(testTx3Hex, append(opts, New())...)
tx3, err := txFromHex(testTx3Hex, append(opts, New())...)
require.NoError(t, err)

txErr = tx3.Save(ctx)
require.NoError(t, txErr)

Expand Down
68 changes: 42 additions & 26 deletions model_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,58 +75,74 @@ type TransactionGetter interface {
GetTransactionsByIDs(ctx context.Context, txIDs []string) ([]*Transaction, error)
}

// newTransactionBase creates the standard transaction model base
func newTransactionBase(hex string, opts ...ModelOps) *Transaction {
func emptyTx(opts ...ModelOps) *Transaction {
return &Transaction{
TransactionBase: TransactionBase{
Hex: hex,
},
TransactionBase: TransactionBase{},
Model: *NewBaseModel(ModelTransaction, opts...),
Status: statusComplete,
transactionService: transactionService{},
XpubOutputValue: map[string]int64{},
}
}

// newTransaction will start a new transaction model
func newTransaction(txHex string, opts ...ModelOps) (tx *Transaction) {
tx = newTransactionBase(txHex, opts...)
// baseTxFromHex creates the standard transaction model base
func baseTxFromHex(hex string, opts ...ModelOps) (*Transaction, error) {
var btTx *bt.Tx
var err error

// Set the ID
if len(tx.Hex) > 0 {
_ = tx.setID()
if btTx, err = bt.NewTxFromString(hex); err != nil {
return nil, err
}

tx := emptyTx(opts...)
tx.ID = btTx.TxID()
tx.Hex = hex
tx.parsedTx = btTx

return tx, nil
}

// txFromHex will start a new transaction model
func txFromHex(txHex string, opts ...ModelOps) (*Transaction, error) {

tx, err := baseTxFromHex(txHex, opts...)
if err != nil {
return nil, err
}

// Set xPub ID
tx.setXPubID()

return
return tx, nil
}

// newTransactionWithDraftID will start a new transaction model and set the draft ID
func newTransactionWithDraftID(txHex, draftID string, opts ...ModelOps) (tx *Transaction) {
tx = newTransaction(txHex, opts...)
func newTransactionWithDraftID(txHex, draftID string, opts ...ModelOps) (*Transaction, error) {

tx, err := txFromHex(txHex, opts...)
if err != nil {
return nil, err
}

tx.DraftID = draftID
return

return tx, nil
}

// newTransactionFromIncomingTransaction will start a new transaction model using an incomingTx
func newTransactionFromIncomingTransaction(incomingTx *IncomingTransaction) *Transaction {
func newTransactionFromIncomingTransaction(incomingTx *IncomingTransaction) (*Transaction, error) {
// Create the base
tx := newTransactionBase(incomingTx.Hex, incomingTx.GetOptions(true)...)
tx.TransactionBase.parsedTx = incomingTx.TransactionBase.parsedTx
tx, err := baseTxFromHex(incomingTx.Hex, incomingTx.GetOptions(true)...)

if err != nil {
return nil, err
}

tx.rawXpubKey = incomingTx.rawXpubKey
tx.setXPubID()

// Set the generic metadata (might be ignored if no xPub is used)
tx.Metadata = incomingTx.Metadata

// Set the ID (run the same method)
if len(tx.Hex) > 0 {
_ = tx.setID()
}

return tx
return tx, nil
}

// setXPubID will set the xPub ID on the model
Expand Down
Loading

0 comments on commit b1173ce

Please sign in to comment.