@@ -14,12 +14,12 @@ const NumBytesForHeight = 4
1414// walMsgCount tracks the number of wal entries at the current height
1515type walMsgCount uint32
1616
17- type walEntry [V types.Hashable [H ], H types.Hash , A types.Addr ] struct {
17+ type WalEntry [V types.Hashable [H ], H types.Hash , A types.Addr ] struct {
1818 Type types.MessageType `cbor:"type"`
1919 Entry types.Message [V , H , A ] `cbor:"data"` // cbor serialised Msg or Timeout
2020}
2121
22- func (w * walEntry [V , H , A ]) UnmarshalCBOR (data []byte ) error {
22+ func (w * WalEntry [V , H , A ]) UnmarshalCBOR (data []byte ) error {
2323 var wrapperType struct {
2424 Type types.MessageType `cbor:"type"`
2525 RawData cbor.RawMessage `cbor:"data"` // Golang can't unmarshal to an interface
@@ -60,15 +60,31 @@ func (w *walEntry[V, H, A]) UnmarshalCBOR(data []byte) error {
6060}
6161
6262// TendermintDB defines the methods for interacting with the Tendermint WAL database.
63+ //
64+ // The purpose of the WAL is to record any event that may result in a state change.
65+ // These events fall into the following categories:
66+ // 1. Incoming messages. We do not need to store outgoing messages.
67+ // 2. When we propose a value.
68+ // 3. When a timeout is triggered (not when it is scheduled).
69+ // The purpose of the WAL is to allow the node to recover the state it was in before the crash.
70+ // No new messages should be broadcast during replay.
71+ //
72+ // We commit the WAL to disk when:
73+ // 1. We start a new round
74+ // 2. Right before we broadcast a message
75+ //
76+ // We call Delete when we start a new height and commit a block
77+ //
78+ //go:generate mockgen -destination=../mocks/mock_db.go -package=mocks github.com/NethermindEth/juno/consensus/db TendermintDB
6379type TendermintDB [V types.Hashable [H ], H types.Hash , A types.Addr ] interface {
64- // CommitBatch writes the accumulated batch operations to the underlying database.
65- CommitBatch () error
66- // GetWALMsgs retrieves all WAL messages (consensus messages and timeouts) stored for a given height from the database.
67- GetWALMsgs (height types.Height ) ([]walEntry [V , H , A ], error )
80+ // Flush writes the accumulated batch operations to the underlying database.
81+ Flush () error
82+ // GetWALEntries retrieves all WAL messages (consensus messages and timeouts) stored for a given height from the database.
83+ GetWALEntries (height types.Height ) ([]WalEntry [V , H , A ], error )
6884 // SetWALEntry schedules the storage of a WAL message in the batch.
6985 SetWALEntry (entry types.Message [V , H , A ]) error
70- // DeleteWALMsgs schedules the deletion of all WAL messages for a specific height in the batch.
71- DeleteWALMsgs (height types.Height ) error
86+ // DeleteWALEntries schedules the deletion of all WAL messages for a specific height in the batch.
87+ DeleteWALEntries (height types.Height ) error
7288}
7389
7490// tendermintDB provides database access for Tendermint consensus state.
@@ -92,8 +108,8 @@ func NewTendermintDB[V types.Hashable[H], H types.Hash, A types.Addr](db db.KeyV
92108 return & tmdb
93109}
94110
95- // CommitBatch implements TMDBInterface.
96- func (s * tendermintDB [V , H , A ]) CommitBatch () error {
111+ // Flush implements TMDBInterface.
112+ func (s * tendermintDB [V , H , A ]) Flush () error {
97113 if err := s .batch .Write (); err != nil {
98114 return err
99115 }
@@ -105,7 +121,7 @@ func (s *tendermintDB[V, H, A]) CommitBatch() error {
105121// getWALCount scans the DB for the number of WAL messages at a given height.
106122// It panics if the DB scan fails.
107123func (s * tendermintDB [V , H , A ]) getWALCount (height types.Height ) walMsgCount {
108- prefix := WALEntry .Key (encodeHeight (height ))
124+ prefix := WALEntryBucket .Key (encodeHeight (height ))
109125 count := walMsgCount (0 )
110126 err := s .db .View (func (snap db.Snapshot ) error {
111127 defer snap .Close ()
@@ -127,16 +143,16 @@ func (s *tendermintDB[V, H, A]) getWALCount(height types.Height) walMsgCount {
127143 return count
128144}
129145
130- // DeleteWALMsgs iterates through the expected message keys based on the stored count.
131- // Note: This operates on the batch. Changes are only persisted after CommitBatch () is called.
132- func (s * tendermintDB [V , H , A ]) DeleteWALMsgs (height types.Height ) error {
146+ // DeleteWALEntries iterates through the expected message keys based on the stored count.
147+ // Note: This operates on the batch. Changes are only persisted after Flush () is called.
148+ func (s * tendermintDB [V , H , A ]) DeleteWALEntries (height types.Height ) error {
133149 heightBytes := encodeHeight (height )
134150 startIterBytes := encodeNumMsgsAtHeight (walMsgCount (1 ))
135151
136- startKey := WALEntry .Key (heightBytes , startIterBytes )
137- endKey := WALEntry .Key (encodeHeight (height + 1 ))
152+ startKey := WALEntryBucket .Key (heightBytes , startIterBytes )
153+ endKey := WALEntryBucket .Key (encodeHeight (height + 1 ))
138154 if err := s .batch .DeleteRange (startKey , endKey ); err != nil {
139- return fmt .Errorf ("DeleteWALMsgs : failed to add delete range [%x, %x) to batch: %w" , startKey , endKey , err )
155+ return fmt .Errorf ("DeleteWALEntries : failed to add delete range [%x, %x) to batch: %w" , startKey , endKey , err )
140156 }
141157
142158 delete (s .walCount , height )
@@ -145,7 +161,7 @@ func (s *tendermintDB[V, H, A]) DeleteWALMsgs(height types.Height) error {
145161
146162// SetWALEntry implements TMDBInterface.
147163func (s * tendermintDB [V , H , A ]) SetWALEntry (entry types.Message [V , H , A ]) error {
148- wrapper := walEntry [V , H , A ]{
164+ wrapper := WalEntry [V , H , A ]{
149165 Type : entry .MsgType (),
150166 Entry : entry ,
151167 }
@@ -160,7 +176,7 @@ func (s *tendermintDB[V, H, A]) SetWALEntry(entry types.Message[V, H, A]) error
160176 }
161177 nextNumMsgsAtHeight := numMsgsAtHeight + 1
162178
163- msgKey := WALEntry .Key (encodeHeight (height ), encodeNumMsgsAtHeight (nextNumMsgsAtHeight ))
179+ msgKey := WALEntryBucket .Key (encodeHeight (height ), encodeNumMsgsAtHeight (nextNumMsgsAtHeight ))
164180 if err := s .batch .Put (msgKey , wrappedEntry ); err != nil {
165181 return fmt .Errorf ("writeWALEntryToBatch: failed to set MsgsAtHeight: %w" , err )
166182 }
@@ -169,14 +185,14 @@ func (s *tendermintDB[V, H, A]) SetWALEntry(entry types.Message[V, H, A]) error
169185 return nil
170186}
171187
172- // GetWALMsgs implements TMDBInterface.
173- func (s * tendermintDB [V , H , A ]) GetWALMsgs (height types.Height ) ([]walEntry [V , H , A ], error ) {
188+ // GetWALEntries implements TMDBInterface.
189+ func (s * tendermintDB [V , H , A ]) GetWALEntries (height types.Height ) ([]WalEntry [V , H , A ], error ) {
174190 numEntries := s .walCount [height ]
175- walMsgs := make ([]walEntry [V , H , A ], numEntries )
191+ walMsgs := make ([]WalEntry [V , H , A ], numEntries )
176192 if numEntries == 0 {
177193 return walMsgs , nil
178194 }
179- startKey := WALEntry .Key (encodeHeight (height ))
195+ startKey := WALEntryBucket .Key (encodeHeight (height ))
180196 err := s .db .View (func (snap db.Snapshot ) error {
181197 defer snap .Close ()
182198 iter , err := snap .NewIterator (startKey , true )
0 commit comments