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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ github.com/coreos/go-etcd v2.0.0+incompatible h1:bXhRBIXoTm9BYHS3gE0TtQuyNZyeEMu
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f h1:JOrtw2xFKzlg+cbHpyrpLDmnN1HqhBfnX7WDiW7eG2c=
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
Expand Down Expand Up @@ -575,6 +576,7 @@ github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KE
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/go-viper/mapstructure/v2 v2.1.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
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
91 changes: 87 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,9 +142,77 @@ 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
}

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

func (a *arkClient) ListVtxos(
ctx context.Context,
) (spendableVtxos, spentVtxos []client.Vtxo, err error) {
offchainAddrs, _, _, err := a.wallet.GetAddresses(ctx)
if err != nil {
return
}

for _, addr := range offchainAddrs {
spendable, spent, err := a.client.ListVtxos(ctx, addr)
if err != nil {
return nil, nil, err
}
spendableVtxos = append(spendableVtxos, spendable...)
spentVtxos = append(spentVtxos, spent...)
}

return
}
Comment on lines +157 to +175
Copy link
Collaborator

Choose a reason for hiding this comment

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

after syncing with master this must be dropped


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) Init(
ctx context.Context, args InitArgs,
) error {
Expand Down Expand Up @@ -201,6 +281,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
Loading