Skip to content

Commit de7ac21

Browse files
author
Jim Posen
committed
Change NewRouter to have ReplayLog parameter.
This decouples the construction of the Router from the implementation of the ReplayLog. Also changes all tests to use MemoryReplayLog instead of DecayedReplayLog.
1 parent d667f7a commit de7ac21

File tree

6 files changed

+36
-47
lines changed

6 files changed

+36
-47
lines changed

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))

decayedlog_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sphinx
22

33
import (
4+
"os"
45
"testing"
56
"time"
67

@@ -99,6 +100,13 @@ func startup(notifier bool) (ReplayLog, *mockNotifier, *HashPrefix, error) {
99100
return log, chainNotifier, hashedSecret, nil
100101
}
101102

103+
// shutdown deletes the temporary directory that the test database uses
104+
// and handles closing the database.
105+
func shutdown(dir string, d ReplayLog) {
106+
d.Stop()
107+
os.RemoveAll(dir)
108+
}
109+
102110
// TestDecayedLogGarbageCollector tests the ability of the garbage collector
103111
// to delete expired cltv values every time a block is received. Expired cltv
104112
// values are cltv values that are < current block height.

replaylog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
const (
99
// HashPrefixSize is the size in bytes of the keys we will be storing
10-
// in the DecayedLog. It represents the first 20 bytes of a truncated
10+
// in the ReplayLog. It represents the first 20 bytes of a truncated
1111
// sha-256 hash of a secret generated by ECDH.
1212
HashPrefixSize = 20
1313
)

sphinx.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"math/big"
1212

1313
"github.com/aead/chacha20"
14-
"github.com/lightningnetwork/lnd/chainntnfs"
1514
"github.com/roasbeef/btcd/btcec"
1615
"github.com/roasbeef/btcd/chaincfg"
1716
"github.com/roasbeef/btcutil"
@@ -631,8 +630,8 @@ type Router struct {
631630

632631
// NewRouter creates a new instance of a Sphinx onion Router given the node's
633632
// currently advertised onion private key, and the target Bitcoin network.
634-
func NewRouter(dbPath string, nodeKey *btcec.PrivateKey, net *chaincfg.Params,
635-
chainNotifier chainntnfs.ChainNotifier) *Router {
633+
func NewRouter(nodeKey *btcec.PrivateKey, net *chaincfg.Params, log ReplayLog,
634+
) *Router {
636635
var nodeID [addressSize]byte
637636
copy(nodeID[:], btcutil.Hash160(nodeKey.PubKey().SerializeCompressed()))
638637

@@ -650,19 +649,17 @@ func NewRouter(dbPath string, nodeKey *btcec.PrivateKey, net *chaincfg.Params,
650649
},
651650
D: nodeKey.D,
652651
},
653-
// TODO(roasbeef): replace instead with bloom filter?
654-
// * https://moderncrypto.org/mail-archive/messaging/2015/001911.html
655-
log: NewDecayedLog(dbPath, chainNotifier),
652+
log: log,
656653
}
657654
}
658655

659-
// Start starts / opens the DecayedLog's channeldb and its accompanying
656+
// Start starts / opens the ReplayLog's channeldb and its accompanying
660657
// garbage collector goroutine.
661658
func (r *Router) Start() error {
662659
return r.log.Start()
663660
}
664661

665-
// Stop stops / closes the DecayedLog's channeldb and its accompanying
662+
// Stop stops / closes the ReplayLog's channeldb and its accompanying
666663
// garbage collector goroutine.
667664
func (r *Router) Stop() {
668665
r.log.Stop()

sphinx_test.go

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"bytes"
55
"encoding/hex"
66
"fmt"
7-
"os"
87
"reflect"
9-
"strconv"
108
"testing"
119

1210
"github.com/davecgh/go-spew/spew"
@@ -99,9 +97,8 @@ func newTestRoute(numHops int) ([]*Router, *[]HopData, *OnionPacket, error) {
9997
" random key for sphinx node: %v", err)
10098
}
10199

102-
dbPath := strconv.Itoa(i)
103-
104-
nodes[i] = NewRouter(dbPath, privKey, &chaincfg.MainNetParams, nil)
100+
nodes[i] = NewRouter(privKey, &chaincfg.MainNetParams,
101+
NewMemoryReplayLog())
105102
}
106103

107104
// Gather all the pub keys in the path.
@@ -181,13 +178,6 @@ func TestBolt4Packet(t *testing.T) {
181178
}
182179
}
183180

184-
// shutdown deletes the temporary directory that the test database uses
185-
// and handles closing the database.
186-
func shutdown(dir string, d ReplayLog) {
187-
d.Stop()
188-
os.RemoveAll(dir)
189-
}
190-
191181
func TestSphinxCorrectness(t *testing.T) {
192182
nodes, hopDatas, fwdMsg, err := newTestRoute(NumMaxHops)
193183
if err != nil {
@@ -197,10 +187,9 @@ func TestSphinxCorrectness(t *testing.T) {
197187
// Now simulate the message propagating through the mix net eventually
198188
// reaching the final destination.
199189
for i := 0; i < len(nodes); i++ {
200-
// Start each node's DecayedLog and defer shutdown
201-
tempDir := strconv.Itoa(i)
190+
// Start each node's ReplayLog and defer shutdown
202191
nodes[i].log.Start()
203-
defer shutdown(tempDir, nodes[i].log)
192+
defer nodes[i].log.Stop()
204193

205194
hop := nodes[i]
206195

@@ -262,9 +251,9 @@ func TestSphinxSingleHop(t *testing.T) {
262251
t.Fatalf("unable to create test route: %v", err)
263252
}
264253

265-
// Start the DecayedLog and defer shutdown
254+
// Start the ReplayLog and defer shutdown
266255
nodes[0].log.Start()
267-
defer shutdown("0", nodes[0].log)
256+
defer nodes[0].log.Stop()
268257

269258
// Simulating a direct single-hop payment, send the sphinx packet to
270259
// the destination node, making it process the packet fully.
@@ -289,9 +278,9 @@ func TestSphinxNodeRelpay(t *testing.T) {
289278
t.Fatalf("unable to create test route: %v", err)
290279
}
291280

292-
// Start the DecayedLog and defer shutdown
281+
// Start the ReplayLog and defer shutdown
293282
nodes[0].log.Start()
294-
defer shutdown("0", nodes[0].log)
283+
defer nodes[0].log.Stop()
295284

296285
// Allow the node to process the initial packet, this should proceed
297286
// without any failures.
@@ -314,9 +303,9 @@ func TestSphinxNodeRelpaySameBatch(t *testing.T) {
314303
t.Fatalf("unable to create test route: %v", err)
315304
}
316305

317-
// Start the DecayedLog and defer shutdown
306+
// Start the ReplayLog and defer shutdown
318307
nodes[0].log.Start()
319-
defer shutdown("0", nodes[0].log)
308+
defer nodes[0].log.Stop()
320309

321310
tx := nodes[0].BeginTxn([]byte("0"), 2)
322311

@@ -360,9 +349,9 @@ func TestSphinxNodeRelpayLaterBatch(t *testing.T) {
360349
t.Fatalf("unable to create test route: %v", err)
361350
}
362351

363-
// Start the DecayedLog and defer shutdown
352+
// Start the ReplayLog and defer shutdown
364353
nodes[0].log.Start()
365-
defer shutdown("0", nodes[0].log)
354+
defer nodes[0].log.Stop()
366355

367356
tx := nodes[0].BeginTxn([]byte("0"), 1)
368357

@@ -397,17 +386,17 @@ func TestSphinxNodeRelpayLaterBatch(t *testing.T) {
397386
}
398387
}
399388

400-
func TestSphinxNodeRelpayBatchIdempotency(t *testing.T) {
389+
func TestSphinxNodeReplayBatchIdempotency(t *testing.T) {
401390
// We'd like to ensure that the sphinx node itself rejects all replayed
402391
// packets which share the same shared secret.
403392
nodes, _, fwdMsg, err := newTestRoute(NumMaxHops)
404393
if err != nil {
405394
t.Fatalf("unable to create test route: %v", err)
406395
}
407396

408-
// Start the DecayedLog and defer shutdown
397+
// Start the ReplayLog and defer shutdown
409398
nodes[0].log.Start()
410-
defer shutdown("0", nodes[0].log)
399+
defer nodes[0].log.Stop()
411400

412401
tx := nodes[0].BeginTxn([]byte("0"), 1)
413402

@@ -456,9 +445,9 @@ func TestSphinxAssocData(t *testing.T) {
456445
t.Fatalf("unable to create random onion packet: %v", err)
457446
}
458447

459-
// Start the DecayedLog and defer shutdown
448+
// Start the ReplayLog and defer shutdown
460449
nodes[0].log.Start()
461-
defer shutdown("0", nodes[0].log)
450+
defer nodes[0].log.Stop()
462451

463452
_, err = nodes[0].ProcessOnionPacket(fwdMsg, []byte("somethingelse"), 1)
464453
if err == nil {

0 commit comments

Comments
 (0)