Skip to content

implement sqlite database - #376

Merged
notmandatory merged 3 commits into
bitcoindevkit:masterfrom
johncantrell97:sqlite-database
Sep 24, 2021
Merged

notmandatory merged 3 commits into
bitcoindevkit:masterfrom
johncantrell97:sqlite-database

Conversation

@johncantrell97

@johncantrell97 johncantrell97 commented Jun 18, 2021 •

Copy link
Copy Markdown
Contributor

Description

This PR implements all the Database related traits for a database backed by sqlite. It currently uses cached prepared statements which simplify the implementation from trying to use actual prepared Statement objects owned by the SqliteDatabase struct. It should have the same performance as long as the cache is large enough to hold all the statements.

In my own project I use a crate called rusqlite_migration to handle migrations but in an attempt to reduce dependencies I borrowed a simple migration implementation by jb55 that manages a version based on the length of the static migrations array.

Notes to the reviewers

Checklists

All Submissions:

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

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature
  • I've updated CHANGELOG.md

Bugfixes:

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

@johncantrell97
johncantrell97 force-pushed the sqlite-database branch 4 times, most recently from aebf6be to 5f35900 Compare June 18, 2021 18:01
@johncantrell97
johncantrell97 marked this pull request as draft June 18, 2021 18:02
@johncantrell97
johncantrell97 force-pushed the sqlite-database branch 2 times, most recently from a744416 to a9d3bde Compare June 29, 2021 21:46

@tcharding tcharding left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor suggestions/ideas. Don't read too much into my review, I'm not that deep in bdk.

Cheers

Comment thread src/database/sqlite.rs Outdated
Comment on lines +517 to +594
// should this ever really happen? wouldn't an adjusted tx
// result in a new txid
self.update_transaction(&transaction.txid(), &serialize(transaction))?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is correct, AFAIU, if the transaction is already in the db we could just noop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah makes sense, I guess in theory there could be a change to the transaction struct that is outside of the txid calculation. Can't hurt to keep the update here I suppose.

Comment thread src/database/sqlite.rs Outdated
}

fn del_raw_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error> {
match self.select_transaction_by_txid(&txid)? {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double reference, same below. If you lint with clippy +nightly it will catch these for you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, yeah I see that clippy finds it. cool.

Comment thread src/database/sqlite.rs Outdated
Comment on lines +778 to +781
// always returns with Option<Transaction> (if exists in db)
// are there cases where we NEED it to be None even if it exists in db?
// or is this just a performance improvement?
match self.select_transaction_details_by_txid(&txid)? {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume that Err is for db connection errors, None means not in db, and Some means found in db.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is actually referring to the fact that my select_transaction_details method ALWAYS includes the raw transaction in the result if it exists in the database. The get_tx method accepts a include_raw flag for whether or not we are supposed to fetch the raw transaction.

So I was just worried about a potential case where the user sets include_raw to false but it's still included. Feels like this should never be an actual problem other than perhaps a performance issue.

Comment thread src/database/sqlite.rs
Comment on lines +791 to +819
fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
let keychain = serde_json::to_string(&keychain)?;
match self.select_last_deriviation_index_by_keychain(keychain)? {
Some(value) => {
Ok(Some(value))
},
None => {
Ok(None)
}
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for your interest, this can also be written as:

    fn get_last_index(&self, keychain: KeychainKind) -> Result<Option<u32>, Error> {
        let keychain = serde_json::to_string(&keychain)?;
        let value = self.select_last_deriviation_index_by_keychain(keychain)?;
        Ok(value)
    }
``

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah good point, looks cleaner.

@johncantrell97
johncantrell97 force-pushed the sqlite-database branch 5 times, most recently from ba14714 to d12bfb5 Compare September 11, 2021 03:04
@johncantrell97
johncantrell97 marked this pull request as ready for review September 11, 2021 03:04
@johncantrell97
johncantrell97 force-pushed the sqlite-database branch 2 times, most recently from 54ae316 to 636de95 Compare September 11, 2021 03:13
Comment thread src/database/any.rs Resolved
@notmandatory

Copy link
Copy Markdown
Contributor

A couple places in the CI workflows also need the new sqlite feature added:

  1. cont_integration.yml: build-test: strategy: matrix: features:
  2. nightly_docs.yml: build_docs: steps: Build Docs:
    run: cargo rustdoc --verbose --features=compiler,electrum,esplora,ureq,compact_filters,key-value-db,all-keys,sqlite -- --cfg docsrs -Dwarnings

Comment thread src/database/sqlite.rs Resolved
Comment thread src/database/sqlite.rs Resolved
@notmandatory

Copy link
Copy Markdown
Contributor

Also this one needs a rebase to pickup latest Blockchain tests required for CI to pass.

Comment thread src/database/any.rs Resolved
Comment thread src/database/sqlite.rs Resolved
Comment thread src/database/sqlite.rs Resolved
Comment thread src/database/sqlite.rs Resolved

@notmandatory notmandatory left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 919522a

@notmandatory
notmandatory merged commit 919522a into bitcoindevkit:master Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants