Skip to content

Commit

Permalink
Merge #920: Wallet creation time
Browse files Browse the repository at this point in the history
7917794 add timestamp field to getinfo (pythcoiner)

Pull request description:

  (partially) address #805
  - [x] daemon
  - [x] tests
  - [x] doc

ACKs for top commit:
  darosior:
    ACK 7917794

Tree-SHA512: ee72761174a09871092fe13313e18180530c1a9c4ef5ddf63cb196760a67bc4bac3c9af134d4be5e464f1ac0e16fed726693340499d192eb9a4d68471bbc2ced
  • Loading branch information
darosior committed Jan 23, 2024
2 parents ec7ac88 + 7917794 commit e9b5a7a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
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},
};

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()
assert res["version"] == "4.0.0-dev"
assert res["network"] == "regtest"
wait_for(lambda: lianad.rpc.getinfo()["block_height"] == 101)
Expand Down

0 comments on commit e9b5a7a

Please sign in to comment.