contractcourt: use T.TempDir to create temporary test directory
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
6 files changed
+60
-152
No files matched your search
@@ -10,10 +10,8 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -639,25 +637,13 @@ func TestMockRetributionStore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeTestChannelDB() (*channeldb.DB, func(), error) {
|
func makeTestChannelDB(t *testing.T) (*channeldb.DB, error) {
|
||||||
// First, create a temporary directory to be used for the duration of
|
db, err := channeldb.Open(t.TempDir())
|
||||||
// this test.
|
|
||||||
tempDirName, err := ioutil.TempDir("", "channeldb")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanUp := func() {
|
return db, nil
|
||||||
os.RemoveAll(tempDirName)
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := channeldb.Open(tempDirName)
|
|
||||||
if err != nil {
|
|
||||||
cleanUp()
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return db, cleanUp, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestChannelDBRetributionStore instantiates a retributionStore backed by a
|
// TestChannelDBRetributionStore instantiates a retributionStore backed by a
|
||||||
@@ -670,12 +656,11 @@ func TestChannelDBRetributionStore(t *testing.T) {
|
|||||||
t.Run(
|
t.Run(
|
||||||
"channeldbDBRetributionStore."+test.name,
|
"channeldbDBRetributionStore."+test.name,
|
||||||
func(tt *testing.T) {
|
func(tt *testing.T) {
|
||||||
db, cleanUp, err := makeTestChannelDB()
|
db, err := makeTestChannelDB(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to open channeldb: %v", err)
|
t.Fatalf("unable to open channeldb: %v", err)
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
restartDb := func() RetributionStorer {
|
restartDb := func() RetributionStorer {
|
||||||
// Close and reopen channeldb
|
// Close and reopen channeldb
|
||||||
@@ -976,7 +961,7 @@ func initBreachedState(t *testing.T) (*BreachArbiter,
|
|||||||
// Create a pair of channels using a notifier that allows us to signal
|
// Create a pair of channels using a notifier that allows us to signal
|
||||||
// a spend of the funding transaction. Alice's channel will be the on
|
// a spend of the funding transaction. Alice's channel will be the on
|
||||||
// observing a breach.
|
// observing a breach.
|
||||||
alice, bob, cleanUpChans, err := createInitChannels(1)
|
alice, bob, cleanUpChans, err := createInitChannels(t, 1)
|
||||||
require.NoError(t, err, "unable to create test channels")
|
require.NoError(t, err, "unable to create test channels")
|
||||||
|
|
||||||
// Instantiate a breach arbiter to handle the breach of alice's channel.
|
// Instantiate a breach arbiter to handle the breach of alice's channel.
|
||||||
@@ -2177,8 +2162,7 @@ func createTestArbiter(t *testing.T, contractBreaches chan *ContractBreachEvent,
|
|||||||
// createInitChannels creates two initialized test channels funded with 10 BTC,
|
// createInitChannels creates two initialized test channels funded with 10 BTC,
|
||||||
// with 5 BTC allocated to each side. Within the channel, Alice is the
|
// with 5 BTC allocated to each side. Within the channel, Alice is the
|
||||||
// initiator.
|
// initiator.
|
||||||
func createInitChannels(revocationWindow int) (*lnwallet.LightningChannel, *lnwallet.LightningChannel, func(), error) {
|
func createInitChannels(t *testing.T, revocationWindow int) (*lnwallet.LightningChannel, *lnwallet.LightningChannel, func(), error) {
|
||||||
|
|
||||||
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(
|
aliceKeyPriv, aliceKeyPub := btcec.PrivKeyFromBytes(
|
||||||
channels.AlicesPrivKey,
|
channels.AlicesPrivKey,
|
||||||
)
|
)
|
||||||
@@ -2285,22 +2269,12 @@ func createInitChannels(revocationWindow int) (*lnwallet.LightningChannel, *lnwa
|
|||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
alicePath, err := ioutil.TempDir("", "alicedb")
|
dbAlice, err := channeldb.Open(t.TempDir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
dbAlice, err := channeldb.Open(alicePath)
|
dbBob, err := channeldb.Open(t.TempDir())
|
||||||
if err != nil {
|
|
||||||
return nil, nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
bobPath, err := ioutil.TempDir("", "bobdb")
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
dbBob, err := channeldb.Open(bobPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -2418,8 +2392,6 @@ func createInitChannels(revocationWindow int) (*lnwallet.LightningChannel, *lnwa
|
|||||||
cleanUpFunc := func() {
|
cleanUpFunc := func() {
|
||||||
dbBob.Close()
|
dbBob.Close()
|
||||||
dbAlice.Close()
|
dbAlice.Close()
|
||||||
os.RemoveAll(bobPath)
|
|
||||||
os.RemoveAll(alicePath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now that the channel are open, simulate the start of a session by
|
// Now that the channel are open, simulate the start of a session by
|
||||||
|
|||||||
@@ -2,9 +2,7 @@ package contractcourt
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"io/ioutil"
|
|
||||||
prand "math/rand"
|
prand "math/rand"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -146,36 +144,28 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeTestDB() (kvdb.Backend, func(), error) {
|
func makeTestDB(t *testing.T) (kvdb.Backend, error) {
|
||||||
// First, create a temporary directory to be used for the duration of
|
|
||||||
// this test.
|
|
||||||
tempDirName, err := ioutil.TempDir("", "arblog")
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := kvdb.Create(
|
db, err := kvdb.Create(
|
||||||
kvdb.BoltBackendName, tempDirName+"/test.db", true,
|
kvdb.BoltBackendName, t.TempDir()+"/test.db", true,
|
||||||
kvdb.DefaultDBTimeout,
|
kvdb.DefaultDBTimeout,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanUp := func() {
|
t.Cleanup(func() {
|
||||||
db.Close()
|
db.Close()
|
||||||
os.RemoveAll(tempDirName)
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return db, cleanUp, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTestBoltArbLog(chainhash chainhash.Hash,
|
func newTestBoltArbLog(t *testing.T, chainhash chainhash.Hash,
|
||||||
op wire.OutPoint) (ArbitratorLog, func(), error) {
|
op wire.OutPoint) (ArbitratorLog, error) {
|
||||||
|
|
||||||
testDB, cleanUp, err := makeTestDB()
|
testDB, err := makeTestDB(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
testArbCfg := ChannelArbitratorConfig{
|
testArbCfg := ChannelArbitratorConfig{
|
||||||
@@ -187,10 +177,10 @@ func newTestBoltArbLog(chainhash chainhash.Hash,
|
|||||||
}
|
}
|
||||||
testLog, err := newBoltArbitratorLog(testDB, testArbCfg, chainhash, op)
|
testLog, err := newBoltArbitratorLog(testDB, testArbCfg, chainhash, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return testLog, cleanUp, err
|
return testLog, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func randOutPoint() wire.OutPoint {
|
func randOutPoint() wire.OutPoint {
|
||||||
@@ -304,11 +294,10 @@ func TestContractInsertionRetrieval(t *testing.T) {
|
|||||||
|
|
||||||
// First, we'll create a test instance of the ArbitratorLog
|
// First, we'll create a test instance of the ArbitratorLog
|
||||||
// implementation backed by boltdb.
|
// implementation backed by boltdb.
|
||||||
testLog, cleanUp, err := newTestBoltArbLog(
|
testLog, err := newTestBoltArbLog(
|
||||||
testChainHash, testChanPoint1,
|
t, testChainHash, testChanPoint1,
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "unable to create test log")
|
require.NoError(t, err, "unable to create test log")
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// The log created, we'll create a series of resolvers, each properly
|
// The log created, we'll create a series of resolvers, each properly
|
||||||
// implementing the ContractResolver interface.
|
// implementing the ContractResolver interface.
|
||||||
@@ -432,11 +421,10 @@ func TestContractResolution(t *testing.T) {
|
|||||||
|
|
||||||
// First, we'll create a test instance of the ArbitratorLog
|
// First, we'll create a test instance of the ArbitratorLog
|
||||||
// implementation backed by boltdb.
|
// implementation backed by boltdb.
|
||||||
testLog, cleanUp, err := newTestBoltArbLog(
|
testLog, err := newTestBoltArbLog(
|
||||||
testChainHash, testChanPoint1,
|
t, testChainHash, testChanPoint1,
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "unable to create test log")
|
require.NoError(t, err, "unable to create test log")
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// We'll now create a timeout resolver that we'll be using for the
|
// We'll now create a timeout resolver that we'll be using for the
|
||||||
// duration of this test.
|
// duration of this test.
|
||||||
@@ -486,11 +474,10 @@ func TestContractSwapping(t *testing.T) {
|
|||||||
|
|
||||||
// First, we'll create a test instance of the ArbitratorLog
|
// First, we'll create a test instance of the ArbitratorLog
|
||||||
// implementation backed by boltdb.
|
// implementation backed by boltdb.
|
||||||
testLog, cleanUp, err := newTestBoltArbLog(
|
testLog, err := newTestBoltArbLog(
|
||||||
testChainHash, testChanPoint1,
|
t, testChainHash, testChanPoint1,
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "unable to create test log")
|
require.NoError(t, err, "unable to create test log")
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// We'll create two resolvers, a regular timeout resolver, and the
|
// We'll create two resolvers, a regular timeout resolver, and the
|
||||||
// contest resolver that eventually turns into the timeout resolver.
|
// contest resolver that eventually turns into the timeout resolver.
|
||||||
@@ -543,11 +530,10 @@ func TestContractResolutionsStorage(t *testing.T) {
|
|||||||
|
|
||||||
// First, we'll create a test instance of the ArbitratorLog
|
// First, we'll create a test instance of the ArbitratorLog
|
||||||
// implementation backed by boltdb.
|
// implementation backed by boltdb.
|
||||||
testLog, cleanUp, err := newTestBoltArbLog(
|
testLog, err := newTestBoltArbLog(
|
||||||
testChainHash, testChanPoint1,
|
t, testChainHash, testChanPoint1,
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "unable to create test log")
|
require.NoError(t, err, "unable to create test log")
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// With the test log created, we'll now craft a contact resolution that
|
// With the test log created, we'll now craft a contact resolution that
|
||||||
// will be using for the duration of this test.
|
// will be using for the duration of this test.
|
||||||
@@ -659,11 +645,10 @@ func TestContractResolutionsStorage(t *testing.T) {
|
|||||||
func TestStateMutation(t *testing.T) {
|
func TestStateMutation(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
testLog, cleanUp, err := newTestBoltArbLog(
|
testLog, err := newTestBoltArbLog(
|
||||||
testChainHash, testChanPoint1,
|
t, testChainHash, testChanPoint1,
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "unable to create test log")
|
require.NoError(t, err, "unable to create test log")
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
// The default state of an arbitrator should be StateDefault.
|
// The default state of an arbitrator should be StateDefault.
|
||||||
arbState, err := testLog.CurrentState(nil)
|
arbState, err := testLog.CurrentState(nil)
|
||||||
@@ -707,17 +692,15 @@ func TestScopeIsolation(t *testing.T) {
|
|||||||
|
|
||||||
// We'll create two distinct test logs. Each log will have a unique
|
// We'll create two distinct test logs. Each log will have a unique
|
||||||
// scope key, and therefore should be isolated from the other on disk.
|
// scope key, and therefore should be isolated from the other on disk.
|
||||||
testLog1, cleanUp1, err := newTestBoltArbLog(
|
testLog1, err := newTestBoltArbLog(
|
||||||
testChainHash, testChanPoint1,
|
t, testChainHash, testChanPoint1,
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "unable to create test log")
|
require.NoError(t, err, "unable to create test log")
|
||||||
defer cleanUp1()
|
|
||||||
|
|
||||||
testLog2, cleanUp2, err := newTestBoltArbLog(
|
testLog2, err := newTestBoltArbLog(
|
||||||
testChainHash, testChanPoint2,
|
t, testChainHash, testChanPoint2,
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "unable to create test log")
|
require.NoError(t, err, "unable to create test log")
|
||||||
defer cleanUp2()
|
|
||||||
|
|
||||||
// We'll now update the current state of both the logs to a unique
|
// We'll now update the current state of both the logs to a unique
|
||||||
// state.
|
// state.
|
||||||
@@ -754,11 +737,10 @@ func TestScopeIsolation(t *testing.T) {
|
|||||||
func TestCommitSetStorage(t *testing.T) {
|
func TestCommitSetStorage(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
testLog, cleanUp, err := newTestBoltArbLog(
|
testLog, err := newTestBoltArbLog(
|
||||||
testChainHash, testChanPoint1,
|
t, testChainHash, testChanPoint1,
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "unable to create test log")
|
require.NoError(t, err, "unable to create test log")
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
activeHTLCs := []channeldb.HTLC{
|
activeHTLCs := []channeldb.HTLC{
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
package contractcourt
|
package contractcourt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
@@ -22,13 +20,7 @@ import (
|
|||||||
func TestChainArbitratorRepublishCloses(t *testing.T) {
|
func TestChainArbitratorRepublishCloses(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
tempPath, err := ioutil.TempDir("", "testdb")
|
db, err := channeldb.Open(t.TempDir())
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempPath)
|
|
||||||
|
|
||||||
db, err := channeldb.Open(tempPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -143,12 +135,7 @@ func TestChainArbitratorRepublishCloses(t *testing.T) {
|
|||||||
func TestResolveContract(t *testing.T) {
|
func TestResolveContract(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// To start with, we'll create a new temp DB for the duration of this
|
db, err := channeldb.Open(t.TempDir())
|
||||||
// test.
|
|
||||||
tempPath, err := ioutil.TempDir("", "testdb")
|
|
||||||
require.NoError(t, err, "unable to make temp dir")
|
|
||||||
defer os.RemoveAll(tempPath)
|
|
||||||
db, err := channeldb.Open(tempPath)
|
|
||||||
require.NoError(t, err, "unable to open db")
|
require.NoError(t, err, "unable to open db")
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
|||||||
@@ -200,27 +200,19 @@ type dlpTestCase struct {
|
|||||||
// state) are returned.
|
// state) are returned.
|
||||||
func executeStateTransitions(t *testing.T, htlcAmount lnwire.MilliSatoshi,
|
func executeStateTransitions(t *testing.T, htlcAmount lnwire.MilliSatoshi,
|
||||||
aliceChannel, bobChannel *lnwallet.LightningChannel,
|
aliceChannel, bobChannel *lnwallet.LightningChannel,
|
||||||
numUpdates uint8) ([]*channeldb.OpenChannel, func(), error) {
|
numUpdates uint8) ([]*channeldb.OpenChannel, error) {
|
||||||
|
|
||||||
// We'll make a copy of the channel state before each transition.
|
// We'll make a copy of the channel state before each transition.
|
||||||
var (
|
var (
|
||||||
chanStates []*channeldb.OpenChannel
|
chanStates []*channeldb.OpenChannel
|
||||||
cleanupFuncs []func()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
cleanAll := func() {
|
state, err := copyChannelState(t, aliceChannel.State())
|
||||||
for _, f := range cleanupFuncs {
|
|
||||||
f()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
state, f, err := copyChannelState(aliceChannel.State())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
chanStates = append(chanStates, state)
|
chanStates = append(chanStates, state)
|
||||||
cleanupFuncs = append(cleanupFuncs, f)
|
|
||||||
|
|
||||||
for i := 0; i < int(numUpdates); i++ {
|
for i := 0; i < int(numUpdates); i++ {
|
||||||
addFakeHTLC(
|
addFakeHTLC(
|
||||||
@@ -229,21 +221,18 @@ func executeStateTransitions(t *testing.T, htlcAmount lnwire.MilliSatoshi,
|
|||||||
|
|
||||||
err := lnwallet.ForceStateTransition(aliceChannel, bobChannel)
|
err := lnwallet.ForceStateTransition(aliceChannel, bobChannel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanAll()
|
return nil, err
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
state, f, err := copyChannelState(aliceChannel.State())
|
state, err := copyChannelState(t, aliceChannel.State())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanAll()
|
return nil, err
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chanStates = append(chanStates, state)
|
chanStates = append(chanStates, state)
|
||||||
cleanupFuncs = append(cleanupFuncs, f)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return chanStates, cleanAll, nil
|
return chanStates, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestChainWatcherDataLossProtect tests that if we've lost data (and are
|
// TestChainWatcherDataLossProtect tests that if we've lost data (and are
|
||||||
@@ -278,7 +267,7 @@ func TestChainWatcherDataLossProtect(t *testing.T) {
|
|||||||
// new HTLC to add to the commitment, and then lock in a state
|
// new HTLC to add to the commitment, and then lock in a state
|
||||||
// transition.
|
// transition.
|
||||||
const htlcAmt = 1000
|
const htlcAmt = 1000
|
||||||
states, cleanStates, err := executeStateTransitions(
|
states, err := executeStateTransitions(
|
||||||
t, htlcAmt, aliceChannel, bobChannel,
|
t, htlcAmt, aliceChannel, bobChannel,
|
||||||
testCase.BroadcastStateNum,
|
testCase.BroadcastStateNum,
|
||||||
)
|
)
|
||||||
@@ -287,7 +276,6 @@ func TestChainWatcherDataLossProtect(t *testing.T) {
|
|||||||
"transition: %v", err)
|
"transition: %v", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer cleanStates()
|
|
||||||
|
|
||||||
// We'll use the state this test case wants Alice to start at.
|
// We'll use the state this test case wants Alice to start at.
|
||||||
aliceChanState := states[testCase.NumUpdates]
|
aliceChanState := states[testCase.NumUpdates]
|
||||||
@@ -455,7 +443,7 @@ func TestChainWatcherLocalForceCloseDetect(t *testing.T) {
|
|||||||
// get more coverage of various state hint encodings beyond 0
|
// get more coverage of various state hint encodings beyond 0
|
||||||
// and 1.
|
// and 1.
|
||||||
const htlcAmt = 1000
|
const htlcAmt = 1000
|
||||||
states, cleanStates, err := executeStateTransitions(
|
states, err := executeStateTransitions(
|
||||||
t, htlcAmt, aliceChannel, bobChannel, numUpdates,
|
t, htlcAmt, aliceChannel, bobChannel, numUpdates,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -463,7 +451,6 @@ func TestChainWatcherLocalForceCloseDetect(t *testing.T) {
|
|||||||
"transition: %v", err)
|
"transition: %v", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer cleanStates()
|
|
||||||
|
|
||||||
// We'll use the state this test case wants Alice to start at.
|
// We'll use the state this test case wants Alice to start at.
|
||||||
aliceChanState := states[localState]
|
aliceChanState := states[localState]
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package contractcourt
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -406,11 +404,7 @@ func createTestChannelArbitrator(t *testing.T, log ArbitratorLog,
|
|||||||
|
|
||||||
var cleanUp func()
|
var cleanUp func()
|
||||||
if log == nil {
|
if log == nil {
|
||||||
dbDir, err := ioutil.TempDir("", "chanArb")
|
dbPath := filepath.Join(t.TempDir(), "testdb")
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
dbPath := filepath.Join(dbDir, "testdb")
|
|
||||||
db, err := kvdb.Create(
|
db, err := kvdb.Create(
|
||||||
kvdb.BoltBackendName, dbPath, true,
|
kvdb.BoltBackendName, dbPath, true,
|
||||||
kvdb.DefaultDBTimeout,
|
kvdb.DefaultDBTimeout,
|
||||||
@@ -427,7 +421,6 @@ func createTestChannelArbitrator(t *testing.T, log ArbitratorLog,
|
|||||||
}
|
}
|
||||||
cleanUp = func() {
|
cleanUp = func() {
|
||||||
db.Close()
|
db.Close()
|
||||||
os.RemoveAll(dbDir)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log = &testArbLog{
|
log = &testArbLog{
|
||||||
|
|||||||
+10
-23
@@ -3,7 +3,6 @@ package contractcourt
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
@@ -52,47 +51,35 @@ func copyFile(dest, src string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// copyChannelState copies the OpenChannel state by copying the database and
|
// copyChannelState copies the OpenChannel state by copying the database and
|
||||||
// creating a new struct from it. The copied state and a cleanup function are
|
// creating a new struct from it. The copied state is returned.
|
||||||
// returned.
|
func copyChannelState(t *testing.T, state *channeldb.OpenChannel) (
|
||||||
func copyChannelState(state *channeldb.OpenChannel) (
|
*channeldb.OpenChannel, error) {
|
||||||
*channeldb.OpenChannel, func(), error) {
|
|
||||||
|
|
||||||
// Make a copy of the DB.
|
// Make a copy of the DB.
|
||||||
dbFile := filepath.Join(state.Db.GetParentDB().Path(), "channel.db")
|
dbFile := filepath.Join(state.Db.GetParentDB().Path(), "channel.db")
|
||||||
tempDbPath, err := ioutil.TempDir("", "past-state")
|
tempDbPath := t.TempDir()
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanup := func() {
|
|
||||||
os.RemoveAll(tempDbPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
tempDbFile := filepath.Join(tempDbPath, "channel.db")
|
tempDbFile := filepath.Join(tempDbPath, "channel.db")
|
||||||
err = copyFile(tempDbFile, dbFile)
|
err := copyFile(tempDbFile, dbFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanup()
|
return nil, err
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newDb, err := channeldb.Open(tempDbPath)
|
newDb, err := channeldb.Open(tempDbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanup()
|
return nil, err
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chans, err := newDb.ChannelStateDB().FetchAllChannels()
|
chans, err := newDb.ChannelStateDB().FetchAllChannels()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanup()
|
return nil, err
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We only support DBs with a single channel, for now.
|
// We only support DBs with a single channel, for now.
|
||||||
if len(chans) != 1 {
|
if len(chans) != 1 {
|
||||||
cleanup()
|
return nil, fmt.Errorf("found %d chans in the db",
|
||||||
return nil, nil, fmt.Errorf("found %d chans in the db",
|
|
||||||
len(chans))
|
len(chans))
|
||||||
}
|
}
|
||||||
|
|
||||||
return chans[0], cleanup, nil
|
return chans[0], nil
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user