Skip to content

Commit

Permalink
Remove StakeStore.
Browse files Browse the repository at this point in the history
All functionality which previously depended on the StakeStore has been
removed, therefore the StakeStore itself can be removed.

Newly created databases will no longer be initialized with the buckets
used by StakeStore, and a database upgrade which changed the data format
in one of those buckets has been updated so it no longer attempts to
make that change.
  • Loading branch information
jholdstock committed Sep 7, 2024
1 parent 77ec2cb commit bff400e
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 690 deletions.
5 changes: 1 addition & 4 deletions wallet/chainntfns.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func (w *Wallet) processTransactionRecord(ctx context.Context, dbtx walletdb.Rea
func selectOwnedTickets(w *Wallet, dbtx walletdb.ReadTx, tickets []*chainhash.Hash) []*chainhash.Hash {
var owned []*chainhash.Hash
for _, ticketHash := range tickets {
if w.txStore.OwnTicket(dbtx, ticketHash) || w.stakeMgr.OwnTicket(ticketHash) {
if w.txStore.OwnTicket(dbtx, ticketHash) {
owned = append(owned, ticketHash)
}
}
Expand Down Expand Up @@ -685,9 +685,6 @@ func (w *Wallet) VoteOnOwnedTickets(ctx context.Context, winningTicketHashes []*

for i, ticketHash := range ticketHashes {
ticketPurchase, err := w.txStore.Tx(txmgrNs, ticketHash)
if err != nil && errors.Is(err, errors.NotExist) {
ticketPurchase, err = w.stakeMgr.TicketPurchase(dbtx, ticketHash)
}
if err != nil {
log.Errorf("Failed to read ticket purchase transaction for "+
"owned winning ticket %v: %v", ticketHash, err)
Expand Down
78 changes: 1 addition & 77 deletions wallet/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,12 @@ import (
"decred.org/dcrwallet/v5/errors"
"decred.org/dcrwallet/v5/wallet/walletdb"
"github.com/decred/dcrd/chaincfg/chainhash"
dcrdtypes "github.com/decred/dcrd/rpc/jsonrpc/types/v4"
"github.com/jrick/bitset"
"golang.org/x/sync/errgroup"
)

// LiveTicketQuerier defines the functions required of a (trusted) network
// backend that provides information about the live ticket pool.
type LiveTicketQuerier interface {
// May be removed if/when there is a header commitment to the utxo
// set.
GetTxOut(ctx context.Context, txHash *chainhash.Hash, index uint32, tree int8, includeMempool bool) (*dcrdtypes.GetTxOutResult, error)

// GetConfirmationHeight relies on the transaction index being enabled
// on the backing dcrd node.
GetConfirmationHeight(ctx context.Context, txHash *chainhash.Hash) (int32, error)

// ExistsLiveTickets relies on the node having the entire live ticket
// pool available. May be removed if/when there is a header commitment
// to the live ticket pool.
Expand All @@ -41,24 +31,9 @@ func (w *Wallet) LiveTicketHashes(ctx context.Context, rpc LiveTicketQuerier, in
var ticketHashes []chainhash.Hash
var maybeLive []*chainhash.Hash

extraTickets := w.stakeMgr.DumpSStxHashes()

var tipHeight int32 // Assigned in view below.

err := walletdb.View(ctx, w.db, func(dbtx walletdb.ReadTx) error {
txmgrNs := dbtx.ReadBucket(wtxmgrNamespaceKey)

// Remove tickets from the extraTickets slice if they will appear in the
// ticket iteration below.
hashes := extraTickets
extraTickets = hashes[:0]
for i := range hashes {
h := &hashes[i]
if !w.txStore.ExistsTx(txmgrNs, h) {
extraTickets = append(extraTickets, *h)
}
}

_, tipHeight = w.txStore.MainChainTip(dbtx)

it := w.txStore.IterateTickets(dbtx)
Expand Down Expand Up @@ -92,62 +67,11 @@ func (w *Wallet) LiveTicketHashes(ctx context.Context, rpc LiveTicketQuerier, in
return nil, errors.E(op, err)
}

// SPV wallet can't evaluate extraTickets.
// SPV wallet can't evaluate possibly live tickets.
if rpc == nil {
return ticketHashes, nil
}

// Determine if the extra tickets are immature or possibly live. Because
// these transactions are not part of the wallet's transaction history, dcrd
// must be queried for their blockchain height. This functionality requires
// the dcrd transaction index to be enabled.
var g errgroup.Group
type extraTicketResult struct {
valid bool // unspent with known height
height int32
}
extraTicketResults := make([]extraTicketResult, len(extraTickets))
for i := range extraTickets {
i := i
g.Go(func() error {
// gettxout is used first as an optimization to check that output 0
// of the ticket is unspent.
const index = 0
const tree = 1
txOut, err := rpc.GetTxOut(ctx, &extraTickets[i], index, tree, true)
if err != nil || txOut == nil {
return nil
}
blockHeight, err := rpc.GetConfirmationHeight(ctx, &extraTickets[i])
if err != nil {
return nil
}
extraTicketResults[i] = extraTicketResult{true, blockHeight}
return nil
})
}
err = g.Wait()
if err != nil {
return nil, err
}
for i := range extraTickets {
r := &extraTicketResults[i]
if !r.valid {
continue
}
// Same checks as above in the db view.
if ticketExpired(w.chainParams, r.height, tipHeight) {
continue
}
if !ticketMatured(w.chainParams, r.height, tipHeight) {
if includeImmature {
ticketHashes = append(ticketHashes, extraTickets[i])
}
continue
}
maybeLive = append(maybeLive, &extraTickets[i])
}

// If there are no possibly live tickets to check, ticketHashes contains all
// of the results.
if len(maybeLive) == 0 {
Expand Down
22 changes: 11 additions & 11 deletions wallet/udb/addressmanager_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2014 The btcsuite developers
// Copyright (c) 2015-2019 The Decred developers
// Copyright (c) 2015-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -432,7 +432,7 @@ func testImportScript(tc *testContext, wb walletdb.ReadWriteBucket) {

func TestManagerImports(t *testing.T) {
ctx := context.Background()
db, mgr, _, _, teardown, err := cloneDB(ctx, "imports.kv")
db, mgr, _, teardown, err := cloneDB(ctx, "imports.kv")
defer teardown()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -474,7 +474,7 @@ func TestManagerImports(t *testing.T) {
// with the manager locked.
func TestImportVotingAccount(t *testing.T) {
ctx := context.Background()
db, mgr, _, _, teardown, err := cloneDB(ctx, "import_voting_account.kv")
db, mgr, _, teardown, err := cloneDB(ctx, "import_voting_account.kv")
defer teardown()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestImportVotingAccount(t *testing.T) {
// with the manager locked.
func TestImportAccount(t *testing.T) {
ctx := context.Background()
db, mgr, _, _, teardown, err := cloneDB(ctx, "import_account.kv")
db, mgr, _, teardown, err := cloneDB(ctx, "import_account.kv")
defer teardown()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1073,7 +1073,7 @@ func testEncryptDecrypt(ctx context.Context, tc *testContext) {
func TestManagerEncryptDecrypt(t *testing.T) {
ctx := context.Background()

db, mgr, _, _, teardown, err := cloneDB(ctx, "encrypt_decrypt.kv")
db, mgr, _, teardown, err := cloneDB(ctx, "encrypt_decrypt.kv")
defer teardown()
if err != nil {
t.Fatal(err)
Expand All @@ -1095,7 +1095,7 @@ func TestManagerEncryptDecrypt(t *testing.T) {

func TestChangePassphrase(t *testing.T) {
ctx := context.Background()
db, mgr, _, _, teardown, err := cloneDB(ctx, "change_passphrase.kv")
db, mgr, _, teardown, err := cloneDB(ctx, "change_passphrase.kv")
defer teardown()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1150,7 +1150,7 @@ func testManagerAPI(ctx context.Context, tc *testContext) {
// copy as well as when it is opened from an existing namespace.
func TestManagerWatchingOnly(t *testing.T) {
ctx := context.Background()
db, mgr, _, _, teardown, err := cloneDB(ctx, "mgr_watching_only.kv")
db, mgr, _, teardown, err := cloneDB(ctx, "mgr_watching_only.kv")
defer teardown()
if err != nil {
t.Fatal(err)
Expand All @@ -1167,7 +1167,7 @@ func TestManagerWatchingOnly(t *testing.T) {
})
mgr.Close()

mgr, _, _, err = Open(ctx, db, chaincfg.TestNet3Params(), pubPassphrase)
mgr, _, err = Open(ctx, db, chaincfg.TestNet3Params(), pubPassphrase)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -1195,7 +1195,7 @@ func TestManagerWatchingOnly(t *testing.T) {
})

// Open the watching-only manager and run all the tests again.
mgr, _, _, err = Open(ctx, db, chaincfg.TestNet3Params(), pubPassphrase)
mgr, _, err = Open(ctx, db, chaincfg.TestNet3Params(), pubPassphrase)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -1220,7 +1220,7 @@ func TestManager(t *testing.T) {
}

ctx := context.Background()
db, mgr, _, _, teardown, err := cloneDB(ctx, "mgr_watching_only.kv")
db, mgr, _, teardown, err := cloneDB(ctx, "mgr_watching_only.kv")
defer teardown()
if err != nil {
t.Fatal(err)
Expand All @@ -1238,7 +1238,7 @@ func TestManager(t *testing.T) {

// Open the manager and run all the tests again in open mode which
// avoids reinserting new addresses like the create mode tests do.
mgr, _, _, err = Open(ctx, db, chaincfg.TestNet3Params(), pubPassphrase)
mgr, _, err = Open(ctx, db, chaincfg.TestNet3Params(), pubPassphrase)
if err != nil {
t.Fatalf("Open: unexpected error: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions wallet/udb/cointype_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017 The Decred developers
// Copyright (c) 2017-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -70,7 +70,7 @@ func TestCoinTypeUpgrade(t *testing.T) {
t.Fatal(err)
}

m, _, _, err := Open(ctx, db, params, pubPass)
m, _, err := Open(ctx, db, params, pubPass)
if err != nil {
t.Fatal(err)
}
Expand Down
20 changes: 10 additions & 10 deletions wallet/udb/common_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2014 The btcsuite developers
// Copyright (c) 2015-2017 The Decred developers
// Copyright (c) 2015-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -64,33 +64,33 @@ func createEmptyDB(ctx context.Context) error {
return nil
}

// cloneDB makes a copy of an empty wallet db. It returns a wallet db, store, a
// stake store and a teardown function.
func cloneDB(ctx context.Context, cloneName string) (walletdb.DB, *Manager, *Store, *StakeStore, func(), error) {
// cloneDB makes a copy of an empty wallet db. It returns a wallet db, store,
// and a teardown function.
func cloneDB(ctx context.Context, cloneName string) (walletdb.DB, *Manager, *Store, func(), error) {
file, err := os.ReadFile(emptyDbPath)
if err != nil {
return nil, nil, nil, nil, nil, fmt.Errorf("unexpected error: %v", err)
return nil, nil, nil, nil, fmt.Errorf("unexpected error: %v", err)
}

err = os.WriteFile(cloneName, file, 0644)
if err != nil {
return nil, nil, nil, nil, nil, fmt.Errorf("unexpected error: %v", err)
return nil, nil, nil, nil, fmt.Errorf("unexpected error: %v", err)
}

db, err := walletdb.Open("bdb", cloneName)
if err != nil {
return nil, nil, nil, nil, nil, fmt.Errorf("unexpected error: %v", err)
return nil, nil, nil, nil, fmt.Errorf("unexpected error: %v", err)
}

mgr, txStore, stkStore, err := Open(ctx, db, chaincfg.TestNet3Params(), pubPassphrase)
mgr, txStore, err := Open(ctx, db, chaincfg.TestNet3Params(), pubPassphrase)
if err != nil {
return nil, nil, nil, nil, nil, fmt.Errorf("unexpected error: %v", err)
return nil, nil, nil, nil, fmt.Errorf("unexpected error: %v", err)
}

teardown := func() {
os.Remove(cloneName)
db.Close()
}

return db, mgr, txStore, stkStore, teardown, err
return db, mgr, txStore, teardown, err
}
22 changes: 3 additions & 19 deletions wallet/udb/initialize.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2018 The Decred developers
// Copyright (c) 2017-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -25,12 +25,8 @@ func Initialize(ctx context.Context, db walletdb.DB, params *chaincfg.Params, se
if err != nil {
return errors.E(errors.IO, err)
}
stakemgrNs, err := tx.CreateTopLevelBucket(wstakemgrBucketKey)
if err != nil {
return errors.E(errors.IO, err)
}

// Create the address manager, transaction store, and stake store.
// Create the address manager and transaction store.
err = createAddressManager(addrmgrNs, seed, pubPass, privPass, params)
if err != nil {
return err
Expand All @@ -39,10 +35,6 @@ func Initialize(ctx context.Context, db walletdb.DB, params *chaincfg.Params, se
if err != nil {
return err
}
err = initializeEmpty(stakemgrNs)
if err != nil {
return err
}

// Create the metadata bucket and write the current database version to
// it.
Expand Down Expand Up @@ -71,12 +63,8 @@ func InitializeWatchOnly(ctx context.Context, db walletdb.DB, params *chaincfg.P
if err != nil {
return errors.E(errors.IO, err)
}
stakemgrNs, err := tx.CreateTopLevelBucket(wstakemgrBucketKey)
if err != nil {
return errors.E(errors.IO, err)
}

// Create the address manager, transaction store, and stake store.
// Create the address manager and transaction store.
err = createWatchOnly(addrmgrNs, hdPubKey, pubPass, params)
if err != nil {
return err
Expand All @@ -85,10 +73,6 @@ func InitializeWatchOnly(ctx context.Context, db walletdb.DB, params *chaincfg.P
if err != nil {
return err
}
err = initializeEmpty(stakemgrNs)
if err != nil {
return err
}

// Create the metadata bucket and write the current database version to
// it.
Expand Down
14 changes: 3 additions & 11 deletions wallet/udb/migration.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2018 The Decred developers
// Copyright (c) 2017-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -15,9 +15,8 @@ import (
// Old package namespace bucket keys. These are still used as of the very first
// unified database layout.
var (
waddrmgrBucketKey = []byte("waddrmgr")
wtxmgrBucketKey = []byte("wtxmgr")
wstakemgrBucketKey = []byte("wstakemgr")
waddrmgrBucketKey = []byte("waddrmgr")
wtxmgrBucketKey = []byte("wtxmgr")
)

// NeedsMigration checks whether the database needs to be converted to the
Expand All @@ -39,9 +38,6 @@ func Migrate(ctx context.Context, db walletdb.DB, params *chaincfg.Params) error
return walletdb.Update(ctx, db, func(tx walletdb.ReadWriteTx) error {
addrmgrNs := tx.ReadWriteBucket(waddrmgrBucketKey)
txmgrNs := tx.ReadWriteBucket(wtxmgrBucketKey)
stakemgrNs := tx.ReadWriteBucket(wstakemgrBucketKey)

stakeStoreVersionName := []byte("stakestorever")

// Perform any necessary upgrades for the old address manager.
err := upgradeManager(addrmgrNs)
Expand All @@ -68,10 +64,6 @@ func Migrate(ctx context.Context, db walletdb.DB, params *chaincfg.Params) error
if err != nil {
return errors.E(errors.IO, err)
}
err = stakemgrNs.NestedReadWriteBucket(mainBucketName).Delete(stakeStoreVersionName)
if err != nil {
return errors.E(errors.IO, err)
}
metadataBucket, err := tx.CreateTopLevelBucket(unifiedDBMetadata{}.rootBucketKey())
if err != nil {
return errors.E(errors.IO, err)
Expand Down
Loading

0 comments on commit bff400e

Please sign in to comment.