diff --git a/channeldb/migration30/migration.go b/channeldb/migration30/migration.go index 55663df5a..4cf24bfac 100644 --- a/channeldb/migration30/migration.go +++ b/channeldb/migration30/migration.go @@ -25,15 +25,29 @@ const recordsPerTx = 20_000 // MigrateRevLogConfig is an interface that defines the config that should be // 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. -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 // and deletes them once finished, with the deletion only happens once ALL the // 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...") var ( @@ -71,7 +85,7 @@ func MigrateRevocationLog(db kvdb.Backend, _ MigrateRevLogConfig) error { // Process the migration. err = kvdb.Update(db, func(tx kvdb.RwTx) error { - finished, err = processMigration(tx) + finished, err = processMigration(tx, cfg) if err != nil { return err } @@ -121,7 +135,7 @@ func MigrateRevocationLog(db kvdb.Backend, _ MigrateRevLogConfig) error { // processMigration finds the next un-migrated revocation logs, reads a max // number of `recordsPerTx` records, converts them into the new revocation logs // 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) // 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. - entryMap, err := readOldRevocationLogs(openChanBucket, locator) + entryMap, err := readOldRevocationLogs(openChanBucket, locator, cfg) if err != nil { 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 // into the new revocation logs. func readOldRevocationLogs(openChanBucket kvdb.RwBucket, - locator *updateLocator) (logEntries, error) { + locator *updateLocator, cfg MigrateRevLogConfig) (logEntries, error) { entries := make(logEntries) 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 // the read tx so the old large revocation log can be set to // 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 { r.errChan <- err } @@ -526,7 +542,8 @@ func readOldRevocationLogs(openChanBucket kvdb.RwBucket, // convertRevocationLog uses the fields `CommitTx` and `Htlcs` from a // ChannelCommitment to construct a revocation log entry. 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. if ourOutputIndex > math.MaxUint16 { diff --git a/channeldb/migration30/migration_test.go b/channeldb/migration30/migration_test.go index 7eef265a6..a83a4f451 100644 --- a/channeldb/migration30/migration_test.go +++ b/channeldb/migration30/migration_test.go @@ -75,6 +75,7 @@ func TestMigrateRevocationLog(t *testing.T) { } fmt.Printf("Running %d test cases...\n", len(testCases)) + fmt.Printf("withAmtData is set to: %v\n", withAmtData) for i, tc := range testCases { tc := tc @@ -103,7 +104,9 @@ func TestMigrateRevocationLog(t *testing.T) { return nil } - cfg := &MigrateRevLogConfigImpl{} + cfg := &MigrateRevLogConfigImpl{ + NoAmountData: !withAmtData, + } migtest.ApplyMigrationWithDB( t, @@ -563,7 +566,9 @@ func BenchmarkMigration(b *testing.B) { return setupTestLogs(db, c, oldLogs, nil) } - cfg := &MigrateRevLogConfigImpl{} + cfg := &MigrateRevLogConfigImpl{ + NoAmountData: !withAmtData, + } // Run the migration test. migtest.ApplyMigrationWithDB( diff --git a/channeldb/migration30/test_utils.go b/channeldb/migration30/test_utils.go index aa061039a..2a3093fb1 100644 --- a/channeldb/migration30/test_utils.go +++ b/channeldb/migration30/test_utils.go @@ -3,6 +3,8 @@ package migration30 import ( "bytes" "fmt" + "math/rand" + "time" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil" @@ -260,6 +262,12 @@ var ( 0xf8, 0xc3, 0xfc, 0x7, 0x2d, 0x15, 0x99, 0x55, 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 @@ -551,3 +559,10 @@ func createFinished(cdb kvdb.Backend, c *mig26.OpenChannel, } return setupTestLogs(cdb, c, oldLogs, newLogs) } + +func init() { + rand.Seed(time.Now().Unix()) + if rand.Intn(2) == 0 { + withAmtData = true + } +}