2019-04-16 11:33:30 +02:00
|
|
|
package contractcourt
|
|
|
|
|
|
|
|
import (
|
2020-11-18 22:45:35 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-04-16 11:33:30 +02:00
|
|
|
"os"
|
2020-11-18 22:45:35 +01:00
|
|
|
"path/filepath"
|
2019-04-16 11:33:30 +02:00
|
|
|
"runtime/pprof"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-11-18 22:45:35 +01:00
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-04-16 11:33:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// timeout implements a test level timeout.
|
2022-08-27 09:04:55 +02:00
|
|
|
func timeout() func() {
|
2019-04-16 11:33:30 +02:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
|
|
|
|
|
|
|
panic("test timeout")
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 22:45:35 +01:00
|
|
|
|
|
|
|
func copyFile(dest, src string) error {
|
|
|
|
s, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
d, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(d, s); err != nil {
|
|
|
|
d.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyChannelState copies the OpenChannel state by copying the database and
|
2022-08-15 15:05:40 +02:00
|
|
|
// creating a new struct from it. The copied state is returned.
|
|
|
|
func copyChannelState(t *testing.T, state *channeldb.OpenChannel) (
|
|
|
|
*channeldb.OpenChannel, error) {
|
2020-11-18 22:45:35 +01:00
|
|
|
|
|
|
|
// Make a copy of the DB.
|
2021-09-21 19:18:17 +02:00
|
|
|
dbFile := filepath.Join(state.Db.GetParentDB().Path(), "channel.db")
|
2022-08-15 15:05:40 +02:00
|
|
|
tempDbPath := t.TempDir()
|
2020-11-18 22:45:35 +01:00
|
|
|
|
|
|
|
tempDbFile := filepath.Join(tempDbPath, "channel.db")
|
2022-08-15 15:05:40 +02:00
|
|
|
err := copyFile(tempDbFile, dbFile)
|
2020-11-18 22:45:35 +01:00
|
|
|
if err != nil {
|
2022-08-15 15:05:40 +02:00
|
|
|
return nil, err
|
2020-11-18 22:45:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
newDb, err := channeldb.Open(tempDbPath)
|
|
|
|
if err != nil {
|
2022-08-15 15:05:40 +02:00
|
|
|
return nil, err
|
2020-11-18 22:45:35 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:18:17 +02:00
|
|
|
chans, err := newDb.ChannelStateDB().FetchAllChannels()
|
2020-11-18 22:45:35 +01:00
|
|
|
if err != nil {
|
2022-08-15 15:05:40 +02:00
|
|
|
return nil, err
|
2020-11-18 22:45:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We only support DBs with a single channel, for now.
|
|
|
|
if len(chans) != 1 {
|
2022-08-15 15:05:40 +02:00
|
|
|
return nil, fmt.Errorf("found %d chans in the db",
|
2020-11-18 22:45:35 +01:00
|
|
|
len(chans))
|
|
|
|
}
|
|
|
|
|
2022-08-15 15:05:40 +02:00
|
|
|
return chans[0], nil
|
2020-11-18 22:45:35 +01:00
|
|
|
}
|