channeldb: convert RevocationLog to use RecordT

This commit is contained in:
Olaoluwa Osuntokun authored and Oliver Gugger committed 2024-08-28 13:22:48 +02:00
1 parent 1b1e7a6168
commit 61f276856a
5 files changed
+145 -113

No files matched your search

+21 -15
View File
@@ -2275,7 +2275,7 @@ func createBreachRetribution(revokedLog *channeldb.RevocationLog,
htlcRetributions := make([]HtlcRetribution, len(revokedLog.HTLCEntries))
for i, htlc := range revokedLog.HTLCEntries {
hr, err := createHtlcRetribution(
chanState, keyRing, commitHash,
chanState, keyRing, commitHash.Val,
commitmentSecret, leaseExpiry, htlc,
)
if err != nil {
@@ -2288,10 +2288,10 @@ func createBreachRetribution(revokedLog *channeldb.RevocationLog,
// Construct the our outpoint.
ourOutpoint := wire.OutPoint{
Hash: commitHash,
Hash: commitHash.Val,
}
if revokedLog.OurOutputIndex != channeldb.OutputIndexEmpty {
ourOutpoint.Index = uint32(revokedLog.OurOutputIndex)
if revokedLog.OurOutputIndex.Val != channeldb.OutputIndexEmpty {
ourOutpoint.Index = uint32(revokedLog.OurOutputIndex.Val)
// If the spend transaction is provided, then we use it to get
// the value of our output.
@@ -2314,26 +2314,29 @@ func createBreachRetribution(revokedLog *channeldb.RevocationLog,
// contains our output amount. Due to a previous
// migration, this field may be empty in which case an
// error will be returned.
if revokedLog.OurBalance == nil {
return nil, 0, 0, ErrRevLogDataMissing
b, err := revokedLog.OurBalance.ValOpt().UnwrapOrErr(
ErrRevLogDataMissing,
)
if err != nil {
return nil, 0, 0, err
}
ourAmt = int64(revokedLog.OurBalance.ToSatoshis())
ourAmt = int64(b.Int().ToSatoshis())
}
}
// Construct the their outpoint.
theirOutpoint := wire.OutPoint{
Hash: commitHash,
Hash: commitHash.Val,
}
if revokedLog.TheirOutputIndex != channeldb.OutputIndexEmpty {
theirOutpoint.Index = uint32(revokedLog.TheirOutputIndex)
if revokedLog.TheirOutputIndex.Val != channeldb.OutputIndexEmpty {
theirOutpoint.Index = uint32(revokedLog.TheirOutputIndex.Val)
// If the spend transaction is provided, then we use it to get
// the value of the remote parties' output.
if spendTx != nil {
// Sanity check that TheirOutputIndex is within range.
if int(revokedLog.TheirOutputIndex) >=
if int(revokedLog.TheirOutputIndex.Val) >=
len(spendTx.TxOut) {
return nil, 0, 0, fmt.Errorf("%w: theirs=%v, "+
@@ -2351,16 +2354,19 @@ func createBreachRetribution(revokedLog *channeldb.RevocationLog,
// contains remote parties' output amount. Due to a
// previous migration, this field may be empty in which
// case an error will be returned.
if revokedLog.TheirBalance == nil {
return nil, 0, 0, ErrRevLogDataMissing
b, err := revokedLog.TheirBalance.ValOpt().UnwrapOrErr(
ErrRevLogDataMissing,
)
if err != nil {
return nil, 0, 0, err
}
theirAmt = int64(revokedLog.TheirBalance.ToSatoshis())
theirAmt = int64(b.Int().ToSatoshis())
}
}
return &BreachRetribution{
BreachTxHash: commitHash,
BreachTxHash: commitHash.Val,
ChainHash: chanState.ChainHash,
LocalOutpoint: ourOutpoint,
RemoteOutpoint: theirOutpoint,
+15 -12
View File
@@ -10028,22 +10028,19 @@ func TestCreateBreachRetribution(t *testing.T) {
// Create a dummy revocation log.
ourAmtMsat := lnwire.MilliSatoshi(ourAmt * 1000)
theirAmtMsat := lnwire.MilliSatoshi(theirAmt * 1000)
revokedLog := channeldb.RevocationLog{
CommitTxHash: commitHash,
OurOutputIndex: uint16(localIndex),
TheirOutputIndex: uint16(remoteIndex),
HTLCEntries: []*channeldb.HTLCEntry{htlc},
TheirBalance: &theirAmtMsat,
OurBalance: &ourAmtMsat,
}
revokedLog := channeldb.NewRevocationLog(
uint16(localIndex), uint16(remoteIndex), commitHash,
fn.Some(ourAmtMsat), fn.Some(theirAmtMsat),
[]*channeldb.HTLCEntry{htlc},
)
// Create a log with an empty local output index.
revokedLogNoLocal := revokedLog
revokedLogNoLocal.OurOutputIndex = channeldb.OutputIndexEmpty
revokedLogNoLocal.OurOutputIndex.Val = channeldb.OutputIndexEmpty
// Create a log with an empty remote output index.
revokedLogNoRemote := revokedLog
revokedLogNoRemote.TheirOutputIndex = channeldb.OutputIndexEmpty
revokedLogNoRemote.TheirOutputIndex.Val = channeldb.OutputIndexEmpty
testCases := []struct {
name string
@@ -10073,14 +10070,20 @@ func TestCreateBreachRetribution(t *testing.T) {
{
name: "fail due to our index too big",
revocationLog: &channeldb.RevocationLog{
OurOutputIndex: uint16(htlcIndex + 1),
//nolint:lll
OurOutputIndex: tlv.NewPrimitiveRecord[tlv.TlvType0](
uint16(htlcIndex + 1),
),
},
expectedErr: ErrOutputIndexOutOfRange,
},
{
name: "fail due to their index too big",
revocationLog: &channeldb.RevocationLog{
TheirOutputIndex: uint16(htlcIndex + 1),
//nolint:lll
TheirOutputIndex: tlv.NewPrimitiveRecord[tlv.TlvType1](
uint16(htlcIndex + 1),
),
},
expectedErr: ErrOutputIndexOutOfRange,
},