Skip to content

Commit

Permalink
api,cmd,sqlite,wallet: add sqlite config and index mode
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Feb 21, 2024
1 parent 182983a commit 59281e3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 15 deletions.
8 changes: 4 additions & 4 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestWallet(t *testing.T) {
}
cm := chain.NewManager(dbstore, tipState)

ws, err := sqlite.OpenDatabase(filepath.Join(t.TempDir(), "wallets.db"), log.Named("sqlite3"))
ws, err := sqlite.OpenDatabase(filepath.Join(t.TempDir(), "wallets.db"), sqlite.WithLogger(log.Named("sqlite3")))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestV2(t *testing.T) {
t.Fatal(err)
}
cm := chain.NewManager(dbstore, tipState)
ws, err := sqlite.OpenDatabase(filepath.Join(t.TempDir(), "wallets.db"), log.Named("sqlite3"))
ws, err := sqlite.OpenDatabase(filepath.Join(t.TempDir(), "wallets.db"), sqlite.WithLogger(log.Named("sqlite3")))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestP2P(t *testing.T) {
}
log1 := logger.Named("one")
cm1 := chain.NewManager(dbstore1, tipState)
store1, err := sqlite.OpenDatabase(filepath.Join(t.TempDir(), "wallets.db"), log1.Named("sqlite3"))
store1, err := sqlite.OpenDatabase(filepath.Join(t.TempDir(), "wallets.db"), sqlite.WithLogger(log1.Named("sqlite3")))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -504,7 +504,7 @@ func TestP2P(t *testing.T) {
}
log2 := logger.Named("two")
cm2 := chain.NewManager(dbstore2, tipState)
store2, err := sqlite.OpenDatabase(filepath.Join(t.TempDir(), "wallets.db"), log2.Named("sqlite3"))
store2, err := sqlite.OpenDatabase(filepath.Join(t.TempDir(), "wallets.db"), sqlite.WithLogger(log2.Named("sqlite3")))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/walletd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func newNode(addr, dir string, chainNetwork string, useUPNP bool, log *zap.Logge
syncerAddr = net.JoinHostPort("127.0.0.1", port)
}

store, err := sqlite.OpenDatabase(filepath.Join(dir, "walletd.sqlite3"), log.Named("sqlite3"))
store, err := sqlite.OpenDatabase(filepath.Join(dir, "walletd.sqlite3"), sqlite.WithLogger(log.Named("sqlite3")))
if err != nil {
return nil, fmt.Errorf("failed to open wallet database: %w", err)
}
Expand Down
22 changes: 22 additions & 0 deletions persist/sqlite/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sqlite

import (
"go.sia.tech/walletd/wallet"
"go.uber.org/zap"
)

// An Option is a functional option for configuring a Store.
type Option func(*Store)

// WithLogger sets the logger used by the Store.
func WithLogger(log *zap.Logger) Option {
return func(s *Store) {
s.log = log
}
}

func WithIndexMode(mode wallet.IndexMode) Option {
return func(s *Store) {
s.indexMode = mode
}
}
6 changes: 3 additions & 3 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func mineV2Block(state consensus.State, txns []types.V2Transaction, minerAddr ty
func TestReorg(t *testing.T) {
log := zaptest.NewLogger(t)
dir := t.TempDir()
db, err := sqlite.OpenDatabase(filepath.Join(dir, "walletd.sqlite3"), log.Named("sqlite3"))
db, err := sqlite.OpenDatabase(filepath.Join(dir, "walletd.sqlite3"), sqlite.WithLogger(log.Named("sqlite3")))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestReorg(t *testing.T) {
func TestEphemeralBalance(t *testing.T) {
log := zaptest.NewLogger(t)
dir := t.TempDir()
db, err := sqlite.OpenDatabase(filepath.Join(dir, "walletd.sqlite3"), log.Named("sqlite3"))
db, err := sqlite.OpenDatabase(filepath.Join(dir, "walletd.sqlite3"), sqlite.WithLogger(log.Named("sqlite3")))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -475,7 +475,7 @@ func TestEphemeralBalance(t *testing.T) {
func TestV2(t *testing.T) {
log := zaptest.NewLogger(t)
dir := t.TempDir()
db, err := sqlite.OpenDatabase(filepath.Join(dir, "walletd.sqlite3"), log.Named("sqlite3"))
db, err := sqlite.OpenDatabase(filepath.Join(dir, "walletd.sqlite3"), sqlite.WithLogger(log.Named("sqlite3")))
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions persist/sqlite/peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestAddPeer(t *testing.T) {
log := zaptest.NewLogger(t)
db, err := OpenDatabase(filepath.Join(t.TempDir(), "test.db"), log)
db, err := OpenDatabase(filepath.Join(t.TempDir(), "test.db"), WithLogger(log))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestAddPeer(t *testing.T) {

func TestBanPeer(t *testing.T) {
log := zaptest.NewLogger(t)
db, err := OpenDatabase(filepath.Join(t.TempDir(), "test.db"), log)
db, err := OpenDatabase(filepath.Join(t.TempDir(), "test.db"), WithLogger(log))
if err != nil {
t.Fatal(err)
}
Expand Down
18 changes: 13 additions & 5 deletions persist/sqlite/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import (
"time"

"go.sia.tech/coreutils/chain"
"go.sia.tech/walletd/wallet"
"go.uber.org/zap"
"lukechampine.com/frand"
)

type (
// A Store is a persistent store that uses a SQL database as its backend.
Store struct {
db *sql.DB
log *zap.Logger
db *sql.DB

log *zap.Logger
indexMode wallet.IndexMode

updates []*chain.ApplyUpdate
}
Expand Down Expand Up @@ -107,14 +110,19 @@ func doTransaction(db *sql.DB, log *zap.Logger, fn func(tx *txn) error) error {

// OpenDatabase creates a new SQLite store and initializes the database. If the
// database does not exist, it is created.
func OpenDatabase(fp string, log *zap.Logger) (*Store, error) {
func OpenDatabase(fp string, opts ...Option) (*Store, error) {
db, err := sql.Open("sqlite3", sqliteFilepath(fp))
if err != nil {
return nil, err
}
store := &Store{
db: db,
log: log,
db: db,

log: zap.NewNop(),
indexMode: wallet.IndexModeDefault,
}
for _, opt := range opts {
opt(store)
}
if err := store.init(); err != nil {
return nil, fmt.Errorf("failed to initialize database: %w", err)
Expand Down
11 changes: 11 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ const (
EventTypeFoundationSubsidy = "foundation subsidy"
)

const (
// IndexModeDefault is the default index mode. It only indexes transactions
// and outputs that are relevant to an existing wallet address.
IndexModeDefault IndexMode = iota + 1
// IndexModeFull indexes all transactions and outputs, regardless of
// relevance to a wallet.
IndexModeFull
)

type (
IndexMode uint8

// Balance is a summary of a siacoin and siafund balance
Balance struct {
Siacoins types.Currency `json:"siacoins"`
Expand Down

0 comments on commit 59281e3

Please sign in to comment.