Skip to content

Commit e5ab5d7

Browse files
authored
Merge pull request #22 from jimpo/replay-log
Remove DecayedLog and move to lnd.
2 parents c861375 + bb636a6 commit e5ab5d7

File tree

11 files changed

+382
-869
lines changed

11 files changed

+382
-869
lines changed

batch.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ var ErrAlreadyCommitted = errors.New("cannot add to batch after committing")
1010
// the replay log. After construction is completed, it can be added to the log
1111
// using the PutBatch method.
1212
type Batch struct {
13-
// isCommitted denotes whether or not this batch has been successfully
13+
// IsCommitted denotes whether or not this batch has been successfully
1414
// written to disk.
15-
isCommitted bool
15+
IsCommitted bool
1616

17-
// id is a unique, caller chosen identifier for this batch.
18-
id []byte
17+
// ID is a unique, caller chosen identifier for this batch.
18+
ID []byte
19+
20+
// ReplaySet contains the sequence numbers of all entries that were
21+
// detected as replays. The set is finalized upon writing the batch to
22+
// disk, and merges replays detected by the replay cache and on-disk
23+
// replay log.
24+
ReplaySet *ReplaySet
1925

2026
// entries stores the set of all potential entries that might get
2127
// written to the replay log. Some entries may be skipped after
@@ -26,12 +32,6 @@ type Batch struct {
2632
// prefix of entries already added to this batch. This allows a quick
2733
// mechanism for intra-batch duplicate detection.
2834
replayCache map[HashPrefix]struct{}
29-
30-
// replaySet contains the sequence numbers of all entries that were
31-
// detected as replays. The set is finalized upon writing the batch to
32-
// disk, and merges replays detected by the replay cache and on-disk
33-
// replay log.
34-
replaySet *ReplaySet
3535
}
3636

3737
// NewBatch initializes an object for constructing a set of entries to
@@ -40,10 +40,10 @@ type Batch struct {
4040
// idempotent result.
4141
func NewBatch(id []byte) *Batch {
4242
return &Batch{
43-
id: id,
43+
ID: id,
44+
ReplaySet: NewReplaySet(),
4445
entries: make(map[uint16]batchEntry),
4546
replayCache: make(map[HashPrefix]struct{}),
46-
replaySet: NewReplaySet(),
4747
}
4848
}
4949

@@ -53,14 +53,14 @@ func NewBatch(id []byte) *Batch {
5353
// is ultimately reported via the batch's ReplaySet after committing to disk.
5454
func (b *Batch) Put(seqNum uint16, hashPrefix *HashPrefix, cltv uint32) error {
5555
// Abort if this batch was already written to disk.
56-
if b.isCommitted {
56+
if b.IsCommitted {
5757
return ErrAlreadyCommitted
5858
}
5959

6060
// Check to see if this hash prefix is already included in this batch.
6161
// If so, we will opportunistically mark this index as replayed.
6262
if _, ok := b.replayCache[*hashPrefix]; ok {
63-
b.replaySet.Add(seqNum)
63+
b.ReplaySet.Add(seqNum)
6464
return nil
6565
}
6666

@@ -81,6 +81,17 @@ func (b *Batch) Put(seqNum uint16, hashPrefix *HashPrefix, cltv uint32) error {
8181
return nil
8282
}
8383

84+
// ForEach iterates through each entry in the batch and calls the provided
85+
// function with the sequence number and entry contents as arguments.
86+
func (b *Batch) ForEach(fn func(seqNum uint16, hashPrefix *HashPrefix, cltv uint32) error) error {
87+
for seqNum, entry := range b.entries {
88+
if err := fn(seqNum, &entry.hashPrefix, entry.cltv); err != nil {
89+
return err
90+
}
91+
}
92+
return nil
93+
}
94+
8495
// batchEntry is a tuple of a secret's hash prefix and the corresponding CLTV at
8596
// which the onion blob from which the secret was derived expires.
8697
type batchEntry struct {

bench_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func BenchmarkProcessPacket(b *testing.B) {
6161
}
6262
b.ReportAllocs()
6363
path[0].log.Start()
64-
defer shutdown("0", path[0].log)
64+
defer path[0].log.Stop()
6565
b.StartTimer()
6666

6767
var (
@@ -75,12 +75,12 @@ func BenchmarkProcessPacket(b *testing.B) {
7575

7676
b.StopTimer()
7777
router := path[0]
78-
shutdown("0", router.log)
78+
router.log.Stop()
7979
path[0] = &Router{
8080
nodeID: router.nodeID,
8181
nodeAddr: router.nodeAddr,
8282
onionKey: router.onionKey,
83-
log: NewDecayedLog("0", nil),
83+
log: NewMemoryReplayLog(),
8484
}
8585
path[0].log.Start()
8686
b.StartTimer()

cmd/main.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ func main() {
2222

2323
assocData := bytes.Repeat([]byte{'B'}, 32)
2424

25-
tempDir, err := ioutil.TempDir("", "onion-test")
26-
defer os.Remove(tempDir)
27-
if err != nil {
28-
panic(err)
29-
}
30-
3125
if len(args) == 1 {
3226
fmt.Printf("Usage: %s (generate|decode) <private-keys>\n", args[0])
3327
} else if args[1] == "generate" {
@@ -85,7 +79,8 @@ func main() {
8579
}
8680

8781
privkey, _ := btcec.PrivKeyFromBytes(btcec.S256(), binKey)
88-
s := sphinx.NewRouter(tempDir, privkey, &chaincfg.TestNet3Params, nil)
82+
s := sphinx.NewRouter(privkey, &chaincfg.TestNet3Params,
83+
sphinx.NewMemoryReplayLog())
8984

9085
var packet sphinx.OnionPacket
9186
err = packet.Decode(bytes.NewBuffer(binMsg))

0 commit comments

Comments
 (0)