Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet creation time #920

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ This command does not take any parameter for now.

#### Response

| Field | Type | Description |
| -------------------- | ------- | -------------------------------------------------------------------------------------------------- |
| Field | Type | Description |
| -------------------- | ------------- | -------------------------------------------------------------------------------------------- |
| `version` | string | Version following the [SimVer](http://www.simver.org/) format |
| `network` | string | Answer can be `mainnet`, `testnet`, `regtest` |
| `block_height` | integer | The block height we are synced at. |
| `sync` | float | The synchronization progress as percentage (`0 < sync < 1`) |
| `descriptors` | object | Object with the name of the descriptor as key and the descriptor string as value |
| `rescan_progress` | float or null | Progress of an ongoing rescan as a percentage (between 0 and 1) if there is any |
| `timestamp` | integer | Unix timestamp of wallet creation date |

### `getnewaddress`

Expand Down
3 changes: 3 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl DaemonControl {
main: self.config.main_descriptor.clone(),
},
rescan_progress,
timestamp: db_conn.timestamp(),
}
}

Expand Down Expand Up @@ -1030,6 +1031,8 @@ pub struct GetInfoResult {
pub descriptors: GetInfoDescriptors,
/// The progress as a percentage (between 0 and 1) of an ongoing rescan if there is any
pub rescan_progress: Option<f64>,
/// Timestamp at wallet creation date
pub timestamp: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
7 changes: 7 additions & 0 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub trait DatabaseConnection {
/// The network we are operating on.
fn network(&mut self) -> bitcoin::Network;

/// The timestamp at wallet creation time
fn timestamp(&mut self) -> u32;

/// Update our best chain seen.
fn update_tip(&mut self, tip: &BlockChainTip);

Expand Down Expand Up @@ -161,6 +164,10 @@ impl DatabaseConnection for SqliteConn {
self.db_tip().network
}

fn timestamp(&mut self) -> u32 {
self.db_wallet().timestamp
}

fn update_tip(&mut self, tip: &BlockChainTip) {
self.update_tip(tip)
}
Expand Down
15 changes: 15 additions & 0 deletions src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use crate::{
descriptors, DaemonHandle,
};

use std::convert::TryInto;
use std::{
collections::{HashMap, HashSet},
env, fs, io, path, process,
str::FromStr,
sync, thread, time,
time::{SystemTime, UNIX_EPOCH},
Comment on lines +8 to +14
Copy link
Member

Choose a reason for hiding this comment

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

Nit, for next time:

Suggested change
use std::convert::TryInto;
use std::{
collections::{HashMap, HashSet},
env, fs, io, path, process,
str::FromStr,
sync, thread, time,
time::{SystemTime, UNIX_EPOCH},
use std::{
collections::{HashMap, HashSet},
convert::TryInto, env, fs, io, path, process,
str::FromStr,
sync, thread, time,
time::{SystemTime, UNIX_EPOCH},

};

use miniscript::{
Expand Down Expand Up @@ -129,6 +131,7 @@ struct DummyDbState {
curr_tip: Option<BlockChainTip>,
coins: HashMap<bitcoin::OutPoint, Coin>,
spend_txs: HashMap<bitcoin::Txid, (Psbt, Option<u32>)>,
timestamp: u32,
}

pub struct DummyDatabase {
Expand All @@ -145,13 +148,21 @@ impl DatabaseInterface for DummyDatabase {

impl DummyDatabase {
pub fn new() -> DummyDatabase {
let now: u32 = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
.try_into()
.unwrap();

DummyDatabase {
db: sync::Arc::new(sync::RwLock::new(DummyDbState {
deposit_index: 0.into(),
change_index: 0.into(),
curr_tip: None,
coins: HashMap::new(),
spend_txs: HashMap::new(),
timestamp: now,
})),
}
}
Expand All @@ -172,6 +183,10 @@ impl DatabaseConnection for DummyDatabase {
self.db.read().unwrap().curr_tip
}

fn timestamp(&mut self) -> u32 {
self.db.read().unwrap().timestamp
}

fn update_tip(&mut self, tip: &BlockChainTip) {
self.db.write().unwrap().curr_tip = Some(*tip);
}
Expand Down
1 change: 1 addition & 0 deletions tests/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

def test_getinfo(lianad):
res = lianad.rpc.getinfo()
assert 'timestamp' in res.keys()
Copy link
Member

Choose a reason for hiding this comment

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

nit: could've sanity checked the value. For instance that it's after now - 1 day and lower or equal to now.

assert res["version"] == "4.0.0-dev"
assert res["network"] == "regtest"
wait_for(lambda: lianad.rpc.getinfo()["block_height"] == 101)
Expand Down
Loading