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

SDK - Tx Storage & Ark SDK func update #320

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7b409f5
empty config check & version flag support
sekulicd Sep 14, 2024
f787e19
fix: empty config check & version flag support (#309)
sekulicd Sep 14, 2024
53618f0
fix
sekulicd Sep 14, 2024
e78e35f
merge
sekulicd Sep 14, 2024
fb7b33f
Merge pull request #310 from sekulicd/CLI-fix-config
sekulicd Sep 14, 2024
b20227e
Merge remote-tracking branch 'upstream/master'
sekulicd Sep 16, 2024
1f1a62f
Merge remote-tracking branch 'upstream/master'
sekulicd Sep 17, 2024
5acf6ed
test vtxosToTxsCovenant
sekulicd Sep 17, 2024
ab21a33
Implement background VTXO syncing and database insertion
sekulicd Sep 19, 2024
120a562
fix
sekulicd Sep 20, 2024
7c87b37
Merge remote-tracking branch 'upstream/master'
sekulicd Sep 20, 2024
c82ddfc
merge with master
sekulicd Sep 20, 2024
e2a7080
Merge remote-tracking branch 'upstream/master'
sekulicd Sep 20, 2024
bde7046
merge
sekulicd Sep 20, 2024
0f3d29b
SDK - replace sqlite with badger, merge appData with config store
sekulicd Sep 23, 2024
e78f5ba
fix
sekulicd Sep 23, 2024
ad2f888
go sync
sekulicd Sep 23, 2024
d43d6d3
Merge remote-tracking branch 'upstream/master'
sekulicd Sep 24, 2024
00b9e1d
Merge remote-tracking branch 'upstream/master' into SDK-Covenant-GetT…
sekulicd Sep 24, 2024
f228858
fix
sekulicd Sep 24, 2024
81b40d3
listen vtxo refactor
sekulicd Sep 25, 2024
f15a807
Merge remote-tracking branch 'upstream/master'
sekulicd Sep 26, 2024
9f37878
arksdk close func, cli transaction cmd
sekulicd Sep 26, 2024
34c2b27
fix
sekulicd Sep 26, 2024
d5bcbf0
fixes
sekulicd Sep 26, 2024
dcb834e
Merge remote-tracking branch 'upstream/master'
sekulicd Sep 26, 2024
0de81d8
merge with master
sekulicd Sep 26, 2024
79d9741
merge and fixes
sekulicd Sep 26, 2024
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
36 changes: 29 additions & 7 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ import (
arksdk "github.com/ark-network/ark/pkg/client-sdk"
"github.com/ark-network/ark/pkg/client-sdk/store"
filestore "github.com/ark-network/ark/pkg/client-sdk/store/file"
sqlitestore "github.com/ark-network/ark/pkg/client-sdk/store/sqlite"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)

const (
DatadirEnvVar = "ARK_WALLET_DATADIR"
sqliteDir = "sqlite"
)

var (
Version string
arkSdkClient arksdk.ArkClient

appDataStoreMigrationPath = os.Getenv("ARK_APP_DATA_STORE_MIGRATION_PATH")
)

func main() {
Expand All @@ -48,6 +52,9 @@ func main() {
networkFlag,
}
app.Before = func(ctx *cli.Context) error {
if appDataStoreMigrationPath == "" {
appDataStoreMigrationPath = "file://../pkg/client-sdk/store/sqlite/migrations"
}
sdk, err := getArkSdkClient(ctx)
if err != nil {
return fmt.Errorf("error initializing ark sdk client: %v", err)
Expand Down Expand Up @@ -222,7 +229,13 @@ func config(ctx *cli.Context) error {
return err
}

cfg := map[string]interface{}{
cfg := map[string]interface{}{}
if cfgData == nil {
fmt.Println("no configuration found, run 'init' command")
return nil
}

cfg = map[string]interface{}{
"asp_url": cfgData.AspUrl,
"asp_pubkey": hex.EncodeToString(cfgData.AspPubkey.SerializeCompressed()),
"wallet_type": cfgData.WalletType,
Expand Down Expand Up @@ -376,10 +389,18 @@ func redeem(ctx *cli.Context) error {
}

func getArkSdkClient(ctx *cli.Context) (arksdk.ArkClient, error) {
cfgStore, err := getConfigStore(ctx.String(datadirFlag.Name))
dataDir := ctx.String(datadirFlag.Name)
cfgStore, err := getConfigStore(dataDir)
if err != nil {
return nil, err
}

dbDir := fmt.Sprintf("%s/%s", dataDir, sqliteDir)
appDataStore, err := sqlitestore.NewAppDataRepository(dbDir, appDataStoreMigrationPath)
if err != nil {
return nil, err
}

cfgData, err := cfgStore.GetData(ctx.Context)
if err != nil {
return nil, err
Expand All @@ -394,22 +415,23 @@ func getArkSdkClient(ctx *cli.Context) (arksdk.ArkClient, error) {

if isBtcChain(net) {
return loadOrCreateClient(
arksdk.LoadCovenantlessClient, arksdk.NewCovenantlessClient, cfgStore,
arksdk.LoadCovenantlessClient, arksdk.NewCovenantlessClient, cfgStore, appDataStore,
)
}
return loadOrCreateClient(
arksdk.LoadCovenantClient, arksdk.NewCovenantClient, cfgStore,
arksdk.LoadCovenantClient, arksdk.NewCovenantClient, cfgStore, appDataStore,
)
}

func loadOrCreateClient(
loadFunc, newFunc func(store.ConfigStore) (arksdk.ArkClient, error),
loadFunc, newFunc func(store.ConfigStore, store.AppDataStore) (arksdk.ArkClient, error),
store store.ConfigStore,
appDataStore store.AppDataStore,
) (arksdk.ArkClient, error) {
client, err := loadFunc(store)
client, err := loadFunc(store, appDataStore)
if err != nil {
if errors.Is(err, arksdk.ErrNotInitialized) {
return newFunc(store)
return newFunc(store, appDataStore)
}
return nil, err
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/client-sdk/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,33 @@ build-wasm:
@mkdir -p $(BUILD_DIR)
@echo "Version: $(VERSION)"
@GOOS=js GOARCH=wasm GO111MODULE=on go build -ldflags="-X 'main.Version=$(VERSION)'" -o $(BUILD_DIR)/ark-sdk.wasm $(WASM_DIR)/main.go

### Sqlc

## mig_file: creates pg migration file(eg. make FILE=init mig_file)
mig_file:
@migrate create -ext sql -dir ./store/sqlite/migration/ $(FILE)

## mig_up: creates db schema for provided db path
mig_up:
@echo "creating db schema..."
@migrate -database "sqlite://$(DB_PATH)/sqlite.db" -path ./store/sqlite/migration/ up

## mig_down: apply down migration
mig_down:
@echo "migration down..."
@migrate -database "sqlite://$(DB_PATH)/sqlite.db" -path ./store/sqlite/migration/ down

## mig_down_yes: apply down migration without prompt
mig_down_yes:
@echo "migration down..."
@"yes" | migrate -database "sqlite://path/to/database" -path ./store/sqlite/migration/ down

## vet_db: check if mig_up and mig_down are ok
vet_db: recreatedb mig_up mig_down_yes
@echo "vet db migration scripts..."

## sqlc: gen sql
sqlc:
@echo "gen sql..."
cd ./store/sqlite; sqlc generate
3 changes: 2 additions & 1 deletion pkg/client-sdk/ark_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ type ArkClient interface {
SendAsync(ctx context.Context, withExpiryCoinselect bool, receivers []Receiver) (string, error)
Claim(ctx context.Context) (string, error)
ListVtxos(ctx context.Context) (spendable, spent []client.Vtxo, err error)
GetTransactionHistory(ctx context.Context) ([]Transaction, error)
Dump(ctx context.Context) (seed string, err error)
GetTransactionHistory(ctx context.Context) ([]store.Transaction, error)
GetTransactionEventChannel() chan store.Transaction
}

type Receiver interface {
Expand Down
71 changes: 67 additions & 4 deletions pkg/client-sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
filestore "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey/store/file"
inmemorystore "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey/store/inmemory"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
log "github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -51,12 +52,23 @@ var (
}
)

const (
vtxoSpent spent = true
vtxoUnspent spent = false
)

type spent bool

type arkClient struct {
*store.StoreData
wallet wallet.WalletService
store store.ConfigStore
explorer explorer.Explorer
client client.ASPClient
wallet wallet.WalletService
store store.ConfigStore
appDataStore store.AppDataStore
explorer explorer.Explorer
client client.ASPClient

vtxosChan chan map[spent]client.Vtxo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain why map[spent]client.Vtxo?

vtxoListeningStarted bool
}

func (a *arkClient) GetConfigData(
Expand Down Expand Up @@ -130,6 +142,9 @@ func (a *arkClient) InitWithWallet(
a.explorer = explorerSvc
a.client = clientSvc

a.vtxosChan = make(chan map[spent]client.Vtxo)
a.listenForVtxos(ctx, a.vtxosChan)

return nil
}

Expand Down Expand Up @@ -201,6 +216,9 @@ func (a *arkClient) Init(
a.explorer = explorerSvc
a.client = clientSvc

a.vtxosChan = make(chan map[spent]client.Vtxo)
a.listenForVtxos(ctx, a.vtxosChan)

return nil
}

Expand Down Expand Up @@ -250,6 +268,51 @@ func (a *arkClient) ListVtxos(
return
}

func (a *arkClient) GetTransactionHistory(
ctx context.Context,
) ([]store.Transaction, error) {
return a.appDataStore.TransactionRepository().GetAll(ctx)
}

func (a *arkClient) listenForVtxos(
ctx context.Context,
vtxoChan chan<- map[spent]client.Vtxo,
) {
go func() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
spendableVtxos, spentVtxos, err := a.ListVtxos(ctx)
if err != nil {
log.Warnf("listenForNewVtxos: failed to list vtxos: %s", err)
continue
}

allVtxos := make(map[spent]client.Vtxo)
for _, vtxo := range spendableVtxos {
allVtxos[vtxoUnspent] = vtxo
}
for _, vtxo := range spentVtxos {
allVtxos[vtxoSpent] = vtxo
}

go func() {
if len(allVtxos) > 0 {
vtxoChan <- allVtxos
}
}()
}
}
}()

a.vtxoListeningStarted = true
}

func (a *arkClient) ping(
ctx context.Context, paymentID string,
) func() {
Expand Down
Loading