contractcourt: abstract call to GetStateNumHint within the closeObserver

In this commit, we abstract the call to `GetStateNumHint` within the
`closeObserver` method to a function closure in the primary config. This
allows us to feed in an arbitrary broadcast state number within unit
tests.
This commit is contained in:
Olaoluwa Osuntokun 2019-03-12 19:19:24 -07:00
parent 4645fc0c95
commit 48532dc9f6
No known key found for this signature in database
GPG Key ID: CE58F7F8E20FD9A2
2 changed files with 11 additions and 3 deletions

@ -375,6 +375,7 @@ func (c *ChainArbitrator) Start() error {
contractBreach: func(retInfo *lnwallet.BreachRetribution) error { contractBreach: func(retInfo *lnwallet.BreachRetribution) error {
return c.cfg.ContractBreach(chanPoint, retInfo) return c.cfg.ContractBreach(chanPoint, retInfo)
}, },
extractStateNumHint: lnwallet.GetStateNumHint,
}, },
) )
if err != nil { if err != nil {
@ -710,6 +711,7 @@ func (c *ChainArbitrator) WatchNewChannel(newChan *channeldb.OpenChannel) error
contractBreach: func(retInfo *lnwallet.BreachRetribution) error { contractBreach: func(retInfo *lnwallet.BreachRetribution) error {
return c.cfg.ContractBreach(chanPoint, retInfo) return c.cfg.ContractBreach(chanPoint, retInfo)
}, },
extractStateNumHint: lnwallet.GetStateNumHint,
}, },
) )
if err != nil { if err != nil {

@ -105,6 +105,11 @@ type chainWatcherConfig struct {
// isOurAddr is a function that returns true if the passed address is // isOurAddr is a function that returns true if the passed address is
// known to us. // known to us.
isOurAddr func(btcutil.Address) bool isOurAddr func(btcutil.Address) bool
// extractStateNumHint extracts the encoded state hint using the passed
// obfuscater. This is used by the chain watcher to identify which
// state was broadcast and confirmed on-chain.
extractStateNumHint func(*wire.MsgTx, [lnwallet.StateHintSize]byte) uint64
} }
// chainWatcher is a system that's assigned to every active channel. The duty // chainWatcher is a system that's assigned to every active channel. The duty
@ -350,10 +355,9 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
"ChannelPoint(%v) ", c.cfg.chanState.FundingOutpoint) "ChannelPoint(%v) ", c.cfg.chanState.FundingOutpoint)
// Decode the state hint encoded within the commitment // Decode the state hint encoded within the commitment
// transaction to determine if this is a revoked state // transaction to determine if this is a revoked state or not.
// or not.
obfuscator := c.stateHintObfuscator obfuscator := c.stateHintObfuscator
broadcastStateNum := lnwallet.GetStateNumHint( broadcastStateNum := c.cfg.extractStateNumHint(
commitTxBroadcast, obfuscator, commitTxBroadcast, obfuscator,
) )
remoteStateNum := remoteCommit.CommitHeight remoteStateNum := remoteCommit.CommitHeight
@ -418,6 +422,7 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
// point, there's not much we can do other than wait // point, there's not much we can do other than wait
// for us to retrieve it. We will attempt to retrieve // for us to retrieve it. We will attempt to retrieve
// it from the peer each time we connect to it. // it from the peer each time we connect to it.
//
// TODO(halseth): actively initiate re-connection to // TODO(halseth): actively initiate re-connection to
// the peer? // the peer?
var commitPoint *btcec.PublicKey var commitPoint *btcec.PublicKey
@ -458,6 +463,7 @@ func (c *chainWatcher) closeObserver(spendNtfn *chainntnfs.SpendEvent) {
// state, we'll just pass an empty commitment. Note // state, we'll just pass an empty commitment. Note
// that this means we won't be able to recover any HTLC // that this means we won't be able to recover any HTLC
// funds. // funds.
//
// TODO(halseth): can we try to recover some HTLCs? // TODO(halseth): can we try to recover some HTLCs?
err = c.dispatchRemoteForceClose( err = c.dispatchRemoteForceClose(
commitSpend, channeldb.ChannelCommitment{}, commitSpend, channeldb.ChannelCommitment{},