Skip to content

Commit

Permalink
client/db: Upgrade to version seven.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Nov 26, 2024
1 parent 4c06ce8 commit a50edc8
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions client/db/bolt/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ var upgrades = [...]upgradefunc{
v5Upgrade,
// v5 => v6 splits matches into separate active and archived buckets.
v6Upgrade,
// v6 => v7 sets the status of all refunded matches to order.MatchConfirmed
// status.
v7Upgrade,
}

// DBVersion is the latest version of the database that is understood. Databases
Expand Down Expand Up @@ -366,6 +369,62 @@ func v6Upgrade(dbtx *bbolt.Tx) error {
})
}

func v7Upgrade(dbtx *bbolt.Tx) error {
const oldVersion = 6

if err := ensureVersion(dbtx, oldVersion); err != nil {
return err
}

amb := []byte("matches") // archived matches

var nRefunded int

defer func() {
upgradeLog.Infof("%d inactive refunded matches set to confirmed status", nRefunded)
}()

archivedMatchesBkt := dbtx.Bucket(amb)

return archivedMatchesBkt.ForEach(func(k, _ []byte) error {
archivedMBkt := archivedMatchesBkt.Bucket(k)
if archivedMBkt == nil {
return fmt.Errorf("match %x bucket is not a bucket", k)
}
matchB := getCopy(archivedMBkt, matchKey)
if matchB == nil {
return fmt.Errorf("nil match bytes for %x", k)
}
match, _, err := order.DecodeMatch(matchB)
if err != nil {
return fmt.Errorf("error decoding match %x: %w", k, err)
}
proofB := getCopy(archivedMBkt, proofKey)
if len(proofB) == 0 {
return fmt.Errorf("empty proof")
}
proof, _, err := dexdb.DecodeMatchProof(proofB)
if err != nil {
return fmt.Errorf("error decoding proof: %w", err)
}

// Check if refund.
if len(proof.RefundCoin) == 0 {
return nil
}
nRefunded++

match.Status = order.MatchConfirmed

updatedMatchB := order.EncodeMatch(match)
if err != nil {
return fmt.Errorf("error encoding match %x: %w", k, err)
}

return archivedMBkt.Put(matchKey, updatedMatchB)
})
}

func ensureVersion(tx *bbolt.Tx, ver uint32) error {
dbVersion, err := getVersionTx(tx)
if err != nil {
Expand Down

0 comments on commit a50edc8

Please sign in to comment.