|
1 | 1 | package channeldb
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "math/rand"
|
5 | 6 | "reflect"
|
6 | 7 | "testing"
|
7 | 8 | "time"
|
8 | 9 |
|
9 | 10 | "github.com/davecgh/go-spew/spew"
|
| 11 | + "github.com/lightningnetwork/lnd/fn/v2" |
| 12 | + "github.com/lightningnetwork/lnd/kvdb" |
10 | 13 | "github.com/lightningnetwork/lnd/lnwire"
|
11 | 14 | "github.com/stretchr/testify/assert"
|
12 | 15 | "github.com/stretchr/testify/require"
|
@@ -41,6 +44,8 @@ func TestForwardingLogBasicStorageAndQuery(t *testing.T) {
|
41 | 44 | OutgoingChanID: lnwire.NewShortChanIDFromInt(uint64(rand.Int63())),
|
42 | 45 | AmtIn: lnwire.MilliSatoshi(rand.Int63()),
|
43 | 46 | AmtOut: lnwire.MilliSatoshi(rand.Int63()),
|
| 47 | + IncomingHtlcID: fn.Some(uint64(i)), |
| 48 | + OutgoingHtlcID: fn.Some(uint64(i)), |
44 | 49 | }
|
45 | 50 |
|
46 | 51 | timestamp = timestamp.Add(time.Minute * 10)
|
@@ -109,6 +114,8 @@ func TestForwardingLogQueryOptions(t *testing.T) {
|
109 | 114 | OutgoingChanID: lnwire.NewShortChanIDFromInt(uint64(rand.Int63())),
|
110 | 115 | AmtIn: lnwire.MilliSatoshi(rand.Int63()),
|
111 | 116 | AmtOut: lnwire.MilliSatoshi(rand.Int63()),
|
| 117 | + IncomingHtlcID: fn.Some(uint64(i)), |
| 118 | + OutgoingHtlcID: fn.Some(uint64(i)), |
112 | 119 | }
|
113 | 120 |
|
114 | 121 | endTime = endTime.Add(time.Minute * 10)
|
@@ -208,6 +215,8 @@ func TestForwardingLogQueryLimit(t *testing.T) {
|
208 | 215 | OutgoingChanID: lnwire.NewShortChanIDFromInt(uint64(rand.Int63())),
|
209 | 216 | AmtIn: lnwire.MilliSatoshi(rand.Int63()),
|
210 | 217 | AmtOut: lnwire.MilliSatoshi(rand.Int63()),
|
| 218 | + IncomingHtlcID: fn.Some(uint64(i)), |
| 219 | + OutgoingHtlcID: fn.Some(uint64(i)), |
211 | 220 | }
|
212 | 221 |
|
213 | 222 | endTime = endTime.Add(time.Minute * 10)
|
@@ -317,6 +326,8 @@ func TestForwardingLogStoreEvent(t *testing.T) {
|
317 | 326 | OutgoingChanID: lnwire.NewShortChanIDFromInt(uint64(rand.Int63())),
|
318 | 327 | AmtIn: lnwire.MilliSatoshi(rand.Int63()),
|
319 | 328 | AmtOut: lnwire.MilliSatoshi(rand.Int63()),
|
| 329 | + IncomingHtlcID: fn.Some(uint64(i)), |
| 330 | + OutgoingHtlcID: fn.Some(uint64(i)), |
320 | 331 | }
|
321 | 332 | }
|
322 | 333 |
|
@@ -360,3 +371,107 @@ func TestForwardingLogStoreEvent(t *testing.T) {
|
360 | 371 | }
|
361 | 372 | }
|
362 | 373 | }
|
| 374 | + |
| 375 | +// TestForwardingLogDecodeForwardingEvent tests that we're able to decode |
| 376 | +// forwarding events that don't have the new incoming and outgoing htlc |
| 377 | +// indices. |
| 378 | +func TestForwardingLogDecodeForwardingEvent(t *testing.T) { |
| 379 | + t.Parallel() |
| 380 | + |
| 381 | + // First, we'll set up a test database, and use that to instantiate the |
| 382 | + // forwarding event log that we'll be using for the duration of the |
| 383 | + // test. |
| 384 | + db, err := MakeTestDB(t) |
| 385 | + require.NoError(t, err) |
| 386 | + |
| 387 | + log := ForwardingLog{ |
| 388 | + db: db, |
| 389 | + } |
| 390 | + |
| 391 | + initialTime := time.Unix(1234, 0) |
| 392 | + endTime := time.Unix(1234, 0) |
| 393 | + |
| 394 | + // We'll create forwarding events that don't have the incoming and |
| 395 | + // outgoing htlc indices. |
| 396 | + numEvents := 10 |
| 397 | + events := make([]ForwardingEvent, numEvents) |
| 398 | + for i := range numEvents { |
| 399 | + events[i] = ForwardingEvent{ |
| 400 | + Timestamp: endTime, |
| 401 | + IncomingChanID: lnwire.NewShortChanIDFromInt( |
| 402 | + uint64(rand.Int63()), |
| 403 | + ), |
| 404 | + OutgoingChanID: lnwire.NewShortChanIDFromInt( |
| 405 | + uint64(rand.Int63()), |
| 406 | + ), |
| 407 | + AmtIn: lnwire.MilliSatoshi(rand.Int63()), |
| 408 | + AmtOut: lnwire.MilliSatoshi(rand.Int63()), |
| 409 | + } |
| 410 | + |
| 411 | + endTime = endTime.Add(time.Minute * 10) |
| 412 | + } |
| 413 | + |
| 414 | + // Now that all of our events are constructed, we'll add them to the |
| 415 | + // database. |
| 416 | + err = writeOldFormatEvents(db, events) |
| 417 | + require.NoError(t, err) |
| 418 | + |
| 419 | + // With all of our events added, we'll now query for them and ensure |
| 420 | + // that the incoming and outgoing htlc indices are set to 0 (default |
| 421 | + // value) for all events. |
| 422 | + eventQuery := ForwardingEventQuery{ |
| 423 | + StartTime: initialTime, |
| 424 | + EndTime: endTime, |
| 425 | + IndexOffset: 0, |
| 426 | + NumMaxEvents: uint32(numEvents * 3), |
| 427 | + } |
| 428 | + timeSlice, err := log.Query(eventQuery) |
| 429 | + require.NoError(t, err) |
| 430 | + require.Equal(t, numEvents, len(timeSlice.ForwardingEvents)) |
| 431 | + |
| 432 | + for _, event := range timeSlice.ForwardingEvents { |
| 433 | + require.Equal(t, fn.None[uint64](), event.IncomingHtlcID) |
| 434 | + require.Equal(t, fn.None[uint64](), event.OutgoingHtlcID) |
| 435 | + } |
| 436 | +} |
| 437 | + |
| 438 | +// writeOldFormatEvents writes forwarding events to the database in the old |
| 439 | +// format (without incoming and outgoing htlc indices). This is used to test |
| 440 | +// backward compatibility. |
| 441 | +func writeOldFormatEvents(db *DB, events []ForwardingEvent) error { |
| 442 | + return kvdb.Batch(db.Backend, func(tx kvdb.RwTx) error { |
| 443 | + bucket, err := tx.CreateTopLevelBucket(forwardingLogBucket) |
| 444 | + if err != nil { |
| 445 | + return err |
| 446 | + } |
| 447 | + |
| 448 | + for _, event := range events { |
| 449 | + var timestamp [8]byte |
| 450 | + byteOrder.PutUint64(timestamp[:], uint64( |
| 451 | + event.Timestamp.UnixNano(), |
| 452 | + )) |
| 453 | + |
| 454 | + // Use the old event size (32 bytes) for writing old |
| 455 | + // format events. |
| 456 | + var eventBytes [32]byte |
| 457 | + eventBuf := bytes.NewBuffer(eventBytes[0:0:32]) |
| 458 | + |
| 459 | + // Write only the original fields without incoming and |
| 460 | + // outgoing htlc indices. |
| 461 | + if err := WriteElements( |
| 462 | + eventBuf, event.IncomingChanID, |
| 463 | + event.OutgoingChanID, event.AmtIn, event.AmtOut, |
| 464 | + ); err != nil { |
| 465 | + return err |
| 466 | + } |
| 467 | + |
| 468 | + if err := bucket.Put( |
| 469 | + timestamp[:], eventBuf.Bytes(), |
| 470 | + ); err != nil { |
| 471 | + return err |
| 472 | + } |
| 473 | + } |
| 474 | + |
| 475 | + return nil |
| 476 | + }) |
| 477 | +} |
0 commit comments