contractcourt/channel_arbitrator: remove state callback
This commit removes the state callback, and instead logs the contract resolutions directly after receiving the unilateral close event. The resolutions won't change so there's not really necessary to wait to log them, and this greatly simplifies the code.
This commit is contained in:
parent
245dae0071
commit
6df302d7f5
@ -1,7 +1,6 @@
|
|||||||
package contractcourt
|
package contractcourt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
@ -248,7 +247,7 @@ func (c *ChannelArbitrator) Start() error {
|
|||||||
// on-chain state, and our set of active contracts.
|
// on-chain state, and our set of active contracts.
|
||||||
startingState := c.state
|
startingState := c.state
|
||||||
nextState, _, err := c.advanceState(
|
nextState, _, err := c.advanceState(
|
||||||
uint32(bestHeight), chainTrigger, nil,
|
uint32(bestHeight), chainTrigger,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.cfg.BlockEpochs.Cancel()
|
c.cfg.BlockEpochs.Cancel()
|
||||||
@ -638,8 +637,7 @@ func (c *ChannelArbitrator) stateStep(triggerHeight uint32,
|
|||||||
// param is a callback that allows the caller to execute an arbitrary action
|
// param is a callback that allows the caller to execute an arbitrary action
|
||||||
// after each state transition.
|
// after each state transition.
|
||||||
func (c *ChannelArbitrator) advanceState(triggerHeight uint32,
|
func (c *ChannelArbitrator) advanceState(triggerHeight uint32,
|
||||||
trigger transitionTrigger, stateCallback func(ArbitratorState) error) (
|
trigger transitionTrigger) (ArbitratorState, *wire.MsgTx, error) {
|
||||||
ArbitratorState, *wire.MsgTx, error) {
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
priorState ArbitratorState
|
priorState ArbitratorState
|
||||||
@ -666,15 +664,6 @@ func (c *ChannelArbitrator) advanceState(triggerHeight uint32,
|
|||||||
forceCloseTx = closeTx
|
forceCloseTx = closeTx
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have a state callback, then we'll attempt to execute
|
|
||||||
// it. If the callback doesn't execute successfully, then we'll
|
|
||||||
// exit early.
|
|
||||||
if stateCallback != nil {
|
|
||||||
if err := stateCallback(nextState); err != nil {
|
|
||||||
return nextState, closeTx, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Our termination transition is a noop transition. If we get
|
// Our termination transition is a noop transition. If we get
|
||||||
// our prior state back as the next state, then we'll
|
// our prior state back as the next state, then we'll
|
||||||
// terminate.
|
// terminate.
|
||||||
@ -1322,7 +1311,7 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) {
|
|||||||
// Now that a new block has arrived, we'll attempt to
|
// Now that a new block has arrived, we'll attempt to
|
||||||
// advance our state forward.
|
// advance our state forward.
|
||||||
nextState, _, err := c.advanceState(
|
nextState, _, err := c.advanceState(
|
||||||
uint32(bestHeight), chainTrigger, nil,
|
uint32(bestHeight), chainTrigger,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("unable to advance state: %v", err)
|
log.Errorf("unable to advance state: %v", err)
|
||||||
@ -1400,31 +1389,21 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// When processing a unilateral close event, we'll
|
// When processing a unilateral close event, we'll
|
||||||
// transition directly to the ContractClosed state.
|
// transition to the ContractClosed state. We'll log
|
||||||
// When the state machine reaches that state, we'll log
|
// out the set of resolutions such that they are
|
||||||
// out the set of resolutions.
|
// available to fetch in that state.
|
||||||
stateCb := func(nextState ArbitratorState) error {
|
err := c.log.LogContractResolutions(contractRes)
|
||||||
if nextState != StateContractClosed {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err := c.log.LogContractResolutions(
|
|
||||||
contractRes,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to "+
|
log.Errorf("unable to write resolutions: %v",
|
||||||
"write resolutions: %v",
|
|
||||||
err)
|
err)
|
||||||
}
|
return
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll now advance our state machine until it reaches
|
// We'll now advance our state machine until it reaches
|
||||||
// a terminal state.
|
// a terminal state.
|
||||||
_, _, err := c.advanceState(
|
_, _, err = c.advanceState(
|
||||||
uint32(closeInfo.SpendingHeight),
|
uint32(closeInfo.SpendingHeight),
|
||||||
localCloseTrigger, stateCb,
|
localCloseTrigger,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("unable to advance state: %v", err)
|
log.Errorf("unable to advance state: %v", err)
|
||||||
@ -1457,30 +1436,21 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) {
|
|||||||
c.activeHTLCs = newHtlcSet(uniClosure.RemoteCommit.Htlcs)
|
c.activeHTLCs = newHtlcSet(uniClosure.RemoteCommit.Htlcs)
|
||||||
|
|
||||||
// When processing a unilateral close event, we'll
|
// When processing a unilateral close event, we'll
|
||||||
// transition directly to the ContractClosed state.
|
// transition to the ContractClosed state. We'll log
|
||||||
// When the state machine reaches that state, we'll log
|
// out the set of resolutions such that they are
|
||||||
// out the set of resolutions.
|
// available to fetch in that state.
|
||||||
stateCb := func(nextState ArbitratorState) error {
|
err := c.log.LogContractResolutions(contractRes)
|
||||||
if nextState != StateContractClosed {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err := c.log.LogContractResolutions(
|
|
||||||
contractRes,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to write "+
|
log.Errorf("unable to write resolutions: %v",
|
||||||
"resolutions: %v", err)
|
err)
|
||||||
}
|
return
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll now advance our state machine until it reaches
|
// We'll now advance our state machine until it reaches
|
||||||
// a terminal state.
|
// a terminal state.
|
||||||
_, _, err := c.advanceState(
|
_, _, err = c.advanceState(
|
||||||
uint32(uniClosure.SpendingHeight),
|
uint32(uniClosure.SpendingHeight),
|
||||||
remoteCloseTrigger, stateCb,
|
remoteCloseTrigger,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("unable to advance state: %v", err)
|
log.Errorf("unable to advance state: %v", err)
|
||||||
@ -1525,7 +1495,7 @@ func (c *ChannelArbitrator) channelAttendant(bestHeight int32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nextState, closeTx, err := c.advanceState(
|
nextState, closeTx, err := c.advanceState(
|
||||||
uint32(bestHeight), userTrigger, nil,
|
uint32(bestHeight), userTrigger,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("unable to advance state: %v", err)
|
log.Errorf("unable to advance state: %v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user