forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This migration fixes the prior migration 20, introduced when porting the upstream code. That migration erroneously encoded outpoints without the Tree field that exists in decred code, thus rendering the index incorrect. The new migration corrects the issue by assuming the tree of every entry is zero, which is true because the channels can only reside in regular (as opposed to stake) transactions.
- Loading branch information
Showing
8 changed files
with
305 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package dcrmigration01 | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
|
||
"github.com/decred/dcrd/wire" | ||
) | ||
|
||
// readMig20Outpoint reads an outpoint that was stored by the migration20. | ||
func readMig20Outpoint(r io.Reader, o *wire.OutPoint) error { | ||
if _, err := io.ReadFull(r, o.Hash[:]); err != nil { | ||
return err | ||
} | ||
if err := binary.Read(r, byteOrder, &o.Index); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// writeMig20Outpoint writes an outpoint from the passed writer. | ||
func writeMig20Outpoint(w io.Writer, o *wire.OutPoint) error { | ||
if _, err := w.Write(o.Hash[:]); err != nil { | ||
return err | ||
} | ||
if err := binary.Write(w, byteOrder, o.Index); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// writeOkOutpoint writes an outpoint with the correct format to the passed | ||
// writer. | ||
func writeOkOutpoint(w io.Writer, o *wire.OutPoint) error { | ||
if _, err := w.Write(o.Hash[:]); err != nil { | ||
return err | ||
} | ||
if err := binary.Write(w, byteOrder, o.Index); err != nil { | ||
return err | ||
} | ||
if err := binary.Write(w, byteOrder, o.Tree); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package dcrmigration01 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
|
||
"github.com/decred/dcrd/wire" | ||
"github.com/decred/dcrlnd/kvdb" | ||
) | ||
|
||
var ( | ||
byteOrder = binary.BigEndian | ||
|
||
// outpointBucket is an index mapping outpoints to a tlv | ||
// stream of channel data. | ||
outpointBucket = []byte("outpoint-bucket") | ||
) | ||
|
||
// FixMigration20 fixes a version of the version 20 that had a wrong | ||
// implementation for the writeOutpoint codec function. This assumes that | ||
// migration20 was executed and now needs to be fixed. | ||
func FixMigration20(tx kvdb.RwTx) error { | ||
// Get the target bucket. | ||
bucket := tx.ReadWriteBucket(outpointBucket) | ||
|
||
// Collect the data that needs migration. | ||
var keys []*wire.OutPoint | ||
values := map[*wire.OutPoint][]byte{} | ||
err := bucket.ForEach(func(k, v []byte) error { | ||
op := new(wire.OutPoint) | ||
r := bytes.NewReader(k) | ||
if err := readMig20Outpoint(r, op); err != nil { | ||
return err | ||
} | ||
|
||
keys = append(keys, op) | ||
switch { | ||
case v == nil: | ||
values[op] = nil | ||
case len(v) == 0: | ||
values[op] = []byte{} | ||
default: | ||
values[op] = append([]byte(nil), v...) | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Infof("Migrating %d entries", len(keys)) | ||
|
||
for _, op := range keys { | ||
log.Debugf("Migrating outpoint %s", op) | ||
|
||
var oldOpBuf bytes.Buffer | ||
if err := writeMig20Outpoint(&oldOpBuf, op); err != nil { | ||
return err | ||
} | ||
|
||
if err := bucket.Delete(oldOpBuf.Bytes()); err != nil { | ||
return err | ||
} | ||
|
||
var newOpBuf bytes.Buffer | ||
if err := writeOkOutpoint(&newOpBuf, op); err != nil { | ||
return err | ||
} | ||
value := values[op] | ||
if err := bucket.Put(newOpBuf.Bytes(), value); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
45 changes: 45 additions & 0 deletions
45
channeldb/dcrmigrations/migration01/dcrmigration01_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package dcrmigration01 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/decred/dcrlnd/channeldb/migtest" | ||
"github.com/decred/dcrlnd/kvdb" | ||
) | ||
|
||
var ( | ||
hexStr = migtest.Hex | ||
|
||
tlvOutpointOpen = hexStr("000100") | ||
tlvOutpointClosed = hexStr("000101") | ||
|
||
outpointMig20 = hexStr("81b637d8fcd2c6da6859e6963113a1170de793e4b725b84d1e0b4cf99ec58ce952d6c6c7") | ||
outpointMig20_2 = hexStr("abb637d8fcd2c6da6859e6963113a1170de793e4b725b84d1e0b4cf99ec58ce952d6c6c7") | ||
outpointDataMig20 = map[string]interface{}{ | ||
outpointMig20: tlvOutpointOpen, | ||
outpointMig20_2: tlvOutpointClosed, | ||
} | ||
|
||
outpointCorrect = hexStr("81b637d8fcd2c6da6859e6963113a1170de793e4b725b84d1e0b4cf99ec58ce952d6c6c700") | ||
outpointCorrect_2 = hexStr("abb637d8fcd2c6da6859e6963113a1170de793e4b725b84d1e0b4cf99ec58ce952d6c6c700") | ||
outpointDataCorrect = map[string]interface{}{ | ||
outpointCorrect: tlvOutpointOpen, | ||
outpointCorrect_2: tlvOutpointClosed, | ||
} | ||
) | ||
|
||
func TestFixMigration20(t *testing.T) { | ||
// Prime the database with the results of migration20 (wrong outpoint | ||
// key). | ||
before := func(tx kvdb.RwTx) error { | ||
return migtest.RestoreDB(tx, outpointBucket, outpointDataMig20) | ||
} | ||
|
||
// Double check the keys were migrated to use the correct serialization | ||
// of outpoint. | ||
after := func(tx kvdb.RwTx) error { | ||
return migtest.VerifyDB(tx, outpointBucket, outpointDataCorrect) | ||
} | ||
|
||
migtest.ApplyMigration(t, before, after, FixMigration20, false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dcrmigration01 | ||
|
||
import ( | ||
"github.com/decred/slog" | ||
) | ||
|
||
// log is a logger that is initialized as disabled. This means the package | ||
// will not perform any logging by default until a logger is set. | ||
var log = slog.Disabled | ||
|
||
// UseLogger uses a specified Logger to output package logging info. | ||
func UseLogger(logger slog.Logger) { | ||
log = logger | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters