chanbackup: create new Single version for tweakless commitment chans
In this commit, we create a new Single version for channels that use the tweakless commitment scheme. When recovering from an SCB into an open channel shell, we'll now check this field and use it to determine the proper channel type. Otherwise, we may attempt to sweep the on chain funds using the commitment point, when it goes directly to our key, or the other way around.
This commit is contained in:
parent
6d97bcbacd
commit
c6ee42d3e2
@ -21,11 +21,16 @@ import (
|
|||||||
type SingleBackupVersion byte
|
type SingleBackupVersion byte
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// DefaultSingleVersion is the defautl version of the single channel
|
// DefaultSingleVersion is the default version of the single channel
|
||||||
// backup. The seralized version of this static channel backup is
|
// backup. The serialized version of this static channel backup is
|
||||||
// simply: version || SCB. Where SCB is the known format of the
|
// simply: version || SCB. Where SCB is the known format of the
|
||||||
// version.
|
// version.
|
||||||
DefaultSingleVersion = 0
|
DefaultSingleVersion = 0
|
||||||
|
|
||||||
|
// TweaklessCommitVersion is the second SCB version. This version
|
||||||
|
// implicitly denotes that this channel uses the new tweakless commit
|
||||||
|
// format.
|
||||||
|
TweaklessCommitVersion = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// Single is a static description of an existing channel that can be used for
|
// Single is a static description of an existing channel that can be used for
|
||||||
@ -121,8 +126,7 @@ func NewSingle(channel *channeldb.OpenChannel,
|
|||||||
// key.
|
// key.
|
||||||
_, shaChainPoint := btcec.PrivKeyFromBytes(btcec.S256(), b.Bytes())
|
_, shaChainPoint := btcec.PrivKeyFromBytes(btcec.S256(), b.Bytes())
|
||||||
|
|
||||||
return Single{
|
single := Single{
|
||||||
Version: DefaultSingleVersion,
|
|
||||||
IsInitiator: channel.IsInitiator,
|
IsInitiator: channel.IsInitiator,
|
||||||
ChainHash: channel.ChainHash,
|
ChainHash: channel.ChainHash,
|
||||||
FundingOutpoint: channel.FundingOutpoint,
|
FundingOutpoint: channel.FundingOutpoint,
|
||||||
@ -139,6 +143,14 @@ func NewSingle(channel *channeldb.OpenChannel,
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if channel.ChanType.IsTweakless() {
|
||||||
|
single.Version = TweaklessCommitVersion
|
||||||
|
} else {
|
||||||
|
single.Version = DefaultSingleVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
return single
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize attempts to write out the serialized version of the target
|
// Serialize attempts to write out the serialized version of the target
|
||||||
@ -148,6 +160,7 @@ func (s *Single) Serialize(w io.Writer) error {
|
|||||||
// we're aware of.
|
// we're aware of.
|
||||||
switch s.Version {
|
switch s.Version {
|
||||||
case DefaultSingleVersion:
|
case DefaultSingleVersion:
|
||||||
|
case TweaklessCommitVersion:
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unable to serialize w/ unknown "+
|
return fmt.Errorf("unable to serialize w/ unknown "+
|
||||||
"version: %v", s.Version)
|
"version: %v", s.Version)
|
||||||
@ -305,6 +318,7 @@ func (s *Single) Deserialize(r io.Reader) error {
|
|||||||
|
|
||||||
switch s.Version {
|
switch s.Version {
|
||||||
case DefaultSingleVersion:
|
case DefaultSingleVersion:
|
||||||
|
case TweaklessCommitVersion:
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unable to de-serialize w/ unknown "+
|
return fmt.Errorf("unable to de-serialize w/ unknown "+
|
||||||
"version: %v", s.Version)
|
"version: %v", s.Version)
|
||||||
|
@ -124,8 +124,14 @@ func genRandomOpenChannelShell() (*channeldb.OpenChannel, error) {
|
|||||||
isInitiator = true
|
isInitiator = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chanType := channeldb.SingleFunder
|
||||||
|
if rand.Int63()%2 == 0 {
|
||||||
|
chanType = channeldb.SingleFunderTweakless
|
||||||
|
}
|
||||||
|
|
||||||
return &channeldb.OpenChannel{
|
return &channeldb.OpenChannel{
|
||||||
ChainHash: chainHash,
|
ChainHash: chainHash,
|
||||||
|
ChanType: chanType,
|
||||||
IsInitiator: isInitiator,
|
IsInitiator: isInitiator,
|
||||||
FundingOutpoint: chanPoint,
|
FundingOutpoint: chanPoint,
|
||||||
ShortChannelID: lnwire.NewShortChanIDFromInt(
|
ShortChannelID: lnwire.NewShortChanIDFromInt(
|
||||||
@ -223,6 +229,12 @@ func TestSinglePackUnpack(t *testing.T) {
|
|||||||
valid: true,
|
valid: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// The new tweakless version, should pack/unpack with no problem.
|
||||||
|
{
|
||||||
|
version: TweaklessCommitVersion,
|
||||||
|
valid: true,
|
||||||
|
},
|
||||||
|
|
||||||
// A non-default version, atm this should result in a failure.
|
// A non-default version, atm this should result in a failure.
|
||||||
{
|
{
|
||||||
version: 99,
|
version: 99,
|
||||||
@ -274,7 +286,7 @@ func TestSinglePackUnpack(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rawBytes := rawSingle.Bytes()
|
rawBytes := rawSingle.Bytes()
|
||||||
rawBytes[0] ^= 1
|
rawBytes[0] ^= 5
|
||||||
|
|
||||||
newReader := bytes.NewReader(rawBytes)
|
newReader := bytes.NewReader(rawBytes)
|
||||||
err = unpackedSingle.Deserialize(newReader)
|
err = unpackedSingle.Deserialize(newReader)
|
||||||
|
@ -83,9 +83,23 @@ func (c *chanDBRestorer) openChannelShell(backup chanbackup.Single) (
|
|||||||
return nil, fmt.Errorf("unable to derive htlc key: %v", err)
|
return nil, fmt.Errorf("unable to derive htlc key: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var chanType channeldb.ChannelType
|
||||||
|
switch backup.Version {
|
||||||
|
|
||||||
|
case chanbackup.DefaultSingleVersion:
|
||||||
|
chanType = channeldb.SingleFunder
|
||||||
|
|
||||||
|
case chanbackup.TweaklessCommitVersion:
|
||||||
|
chanType = channeldb.SingleFunderTweakless
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unknown Single version: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
chanShell := channeldb.ChannelShell{
|
chanShell := channeldb.ChannelShell{
|
||||||
NodeAddrs: backup.Addresses,
|
NodeAddrs: backup.Addresses,
|
||||||
Chan: &channeldb.OpenChannel{
|
Chan: &channeldb.OpenChannel{
|
||||||
|
ChanType: chanType,
|
||||||
ChainHash: backup.ChainHash,
|
ChainHash: backup.ChainHash,
|
||||||
IsInitiator: backup.IsInitiator,
|
IsInitiator: backup.IsInitiator,
|
||||||
Capacity: backup.Capacity,
|
Capacity: backup.Capacity,
|
||||||
|
Loading…
Reference in New Issue
Block a user