bdk icon indicating copy to clipboard operation
bdk copied to clipboard

Recover file store previous state at open

Open nymius opened this issue 1 year ago • 0 comments

Description

The Store::open method doesn't recovers the previous Store state saved in the file and emplaces the file pointer just after the magic bytes prefix, this later is agravated by Store::append_changeset which sets the file pointer after the last written changeset. The combination of both causes the lost of any previous changeset there may have been in the file.

Is natural to think this shouldn't be the expected behavior, as @KnowWhoami pointed out in #1517, and the Store should recover the previous changesets stored in the file store.

To avoid producing breaking changes the expected logic has been implemented in a new Store::reopen method, which opens the file, iterates the changesets and returns a new Store with the file pointer correctly placed at the end of the already existing changesets. As this method checks the integrity of the stored changesets while iterating them, if any error is found, the method will return a StoreError::EntryIter error with the index of the offending changeset, the error caused by it and the number of bytes read when the error happened.

The StoreError is a new error enum including the following variants:

  • EntryIter: any error related to the decoding of the changesets of the file.
  • Io: errors related with file management internals.
  • InvalidMagicBytes: error caused by mismatching with the expected magic bytes.

StoreError::Io and StoreError::InvalidMagicBytes are the same than FileError::Io and FileError::InvalidMagicBytes. My idea is to propose StoreError as a replacement of FileError in the future. It has been implemented in this way to avoid the addition of extra nested variants at the moment of matching the error.

Fixes #1517.

Notes to the reviewers

I have committed the changes as feat and not fix because, although this PR fixes #1517, the changes do not imply a change in the current Store methods, but the addition of new one and a new Error enum.

Changelog notice

  • New Store::reopen method in file_store.
  • New StoreError enum in file_store.

Checklists

All Submissions:

  • [x] I've signed all my commits
  • [x] I followed the contribution guidelines
  • [x] I ran cargo fmt and cargo clippy before committing

New Features:

  • [x] I've added tests for the new feature
  • [x] I've added docs for the new feature

Bugfixes:

  • [ ] This pull request breaks the existing API
  • [x] I've added tests to reproduce the issue which are now passing
  • [x] I'm linking the issue being fixed by this PR

nymius avatar Sep 29 '24 21:09 nymius