Skip to content

Commit

Permalink
Merge pull request #112 from SiaFoundation/nate/index-mode
Browse files Browse the repository at this point in the history
Add "full" and "none" indexing modes
  • Loading branch information
n8maninger authored May 8, 2024
2 parents 6123d86 + 7a945d4 commit 35b3095
Show file tree
Hide file tree
Showing 16 changed files with 1,058 additions and 437 deletions.
11 changes: 6 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
// A StateResponse returns information about the current state of the walletd
// daemon.
type StateResponse struct {
Version string `json:"version"`
Commit string `json:"commit"`
OS string `json:"os"`
BuildTime time.Time `json:"buildTime"`
StartTime time.Time `json:"startTime"`
Version string `json:"version"`
Commit string `json:"commit"`
OS string `json:"os"`
BuildTime time.Time `json:"buildTime"`
StartTime time.Time `json:"startTime"`
IndexMode wallet.IndexMode `json:"indexMode"`
}

// A GatewayPeer is a currently-connected peer.
Expand Down
30 changes: 24 additions & 6 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ func TestWalletAdd(t *testing.T) {
}
defer ws.Close()

wm := wallet.NewManager(cm, ws, log.Named("wallet"))
wm, err := wallet.NewManager(cm, ws, wallet.WithLogger(log.Named("wallet")))
if err != nil {
t.Fatal(err)
}
defer wm.Close()

c, shutdown := runServer(cm, nil, wm)
Expand Down Expand Up @@ -273,7 +276,10 @@ func TestWallet(t *testing.T) {
})

// create the wallet manager
wm := wallet.NewManager(cm, ws, log.Named("wallet"))
wm, err := wallet.NewManager(cm, ws, wallet.WithLogger(log.Named("wallet")))
if err != nil {
t.Fatal(err)
}
defer wm.Close()

// create seed address vault
Expand Down Expand Up @@ -492,7 +498,10 @@ func TestAddresses(t *testing.T) {
}
defer ws.Close()

wm := wallet.NewManager(cm, ws, log.Named("wallet"))
wm, err := wallet.NewManager(cm, ws, wallet.WithLogger(log.Named("wallet")))
if err != nil {
t.Fatal(err)
}
defer wm.Close()

sav := wallet.NewSeedAddressVault(wallet.NewSeed(), 0, 20)
Expand Down Expand Up @@ -686,7 +695,10 @@ func TestV2(t *testing.T) {
t.Fatal(err)
}
defer ws.Close()
wm := wallet.NewManager(cm, ws, log.Named("wallet"))
wm, err := wallet.NewManager(cm, ws, wallet.WithLogger(log.Named("wallet")))
if err != nil {
t.Fatal(err)
}
defer wm.Close()

c, shutdown := runServer(cm, nil, wm)
Expand Down Expand Up @@ -909,7 +921,10 @@ func TestP2P(t *testing.T) {
t.Fatal(err)
}

wm1 := wallet.NewManager(cm1, store1, log1.Named("wallet"))
wm1, err := wallet.NewManager(cm1, store1, wallet.WithLogger(log1.Named("wallet")))
if err != nil {
t.Fatal(err)
}
defer wm1.Close()

l1, err := net.Listen("tcp", ":0")
Expand Down Expand Up @@ -949,7 +964,10 @@ func TestP2P(t *testing.T) {
t.Fatal(err)
}
defer store2.Close()
wm2 := wallet.NewManager(cm2, store2, log2.Named("wallet"))
wm2, err := wallet.NewManager(cm2, store2, wallet.WithLogger(log2.Named("wallet")))
if err != nil {
t.Fatal(err)
}
defer wm2.Close()

l2, err := net.Listen("tcp", ":0")
Expand Down
2 changes: 2 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type (

// A WalletManager manages wallets, keyed by name.
WalletManager interface {
IndexMode() wallet.IndexMode
Tip() (types.ChainIndex, error)
Scan(_ context.Context, index types.ChainIndex) error

Expand Down Expand Up @@ -97,6 +98,7 @@ func (s *server) stateHandler(jc jape.Context) {
OS: runtime.GOOS,
BuildTime: build.Time(),
StartTime: s.startTime,
IndexMode: s.wm.IndexMode(),
})
}

Expand Down
16 changes: 14 additions & 2 deletions cmd/walletd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
cwallet "go.sia.tech/coreutils/wallet"
"go.sia.tech/walletd/api"
"go.sia.tech/walletd/build"
"go.sia.tech/walletd/wallet"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/term"
Expand Down Expand Up @@ -68,7 +69,7 @@ Runs a CPU miner. Not intended for production use.
func main() {
log.SetFlags(0)

var gatewayAddr, apiAddr, dir, network, seed string
var gatewayAddr, apiAddr, dir, network, seed, indexModeStr string
var upnp, bootstrap bool

var minerAddrStr string
Expand All @@ -83,6 +84,7 @@ func main() {
rootCmd.BoolVar(&upnp, "upnp", true, "attempt to forward ports and discover IP with UPnP")
rootCmd.BoolVar(&bootstrap, "bootstrap", true, "attempt to bootstrap the network")
rootCmd.StringVar(&seed, "seed", "", "testnet seed")
rootCmd.StringVar(&indexModeStr, "index", "full", "address index mode (full, partial, off)")
versionCmd := flagg.New("version", versionUsage)
seedCmd := flagg.New("seed", seedUsage)
mineCmd := flagg.New("mine", mineUsage)
Expand Down Expand Up @@ -135,7 +137,17 @@ func main() {
// redirect stdlib log to zap
zap.RedirectStdLog(logger.Named("stdlib"))

n, err := newNode(gatewayAddr, dir, network, upnp, bootstrap, logger)
var indexMode wallet.IndexMode
switch indexModeStr {
case "full":
indexMode = wallet.IndexModeFull
case "partial":
indexMode = wallet.IndexModePartial
case "off":
indexMode = wallet.IndexModeNone
}

n, err := newNode(gatewayAddr, dir, network, upnp, bootstrap, indexMode, logger)
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/walletd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (n *node) Close() error {
return n.store.Close()
}

func newNode(addr, dir string, chainNetwork string, useUPNP, useBootstrap bool, log *zap.Logger) (*node, error) {
func newNode(addr, dir string, chainNetwork string, useUPNP, useBootstrap bool, indexMode wallet.IndexMode, log *zap.Logger) (*node, error) {
var network *consensus.Network
var genesisBlock types.Block
var bootstrapPeers []string
Expand Down Expand Up @@ -187,8 +187,10 @@ func newNode(addr, dir string, chainNetwork string, useUPNP, useBootstrap bool,
}

s := syncer.New(l, cm, ps, header, syncer.WithLogger(log.Named("syncer")))
wm := wallet.NewManager(cm, store, log.Named("wallet"))

wm, err := wallet.NewManager(cm, store, wallet.WithLogger(log.Named("wallet")), wallet.WithIndexMode(indexMode))
if err != nil {
return nil, fmt.Errorf("failed to create wallet manager: %w", err)
}
return &node{
chainStore: bdb,
cm: cm,
Expand Down
49 changes: 46 additions & 3 deletions persist/sqlite/addresses.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sqlite

import (
"database/sql"
"errors"
"fmt"

"go.sia.tech/core/types"
Expand All @@ -11,7 +13,12 @@ import (
func (s *Store) AddressBalance(address types.Address) (balance wallet.Balance, err error) {
err = s.transaction(func(tx *txn) error {
const query = `SELECT siacoin_balance, immature_siacoin_balance, siafund_balance FROM sia_addresses WHERE sia_address=$1`
return tx.QueryRow(query, encode(address)).Scan(decode(&balance.Siacoins), decode(&balance.ImmatureSiacoins), &balance.Siafunds)
err := tx.QueryRow(query, encode(address)).Scan(decode(&balance.Siacoins), decode(&balance.ImmatureSiacoins), &balance.Siafunds)
if errors.Is(err, sql.ErrNoRows) {
balance = wallet.Balance{}
return nil
}
return err
})
return
}
Expand Down Expand Up @@ -70,7 +77,25 @@ func (s *Store) AddressSiacoinOutputs(address types.Address, offset, limit int)

siacoins = append(siacoins, siacoin)
}
return rows.Err()
if err := rows.Err(); err != nil {
return err
}

// retrieve the merkle proofs for the siacoin elements
if s.indexMode == wallet.IndexModeFull {
indices := make([]uint64, len(siacoins))
for i, se := range siacoins {
indices[i] = se.LeafIndex
}
proofs, err := fillElementProofs(tx, indices)
if err != nil {
return fmt.Errorf("failed to fill element proofs: %w", err)
}
for i, proof := range proofs {
siacoins[i].MerkleProof = proof
}
}
return nil
})
return
}
Expand All @@ -97,7 +122,25 @@ func (s *Store) AddressSiafundOutputs(address types.Address, offset, limit int)
}
siafunds = append(siafunds, siafund)
}
return rows.Err()
if err := rows.Err(); err != nil {
return err
}

// retrieve the merkle proofs for the siafund elements
if s.indexMode == wallet.IndexModeFull {
indices := make([]uint64, len(siafunds))
for i, se := range siafunds {
indices[i] = se.LeafIndex
}
proofs, err := fillElementProofs(tx, indices)
if err != nil {
return fmt.Errorf("failed to fill element proofs: %w", err)
}
for i, proof := range proofs {
siafunds[i].MerkleProof = proof
}
}
return nil
})
return
}
Loading

0 comments on commit 35b3095

Please sign in to comment.