Skip to content

Commit ed03b01

Browse files
committed
wallet: adjust method visibility, add more comments
1 parent deae3d4 commit ed03b01

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

src/server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ impl WalletService for Arc<crate::wallet::Wallet> {
770770
.map(bdk::bitcoin::BlockHash::from_byte_array)?;
771771

772772
let mainchain_tip = self
773+
.validator()
773774
.get_mainchain_tip()
774775
.map_err(|err| tonic::Status::from_error(err.into()))?;
775776

src/wallet/mod.rs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,18 @@ impl Wallet {
151151
bundle_hash BLOB NOT NULL,
152152
UNIQUE(sidechain_number, bundle_hash));",
153153
),
154+
// TODO: delete this? Not persisting to this table anywhere.
155+
// Not clear how we're going to keep this in sync with the
156+
// actual mempool. Seems like we could accomplish the same
157+
// thing through `listtransactions`
154158
M::up(
155159
"CREATE TABLE mempool
156160
(txid BLOB UNIQUE NOT NULL,
157161
tx_data BLOB NOT NULL);",
158162
),
163+
// This is really a table of _pending_ deposits. Gets wiped
164+
// upon generating a fresh block. How will this work for
165+
// non-regtest?
159166
M::up(
160167
"CREATE TABLE deposits
161168
(sidechain_number INTEGER NOT NULL,
@@ -343,22 +350,27 @@ impl Wallet {
343350
tracing::info!("Generate: creating {} blocks", count);
344351

345352
for _ in 0..count {
353+
// This is a list of pending sidechain proposals from /our/ wallet, fetched from
354+
// the DB.
346355
let sidechain_proposals = self.get_our_sidechain_proposals().into_diagnostic()?;
347356
let mut coinbase_builder = CoinbaseBuilder::new();
348357
for sidechain_proposal in sidechain_proposals {
349358
coinbase_builder = coinbase_builder.propose_sidechain(sidechain_proposal);
350359
}
351360

352361
let mut sidechain_acks = self.get_sidechain_acks()?;
353-
let pending_sidechain_proposals = self.get_pending_sidechain_proposals().await?;
362+
363+
// This is a map of pending sidechain proposals from the /validator/, i.e.
364+
// proposals broadcasted by (potentially) someone else, and already active.
365+
let active_sidechain_proposals = self.get_active_sidechain_proposals().await?;
354366

355367
if ack_all_proposals {
356368
tracing::info!(
357369
"Handle sidechain ACK: acking all sidechains irregardless of what DB says"
358370
);
359371

360372
let acks = sidechain_acks.clone();
361-
for (sidechain_number, sidechain_proposal) in &pending_sidechain_proposals {
373+
for (sidechain_number, sidechain_proposal) in &active_sidechain_proposals {
362374
let sidechain_number = *sidechain_number;
363375

364376
if !acks
@@ -384,7 +396,7 @@ impl Wallet {
384396
}
385397

386398
for sidechain_ack in sidechain_acks {
387-
if !self.validate_sidechain_ack(&sidechain_ack, &pending_sidechain_proposals) {
399+
if !self.validate_sidechain_ack(&sidechain_ack, &active_sidechain_proposals) {
388400
self.delete_sidechain_ack(&sidechain_ack)?;
389401
tracing::info!(
390402
"Unable to handle sidechain ack, deleted: {}",
@@ -428,13 +440,13 @@ impl Wallet {
428440
);
429441

430442
self.mine(&coinbase_outputs, deposit_transactions).await?;
431-
self.delete_sidechain_proposals()?;
432-
self.delete_deposits()?;
443+
self.delete_pending_sidechain_proposals()?;
444+
self.delete_pending_deposits()?;
433445
}
434446
Ok(())
435447
}
436448

437-
pub fn delete_deposits(&self) -> Result<()> {
449+
fn delete_pending_deposits(&self) -> Result<()> {
438450
self.db_connection
439451
.lock()
440452
.execute("DELETE FROM deposits;", ())
@@ -444,7 +456,7 @@ impl Wallet {
444456

445457
/// Fetches pending deposits. If `sidechain_number` is provided, only
446458
/// deposits for that sidechain are returned.
447-
pub fn get_pending_deposits(
459+
fn get_pending_deposits(
448460
&self,
449461
sidechain_number: Option<SidechainNumber>,
450462
) -> Result<Vec<Deposit>> {
@@ -498,7 +510,7 @@ impl Wallet {
498510
with_connection(&self.db_connection.lock())
499511
}
500512

501-
pub fn get_bmm_requests(&self) -> Result<Vec<(SidechainNumber, [u8; 32])>> {
513+
fn get_bmm_requests(&self) -> Result<Vec<(SidechainNumber, [u8; 32])>> {
502514
// Satisfy clippy with a single function call per lock
503515
let with_connection = |connection: &Connection| -> Result<_, _> {
504516
let mut statement = connection
@@ -587,7 +599,7 @@ impl Wallet {
587599
Ok(())
588600
}
589601

590-
pub fn get_utxos(&self) -> Result<()> {
602+
fn get_utxos(&self) -> Result<()> {
591603
if self.last_sync.read().is_none() {
592604
return Err(miette!("get utxos: wallet not synced"));
593605
}
@@ -607,6 +619,9 @@ impl Wallet {
607619
Ok(())
608620
}
609621

622+
/// Persists a sidechain proposal into our database.
623+
/// On regtest: picked up by the next block generation.
624+
/// On signet: TBD, but needs some way of getting communicated to the miner.
610625
pub fn propose_sidechain(&self, proposal: &SidechainProposal) -> Result<(), rusqlite::Error> {
611626
let sidechain_number: u8 = proposal.sidechain_number.into();
612627
self.db_connection.lock().execute(
@@ -644,7 +659,7 @@ impl Wallet {
644659
Ok(())
645660
}
646661

647-
pub fn get_sidechain_acks(&self) -> Result<Vec<SidechainAck>> {
662+
fn get_sidechain_acks(&self) -> Result<Vec<SidechainAck>> {
648663
// Satisfy clippy with a single function call per lock
649664
let with_connection = |connection: &Connection| -> Result<_> {
650665
let mut statement = connection
@@ -666,7 +681,7 @@ impl Wallet {
666681
with_connection(&self.db_connection.lock())
667682
}
668683

669-
pub fn delete_sidechain_ack(&self, ack: &SidechainAck) -> Result<()> {
684+
fn delete_sidechain_ack(&self, ack: &SidechainAck) -> Result<()> {
670685
self.db_connection
671686
.lock()
672687
.execute(
@@ -677,13 +692,14 @@ impl Wallet {
677692
Ok(())
678693
}
679694

680-
pub async fn get_pending_sidechain_proposals(
695+
/// Fetches sidechain proposals from the validator. Returns proposals that
696+
/// are already included into a block, and possible to vote on.
697+
async fn get_active_sidechain_proposals(
681698
&self,
682699
) -> Result<HashMap<SidechainNumber, SidechainProposal>> {
683700
let pending_proposals = self
684701
.validator
685-
.get_sidechains()
686-
.map_err(|e| miette::miette!(e.to_string()))?
702+
.get_sidechains()?
687703
.into_iter()
688704
.map(|(_, sidechain)| (sidechain.proposal.sidechain_number, sidechain.proposal))
689705
.collect();
@@ -714,11 +730,7 @@ impl Wallet {
714730
with_connection(&self.db_connection.lock())
715731
}
716732

717-
pub fn get_mainchain_tip(&self) -> Result<bitcoin::BlockHash> {
718-
self.validator.get_mainchain_tip()
719-
}
720-
721-
pub async fn get_sidechain_ctip(
733+
async fn get_sidechain_ctip(
722734
&self,
723735
sidechain_number: SidechainNumber,
724736
) -> Result<Option<(bitcoin::OutPoint, Amount, u64)>> {
@@ -737,7 +749,9 @@ impl Wallet {
737749
}
738750
}
739751

740-
pub fn delete_sidechain_proposals(&self) -> Result<()> {
752+
// Gets wiped upon generating a new block.
753+
// TODO: how will this work for non-regtest?
754+
fn delete_pending_sidechain_proposals(&self) -> Result<()> {
741755
self.db_connection
742756
.lock()
743757
.execute("DELETE FROM sidechain_proposals;", ())
@@ -747,12 +761,11 @@ impl Wallet {
747761

748762
pub fn is_sidechain_active(&self, sidechain_number: SidechainNumber) -> Result<bool> {
749763
let sidechains = self.validator.get_active_sidechains()?;
750-
for sidechain in sidechains {
751-
if sidechain.proposal.sidechain_number == sidechain_number {
752-
return Ok(true);
753-
}
754-
}
755-
Ok(false)
764+
let active = sidechains
765+
.iter()
766+
.any(|sc| sc.proposal.sidechain_number == sidechain_number);
767+
768+
Ok(active)
756769
}
757770

758771
// Creates a BMM request transaction. Does NOT broadcast. `height` is the block height this

0 commit comments

Comments
 (0)