mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-10-09 19:23:51 +02:00
channeldb: add NoAmountData field to MigrateRevLogConfigImpl
Add a NoAmountData field to the MigrateRevLogConfigImpl struct and set it for tests. This field is still a no-op in the migration.
This commit is contained in:
@@ -25,15 +25,29 @@ const recordsPerTx = 20_000
|
|||||||
|
|
||||||
// MigrateRevLogConfig is an interface that defines the config that should be
|
// MigrateRevLogConfig is an interface that defines the config that should be
|
||||||
// passed to the MigrateRevocationLog function.
|
// passed to the MigrateRevocationLog function.
|
||||||
type MigrateRevLogConfig interface{}
|
type MigrateRevLogConfig interface {
|
||||||
|
// GetNoAmountData returns true if the amount data of revoked commitment
|
||||||
|
// transactions should not be stored in the revocation log.
|
||||||
|
GetNoAmountData() bool
|
||||||
|
}
|
||||||
|
|
||||||
// MigrateRevLogConfigImpl implements the MigrationRevLogConfig interface.
|
// MigrateRevLogConfigImpl implements the MigrationRevLogConfig interface.
|
||||||
type MigrateRevLogConfigImpl struct{}
|
type MigrateRevLogConfigImpl struct {
|
||||||
|
// NoAmountData if set to true will result in the amount data of revoked
|
||||||
|
// commitment transactions not being stored in the revocation log.
|
||||||
|
NoAmountData bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNoAmountData returns true if the amount data of revoked commitment
|
||||||
|
// transactions should not be stored in the revocation log.
|
||||||
|
func (c *MigrateRevLogConfigImpl) GetNoAmountData() bool {
|
||||||
|
return c.NoAmountData
|
||||||
|
}
|
||||||
|
|
||||||
// MigrateRevocationLog migrates the old revocation logs into the newer format
|
// MigrateRevocationLog migrates the old revocation logs into the newer format
|
||||||
// and deletes them once finished, with the deletion only happens once ALL the
|
// and deletes them once finished, with the deletion only happens once ALL the
|
||||||
// old logs have been migrates.
|
// old logs have been migrates.
|
||||||
func MigrateRevocationLog(db kvdb.Backend, _ MigrateRevLogConfig) error {
|
func MigrateRevocationLog(db kvdb.Backend, cfg MigrateRevLogConfig) error {
|
||||||
log.Infof("Migrating revocation logs, might take a while...")
|
log.Infof("Migrating revocation logs, might take a while...")
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -71,7 +85,7 @@ func MigrateRevocationLog(db kvdb.Backend, _ MigrateRevLogConfig) error {
|
|||||||
|
|
||||||
// Process the migration.
|
// Process the migration.
|
||||||
err = kvdb.Update(db, func(tx kvdb.RwTx) error {
|
err = kvdb.Update(db, func(tx kvdb.RwTx) error {
|
||||||
finished, err = processMigration(tx)
|
finished, err = processMigration(tx, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -121,7 +135,7 @@ func MigrateRevocationLog(db kvdb.Backend, _ MigrateRevLogConfig) error {
|
|||||||
// processMigration finds the next un-migrated revocation logs, reads a max
|
// processMigration finds the next un-migrated revocation logs, reads a max
|
||||||
// number of `recordsPerTx` records, converts them into the new revocation logs
|
// number of `recordsPerTx` records, converts them into the new revocation logs
|
||||||
// and save them to disk.
|
// and save them to disk.
|
||||||
func processMigration(tx kvdb.RwTx) (bool, error) {
|
func processMigration(tx kvdb.RwTx, cfg MigrateRevLogConfig) (bool, error) {
|
||||||
openChanBucket := tx.ReadWriteBucket(openChannelBucket)
|
openChanBucket := tx.ReadWriteBucket(openChannelBucket)
|
||||||
|
|
||||||
// If no bucket is found, we can exit early.
|
// If no bucket is found, we can exit early.
|
||||||
@@ -141,7 +155,7 @@ func processMigration(tx kvdb.RwTx) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read a list of old revocation logs.
|
// Read a list of old revocation logs.
|
||||||
entryMap, err := readOldRevocationLogs(openChanBucket, locator)
|
entryMap, err := readOldRevocationLogs(openChanBucket, locator, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("read old logs err: %v", err)
|
return false, fmt.Errorf("read old logs err: %v", err)
|
||||||
}
|
}
|
||||||
@@ -375,7 +389,7 @@ type result struct {
|
|||||||
// readOldRevocationLogs finds a list of old revocation logs and converts them
|
// readOldRevocationLogs finds a list of old revocation logs and converts them
|
||||||
// into the new revocation logs.
|
// into the new revocation logs.
|
||||||
func readOldRevocationLogs(openChanBucket kvdb.RwBucket,
|
func readOldRevocationLogs(openChanBucket kvdb.RwBucket,
|
||||||
locator *updateLocator) (logEntries, error) {
|
locator *updateLocator, cfg MigrateRevLogConfig) (logEntries, error) {
|
||||||
|
|
||||||
entries := make(logEntries)
|
entries := make(logEntries)
|
||||||
results := make([]*result, 0)
|
results := make([]*result, 0)
|
||||||
@@ -422,7 +436,9 @@ func readOldRevocationLogs(openChanBucket kvdb.RwBucket,
|
|||||||
// Convert the old logs into the new logs. We do this early in
|
// Convert the old logs into the new logs. We do this early in
|
||||||
// the read tx so the old large revocation log can be set to
|
// the read tx so the old large revocation log can be set to
|
||||||
// nil here so save us some memory space.
|
// nil here so save us some memory space.
|
||||||
newLog, err := convertRevocationLog(&c, ourIndex, theirIndex)
|
newLog, err := convertRevocationLog(
|
||||||
|
&c, ourIndex, theirIndex, cfg.GetNoAmountData(),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.errChan <- err
|
r.errChan <- err
|
||||||
}
|
}
|
||||||
@@ -526,7 +542,8 @@ func readOldRevocationLogs(openChanBucket kvdb.RwBucket,
|
|||||||
// convertRevocationLog uses the fields `CommitTx` and `Htlcs` from a
|
// convertRevocationLog uses the fields `CommitTx` and `Htlcs` from a
|
||||||
// ChannelCommitment to construct a revocation log entry.
|
// ChannelCommitment to construct a revocation log entry.
|
||||||
func convertRevocationLog(commit *mig.ChannelCommitment,
|
func convertRevocationLog(commit *mig.ChannelCommitment,
|
||||||
ourOutputIndex, theirOutputIndex uint32) (*RevocationLog, error) {
|
ourOutputIndex, theirOutputIndex uint32,
|
||||||
|
noAmtData bool) (*RevocationLog, error) {
|
||||||
|
|
||||||
// Sanity check that the output indexes can be safely converted.
|
// Sanity check that the output indexes can be safely converted.
|
||||||
if ourOutputIndex > math.MaxUint16 {
|
if ourOutputIndex > math.MaxUint16 {
|
||||||
|
@@ -75,6 +75,7 @@ func TestMigrateRevocationLog(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Running %d test cases...\n", len(testCases))
|
fmt.Printf("Running %d test cases...\n", len(testCases))
|
||||||
|
fmt.Printf("withAmtData is set to: %v\n", withAmtData)
|
||||||
|
|
||||||
for i, tc := range testCases {
|
for i, tc := range testCases {
|
||||||
tc := tc
|
tc := tc
|
||||||
@@ -103,7 +104,9 @@ func TestMigrateRevocationLog(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := &MigrateRevLogConfigImpl{}
|
cfg := &MigrateRevLogConfigImpl{
|
||||||
|
NoAmountData: !withAmtData,
|
||||||
|
}
|
||||||
|
|
||||||
migtest.ApplyMigrationWithDB(
|
migtest.ApplyMigrationWithDB(
|
||||||
t,
|
t,
|
||||||
@@ -563,7 +566,9 @@ func BenchmarkMigration(b *testing.B) {
|
|||||||
return setupTestLogs(db, c, oldLogs, nil)
|
return setupTestLogs(db, c, oldLogs, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := &MigrateRevLogConfigImpl{}
|
cfg := &MigrateRevLogConfigImpl{
|
||||||
|
NoAmountData: !withAmtData,
|
||||||
|
}
|
||||||
|
|
||||||
// Run the migration test.
|
// Run the migration test.
|
||||||
migtest.ApplyMigrationWithDB(
|
migtest.ApplyMigrationWithDB(
|
||||||
|
@@ -3,6 +3,8 @@ package migration30
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
"github.com/btcsuite/btcd/btcutil"
|
"github.com/btcsuite/btcd/btcutil"
|
||||||
@@ -260,6 +262,12 @@ var (
|
|||||||
0xf8, 0xc3, 0xfc, 0x7, 0x2d, 0x15, 0x99, 0x55,
|
0xf8, 0xc3, 0xfc, 0x7, 0x2d, 0x15, 0x99, 0x55,
|
||||||
0x8, 0x69, 0xf6, 0x1, 0xa2, 0xcd, 0x6b, 0xa7,
|
0x8, 0x69, 0xf6, 0x1, 0xa2, 0xcd, 0x6b, 0xa7,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// withAmtData if set, will result in the amount data of the revoked
|
||||||
|
// commitment transactions also being stored in the new revocation log.
|
||||||
|
// The value of this variable is set randomly in the init function of
|
||||||
|
// this package.
|
||||||
|
withAmtData bool
|
||||||
)
|
)
|
||||||
|
|
||||||
// setupTestLogs takes care of creating the related buckets and inserts testing
|
// setupTestLogs takes care of creating the related buckets and inserts testing
|
||||||
@@ -551,3 +559,10 @@ func createFinished(cdb kvdb.Backend, c *mig26.OpenChannel,
|
|||||||
}
|
}
|
||||||
return setupTestLogs(cdb, c, oldLogs, newLogs)
|
return setupTestLogs(cdb, c, oldLogs, newLogs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().Unix())
|
||||||
|
if rand.Intn(2) == 0 {
|
||||||
|
withAmtData = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user