mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-23 22:46:40 +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)
|
||||
}
|
||||
|
||||
if err := arbitrator.Start(startState); err != nil {
|
||||
if err := arbitrator.Start(startState, c.beat); err != nil {
|
||||
stopAndLog()
|
||||
return err
|
||||
}
|
||||
|
@ -1211,7 +1211,7 @@ func (c *ChainArbitrator) WatchNewChannel(newChan *channeldb.OpenChannel) error
|
|||
// arbitrators, then launch it.
|
||||
c.activeChannels[chanPoint] = channelArb
|
||||
|
||||
if err := channelArb.Start(nil); err != nil {
|
||||
if err := channelArb.Start(nil, c.beat); err != nil {
|
||||
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.
|
||||
// If takes a start state, which will be looked up on disk if it is not
|
||||
// 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) {
|
||||
return nil
|
||||
}
|
||||
|
@ -484,10 +486,8 @@ func (c *ChannelArbitrator) Start(state *chanArbStartState) error {
|
|||
// Set our state from our starting state.
|
||||
c.state = state.currentState
|
||||
|
||||
_, bestHeight, err := c.cfg.ChainIO.GetBestBlock()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Get the starting height.
|
||||
bestHeight := beat.Height()
|
||||
|
||||
c.wg.Add(1)
|
||||
go c.channelAttendant(bestHeight, state.commitSet)
|
||||
|
|
|
@ -296,7 +296,8 @@ func (c *chanArbTestCtx) Restart(restartClosure func(*chanArbTestCtx)) (*chanArb
|
|||
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
|
||||
}
|
||||
|
||||
|
@ -523,7 +524,8 @@ func TestChannelArbitratorCooperativeClose(t *testing.T) {
|
|||
chanArbCtx, err := createTestChannelArbitrator(t, log)
|
||||
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.Cleanup(func() {
|
||||
|
@ -581,7 +583,8 @@ func TestChannelArbitratorRemoteForceClose(t *testing.T) {
|
|||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||
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)
|
||||
}
|
||||
defer chanArb.Stop()
|
||||
|
@ -634,7 +637,8 @@ func TestChannelArbitratorLocalForceClose(t *testing.T) {
|
|||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||
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)
|
||||
}
|
||||
defer chanArb.Stop()
|
||||
|
@ -746,7 +750,8 @@ func TestChannelArbitratorBreachClose(t *testing.T) {
|
|||
chanArb.cfg.PreimageDB = newMockWitnessBeacon()
|
||||
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.Cleanup(func() {
|
||||
|
@ -873,7 +878,8 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) {
|
|||
chanArb.cfg.PreimageDB = newMockWitnessBeacon()
|
||||
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)
|
||||
}
|
||||
defer chanArb.Stop()
|
||||
|
@ -1163,7 +1169,8 @@ func TestChannelArbitratorLocalForceCloseRemoteConfirmed(t *testing.T) {
|
|||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||
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)
|
||||
}
|
||||
defer chanArb.Stop()
|
||||
|
@ -1270,7 +1277,8 @@ func TestChannelArbitratorLocalForceDoubleSpend(t *testing.T) {
|
|||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||
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)
|
||||
}
|
||||
defer chanArb.Stop()
|
||||
|
@ -1376,7 +1384,8 @@ func TestChannelArbitratorPersistence(t *testing.T) {
|
|||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -1494,7 +1503,8 @@ func TestChannelArbitratorForceCloseBreachedChannel(t *testing.T) {
|
|||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -1681,7 +1691,8 @@ func TestChannelArbitratorCommitFailure(t *testing.T) {
|
|||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -1765,7 +1776,8 @@ func TestChannelArbitratorEmptyResolutions(t *testing.T) {
|
|||
chanArb.cfg.ClosingHeight = 100
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -1795,7 +1807,8 @@ func TestChannelArbitratorAlreadyForceClosed(t *testing.T) {
|
|||
chanArbCtx, err := createTestChannelArbitrator(t, log)
|
||||
require.NoError(t, err, "unable to create ChannelArbitrator")
|
||||
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)
|
||||
}
|
||||
defer chanArb.Stop()
|
||||
|
@ -1893,9 +1906,10 @@ func TestChannelArbitratorDanglingCommitForceClose(t *testing.T) {
|
|||
t.Fatalf("unable to create ChannelArbitrator: %v", err)
|
||||
}
|
||||
chanArb := chanArbCtx.chanArb
|
||||
if err := chanArb.Start(nil); err != nil {
|
||||
t.Fatalf("unable to start ChannelArbitrator: %v", err)
|
||||
}
|
||||
beat := newBeatFromHeight(0)
|
||||
err = chanArb.Start(nil, beat)
|
||||
require.NoError(t, err)
|
||||
|
||||
defer chanArb.Stop()
|
||||
|
||||
// Now that our channel arb has started, we'll set up
|
||||
|
@ -2089,7 +2103,8 @@ func TestChannelArbitratorPendingExpiredHTLC(t *testing.T) {
|
|||
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.Cleanup(func() {
|
||||
|
@ -2123,7 +2138,7 @@ func TestChannelArbitratorPendingExpiredHTLC(t *testing.T) {
|
|||
// We will advance the uptime to 10 seconds which should be still within
|
||||
// the grace period and should not trigger going to chain.
|
||||
testClock.SetTime(startTime.Add(time.Second * 10))
|
||||
beat := newBeatFromHeight(5)
|
||||
beat = newBeatFromHeight(5)
|
||||
chanArbCtx.chanArb.BlockbeatChan <- beat
|
||||
chanArbCtx.AssertState(StateDefault)
|
||||
|
||||
|
@ -2244,8 +2259,8 @@ func TestRemoteCloseInitiator(t *testing.T) {
|
|||
"ChannelArbitrator: %v", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
@ -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.Cleanup(func() {
|
||||
|
@ -2809,8 +2826,7 @@ func TestChannelArbitratorAnchors(t *testing.T) {
|
|||
chanArb.UpdateContractSignals(signals)
|
||||
|
||||
// Set current block height.
|
||||
heightHint := uint32(1000)
|
||||
beat := newBeatFromHeight(int32(heightHint))
|
||||
beat = newBeatFromHeight(int32(heightHint))
|
||||
chanArbCtx.chanArb.BlockbeatChan <- beat
|
||||
|
||||
htlcAmt := lnwire.MilliSatoshi(1_000_000)
|
||||
|
@ -3089,7 +3105,8 @@ func TestChannelArbitratorStartForceCloseFail(t *testing.T) {
|
|||
return test.broadcastErr
|
||||
}
|
||||
|
||||
err = chanArb.Start(nil)
|
||||
beat := newBeatFromHeight(0)
|
||||
err = chanArb.Start(nil, beat)
|
||||
|
||||
if !test.expectedStartup {
|
||||
require.ErrorIs(t, err, test.broadcastErr)
|
||||
|
|
Loading…
Add table
Reference in a new issue