Skip to content

Db service#1361

Open
zealsham wants to merge 1 commit intopayjoin:masterfrom
zealsham:db-service
Open

Db service#1361
zealsham wants to merge 1 commit intopayjoin:masterfrom
zealsham:db-service

Conversation

@zealsham
Copy link
Collaborator

@zealsham zealsham commented Feb 26, 2026

This PR addresses part of #941 by making Db a tower service. Since payjoin-directory will eventually be deprecated, this PR moves the db implementation to payjoin-mailroom while still allowing the directory binary to be built if needed. The PR also ensures directory::Service remains compatible via the Db trait adapter.

Some parts of these PR was bootstrapped using chatgpt web

Pull Request Checklist

Please confirm the following before requesting review:

@zealsham zealsham marked this pull request as draft February 26, 2026 01:03
@zealsham zealsham force-pushed the db-service branch 4 times, most recently from f80134e to a91e3e0 Compare February 26, 2026 14:06
Comment on lines +848 to +874
async fn wait_for_v2_payload(
&self,
id: &ShortId,
) -> Result<Arc<Vec<u8>>, DbError<Self::OperationalError>> {
let receiver = {
let mut state = self.state.lock().await;
if let Some(payload) = state.v2_payloads.get(id) {
return Ok(payload.clone());
}
if let Some((ref mut payload, _response_tx)) = state.v1_pending.get_mut(id) {
if let Some(p) = payload.take() {
return Ok(p);
}
}
let (tx, rx) = oneshot::channel();
state.v2_waiters.insert(*id, tx);
rx
};
match tokio::time::timeout(self.timeout, receiver).await {
Ok(Ok(payload)) => Ok(payload),
Ok(Err(_)) => Err(DbError::Operational(std::io::Error::new(
std::io::ErrorKind::ConnectionReset,
"closed",
))),
Err(elapsed) => Err(DbError::Timeout(elapsed)),
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this function we just return an error on a Sender error or timeout but the TestDbState keeps a v2_waiters with this ShortId. Is there some value in mimicing the behavior in the FilesDb where the v2_waiter is cleared no matter what?

I ask this only if others see value in a long running mailroom test, say to demonstrate an overfilling mailroom.

Comment on lines +906 to +912
match tokio::time::timeout(self.timeout, receiver).await {
Ok(Ok(payload)) => Ok(Arc::new(payload)),
Ok(Err(_)) => Err(DbError::V1SenderUnavailable),
Err(elapsed) => Err(DbError::Timeout(elapsed)),
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here where the v1_pending is not cleared on an error

Copy link
Collaborator

@spacebear21 spacebear21 left a comment

Choose a reason for hiding this comment

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

This is looking promising! I left some high-level feedback while this is still in draft. Another question I had: do you expect this do be backwards-compatible with an existing DB? Say I started running a payjoin-mailroom yesterday and have active sessions, and upgrade to this, will it keep existing sessions around?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We can kill the standalone directory binary entirely

Comment on lines +47 to +56
pub(crate) capacity: usize,
persistent_storage: DiskStorage,
pending_v1: HashMap<ShortId, V1WaitMapEntry>,
pub(crate) pending_v1: HashMap<ShortId, V1WaitMapEntry>,
pending_v2: HashMap<ShortId, V2WaitMapEntry>,
insert_order: VecDeque<(SystemTime, ShortId)>,
read_order: VecDeque<(SystemTime, ShortId)>,
pub(crate) insert_order: VecDeque<(SystemTime, ShortId)>,
pub(crate) read_order: VecDeque<(SystemTime, ShortId)>,
read_mailbox_ids: HashSet<ShortId>,
unread_ttl_below_capacity: Duration,
unread_ttl_at_capacity: Duration,
read_ttl: Duration,
pub(crate) unread_ttl_below_capacity: Duration,
pub(crate) unread_ttl_at_capacity: Duration,
pub(crate) read_ttl: Duration,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did these change to pub(crate)? I don't see where they are being used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oops!, when i started writting this , i was importing types i needed from the payjoin-directory crate, made them pub crate there and copied them over here.

will change it

Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like this is still unresolved in the latest push? there are a few other pub(crate) additions throughout this file which also seem unnecessary.

Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a reason to keep the payjoin-directory db mod?

let db = FilesDb::init(Duration::from_millis(100), dir.keep()).await.expect("db init");
/// In-memory Db implementation for directory service tests.
#[derive(Clone)]
struct TestDb {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similarly, would it be more appropriate to have these tests live in payjoin-mailroom's db mod?

@coveralls
Copy link
Collaborator

coveralls commented Mar 4, 2026

Pull Request Test Coverage Report for Build 22786494612

Details

  • 149 of 187 (79.68%) changed or added relevant lines in 6 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+1.0%) to 83.507%

Changes Missing Coverage Covered Lines Changed/Added Lines %
payjoin-mailroom/src/access_control.rs 0 1 0.0%
payjoin-mailroom/src/db/files.rs 18 20 90.0%
payjoin-mailroom/src/lib.rs 11 14 78.57%
payjoin-mailroom/src/directory.rs 39 47 82.98%
payjoin-mailroom/src/db/mod.rs 80 104 76.92%
Totals Coverage Status
Change from base Build 22741440695: 1.0%
Covered Lines: 10688
Relevant Lines: 12799

💛 - Coveralls

@zealsham zealsham force-pushed the db-service branch 2 times, most recently from fde034f to f9b75aa Compare March 4, 2026 10:36
@zealsham zealsham marked this pull request as ready for review March 4, 2026 10:57
Copy link
Collaborator

@spacebear21 spacebear21 left a comment

Choose a reason for hiding this comment

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

Epic net deletion!

cACK on deleting the payjoin-directory crate and moving it to a service with axum routing.

It looks like the LLM was a bit overzealous in deleting docstrings and comments, tests, and random line breaks. The latter is fine, but the tests and comments need to be restored where applicable.

Lastly, can you please clean up the commit message per the contributing guidelines?

Thanks for taking on this big refactor.

Comment on lines +47 to +56
pub(crate) capacity: usize,
persistent_storage: DiskStorage,
pending_v1: HashMap<ShortId, V1WaitMapEntry>,
pub(crate) pending_v1: HashMap<ShortId, V1WaitMapEntry>,
pending_v2: HashMap<ShortId, V2WaitMapEntry>,
insert_order: VecDeque<(SystemTime, ShortId)>,
read_order: VecDeque<(SystemTime, ShortId)>,
pub(crate) insert_order: VecDeque<(SystemTime, ShortId)>,
pub(crate) read_order: VecDeque<(SystemTime, ShortId)>,
read_mailbox_ids: HashSet<ShortId>,
unread_ttl_below_capacity: Duration,
unread_ttl_at_capacity: Duration,
read_ttl: Duration,
pub(crate) unread_ttl_below_capacity: Duration,
pub(crate) unread_ttl_at_capacity: Duration,
pub(crate) read_ttl: Duration,
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks like this is still unresolved in the latest push? there are a few other pub(crate) additions throughout this file which also seem unnecessary.

@spacebear21
Copy link
Collaborator

spacebear21 commented Mar 4, 2026

Also I just realized we reference payjoin-directory in various links through the codebase, for example in the README. We should grep the whole codebase for payjoin-directory to ensure no dangling links.

@zealsham zealsham force-pushed the db-service branch 2 times, most recently from 35ad2c2 to 44fda1e Compare March 6, 2026 00:56
This pr addresses a part of payjoin#941 consigned with making Db a tower
service. It also removes the payjoin-directory crate and moves all
directory functionality into the payjoin-mailroom crate as a tower
service.
Copy link
Collaborator

@spacebear21 spacebear21 left a comment

Choose a reason for hiding this comment

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

ACK f2d07db

I took the liberty to force push to fix a few minor things:

  • added back more comments that had been inadvertently removed
  • changed the payjoin dependency in payjoin-mailroom to rc.2 instead of rc.1
  • cleaned up the commit message a bit for typos/grammar

We'll need an ACK from someone else to merge this because I was the last to push on the PR so my approval doesn't count per github rules.

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.

4 participants