Skip to content

Commit 5a4b56a

Browse files
committed
client/db: Upgrade to version seven.
1 parent 4c06ce8 commit 5a4b56a

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

client/db/bolt/upgrades.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ var upgrades = [...]upgradefunc{
4242
v5Upgrade,
4343
// v5 => v6 splits matches into separate active and archived buckets.
4444
v6Upgrade,
45+
// v6 => v7 sets the status of all refunded matches to order.MatchConfirmed
46+
// status.
47+
v7Upgrade,
4548
}
4649

4750
// DBVersion is the latest version of the database that is understood. Databases
@@ -366,6 +369,65 @@ func v6Upgrade(dbtx *bbolt.Tx) error {
366369
})
367370
}
368371

372+
// v7Upgrade sets the status to all archived refund matches to order.MatchConfirmed.
373+
// From now on all refunds must also have confirmations for the trade to be
374+
// considered inactive.
375+
func v7Upgrade(dbtx *bbolt.Tx) error {
376+
const oldVersion = 6
377+
378+
if err := ensureVersion(dbtx, oldVersion); err != nil {
379+
return err
380+
}
381+
382+
amb := []byte("matches") // archived matches
383+
384+
var nRefunded int
385+
386+
defer func() {
387+
upgradeLog.Infof("%d inactive refunded matches set to confirmed status", nRefunded)
388+
}()
389+
390+
archivedMatchesBkt := dbtx.Bucket(amb)
391+
392+
return archivedMatchesBkt.ForEach(func(k, _ []byte) error {
393+
archivedMBkt := archivedMatchesBkt.Bucket(k)
394+
if archivedMBkt == nil {
395+
return fmt.Errorf("match %x bucket is not a bucket", k)
396+
}
397+
matchB := getCopy(archivedMBkt, matchKey)
398+
if matchB == nil {
399+
return fmt.Errorf("nil match bytes for %x", k)
400+
}
401+
match, _, err := order.DecodeMatch(matchB)
402+
if err != nil {
403+
return fmt.Errorf("error decoding match %x: %w", k, err)
404+
}
405+
proofB := getCopy(archivedMBkt, proofKey)
406+
if len(proofB) == 0 {
407+
return fmt.Errorf("empty proof")
408+
}
409+
proof, _, err := dexdb.DecodeMatchProof(proofB)
410+
if err != nil {
411+
return fmt.Errorf("error decoding proof: %w", err)
412+
}
413+
414+
// Check if refund.
415+
if len(proof.RefundCoin) == 0 {
416+
return nil
417+
}
418+
nRefunded++
419+
420+
match.Status = order.MatchConfirmed
421+
422+
updatedMatchB := order.EncodeMatch(match)
423+
if err != nil {
424+
return fmt.Errorf("error encoding match %x: %w", k, err)
425+
}
426+
427+
return archivedMBkt.Put(matchKey, updatedMatchB)
428+
})
429+
}
430+
369431
func ensureVersion(tx *bbolt.Tx, ver uint32) error {
370432
dbVersion, err := getVersionTx(tx)
371433
if err != nil {

0 commit comments

Comments
 (0)