Skip to content

Commit 38e1504

Browse files
author
Jim Posen
committed
Move ReplayLog interface definition to its own file.
1 parent eeb3c0a commit 38e1504

File tree

4 files changed

+66
-59
lines changed

4 files changed

+66
-59
lines changed

decayedlog.go

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package sphinx
22

33
import (
44
"bytes"
5-
"crypto/sha256"
65
"encoding/binary"
76
"errors"
87
"fmt"
@@ -20,15 +19,10 @@ const (
2019

2120
// dbPermissions sets the database permissions to user write-and-readable.
2221
dbPermissions = 0600
23-
24-
// sharedHashSize is the size in bytes of the keys we will be storing
25-
// in the DecayedLog. It represents the first 20 bytes of a truncated
26-
// sha-256 hash of a secret generated by ECDH.
27-
sharedHashSize = 20
2822
)
2923

3024
var (
31-
// sharedHashBucket is a bucket which houses the first sharedHashSize
25+
// sharedHashBucket is a bucket which houses the first HashPrefixSize
3226
// bytes of a received HTLC's hashed shared secret as the key and the HTLC's
3327
// CLTV expiry as the value.
3428
sharedHashBucket = []byte("shared-hash")
@@ -39,10 +33,6 @@ var (
3933
batchReplayBucket = []byte("batch-replay")
4034
)
4135

42-
// HashPrefix is a statically size, 20-byte array containing the prefix
43-
// of a Hash256, and is used to detect duplicate sphinx packets.
44-
type HashPrefix [sharedHashSize]byte
45-
4636
var (
4737
// ErrDecayedLogInit is used to indicate a decayed log failed to create
4838
// the proper bucketing structure on startup.
@@ -53,37 +43,8 @@ var (
5343
ErrDecayedLogCorrupted = errors.New("decayed log structure corrupted")
5444
)
5545

56-
// ReplayLog is an interface that defines a new on-disk data structure that
57-
// contains a persistent log to enable strong replay protection. The interface
58-
// is general to allow implementations near-complete autonomy. All of these
59-
// calls should be safe for concurrent access.
60-
type ReplayLog interface {
61-
// Start starts up the on-disk persistent log. It returns an error if
62-
// one occurs.
63-
Start() error
64-
65-
// Stop safely stops the on-disk persistent log.
66-
Stop() error
67-
68-
// Get retrieves an entry from the persistent log given its hash prefix. It
69-
// returns the value stored and an error if one occurs. Returns
70-
// ErrLogEntryNotFound if hash prefix is not in the log.
71-
Get(*HashPrefix) (uint32, error)
72-
73-
// Put stores an entry into the persistent log given a []byte and an
74-
// accompanying purposefully general type. It returns an error if the
75-
// provided hash prefix already exists in the log.
76-
Put(*HashPrefix, uint32) error
77-
78-
// PutBatch stores
79-
PutBatch(*Batch) (*ReplaySet, error)
80-
81-
// Delete deletes an entry from the persistent log given its hash prefix.
82-
Delete(*HashPrefix) error
83-
}
84-
8546
// DecayedLog implements the PersistLog interface. It stores the first
86-
// sharedHashSize bytes of a sha256-hashed shared secret along with a node's
47+
// HashPrefixSize bytes of a sha256-hashed shared secret along with a node's
8748
// CLTV value. It is a decaying log meaning there will be a garbage collector
8849
// to collect entries which are expired according to their stored CLTV value
8950
// and the current block height. DecayedLog wraps boltdb for simplicity and
@@ -282,20 +243,6 @@ func (d *DecayedLog) gcExpiredHashes(height uint32) (uint32, error) {
282243
return numExpiredHashes, nil
283244
}
284245

285-
// hashSharedSecret Sha-256 hashes the shared secret and returns the first
286-
// sharedHashSize bytes of the hash.
287-
func hashSharedSecret(sharedSecret *Hash256) HashPrefix {
288-
// Sha256 hash of sharedSecret
289-
h := sha256.New()
290-
h.Write(sharedSecret[:])
291-
292-
var sharedHash HashPrefix
293-
294-
// Copy bytes to sharedHash
295-
copy(sharedHash[:], h.Sum(nil))
296-
return sharedHash
297-
}
298-
299246
// Delete removes a <shared secret hash, CLTV> key-pair from the
300247
// sharedHashBucket.
301248
func (d *DecayedLog) Delete(hash *HashPrefix) error {

decayedlog_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (m *mockNotifier) Stop() error {
5858
func startup(notifier bool) (ReplayLog, *mockNotifier, *HashPrefix, error) {
5959
var log ReplayLog
6060
var chainNotifier *mockNotifier
61-
var hashedSecret HashPrefix
61+
var hashedSecret *HashPrefix
6262
if notifier {
6363

6464
// Create the MockNotifier which triggers the garbage collector
@@ -96,7 +96,7 @@ func startup(notifier bool) (ReplayLog, *mockNotifier, *HashPrefix, error) {
9696
// This is used as a key to retrieve the cltv value.
9797
hashedSecret = hashSharedSecret(&secret)
9898

99-
return log, chainNotifier, &hashedSecret, nil
99+
return log, chainNotifier, hashedSecret, nil
100100
}
101101

102102
// TestDecayedLogGarbageCollector tests the ability of the garbage collector

replaylog.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package sphinx
2+
3+
import (
4+
"crypto/sha256"
5+
)
6+
7+
const (
8+
// HashPrefixSize is the size in bytes of the keys we will be storing
9+
// in the DecayedLog. It represents the first 20 bytes of a truncated
10+
// sha-256 hash of a secret generated by ECDH.
11+
HashPrefixSize = 20
12+
)
13+
14+
// HashPrefix is a statically size, 20-byte array containing the prefix
15+
// of a Hash256, and is used to detect duplicate sphinx packets.
16+
type HashPrefix [HashPrefixSize]byte
17+
18+
// hashSharedSecret Sha-256 hashes the shared secret and returns the first
19+
// HashPrefixSize bytes of the hash.
20+
func hashSharedSecret(sharedSecret *Hash256) *HashPrefix {
21+
// Sha256 hash of sharedSecret
22+
h := sha256.New()
23+
h.Write(sharedSecret[:])
24+
25+
var sharedHash HashPrefix
26+
27+
// Copy bytes to sharedHash
28+
copy(sharedHash[:], h.Sum(nil))
29+
return &sharedHash
30+
}
31+
32+
// ReplayLog is an interface that defines a log of incoming sphinx packets,
33+
// enabling strong replay protection. The interface is general to allow
34+
// implementations near-complete autonomy. All methods must be safe for
35+
// concurrent access.
36+
type ReplayLog interface {
37+
// Start starts up the log. It returns an error if one occurs.
38+
Start() error
39+
40+
// Stop safely stops the log. It returns an error if one occurs.
41+
Stop() error
42+
43+
// Get retrieves an entry from the log given its hash prefix. It returns the
44+
// value stored and an error if one occurs. It returns ErrLogEntryNotFound
45+
// if the entry is not in the log.
46+
Get(*HashPrefix) (uint32, error)
47+
48+
// Put stores an entry into the log given its hash prefix and an
49+
// accompanying purposefully general type. It returns ErrReplayedPacket if
50+
// the provided hash prefix already exists in the log.
51+
Put(*HashPrefix, uint32) error
52+
53+
// Delete deletes an entry from the log given its hash prefix.
54+
Delete(*HashPrefix) error
55+
56+
// PutBatch stores a batch of sphinx packets into the log given their hash
57+
// prefixes and accompanying values. Returns the set of entries in the batch
58+
// that are replays and an error if one occurs.
59+
PutBatch(*Batch) (*ReplaySet, error)
60+
}

sphinx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ func (r *Router) ProcessOnionPacket(onionPkt *OnionPacket,
700700

701701
// Atomically compare this hash prefix with the contents of the on-disk
702702
// log, persisting it only if this entry was not detected as a replay.
703-
if err := r.log.Put(&hashPrefix, incomingCltv); err != nil {
703+
if err := r.log.Put(hashPrefix, incomingCltv); err != nil {
704704
return nil, err
705705
}
706706

@@ -876,7 +876,7 @@ func (t *Tx) ProcessOnionPacket(seqNum uint16, onionPkt *OnionPacket,
876876

877877
// Add the hash prefix to pending batch of shared secrets that will be
878878
// written later via Commit().
879-
err = t.batch.Put(seqNum, &hashPrefix, incomingCltv)
879+
err = t.batch.Put(seqNum, hashPrefix, incomingCltv)
880880
if err != nil {
881881
return err
882882
}

0 commit comments

Comments
 (0)