Skip to content

Commit

Permalink
Store each ticket in its own DB bucket.
Browse files Browse the repository at this point in the history
**NOTE: This contains a backwards incompatible database migration, so if you plan to test it, please make a copy of your database first.**

Moves tickets from a single database bucket containing JSON encoded strings, to a bucket for each ticket.

This change is to preemptively deal with scaling issues seen with databases containing tens of thousands of tickets.
  • Loading branch information
jholdstock committed May 19, 2021
1 parent ed274b9 commit 0dc33d7
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 92 deletions.
2 changes: 1 addition & 1 deletion background/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func checkWalletConsistency() {
}
}

func findOldestHeight(tickets []database.Ticket) int64 {
func findOldestHeight(tickets []*database.Ticket) int64 {
var oldestHeight int64
for _, ticket := range tickets {
// skip unconfirmed tickets
Expand Down
10 changes: 10 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ func writeHotBackupFile(db *bolt.DB) error {
return err
}

func int64ToBytes(i int64) []byte {
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(i))
return bytes
}

func bytesToInt64(bytes []byte) int64 {
return int64(binary.LittleEndian.Uint64(bytes))
}

func uint32ToBytes(i uint32) []byte {
bytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bytes, i)
Expand Down
29 changes: 27 additions & 2 deletions database/database_upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,41 @@ const (
// need to keep these, and they take up a lot of space.
removeOldFeeTxVersion = 2

// ticketBucketVersion changes the way tickets are stored. Previously they
// were stored as JSON encoded strings in a single bucket. This upgrade
// moves each ticket into its own bucket and does away with JSON encoding.
ticketBucketVersion = 3

// latestVersion is the latest version of the database that is understood by
// vspd. Databases with recorded versions higher than this will fail to open
// (meaning any upgrades prevent reverting to older software).
latestVersion = removeOldFeeTxVersion
latestVersion = ticketBucketVersion
)

// upgrades maps between old database versions and the upgrade function to
// upgrade the database to the next version.
var upgrades = []func(tx *bolt.DB) error{
initialVersion: removeOldFeeTxUpgrade,
initialVersion: removeOldFeeTxUpgrade,
removeOldFeeTxVersion: ticketBucketUpgrade,
}

// v1Ticket has the json tags required to unmarshal tickets stored in the
// v1 database format.
type v1Ticket struct {
Hash string `json:"hsh"`
PurchaseHeight int64 `json:"phgt"`
CommitmentAddress string `json:"cmtaddr"`
FeeAddressIndex uint32 `json:"faddridx"`
FeeAddress string `json:"faddr"`
FeeAmount int64 `json:"famt"`
FeeExpiration int64 `json:"fexp"`
Confirmed bool `json:"conf"`
VotingWIF string `json:"vwif"`
VoteChoices map[string]string `json:"vchces"`
FeeTxHex string `json:"fhex"`
FeeTxHash string `json:"fhsh"`
FeeTxStatus FeeStatus `json:"fsts"`
Outcome TicketOutcome `json:"otcme"`
}

// Upgrade will update the database to the latest known version.
Expand Down
Loading

0 comments on commit 0dc33d7

Please sign in to comment.