Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return uncofirmed utxos when listing & Support filtering by address #92

Merged
merged 6 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 120 additions & 107 deletions api-spec/protobuf/gen/go/ocean/v1/account.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api-spec/protobuf/ocean/v1/account.proto
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ message ListUtxosResponse{
Utxos spendable_utxos = 1;
// List of currently locked utxos.
Utxos locked_utxos = 2;
// List of unconfirmed utxos.
Utxos unconfirmed_utxos = 3;
}

message DeleteAccountRequest{
Expand Down
27 changes: 18 additions & 9 deletions internal/core/application/account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (as *AccountService) GetBalanceForAccount(
}

func (as *AccountService) ListUtxosForAccount(
ctx context.Context, accountName string,
ctx context.Context, accountName string, scripts [][]byte,
) (*UtxoInfo, error) {
w, err := as.repoManager.WalletRepository().GetWallet(ctx)
if err != nil {
Expand All @@ -172,21 +172,30 @@ func (as *AccountService) ListUtxosForAccount(
return nil, err
}

spendableUtxos, err := as.repoManager.UtxoRepository().GetSpendableUtxosForAccount(
ctx, account.Namespace,
utxos, err := as.repoManager.UtxoRepository().GetAllUtxosForAccount(
ctx, account.Namespace, scripts,
)
if err != nil {
return nil, err
}

lockedUtxos, err := as.repoManager.UtxoRepository().GetLockedUtxosForAccount(
ctx, account.Namespace,
)
if err != nil {
return nil, err
spendableUtxos := make([]*domain.Utxo, 0, len(utxos))
unconfirmedUtxos := make([]*domain.Utxo, 0, len(utxos))
lockedUtxos := make([]*domain.Utxo, 0, len(utxos))

for _, u := range utxos {
if u.IsLocked() {
lockedUtxos = append(lockedUtxos, u)
} else {
if u.IsConfirmed() {
spendableUtxos = append(spendableUtxos, u)
} else {
unconfirmedUtxos = append(unconfirmedUtxos, u)
}
}
}

return &UtxoInfo{spendableUtxos, lockedUtxos}, nil
return &UtxoInfo{spendableUtxos, lockedUtxos, unconfirmedUtxos}, nil
}

func (as *AccountService) DeleteAccount(
Expand Down
2 changes: 1 addition & 1 deletion internal/core/application/account_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestAccountService(t *testing.T) {
require.NoError(t, err)
require.GreaterOrEqual(t, len(addresses), 2)

utxos, err := svc.ListUtxosForAccount(ctx, accountName)
utxos, err := svc.ListUtxosForAccount(ctx, accountName, nil)
require.NoError(t, err)
require.NotNil(t, utxos)
require.NotEmpty(t, utxos.Spendable)
Expand Down
5 changes: 3 additions & 2 deletions internal/core/application/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ type BuildInfo struct {
}

type UtxoInfo struct {
Spendable Utxos
Locked Utxos
Spendable Utxos
Locked Utxos
Unconfirmed Utxos
}

type TransactionInfo domain.Transaction
Expand Down
4 changes: 2 additions & 2 deletions internal/core/domain/utxo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type UtxoRepository interface {
// GetSpendableUtxos returns all unlocked utxo UTXOs.
GetSpendableUtxos(ctx context.Context) ([]*Utxo, error)
// GetAllUtxosForAccount returns the list of all utxos for the given
// account.
GetAllUtxosForAccount(ctx context.Context, account string) ([]*Utxo, error)
// account. Can be filtered by output scripts.
GetAllUtxosForAccount(ctx context.Context, account string, scripts [][]byte) ([]*Utxo, error)
// GetSpendableUtxosForAccount returns the list of spendable utxos for the
// given account. The list incldues only confirmed and unlocked utxos.
GetSpendableUtxosForAccount(ctx context.Context, account string) ([]*Utxo, error)
Expand Down
25 changes: 22 additions & 3 deletions internal/infrastructure/storage/db/badger/utxo_repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dbbadger

import (
"bytes"
"context"
"fmt"
"sync"
Expand Down Expand Up @@ -75,11 +76,29 @@ func (r *utxoRepository) GetSpendableUtxos(
}

func (r *utxoRepository) GetAllUtxosForAccount(
ctx context.Context, accountName string,
ctx context.Context, accountName string, scripts [][]byte,
) ([]*domain.Utxo, error) {
query := badgerhold.Where("AccountName").Eq(accountName)

return r.findUtxos(ctx, query)
utxos, err := r.findUtxos(ctx, query)
if err != nil {
return nil, err
}

if len(scripts) <= 0 {
return utxos, nil
}

filteredUtxos := make([]*domain.Utxo, 0, len(utxos))
for _, u := range utxos {
for _, script := range scripts {
if bytes.Equal(u.Script, script) {
filteredUtxos = append(filteredUtxos, u)
break
}
}
}
return filteredUtxos, nil
}

func (r *utxoRepository) GetSpendableUtxosForAccount(
Expand All @@ -104,7 +123,7 @@ func (r *utxoRepository) GetLockedUtxosForAccount(
func (r *utxoRepository) GetBalanceForAccount(
ctx context.Context, accountName string,
) (map[string]*domain.Balance, error) {
utxos, err := r.GetAllUtxosForAccount(ctx, accountName)
utxos, err := r.GetAllUtxosForAccount(ctx, accountName, nil)
if err != nil {
return nil, err
}
Expand Down
32 changes: 25 additions & 7 deletions internal/infrastructure/storage/db/inmemory/utxo_repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inmemory

import (
"bytes"
"context"
"sync"

Expand Down Expand Up @@ -79,12 +80,12 @@ func (r *utxoRepository) GetSpendableUtxos(_ context.Context) ([]*domain.Utxo, e
}

func (r *utxoRepository) GetAllUtxosForAccount(
_ context.Context, account string,
_ context.Context, account string, scripts [][]byte,
) ([]*domain.Utxo, error) {
r.store.lock.RLock()
defer r.store.lock.RUnlock()

return r.getUtxosForAccount(account, false, false)
return r.getUtxosForAccount(account, false, false, scripts)
}

func (r *utxoRepository) GetSpendableUtxosForAccount(
Expand All @@ -93,7 +94,7 @@ func (r *utxoRepository) GetSpendableUtxosForAccount(
r.store.lock.RLock()
defer r.store.lock.RUnlock()

return r.getUtxosForAccount(account, true, false)
return r.getUtxosForAccount(account, true, false, nil)
}

func (r *utxoRepository) GetLockedUtxosForAccount(
Expand All @@ -102,7 +103,7 @@ func (r *utxoRepository) GetLockedUtxosForAccount(
r.store.lock.RLock()
defer r.store.lock.RUnlock()

return r.getUtxosForAccount(account, false, true)
return r.getUtxosForAccount(account, false, true, nil)
}

func (r *utxoRepository) GetBalanceForAccount(
Expand All @@ -111,7 +112,7 @@ func (r *utxoRepository) GetBalanceForAccount(
r.store.lock.RLock()
defer r.store.lock.RUnlock()

utxos, _ := r.getUtxosForAccount(account, false, false)
utxos, _ := r.getUtxosForAccount(account, false, false, nil)
balance := make(map[string]*domain.Balance)
for _, u := range utxos {
if u.IsSpent() {
Expand Down Expand Up @@ -239,7 +240,7 @@ func (r *utxoRepository) getUtxos(spendableOnly bool) []*domain.Utxo {
}

func (r *utxoRepository) getUtxosForAccount(
account string, spendableOnly, lockedOnly bool,
account string, spendableOnly, lockedOnly bool, scripts [][]byte,
) ([]*domain.Utxo, error) {
keys := r.store.utxosByAccount[account]
if len(keys) == 0 {
Expand All @@ -266,7 +267,24 @@ func (r *utxoRepository) getUtxosForAccount(
utxos = append(utxos, u)
}

return utxos, nil
if len(scripts) <= 0 {
return utxos, nil
}

filteredUtxos := make([]*domain.Utxo, 0, len(utxos))
for _, u := range utxos {
found := false
for _, script := range scripts {
if bytes.Equal(u.Script, script) {
found = true
altafan marked this conversation as resolved.
Show resolved Hide resolved
break
}
}
if found {
filteredUtxos = append(filteredUtxos, u)
}
}
return filteredUtxos, nil
}

func (r *utxoRepository) spendUtxos(
Expand Down
14 changes: 12 additions & 2 deletions internal/infrastructure/storage/db/postgres/utxo_repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postgresdb

import (
"bytes"
"context"
"database/sql"
"sync"
Expand Down Expand Up @@ -238,7 +239,7 @@ func (u *utxoRepositoryPg) GetSpendableUtxos(
}

func (u *utxoRepositoryPg) GetAllUtxosForAccount(
ctx context.Context, account string,
ctx context.Context, account string, scripts [][]byte,
) ([]*domain.Utxo, error) {
resp := make([]*domain.Utxo, 0)
utxos, err := u.querier.GetUtxosForAccount(ctx, account)
Expand All @@ -261,7 +262,16 @@ func (u *utxoRepositoryPg) GetAllUtxosForAccount(
}

for _, v := range utxosByKey {
resp = append(resp, v)
found := len(scripts) <= 0
for _, script := range scripts {
if bytes.Equal(v.Script, script) {
found = true
break
}
}
if found {
resp = append(resp, v)
}
}

return resp, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ func testAddAndGetUtxos(t *testing.T, repo domain.UtxoRepository) {
require.NoError(t, err)
require.Len(t, utxos, len(newUtxos))

utxos, err = repo.GetAllUtxosForAccount(ctx, accountName)
utxos, err = repo.GetAllUtxosForAccount(ctx, accountName, nil)
require.NoError(t, err)
require.Len(t, utxos, len(newUtxos))

utxos, err = repo.GetAllUtxosForAccount(ctx, wrongAccountName)
utxos, err = repo.GetAllUtxosForAccount(ctx, wrongAccountName, nil)
require.NoError(t, err)
require.Empty(t, utxos)

Expand Down
17 changes: 16 additions & 1 deletion internal/interfaces/grpc/handler/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/vulpemventures/go-elements/address"
pb "github.com/vulpemventures/ocean/api-spec/protobuf/gen/go/ocean/v1"
"github.com/vulpemventures/ocean/internal/core/application"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -173,13 +174,23 @@ func (a *account) ListUtxos(
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
addresses := req.GetAddresses()
scripts := make([][]byte, 0, len(addresses))
for _, addr := range addresses {
script, err := address.ToOutputScript(addr)
if err != nil {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid address %s", addr))
}
scripts = append(scripts, script)
}

utxosInfo, err := a.appSvc.ListUtxosForAccount(ctx, name)
utxosInfo, err := a.appSvc.ListUtxosForAccount(ctx, name, scripts)
if err != nil {
return nil, err
}
spendableUtxos := parseUtxos(utxosInfo.Spendable.Info())
lockedUtxos := parseUtxos(utxosInfo.Locked.Info())
unconfirmedUtxos := parseUtxos(utxosInfo.Unconfirmed.Info())
return &pb.ListUtxosResponse{
SpendableUtxos: &pb.Utxos{
AccountName: name,
Expand All @@ -189,6 +200,10 @@ func (a *account) ListUtxos(
AccountName: name,
Utxos: lockedUtxos,
},
UnconfirmedUtxos: &pb.Utxos{
AccountName: name,
Utxos: unconfirmedUtxos,
},
}, nil
}

Expand Down
Loading