forked from lightninglabs/loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_02_swap_publication_deadline.go
58 lines (49 loc) · 1.49 KB
/
migration_02_swap_publication_deadline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package loopdb
import (
"bytes"
"errors"
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/coreos/bbolt"
)
// migrateSwapPublicationDeadline migrates the database to v02, by adding the
// SwapPublicationDeadline field to loop out contracts.
func migrateSwapPublicationDeadline(tx *bbolt.Tx, chainParams *chaincfg.Params) error {
rootBucket := tx.Bucket(loopOutBucketKey)
if rootBucket == nil {
return errors.New("bucket does not exist")
}
return rootBucket.ForEach(func(swapHash, v []byte) error {
// Only go into things that we know are sub-bucket
// keys.
if v != nil {
return nil
}
// From the root bucket, we'll grab the next swap
// bucket for this swap from its swaphash.
swapBucket := rootBucket.Bucket(swapHash)
if swapBucket == nil {
return fmt.Errorf("swap bucket %x not found",
swapHash)
}
// With the main swap bucket obtained, we'll grab the
// raw swap contract bytes.
contractBytes := swapBucket.Get(contractKey)
if contractBytes == nil {
return errors.New("contract not found")
}
// Write the current contract serialization into a buffer.
b := &bytes.Buffer{}
if _, err := b.Write(contractBytes); err != nil {
return err
}
// We migrate to the new format by copying the first 8 bytes
// (the creation time) to the end (the swap deadline)
var swapDeadline [8]byte
copy(swapDeadline[:], contractBytes[:8])
if _, err := b.Write(swapDeadline[:]); err != nil {
return err
}
return swapBucket.Put(contractKey, b.Bytes())
})
}