@@ -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.
1212type 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.
4141func 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.
5454func (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.
8697type batchEntry struct {
0 commit comments