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

Add support for filtering utxos by addresses #96

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 14 additions & 3 deletions internal/core/application/account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

log "github.com/sirupsen/logrus"
"github.com/vulpemventures/go-elements/address"
"github.com/vulpemventures/ocean/internal/core/domain"
"github.com/vulpemventures/ocean/internal/core/ports"
)
Expand Down Expand Up @@ -160,8 +161,18 @@ func (as *AccountService) GetBalanceForAccount(
}

func (as *AccountService) ListUtxosForAccount(
ctx context.Context, accountName string,
ctx context.Context, accountName string, addresses []string,
) (*UtxoInfo, error) {
scripts := make([][]byte, 0, len(addresses))
if len(addresses) > 0 {
for _, addr := range addresses {
script, err := address.ToOutputScript(addr)
if err != nil {
return nil, fmt.Errorf("invalid address %s", addr)
}
scripts = append(scripts, script)
}
}
w, err := as.repoManager.WalletRepository().GetWallet(ctx)
if err != nil {
return nil, err
Expand All @@ -173,14 +184,14 @@ func (as *AccountService) ListUtxosForAccount(
}

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

lockedUtxos, err := as.repoManager.UtxoRepository().GetLockedUtxosForAccount(
ctx, account.Namespace,
ctx, account.Namespace, scripts...,
)
if err != nil {
return nil, err
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
8 changes: 6 additions & 2 deletions internal/core/domain/utxo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ type UtxoRepository interface {
GetAllUtxosForAccount(ctx context.Context, account string) ([]*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)
GetSpendableUtxosForAccount(
ctx context.Context, account string, addresses ...[]byte,
) ([]*Utxo, error)
// GetLockedUtxosForAccount returns the list of all currently locked utxos
// for the given account.
GetLockedUtxosForAccount(ctx context.Context, account string) ([]*Utxo, error)
GetLockedUtxosForAccount(
ctx context.Context, account string, scripts ...[]byte,
) ([]*Utxo, error)
// GetBalanceForAccount returns the confirmed, unconfirmed and locked
// balances per each asset for the given account.
GetBalanceForAccount(ctx context.Context, account string) (map[string]*Balance, error)
Expand Down
53 changes: 49 additions & 4 deletions internal/infrastructure/storage/db/badger/utxo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbbadger

import (
"context"
"encoding/hex"
"fmt"
"sync"

Expand Down Expand Up @@ -83,22 +84,66 @@ func (r *utxoRepository) GetAllUtxosForAccount(
}

func (r *utxoRepository) GetSpendableUtxosForAccount(
ctx context.Context, accountName string,
ctx context.Context, accountName string, scripts ...[]byte,
) ([]*domain.Utxo, error) {
query := badgerhold.Where("SpentStatus").Eq(domain.UtxoStatus{}).
And("ConfirmedStatus").Ne(domain.UtxoStatus{}).
And("LockTimestamp").Eq(int64(0)).And("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
}

indexedScripts := make(map[string]struct{})
for _, script := range scripts {
indexedScripts[hex.EncodeToString(script)] = struct{}{}
}

filteredUtxos := make([]*domain.Utxo, 0, len(utxos))
for _, u := range utxos {
script := hex.EncodeToString(u.Script)
if _, ok := indexedScripts[script]; ok {
filteredUtxos = append(filteredUtxos, u)
}
}

return filteredUtxos, nil
}

func (r *utxoRepository) GetLockedUtxosForAccount(
ctx context.Context, accountName string,
ctx context.Context, accountName string, scripts ...[]byte,
) ([]*domain.Utxo, error) {
query := badgerhold.Where("SpentStatus").Eq(domain.UtxoStatus{}).
And("LockTimestamp").Gt(int64(0)).And("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
}

indexedScripts := make(map[string]struct{})
for _, script := range scripts {
indexedScripts[hex.EncodeToString(script)] = struct{}{}
}

filteredUtxos := make([]*domain.Utxo, 0, len(utxos))
for _, u := range utxos {
script := hex.EncodeToString(u.Script)
if _, ok := indexedScripts[script]; ok {
filteredUtxos = append(filteredUtxos, u)
}
}

return filteredUtxos, nil
}

func (r *utxoRepository) GetBalanceForAccount(
Expand Down
53 changes: 49 additions & 4 deletions internal/infrastructure/storage/db/inmemory/utxo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inmemory

import (
"context"
"encoding/hex"
"sync"

"github.com/vulpemventures/ocean/internal/core/domain"
Expand Down Expand Up @@ -88,21 +89,65 @@ func (r *utxoRepository) GetAllUtxosForAccount(
}

func (r *utxoRepository) GetSpendableUtxosForAccount(
_ 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, true, false)
utxos, err := r.getUtxosForAccount(account, true, false)
if err != nil {
return nil, err
}

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

indexedScripts := make(map[string]struct{})
for _, script := range scripts {
indexedScripts[hex.EncodeToString(script)] = struct{}{}
}

filteredUtxos := make([]*domain.Utxo, 0, len(utxos))
for _, u := range utxos {
script := hex.EncodeToString(u.Script)
if _, ok := indexedScripts[script]; ok {
filteredUtxos = append(filteredUtxos, u)
}
}

return filteredUtxos, nil
}

func (r *utxoRepository) GetLockedUtxosForAccount(
_ 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, true)
utxos, err := r.getUtxosForAccount(account, true, false)
if err != nil {
return nil, err
}

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

indexedScripts := make(map[string]struct{})
for _, script := range scripts {
indexedScripts[hex.EncodeToString(script)] = struct{}{}
}

filteredUtxos := make([]*domain.Utxo, 0, len(utxos))
for _, u := range utxos {
script := hex.EncodeToString(u.Script)
if _, ok := indexedScripts[script]; ok {
filteredUtxos = append(filteredUtxos, u)
}
}

return filteredUtxos, nil
}

func (r *utxoRepository) GetBalanceForAccount(
Expand Down
81 changes: 51 additions & 30 deletions internal/infrastructure/storage/db/postgres/utxo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package postgresdb
import (
"context"
"database/sql"
"encoding/hex"
"sync"

"github.com/jackc/pgconn"
Expand Down Expand Up @@ -268,67 +269,87 @@ func (u *utxoRepositoryPg) GetAllUtxosForAccount(
}

func (u *utxoRepositoryPg) GetSpendableUtxosForAccount(
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)
utxos := make([]*domain.Utxo, 0)
rows, err := u.querier.GetUtxosForAccount(ctx, account)
if err != nil {
return nil, nil
}

req := make([]queries.GetAllUtxosRow, 0, len(utxos))
for _, v := range utxos {
req = append(
req,
toGetAllUtxosRow(v),
)

query := make([]queries.GetAllUtxosRow, 0, len(rows))
for _, row := range rows {
query = append(query, toGetAllUtxosRow(row))
}

utxosByKey, err := u.convertToUtxos(req)
utxosByKey, err := u.convertToUtxos(query)
if err != nil {
return nil, nil
}

for _, v := range utxosByKey {
if !v.IsLocked() && v.IsConfirmed() && !v.IsSpent() {
resp = append(resp, v)
indexedScripts := make(map[string]struct{})
if len(scripts) > 0 {
for _, script := range scripts {
indexedScripts[hex.EncodeToString(script)] = struct{}{}
}
}

return resp, nil
for _, utxo := range utxosByKey {
if !utxo.IsLocked() && utxo.IsConfirmed() && !utxo.IsSpent() {
if len(indexedScripts) <= 0 {
utxos = append(utxos, utxo)
continue
}

if _, ok := indexedScripts[hex.EncodeToString(utxo.Script)]; ok {
utxos = append(utxos, utxo)
}
}
}

return utxos, nil
}

func (u *utxoRepositoryPg) GetLockedUtxosForAccount(
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)
utxos := make([]*domain.Utxo, 0)
rows, err := u.querier.GetUtxosForAccount(ctx, account)
if err != nil {
return nil, nil
}

req := make([]queries.GetAllUtxosRow, 0, len(utxos))
for _, v := range utxos {
req = append(
req,
toGetAllUtxosRow(v),
)

query := make([]queries.GetAllUtxosRow, 0, len(rows))
for _, v := range rows {
query = append(query, toGetAllUtxosRow(v))
}

utxosByKey, err := u.convertToUtxos(req)
utxosByKey, err := u.convertToUtxos(query)
if err != nil {
return nil, nil
}

for _, v := range utxosByKey {
if v.IsLocked() {
resp = append(resp, v)
indexedScripts := make(map[string]struct{})
if len(scripts) > 0 {
for _, script := range scripts {
indexedScripts[hex.EncodeToString(script)] = struct{}{}
}
}

return resp, nil
for _, utxo := range utxosByKey {
if utxo.IsLocked() {
if len(indexedScripts) <= 0 {
utxos = append(utxos, utxo)
continue
}

if _, ok := indexedScripts[hex.EncodeToString(utxo.Script)]; ok {
utxos = append(utxos, utxo)
}
}
}

return utxos, nil
}

func (u *utxoRepositoryPg) GetBalanceForAccount(
Expand Down
3 changes: 2 additions & 1 deletion internal/interfaces/grpc/handler/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ func (a *account) ListUtxos(
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
addresses := req.GetAddresses()

utxosInfo, err := a.appSvc.ListUtxosForAccount(ctx, name)
utxosInfo, err := a.appSvc.ListUtxosForAccount(ctx, name, addresses)
if err != nil {
return nil, err
}
Expand Down
Loading