1717# You should have received a copy of the GNU Affero General Public License
1818# along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
20- import json
2120import logging
2221from pathlib import Path
2322
@@ -35,18 +34,16 @@ def __init__(self, state_file: str | Path):
3534 self .state_file = Path (state_file )
3635
3736 def load (self , initial_from_blocks : dict [str , int ]) -> State :
37+ from_blocks = dict (initial_from_blocks )
3838 if self .state_file .exists ():
39- try :
40- stored = State (** json .loads (self .state_file .read_text ())).from_blocks
41- logger .info (f'Loaded state from { self .state_file } ' )
42- from_blocks = {n : stored .get (n , b ) for n , b in initial_from_blocks .items ()}
43- return State (from_blocks = from_blocks )
44- except Exception as e :
45- logger .warning (
46- f'Failed to load state from { self .state_file } : { e } . Using initial values.'
47- )
48- logger .info (f'Creating new state with from_blocks={ initial_from_blocks } ' )
49- return State (from_blocks = dict (initial_from_blocks ))
39+ stored = State .model_validate_json (self .state_file .read_text ()).from_blocks
40+ logger .info (f'Loaded state from { self .state_file } ' )
41+ for name in from_blocks :
42+ if name in stored :
43+ from_blocks [name ] = stored [name ]
44+ else :
45+ logger .info (f'Creating new state with from_blocks={ from_blocks } ' )
46+ return State (from_blocks = from_blocks )
5047
5148 def save (self , state : State ) -> None :
5249 self .state_file .write_text (state .model_dump_json (indent = 2 ))
0 commit comments