Files
ark/pkg/client-sdk/client/client.go
Dusan Sekulic b1c9261f14 [SDK] Persist tx history, vtxos & Provide transaction feed (#354)
* SDK - GetTransactionsStream add to rest/grpc

no session, pattern or user messages provided

* SDK - AppDataRepository added, store refactor

This patch introduces a significant refactor of the Ark SDK codebase. The primary changes involve:

1. **Domain Package Introduction**: A new `domain` package has been created under `store`, encapsulating core data structures and interfaces such as `ConfigData`, `Transaction`, `SdkRepository`, and various repository interfaces. This enhances modularity and separation of concerns.

2. **Replacement of Store with Domain Objects**: All references to the `store` package's `StoreData` have been replaced with `domain.ConfigData`. This change reflects a shift towards using domain-driven design, where the core business logic is represented by domain objects.

3. **Repository Pattern Implementation**: The code has been refactored to use repositories for accessing and managing data. `ConfigRepository` and `TransactionRepository` interfaces are introduced, providing methods for managing configuration data and transactions, respectively.

4. **New Storage Implementations**: New storage implementations using BadgerDB are introduced for persisting transactions (`transaction_repository.go`). This change indicates a move towards more robust and scalable data storage solutions.

5. **Service Layer Introduction**: A service layer (`service.go`) is introduced, which acts as a facade for the underlying data repositories. This layer abstracts the data access logic and provides a unified interface for interacting with configuration and transaction data.

6. **Code Simplification and Cleanup**: The patch removes redundant code and simplifies the existing codebase by consolidating store-related logic and enhancing the overall structure of the code.

7. **Testing Enhancements**: Test cases are updated to reflect the changes in the data access layer, ensuring that the new repository pattern and domain objects are properly tested.

Overall, this refactor aims to improve the maintainability, scalability, and testability of the codebase by adopting best practices such as domain-driven design, repository pattern, and separation of concerns.

* 'SDK listen for tx stream

This diff represents a substantial refactor and enhancement of the `ark-sdk` client, which involves modifications to multiple files, primarily in the `pkg/client-sdk` and `server/internal/core/application` directories. Here's a summary of the key changes:

1. **Configuration and Initialization**:
   - Refactor to replace file-based configuration store with a domain-based approach.
   - Introduced `ListenTransactionStream` configuration to allow real-time transaction event listening.

2. **Client SDK Enhancements**:
   - The client SDK now supports asynchronous transaction streaming and handling.
   - Introduced methods to get transaction event channels and stop the client gracefully.
   - Expanded the `ArkClient` interface to include these new functionalities.

3. **Transaction Handling**:
   - Added support for listening to and processing transaction events in real-time.
   - Implemented methods to handle different types of transactions, including boarding, redeem, and round transactions.
   - Expanded the `Transaction` struct to include more metadata, such as `BoardingVOut`.

4. **Repository Changes**:
   - Introduced `VtxoRepository` for managing VTXO-related operations.
   - Modified `AppDataRepository` to include `VtxoRepository`.

5. **Example and Test Adjustments**:
   - Updated example code to utilize the new transaction streaming capabilities.
   - Adjusted tests to reflect changes in client initialization and configuration handling.

6. **Dependency Updates**:
   - Updated `go.mod` and `go.sum` to include new dependencies such as `badger` for data storage and potentially updated versions of existing packages.

These changes collectively enhance the functionality of the `ark-sdk` client by enabling real-time transaction handling, improving configuration management, and providing a more robust and scalable client architecture. This update seems to be aimed at improving the efficiency and responsiveness of the client application, likely in response to increased demands for real-time data processing and analysis.'

* merge

* go work sync

* test fix

* Tiny refactor (#2)

* tiny refactor

* renaming

* Renamings

* Fixes (#3)

---------

Co-authored-by: Pietralberto Mazza <18440657+altafan@users.noreply.github.com>
2024-10-17 17:36:48 +02:00

193 lines
4.2 KiB
Go

package client
import (
"context"
"time"
"github.com/ark-network/ark/common/bitcointree"
"github.com/ark-network/ark/common/tree"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
)
const (
GrpcClient = "grpc"
RestClient = "rest"
)
type RoundEvent interface {
isRoundEvent()
}
type ASPClient interface {
GetInfo(ctx context.Context) (*Info, error)
RegisterInputsForNextRound(
ctx context.Context, inputs []Input, ephemeralKey string,
) (string, error)
RegisterOutputsForNextRound(
ctx context.Context, paymentID string, outputs []Output,
) error
SubmitTreeNonces(
ctx context.Context, roundID, cosignerPubkey string, nonces bitcointree.TreeNonces,
) error
SubmitTreeSignatures(
ctx context.Context, roundID, cosignerPubkey string, signatures bitcointree.TreePartialSigs,
) error
SubmitSignedForfeitTxs(
ctx context.Context, signedForfeitTxs []string, signedRoundTx string,
) error
GetEventStream(
ctx context.Context, paymentID string,
) (<-chan RoundEventChannel, func(), error)
Ping(ctx context.Context, paymentID string) (RoundEvent, error)
CreatePayment(
ctx context.Context, inputs []Input, outputs []Output,
) (string, error)
CompletePayment(
ctx context.Context, signedRedeemTx string,
) error
ListVtxos(ctx context.Context, addr string) ([]Vtxo, []Vtxo, error)
GetRound(ctx context.Context, txID string) (*Round, error)
GetRoundByID(ctx context.Context, roundID string) (*Round, error)
Close()
GetTransactionsStream(ctx context.Context) (<-chan TransactionEvent, func(), error)
}
type Info struct {
Pubkey string
RoundLifetime int64
UnilateralExitDelay int64
RoundInterval int64
Network string
Dust uint64
BoardingDescriptorTemplate string
ForfeitAddress string
}
type RoundEventChannel struct {
Event RoundEvent
Err error
}
type Outpoint struct {
Txid string
VOut uint32
}
type Input struct {
Outpoint
Descriptor string
}
type Vtxo struct {
Outpoint
Descriptor string
Amount uint64
RoundTxid string
ExpiresAt *time.Time
RedeemTx string
Pending bool
SpentBy string
}
type Output struct {
Address string // onchain output address
Descriptor string // offchain vtxo descriptor
Amount uint64
}
type RoundStage int
func (s RoundStage) String() string {
switch s {
case RoundStageRegistration:
return "ROUND_STAGE_REGISTRATION"
case RoundStageFinalization:
return "ROUND_STAGE_FINALIZATION"
case RoundStageFinalized:
return "ROUND_STAGE_FINALIZED"
case RoundStageFailed:
return "ROUND_STAGE_FAILED"
default:
return "ROUND_STAGE_UNDEFINED"
}
}
const (
RoundStageUndefined RoundStage = iota
RoundStageRegistration
RoundStageFinalization
RoundStageFinalized
RoundStageFailed
)
type Round struct {
ID string
StartedAt *time.Time
EndedAt *time.Time
Tx string
Tree tree.CongestionTree
ForfeitTxs []string
Connectors []string
Stage RoundStage
}
type RoundFinalizationEvent struct {
ID string
Tx string
Tree tree.CongestionTree
Connectors []string
MinRelayFeeRate chainfee.SatPerKVByte
}
func (e RoundFinalizationEvent) isRoundEvent() {}
type RoundFinalizedEvent struct {
ID string
Txid string
}
func (e RoundFinalizedEvent) isRoundEvent() {}
type RoundFailedEvent struct {
ID string
Reason string
}
func (e RoundFailedEvent) isRoundEvent() {}
type RoundSigningStartedEvent struct {
ID string
UnsignedTree tree.CongestionTree
CosignersPublicKeys []*secp256k1.PublicKey
UnsignedRoundTx string
}
func (e RoundSigningStartedEvent) isRoundEvent() {}
type RoundSigningNoncesGeneratedEvent struct {
ID string
Nonces bitcointree.TreeNonces
}
func (e RoundSigningNoncesGeneratedEvent) isRoundEvent() {}
type TransactionEvent struct {
Round *RoundTransaction
Redeem *RedeemTransaction
Err error
}
type RoundTransaction struct {
Txid string
SpentVtxos []Outpoint
SpendableVtxos []Vtxo
ClaimedBoardingUtxos []Outpoint
}
type RedeemTransaction struct {
Txid string
SpentVtxos []Outpoint
SpendableVtxos []Vtxo
}