From 79177945ada4803af409f963dd6185d436aa6e21 Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Sun, 14 Jan 2024 07:09:20 +0100 Subject: [PATCH] add timestamp field to getinfo --- doc/API.md | 5 +++-- src/commands/mod.rs | 3 +++ src/database/mod.rs | 7 +++++++ src/testutils.rs | 15 +++++++++++++++ tests/test_rpc.py | 1 + 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/doc/API.md b/doc/API.md index d629bc7c0..e0ea64d2f 100644 --- a/doc/API.md +++ b/doc/API.md @@ -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` diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 751726fa3..d4fecbc47 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -294,6 +294,7 @@ impl DaemonControl { main: self.config.main_descriptor.clone(), }, rescan_progress, + timestamp: db_conn.timestamp(), } } @@ -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, + /// Timestamp at wallet creation date + pub timestamp: u32, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/src/database/mod.rs b/src/database/mod.rs index 9c2be7c6a..050581d5b 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -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); @@ -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) } diff --git a/src/testutils.rs b/src/testutils.rs index c175c1566..9034aaec2 100644 --- a/src/testutils.rs +++ b/src/testutils.rs @@ -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::{ @@ -129,6 +131,7 @@ struct DummyDbState { curr_tip: Option, coins: HashMap, spend_txs: HashMap)>, + timestamp: u32, } pub struct DummyDatabase { @@ -145,6 +148,13 @@ 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(), @@ -152,6 +162,7 @@ impl DummyDatabase { curr_tip: None, coins: HashMap::new(), spend_txs: HashMap::new(), + timestamp: now, })), } } @@ -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); } diff --git a/tests/test_rpc.py b/tests/test_rpc.py index 65c264295..18476c978 100644 --- a/tests/test_rpc.py +++ b/tests/test_rpc.py @@ -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)