Skip to content

Commit e8c340a

Browse files
committed
test: cover WAL record and prune watermark decode paths
1 parent a188a81 commit e8c340a

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

consensus/db/codec_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package db_test
33
import (
44
"testing"
55

6+
consensusdb "github.com/NethermindEth/juno/consensus/db"
67
"github.com/NethermindEth/juno/consensus/starknet"
78
"github.com/NethermindEth/juno/consensus/types"
89
"github.com/NethermindEth/juno/consensus/types/wal"
@@ -70,6 +71,25 @@ func TestWALRecordsRoundTripThroughReopen(t *testing.T) {
7071
assertLoadedEntries(t, walStore, entries)
7172
}
7273

74+
func TestWALRecordDecodeRejectsMalformedPayloads(t *testing.T) {
75+
start := wal.Start(types.Height(1))
76+
valid, err := consensusdb.EncodeWALRecordPayload(&start)
77+
require.NoError(t, err)
78+
79+
tests := map[string][]byte{
80+
"empty": {},
81+
"unknown kind": {0xFF},
82+
"truncated": valid[:len(valid)-1],
83+
"trailing byte": append(append([]byte(nil), valid...), 0x00),
84+
}
85+
86+
for name, payload := range tests {
87+
t.Run(name, func(t *testing.T) {
88+
require.Error(t, consensusdb.DecodeWALRecordPayload(payload))
89+
})
90+
}
91+
}
92+
7393
func walAddress(v uint64) starknet.Address {
7494
return felt.FromUint64[starknet.Address](v)
7595
}

consensus/db/export_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,34 @@ package db
22

33
import (
44
"github.com/NethermindEth/juno/consensus/starknet"
5+
"github.com/NethermindEth/juno/consensus/types"
56
)
67

8+
func LoadPruneWatermark(walDir string) (types.Height, error) {
9+
return loadPruneWatermark(walDir)
10+
}
11+
12+
func WritePruneWatermark(walDir string, height types.Height) error {
13+
return writePruneWatermark(walDir, height)
14+
}
15+
716
// ForcePendingCleanup primes the store so the next prune flush triggers cleanup.
817
func ForcePendingCleanup(
918
walStore TendermintWALStore[starknet.Value, starknet.Hash, starknet.Address],
1019
) {
1120
walStore.(*tendermintWALStore[starknet.Value, starknet.Hash, starknet.Address]).
1221
pruneRecordsSinceCleanup = cleanupPruneRecordInterval - 1
1322
}
23+
24+
func EncodeWALRecordPayload(entry starknet.WALEntry) ([]byte, error) {
25+
envelope := walRecordEnvelope[starknet.Value, starknet.Hash, starknet.Address]{Kind: walRecordEntry}
26+
if err := envelope.setEntry(entry); err != nil {
27+
return nil, err
28+
}
29+
return appendWALRecordPayload(nil, envelope)
30+
}
31+
32+
func DecodeWALRecordPayload(payload []byte) error {
33+
_, err := decodeWALRecord[starknet.Value, starknet.Hash, starknet.Address](payload)
34+
return err
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package db_test
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
consensusdb "github.com/NethermindEth/juno/consensus/db"
9+
"github.com/NethermindEth/juno/consensus/types"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
const testPruneWatermarkHeight = types.Height(1)
14+
15+
func TestPruneWatermarkRoundTrips(t *testing.T) {
16+
walDir := t.TempDir()
17+
18+
height, err := consensusdb.LoadPruneWatermark(walDir)
19+
require.NoError(t, err)
20+
require.Zero(t, height)
21+
22+
require.NoError(t, consensusdb.WritePruneWatermark(walDir, testPruneWatermarkHeight))
23+
height, err = consensusdb.LoadPruneWatermark(walDir)
24+
require.NoError(t, err)
25+
require.Equal(t, testPruneWatermarkHeight, height)
26+
}
27+
28+
func TestLoadPruneWatermarkRejectsCorruptFile(t *testing.T) {
29+
walDir := t.TempDir()
30+
require.NoError(t, consensusdb.WritePruneWatermark(walDir, testPruneWatermarkHeight))
31+
validWatermark, err := os.ReadFile(filepath.Join(walDir, "prune-watermark"))
32+
require.NoError(t, err)
33+
34+
wrongHeader := make([]byte, len(validWatermark))
35+
copy(wrongHeader, "wrong-header")
36+
37+
tests := map[string][]byte{
38+
"wrong size": []byte("short"),
39+
"bad header": wrongHeader,
40+
}
41+
42+
for name, contents := range tests {
43+
t.Run(name, func(t *testing.T) {
44+
walDir := t.TempDir()
45+
path := filepath.Join(walDir, "prune-watermark")
46+
require.NoError(t, os.WriteFile(path, contents, 0o644))
47+
48+
_, err := consensusdb.LoadPruneWatermark(walDir)
49+
require.Error(t, err)
50+
})
51+
}
52+
}

0 commit comments

Comments
 (0)