[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>
This commit is contained in:
Dusan Sekulic
2024-10-17 17:36:48 +02:00
committed by GitHub
parent 03670c9c4b
commit b1c9261f14
44 changed files with 2461 additions and 725 deletions

View File

@@ -17,7 +17,7 @@ import (
"github.com/ark-network/ark/pkg/client-sdk/explorer"
"github.com/ark-network/ark/pkg/client-sdk/internal/utils"
"github.com/ark-network/ark/pkg/client-sdk/redemption"
"github.com/ark-network/ark/pkg/client-sdk/store"
"github.com/ark-network/ark/pkg/client-sdk/types"
"github.com/ark-network/ark/pkg/client-sdk/wallet"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -56,86 +56,199 @@ type covenantArkClient struct {
*arkClient
}
func NewCovenantClient(storeSvc store.ConfigStore) (ArkClient, error) {
data, err := storeSvc.GetData(context.Background())
func NewCovenantClient(sdkStore types.Store) (ArkClient, error) {
cfgData, err := sdkStore.ConfigStore().GetData(context.Background())
if err != nil {
return nil, err
}
if data != nil {
if cfgData != nil {
return nil, ErrAlreadyInitialized
}
return &covenantArkClient{&arkClient{store: storeSvc}}, nil
return &covenantArkClient{
&arkClient{
store: sdkStore,
},
}, nil
}
func LoadCovenantClient(storeSvc store.ConfigStore) (ArkClient, error) {
if storeSvc == nil {
return nil, fmt.Errorf("missin store service")
func LoadCovenantClient(sdkStore types.Store) (ArkClient, error) {
if sdkStore == nil {
return nil, fmt.Errorf("missin sdk repository")
}
data, err := storeSvc.GetData(context.Background())
cfgData, err := sdkStore.ConfigStore().GetData(context.Background())
if err != nil {
return nil, err
}
if data == nil {
if cfgData == nil {
return nil, ErrNotInitialized
}
clientSvc, err := getClient(
supportedClients, data.ClientType, data.AspUrl,
supportedClients, cfgData.ClientType, cfgData.AspUrl,
)
if err != nil {
return nil, fmt.Errorf("failed to setup transport client: %s", err)
}
explorerSvc, err := getExplorer(data.ExplorerURL, data.Network.Name)
explorerSvc, err := getExplorer(cfgData.ExplorerURL, cfgData.Network.Name)
if err != nil {
return nil, fmt.Errorf("failed to setup explorer: %s", err)
}
walletSvc, err := getWallet(storeSvc, data, supportedWallets)
walletSvc, err := getWallet(
sdkStore.ConfigStore(),
cfgData,
supportedWallets,
)
if err != nil {
return nil, fmt.Errorf("faile to setup wallet: %s", err)
}
return &covenantArkClient{
&arkClient{data, walletSvc, storeSvc, explorerSvc, clientSvc},
}, nil
covenantClient := covenantArkClient{
&arkClient{
Config: cfgData,
wallet: walletSvc,
store: sdkStore,
explorer: explorerSvc,
client: clientSvc,
},
}
if cfgData.WithTransactionFeed {
txStreamCtx, txStreamCtxCancel := context.WithCancel(context.Background())
covenantClient.txStreamCtxCancel = txStreamCtxCancel
go covenantClient.listenForTxStream(txStreamCtx)
go covenantClient.listenForBoardingUtxos(txStreamCtx)
}
return &covenantClient, nil
}
func LoadCovenantClientWithWallet(
storeSvc store.ConfigStore, walletSvc wallet.WalletService,
sdkStore types.Store, walletSvc wallet.WalletService,
) (ArkClient, error) {
if storeSvc == nil {
return nil, fmt.Errorf("missin store service")
if sdkStore == nil {
return nil, fmt.Errorf("missin sdk repository")
}
if walletSvc == nil {
return nil, fmt.Errorf("missin wallet service")
}
data, err := storeSvc.GetData(context.Background())
cfgData, err := sdkStore.ConfigStore().GetData(context.Background())
if err != nil {
return nil, err
}
if data == nil {
if cfgData == nil {
return nil, ErrNotInitialized
}
clientSvc, err := getClient(
supportedClients, data.ClientType, data.AspUrl,
supportedClients, cfgData.ClientType, cfgData.AspUrl,
)
if err != nil {
return nil, fmt.Errorf("failed to setup transport client: %s", err)
}
explorerSvc, err := getExplorer(data.ExplorerURL, data.Network.Name)
explorerSvc, err := getExplorer(cfgData.ExplorerURL, cfgData.Network.Name)
if err != nil {
return nil, fmt.Errorf("failed to setup explorer: %s", err)
}
return &covenantArkClient{
&arkClient{data, walletSvc, storeSvc, explorerSvc, clientSvc},
}, nil
covenantClient := covenantArkClient{
&arkClient{
Config: cfgData,
wallet: walletSvc,
store: sdkStore,
explorer: explorerSvc,
client: clientSvc,
},
}
if cfgData.WithTransactionFeed {
txStreamCtx, txStreamCtxCancel := context.WithCancel(context.Background())
covenantClient.txStreamCtxCancel = txStreamCtxCancel
go covenantClient.listenForTxStream(txStreamCtx)
go covenantClient.listenForBoardingUtxos(txStreamCtx)
}
return &covenantClient, nil
}
func (a *covenantArkClient) Init(ctx context.Context, args InitArgs) error {
err := a.arkClient.init(ctx, args)
if err != nil {
return err
}
if args.ListenTransactionStream {
txStreamCtx, txStreamCtxCancel := context.WithCancel(context.Background())
a.txStreamCtxCancel = txStreamCtxCancel
go a.listenForTxStream(txStreamCtx)
go a.listenForBoardingUtxos(txStreamCtx)
}
return nil
}
func (a *covenantArkClient) InitWithWallet(ctx context.Context, args InitWithWalletArgs) error {
err := a.arkClient.initWithWallet(ctx, args)
if err != nil {
return err
}
if a.WithTransactionFeed {
txStreamCtx, txStreamCtxCancel := context.WithCancel(context.Background())
a.txStreamCtxCancel = txStreamCtxCancel
go a.listenForTxStream(txStreamCtx)
go a.listenForBoardingUtxos(txStreamCtx)
}
return nil
}
func (a *covenantArkClient) listenForTxStream(ctx context.Context) {
eventChan, closeFunc, err := a.client.GetTransactionsStream(ctx)
if err != nil {
log.WithError(err).Error("Failed to get transaction stream")
return
}
defer closeFunc()
for {
select {
case event, ok := <-eventChan:
if !ok {
return
}
a.processTransactionEvent(event)
case <-ctx.Done():
return
}
}
}
func (a *covenantArkClient) processTransactionEvent(
event client.TransactionEvent,
) {
// TODO considering current covenant state where all payments happening in round
//and that this is going to change we leave this unimplemented until asnc payments are implemented
//also with current state it is not possible to cover some edge cases like when in a round there
//are multiple boarding inputs + spent vtxo with change in spendable + received in the same round
}
func (a *covenantArkClient) listenForBoardingUtxos(
ctx context.Context,
) {
// TODO considering current covenant state where all payments happening in round
//and that this is going to change we leave this unimplemented until asnc payments are implemented
//also with current state it is not possible to cover some edge cases like when in a round there
//are multiple boarding inputs + spent vtxo with change in spendable + received in the same round
}
func (a *covenantArkClient) ListVtxos(
@@ -549,13 +662,19 @@ func (a *covenantArkClient) Claim(ctx context.Context) (string, error) {
)
}
func (a *covenantArkClient) GetTransactionHistory(ctx context.Context) ([]Transaction, error) {
func (a *covenantArkClient) GetTransactionHistory(
ctx context.Context,
) ([]types.Transaction, error) {
if a.Config.WithTransactionFeed {
return a.store.TransactionStore().GetAllTransactions(ctx)
}
spendableVtxos, spentVtxos, err := a.ListVtxos(ctx)
if err != nil {
return nil, err
}
config, err := a.store.GetData(ctx)
config, err := a.store.ConfigStore().GetData(ctx)
if err != nil {
return nil, err
}
@@ -1183,7 +1302,7 @@ func (a *covenantArkClient) validateCongestionTree(
if !utils.IsOnchainOnly(receivers) {
if err := tree.ValidateCongestionTree(
event.Tree, poolTx, a.StoreData.AspPubkey, a.RoundLifetime,
event.Tree, poolTx, a.Config.AspPubkey, a.RoundLifetime,
); err != nil {
return err
}
@@ -1666,7 +1785,9 @@ func (a *covenantArkClient) offchainAddressToDefaultVtxoDescriptor(addr string)
return vtxoScript.ToDescriptor(), nil
}
func (a *covenantArkClient) getBoardingTxs(ctx context.Context) (transactions []Transaction) {
func (a *covenantArkClient) getBoardingTxs(
ctx context.Context,
) (transactions []types.Transaction) {
utxos, err := a.getClaimableBoardingUtxos(ctx)
if err != nil {
return nil
@@ -1683,21 +1804,29 @@ func (a *covenantArkClient) getBoardingTxs(ctx context.Context) (transactions []
}
for _, u := range allUtxos {
transactions = append(transactions, Transaction{
BoardingTxid: u.Txid,
Amount: u.Amount,
Type: TxReceived,
CreatedAt: u.CreatedAt,
pending := false
if isPending[u.Txid] {
pending = true
}
transactions = append(transactions, types.Transaction{
TransactionKey: types.TransactionKey{
BoardingTxid: u.Txid,
},
Amount: u.Amount,
Type: types.TxReceived,
CreatedAt: u.CreatedAt,
IsPending: pending,
})
}
return
}
func vtxosToTxsCovenant(
roundLifetime int64, spendable, spent []client.Vtxo, boardingTxs []Transaction,
) ([]Transaction, error) {
transactions := make([]Transaction, 0)
unconfirmedBoardingTxs := make([]Transaction, 0)
roundLifetime int64, spendable, spent []client.Vtxo, boardingTxs []types.Transaction,
) ([]types.Transaction, error) {
transactions := make([]types.Transaction, 0)
unconfirmedBoardingTxs := make([]types.Transaction, 0)
for _, tx := range boardingTxs {
emptyTime := time.Time{}
if tx.CreatedAt == emptyTime {
@@ -1722,9 +1851,9 @@ func vtxosToTxsCovenant(
}
}
// what kind of tx was this? send or receive?
txType := TxReceived
txType := types.TxReceived
if amount < 0 {
txType = TxSent
txType = types.TxSent
}
// get redeem txid
redeemTxid := ""
@@ -1736,12 +1865,14 @@ func vtxosToTxsCovenant(
redeemTxid = txid
}
// add transaction
transactions = append(transactions, Transaction{
RoundTxid: v.RoundTxid,
RedeemTxid: redeemTxid,
Amount: uint64(math.Abs(float64(amount))),
Type: txType,
CreatedAt: getCreatedAtFromExpiry(roundLifetime, *v.ExpiresAt),
transactions = append(transactions, types.Transaction{
TransactionKey: types.TransactionKey{
RoundTxid: v.RoundTxid,
RedeemTxid: redeemTxid,
},
Amount: uint64(math.Abs(float64(amount))),
Type: txType,
CreatedAt: getCreatedAtFromExpiry(roundLifetime, *v.ExpiresAt),
})
}