1
1
from __future__ import annotations
2
2
3
- from collections .abc import Collection
4
- from contextlib import AbstractAsyncContextManager
3
+ from collections .abc import AsyncIterator , Collection
4
+ from contextlib import asynccontextmanager
5
5
from dataclasses import dataclass
6
6
from pathlib import Path
7
- from types import TracebackType
8
- from typing import TYPE_CHECKING , Any , Optional
7
+ from typing import TYPE_CHECKING , Optional
9
8
10
9
from chia_rs import BlockRecord , FullBlock , SubEpochChallengeSegment , SubEpochSummary
11
10
from chia_rs .sized_bytes import bytes32
@@ -55,6 +54,12 @@ async def new_block(
55
54
) -> None :
56
55
await self .coin_store .new_block (height , timestamp , included_reward_coins , tx_additions , tx_removals )
57
56
57
+ @asynccontextmanager
58
+ async def writer (self ) -> AsyncIterator [ConsensusStoreSQLite3Writer ]:
59
+ # Return self as the writer facade
60
+ async with self .block_store .transaction ():
61
+ yield self
62
+
58
63
59
64
@dataclass
60
65
class ConsensusStoreSQLite3 :
@@ -66,11 +71,6 @@ class ConsensusStoreSQLite3:
66
71
coin_store : CoinStoreProtocol
67
72
height_map : BlockHeightMap
68
73
69
- # Writer context and writer facade for transactional writes (re-entrant via depth counter)
70
- _writer_ctx : Optional [AbstractAsyncContextManager [Any ]] = None
71
- _writer : Optional [ConsensusStoreSQLite3Writer ] = None
72
- _txn_depth : int = 0
73
-
74
74
@classmethod
75
75
async def create (
76
76
cls ,
@@ -99,41 +99,12 @@ async def create(
99
99
height_map = height_map ,
100
100
)
101
101
102
- # Async context manager yielding a writer for atomic writes
103
- async def __aenter__ (self ) -> ConsensusStoreSQLite3Writer :
104
- # Re-entrant async context manager:
105
- # Begin a transaction only at the outermost level. CoinStore shares the same DB.
106
- if self ._writer is None :
107
- self ._writer_ctx = self .block_store .transaction ()
108
- await self ._writer_ctx .__aenter__ ()
109
- # Create writer facade bound to this transaction
110
- self ._writer = ConsensusStoreSQLite3Writer (self .block_store , self .coin_store )
111
- self ._txn_depth += 1
112
- return self ._writer # Return the same writer for nested contexts
113
-
114
- async def __aexit__ (
115
- self ,
116
- exc_type : Optional [type [BaseException ]],
117
- exc : Optional [BaseException ],
118
- tb : Optional [TracebackType ],
119
- ) -> Optional [bool ]:
120
- try :
121
- # Check if we're at the outermost level before decrementing
122
- if self ._txn_depth == 1 :
123
- # This is the outermost context, handle transaction exit
124
- if self ._writer_ctx is not None :
125
- return await self ._writer_ctx .__aexit__ (exc_type , exc , tb )
126
- return None
127
- else :
128
- # This is a nested context, just return None (don't suppress exceptions)
129
- return None
130
- finally :
131
- # Always decrement depth and clean up if we're at the outermost level
132
- if self ._txn_depth > 0 :
133
- self ._txn_depth -= 1
134
- if self ._txn_depth == 0 :
135
- self ._writer_ctx = None
136
- self ._writer = None
102
+ @asynccontextmanager
103
+ async def writer (self ) -> AsyncIterator [ConsensusStoreSQLite3Writer ]:
104
+ """Async context manager that yields a writer facade for performing transactional writes."""
105
+ csw = ConsensusStoreSQLite3Writer (self .block_store , self .coin_store )
106
+ async with csw .writer () as writer :
107
+ yield writer
137
108
138
109
# Block store methods
139
110
0 commit comments