mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-24 06:47:44 +01:00
contractcourt: start channel arbitrator with blockbeat
To avoid calling GetBestBlock again.
This commit is contained in:
parent
e2e59bd90c
commit
802353036e
3 changed files with 48 additions and 31 deletions
|
@ -794,7 +794,7 @@ func (c *ChainArbitrator) Start() error {
|
||||||
arbitrator.cfg.ChanPoint)
|
arbitrator.cfg.ChanPoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := arbitrator.Start(startState); err != nil {
|
if err := arbitrator.Start(startState, c.beat); err != nil {
|
||||||
stopAndLog()
|
stopAndLog()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1211,7 +1211,7 @@ func (c *ChainArbitrator) WatchNewChannel(newChan *channeldb.OpenChannel) error
|
||||||
// arbitrators, then launch it.
|
// arbitrators, then launch it.
|
||||||
c.activeChannels[chanPoint] = channelArb
|
c.activeChannels[chanPoint] = channelArb
|
||||||
|
|
||||||
if err := channelArb.Start(nil); err != nil {
|
if err := channelArb.Start(nil, c.beat); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -462,7 +462,9 @@ func (c *ChannelArbitrator) getStartState(tx kvdb.RTx) (*chanArbStartState,
|
||||||
// Start starts all the goroutines that the ChannelArbitrator needs to operate.
|
// Start starts all the goroutines that the ChannelArbitrator needs to operate.
|
||||||
// If takes a start state, which will be looked up on disk if it is not
|
// If takes a start state, which will be looked up on disk if it is not
|
||||||
// provided.
|
// provided.
|
||||||
func (c *ChannelArbitrator) Start(state *chanArbStartState) error {
|
func (c *ChannelArbitrator) Start(state *chanArbStartState,
|
||||||
|
beat chainio.Blockbeat) error {
|
||||||
|
|
||||||
if !atomic.CompareAndSwapInt32(&c.started, 0, 1) {
|
if !atomic.CompareAndSwapInt32(&c.started, 0, 1) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -484,10 +486,8 @@ func (c *ChannelArbitrator) Start(state *chanArbStartState) error {
|
||||||
// Set our state from our starting state.
|
// Set our state from our starting state.
|
||||||
c.state = state.currentState
|
c.state = state.currentState
|
||||||
|
|
||||||
_, bestHeight, err := c.cfg.ChainIO.GetBestBlock()
|
// Get the starting height.
|
||||||
if err != nil {
|
bestHeight := beat.Height()
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.wg.Add(1)
|
c.wg.Add(1)
|
||||||
go c.channelAttendant(bestHeight, state.commitSet)
|
go c.channelAttendant(bestHeight, state.commitSet)
|
||||||
|
|
|
@ -296,7 +296,8 @@ func (c *chanArbTestCtx) Restart(restartClosure func(*chanArbTestCtx)) (*chanArb
|
||||||
restartClosure(newCtx)
|
restartClosure(newCtx)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := newCtx.chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := newCtx.chanArb.Start(nil, beat); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -523,7 +524,8 @@ func TestChannelArbitratorCooperativeClose(t *testing.T) {
|
||||||
chanArbCtx, err := createTestChannelArbitrator(t, log)
|
chanArbCtx, err := createTestChannelArbitrator(t, log)
|
||||||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||||
|
|
||||||
if err := chanArbCtx.chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArbCtx.chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
|
@ -581,7 +583,8 @@ func TestChannelArbitratorRemoteForceClose(t *testing.T) {
|
||||||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
|
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
defer chanArb.Stop()
|
defer chanArb.Stop()
|
||||||
|
@ -634,7 +637,8 @@ func TestChannelArbitratorLocalForceClose(t *testing.T) {
|
||||||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
|
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
defer chanArb.Stop()
|
defer chanArb.Stop()
|
||||||
|
@ -746,7 +750,8 @@ func TestChannelArbitratorBreachClose(t *testing.T) {
|
||||||
chanArb.cfg.PreimageDB = newMockWitnessBeacon()
|
chanArb.cfg.PreimageDB = newMockWitnessBeacon()
|
||||||
chanArb.cfg.Registry = &mockRegistry{}
|
chanArb.cfg.Registry = &mockRegistry{}
|
||||||
|
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
|
@ -873,7 +878,8 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) {
|
||||||
chanArb.cfg.PreimageDB = newMockWitnessBeacon()
|
chanArb.cfg.PreimageDB = newMockWitnessBeacon()
|
||||||
chanArb.cfg.Registry = &mockRegistry{}
|
chanArb.cfg.Registry = &mockRegistry{}
|
||||||
|
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
defer chanArb.Stop()
|
defer chanArb.Stop()
|
||||||
|
@ -1163,7 +1169,8 @@ func TestChannelArbitratorLocalForceCloseRemoteConfirmed(t *testing.T) {
|
||||||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
|
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
defer chanArb.Stop()
|
defer chanArb.Stop()
|
||||||
|
@ -1270,7 +1277,8 @@ func TestChannelArbitratorLocalForceDoubleSpend(t *testing.T) {
|
||||||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
|
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
defer chanArb.Stop()
|
defer chanArb.Stop()
|
||||||
|
@ -1376,7 +1384,8 @@ func TestChannelArbitratorPersistence(t *testing.T) {
|
||||||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||||
|
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1494,7 +1503,8 @@ func TestChannelArbitratorForceCloseBreachedChannel(t *testing.T) {
|
||||||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||||
|
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1681,7 +1691,8 @@ func TestChannelArbitratorCommitFailure(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1765,7 +1776,8 @@ func TestChannelArbitratorEmptyResolutions(t *testing.T) {
|
||||||
chanArb.cfg.ClosingHeight = 100
|
chanArb.cfg.ClosingHeight = 100
|
||||||
chanArb.cfg.CloseType = channeldb.RemoteForceClose
|
chanArb.cfg.CloseType = channeldb.RemoteForceClose
|
||||||
|
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(100)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1795,7 +1807,8 @@ func TestChannelArbitratorAlreadyForceClosed(t *testing.T) {
|
||||||
chanArbCtx, err := createTestChannelArbitrator(t, log)
|
chanArbCtx, err := createTestChannelArbitrator(t, log)
|
||||||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
defer chanArb.Stop()
|
defer chanArb.Stop()
|
||||||
|
@ -1893,9 +1906,10 @@ func TestChannelArbitratorDanglingCommitForceClose(t *testing.T) {
|
||||||
t.Fatalf("unable to create ChannelArbitrator: %v", err)
|
t.Fatalf("unable to create ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
err = chanArb.Start(nil, beat)
|
||||||
}
|
require.NoError(t, err)
|
||||||
|
|
||||||
defer chanArb.Stop()
|
defer chanArb.Stop()
|
||||||
|
|
||||||
// Now that our channel arb has started, we'll set up
|
// Now that our channel arb has started, we'll set up
|
||||||
|
@ -2089,7 +2103,8 @@ func TestChannelArbitratorPendingExpiredHTLC(t *testing.T) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := chanArb.Start(nil); err != nil {
|
beat := newBeatFromHeight(0)
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
|
@ -2123,7 +2138,7 @@ func TestChannelArbitratorPendingExpiredHTLC(t *testing.T) {
|
||||||
// We will advance the uptime to 10 seconds which should be still within
|
// We will advance the uptime to 10 seconds which should be still within
|
||||||
// the grace period and should not trigger going to chain.
|
// the grace period and should not trigger going to chain.
|
||||||
testClock.SetTime(startTime.Add(time.Second * 10))
|
testClock.SetTime(startTime.Add(time.Second * 10))
|
||||||
beat := newBeatFromHeight(5)
|
beat = newBeatFromHeight(5)
|
||||||
chanArbCtx.chanArb.BlockbeatChan <- beat
|
chanArbCtx.chanArb.BlockbeatChan <- beat
|
||||||
chanArbCtx.AssertState(StateDefault)
|
chanArbCtx.AssertState(StateDefault)
|
||||||
|
|
||||||
|
@ -2244,8 +2259,8 @@ func TestRemoteCloseInitiator(t *testing.T) {
|
||||||
"ChannelArbitrator: %v", err)
|
"ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
chanArb := chanArbCtx.chanArb
|
chanArb := chanArbCtx.chanArb
|
||||||
|
beat := newBeatFromHeight(0)
|
||||||
if err := chanArb.Start(nil); err != nil {
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start "+
|
t.Fatalf("unable to start "+
|
||||||
"ChannelArbitrator: %v", err)
|
"ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -2796,7 +2811,9 @@ func TestChannelArbitratorAnchors(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := chanArb.Start(nil); err != nil {
|
heightHint := uint32(1000)
|
||||||
|
beat := newBeatFromHeight(int32(heightHint))
|
||||||
|
if err := chanArb.Start(nil, beat); err != nil {
|
||||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||||
}
|
}
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
|
@ -2809,8 +2826,7 @@ func TestChannelArbitratorAnchors(t *testing.T) {
|
||||||
chanArb.UpdateContractSignals(signals)
|
chanArb.UpdateContractSignals(signals)
|
||||||
|
|
||||||
// Set current block height.
|
// Set current block height.
|
||||||
heightHint := uint32(1000)
|
beat = newBeatFromHeight(int32(heightHint))
|
||||||
beat := newBeatFromHeight(int32(heightHint))
|
|
||||||
chanArbCtx.chanArb.BlockbeatChan <- beat
|
chanArbCtx.chanArb.BlockbeatChan <- beat
|
||||||
|
|
||||||
htlcAmt := lnwire.MilliSatoshi(1_000_000)
|
htlcAmt := lnwire.MilliSatoshi(1_000_000)
|
||||||
|
@ -3089,7 +3105,8 @@ func TestChannelArbitratorStartForceCloseFail(t *testing.T) {
|
||||||
return test.broadcastErr
|
return test.broadcastErr
|
||||||
}
|
}
|
||||||
|
|
||||||
err = chanArb.Start(nil)
|
beat := newBeatFromHeight(0)
|
||||||
|
err = chanArb.Start(nil, beat)
|
||||||
|
|
||||||
if !test.expectedStartup {
|
if !test.expectedStartup {
|
||||||
require.ErrorIs(t, err, test.broadcastErr)
|
require.ErrorIs(t, err, test.broadcastErr)
|
||||||
|
|
Loading…
Add table
Reference in a new issue