Skip to content

Commit bb636a6

Browse files
author
Jim Posen
committed
Expose some fields and methods publicly on Batch struct.
1 parent 69fa8b4 commit bb636a6

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
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 {

replaylog.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,31 @@ func (rl *MemoryReplayLog) PutBatch(batch *Batch) (*ReplaySet, error) {
155155

156156
// Return the result when the batch was first processed to provide
157157
// idempotence.
158-
replays, exists := rl.batches[string(batch.id)]
158+
replays, exists := rl.batches[string(batch.ID)]
159159

160160
if !exists {
161161
replays = NewReplaySet()
162-
for seqNum, entry := range batch.entries {
163-
err := rl.Put(&entry.hashPrefix, entry.cltv)
162+
err := batch.ForEach(func(seqNum uint16, hashPrefix *HashPrefix, cltv uint32) error {
163+
err := rl.Put(hashPrefix, cltv)
164164
if err == ErrReplayedPacket {
165165
replays.Add(seqNum)
166-
continue
166+
return nil
167167
}
168168

169-
// This would be bad because we have already updated the entries
169+
// An error would be bad because we have already updated the entries
170170
// map, but no errors other than ErrReplayedPacket should occur.
171-
if err != nil {
172-
return nil, err
173-
}
171+
return err
172+
})
173+
if err != nil {
174+
return nil, err
174175
}
175176

176-
replays.Merge(batch.replaySet)
177-
rl.batches[string(batch.id)] = replays
177+
replays.Merge(batch.ReplaySet)
178+
rl.batches[string(batch.ID)] = replays
178179
}
179180

180-
batch.replaySet = replays
181-
batch.isCommitted = true
181+
batch.ReplaySet = replays
182+
batch.IsCommitted = true
182183

183184
return replays, nil
184185
}

sphinx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,8 @@ func (t *Tx) ProcessOnionPacket(seqNum uint16, onionPkt *OnionPacket,
889889
// Commit writes this transaction's batch of sphinx packets to the replay log,
890890
// performing a final check against the log for replays.
891891
func (t *Tx) Commit() ([]ProcessedPacket, *ReplaySet, error) {
892-
if t.batch.isCommitted {
893-
return t.packets, t.batch.replaySet, nil
892+
if t.batch.IsCommitted {
893+
return t.packets, t.batch.ReplaySet, nil
894894
}
895895

896896
rs, err := t.router.log.PutBatch(t.batch)

0 commit comments

Comments
 (0)