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

Commit

Permalink
Removed unreserveUtxos
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii-4chain committed Jan 31, 2024
1 parent d9a5269 commit a7ec549
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 90 deletions.
8 changes: 0 additions & 8 deletions action_utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,6 @@ func (c *Client) GetUtxoByTransactionID(ctx context.Context, txID string, output
return utxo, nil
}

// UnReserveUtxos remove the reservation on the utxos for the given draft ID
func (c *Client) UnReserveUtxos(ctx context.Context, xPubID, draftID string) error {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "unreserve_uxtos_by_draft_id")

return unReserveUtxos(ctx, xPubID, draftID, c.DefaultModelOptions()...)
}

// should this be optional in the results?
func (c *Client) enrichUtxoTransactions(ctx context.Context, utxos []*Utxo) {
for index, utxo := range utxos {
Expand Down
1 change: 0 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ type UTXOService interface {
conditions *map[string]interface{}, opts ...ModelOps) (int64, error)
GetUtxosByXpubID(ctx context.Context, xPubID string, metadata *Metadata, conditions *map[string]interface{},
queryParams *datastore.QueryParams) ([]*Utxo, error)
UnReserveUtxos(ctx context.Context, xPubID, draftID string) error
}

// XPubService is the xPub actions
Expand Down
8 changes: 0 additions & 8 deletions model_draft_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,6 @@ func (m *DraftTransaction) Save(ctx context.Context) (err error) {
m.Client().Logger().Error().
Str("draftTxID", m.GetID()).
Msgf("save tx error: %s", err.Error())

// todo: run in a go routine?
// un-reserve the utxos
if utxoErr := unReserveUtxos(
ctx, m.XpubID, m.ID, m.GetOptions(false)...,
); utxoErr != nil {
err = errors.Wrap(err, utxoErr.Error())
}
}
return
}
Expand Down
38 changes: 0 additions & 38 deletions model_utxos.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,39 +110,6 @@ func getSpendableUtxos(ctx context.Context, xPubID, utxoType string, queryParams
return utxos, nil
}

// unReserveUtxos remove the reservation on the utxos for the given draft ID
func unReserveUtxos(ctx context.Context, xPubID, draftID string, opts ...ModelOps) error {
var models []Utxo
conditions := map[string]interface{}{
xPubIDField: xPubID,
draftIDField: draftID,
}

// Get the records
if err := getModels(
ctx, NewBaseModel(ModelNameEmpty, opts...).Client().Datastore(),
&models, conditions, nil, defaultDatabaseReadTimeout,
); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil
}
return err
}

// Loop and un-reserve
for index := range models {
utxo := models[index]
utxo.enrich(ModelUtxo, opts...)
utxo.DraftID.Valid = false
utxo.ReservedAt.Valid = false
if err := utxo.Save(ctx); err != nil {
return err
}
}

return nil
}

// reserveUtxos reserve utxos for the given draft ID and amount
func reserveUtxos(ctx context.Context, xPubID, draftID string,
satoshis uint64, feePerByte float64, fromUtxos []*UtxoPointer, opts ...ModelOps,
Expand Down Expand Up @@ -225,11 +192,6 @@ reserveUtxoLoop:
}

if reservedSatoshis < satoshis {
if err = unReserveUtxos(
ctx, xPubID, draftID, m.GetOptions(false)...,
); err != nil {
return nil, errors.Wrap(err, ErrNotEnoughUtxos.Error())
}
return nil, ErrNotEnoughUtxos
}

Expand Down
35 changes: 0 additions & 35 deletions model_utxos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,38 +153,6 @@ func TestUtxo_GetModelName(t *testing.T) {
assert.Equal(t, ModelUtxo.String(), utxo.GetModelName())
}

// TestUtxo_UnReserveUtxos un-reserve utxos
func TestUtxo_UnReserveUtxos(t *testing.T) {
t.Run("un-reserve 2000", func(t *testing.T) {
ctx, client, deferMe := CreateTestSQLiteClient(t, false, false, withTaskManagerMockup())
defer deferMe()

err := createTestUtxos(ctx, client)
require.NoError(t, err)

var utxos []*Utxo
utxos, err = reserveUtxos(ctx, testXPubID, testDraftID2, 2000, 0.5, nil, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Len(t, utxos, 2)
for _, utxo := range utxos {
assert.True(t, utxo.DraftID.Valid)
assert.True(t, utxo.ReservedAt.Valid)
}

err = unReserveUtxos(ctx, testXPubID, testDraftID2, client.DefaultModelOptions()...)
require.NoError(t, err)
for _, utxo := range utxos {
var u *Utxo
u, err = getUtxo(ctx, utxo.TransactionID, utxo.OutputIndex, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Equal(t, utxo.TransactionID, u.TransactionID)
assert.Equal(t, utxo.OutputIndex, u.OutputIndex)
assert.False(t, u.DraftID.Valid)
assert.False(t, u.ReservedAt.Valid)
}
})
}

// TestUtxo_ReserveUtxos reserve utxos
func TestUtxo_ReserveUtxos(t *testing.T) {
t.Run("reserve 1000", func(t *testing.T) {
Expand Down Expand Up @@ -353,9 +321,6 @@ func TestUtxo_GetSpendableUtxos(t *testing.T) {
require.NoError(t, err)
assert.Len(t, utxos, 2)

err = unReserveUtxos(ctx, testXPubID, testDraftID2, opts...)
require.NoError(t, err)

utxos, err = getSpendableUtxos(ctx, testXPubID, utils.ScriptTypePubKeyHash, nil, nil, opts...)
require.NoError(t, err)
assert.Len(t, utxos, 4)
Expand Down

0 comments on commit a7ec549

Please sign in to comment.