From ac2880bb230ec519ff3d271fb7117fa268674692 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Wed, 14 Jul 2021 11:12:11 -0700 Subject: [PATCH] Monitor mempool for txns --- .vscode/tasks.json | 2 +- native/Cargo.lock | 2 +- native/Cargo.toml | 2 +- native/src/lib.rs | 36 +++++++++++++------------- src/rpc.ts | 64 ++++++++++++++++++++++++++++++++++++++++++---- yarn.lock | 5 ++++ 6 files changed, 85 insertions(+), 26 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index f889d29..67037d5 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -5,7 +5,7 @@ "tasks": [ { "type": "npm", - "script": "dev", + "script": "start", "group": { "kind": "build", "isDefault": true }, "problemMatcher": [] }, diff --git a/native/Cargo.lock b/native/Cargo.lock index 17e78af..46449aa 100644 --- a/native/Cargo.lock +++ b/native/Cargo.lock @@ -2636,7 +2636,7 @@ dependencies = [ [[package]] name = "zecwalletlitelib" version = "0.1.0" -source = "git+https://github.com/adityapk00/zecwallet-light-cli?rev=623be1872b92edf441212d4a68b45e63e3036c2c#623be1872b92edf441212d4a68b45e63e3036c2c" +source = "git+https://github.com/adityapk00/zecwallet-light-cli?rev=bcf881d4ff29ff267c09d6abc4244ec09717da5a#bcf881d4ff29ff267c09d6abc4244ec09717da5a" dependencies = [ "arr_macro", "base58", diff --git a/native/Cargo.toml b/native/Cargo.toml index 5d6c71e..347e618 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -15,6 +15,6 @@ default-features = false features = ["napi-6"] [dependencies] -zecwalletlitelib = { git = "https://github.com/adityapk00/zecwallet-light-cli", rev = "623be1872b92edf441212d4a68b45e63e3036c2c" } +zecwalletlitelib = { git = "https://github.com/adityapk00/zecwallet-light-cli", rev = "bcf881d4ff29ff267c09d6abc4244ec09717da5a" } #zecwalletlitelib = { path = "../../zecwallet-light-cli/lib" } lazy_static = "1.4.0" diff --git a/native/src/lib.rs b/native/src/lib.rs index 6eebfb6..17e0643 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -25,7 +25,7 @@ lazy_static! { } register_module!(mut m, { - m.export_function("litelib_say_hello", litelib_say_hello)?; + //m.export_function("litelib_say_hello", litelib_say_hello)?; m.export_function("litelib_wallet_exists", litelib_wallet_exists)?; m.export_function("litelib_initialize_new", litelib_initialize_new)?; m.export_function("litelib_initialize_existing", litelib_initialize_existing)?; @@ -38,13 +38,13 @@ register_module!(mut m, { Ok(()) }); -fn litelib_say_hello(mut cx: FunctionContext) -> JsResult { - let to = cx.argument::(0)?.value(&mut cx); +// fn litelib_say_hello(mut cx: FunctionContext) -> JsResult { +// let to = cx.argument::(0)?.value(&mut cx); - let ret = format!("Hello {}", to); +// let ret = format!("Hello {}", to); - Ok(cx.string(ret)) -} +// Ok(cx.string(ret)) +// } // Check if there is an existing wallet fn litelib_wallet_exists(mut cx: FunctionContext) -> JsResult { @@ -84,10 +84,10 @@ fn litelib_initialize_new(mut cx: FunctionContext) -> JsResult { } }; - LIGHTCLIENT - .lock() - .unwrap() - .replace(Some(Arc::new(lightclient))); + let lc = Arc::new(lightclient); + LightClient::start_mempool_monitor(lc.clone()); + + LIGHTCLIENT.lock().unwrap().replace(Some(lc)); // Return the wallet's seed seed @@ -123,10 +123,10 @@ fn litelib_initialize_new_from_phrase(mut cx: FunctionContext) -> JsResult JsResult { // Initialize logging let _ = lightclient.init_logging(); - LIGHTCLIENT - .lock() - .unwrap() - .replace(Some(Arc::new(lightclient))); + let lc = Arc::new(lightclient); + LightClient::start_mempool_monitor(lc.clone()); + + LIGHTCLIENT.lock().unwrap().replace(Some(lc)); format!("OK") }; diff --git a/src/rpc.ts b/src/rpc.ts index d8a58ef..aa91960 100644 --- a/src/rpc.ts +++ b/src/rpc.ts @@ -23,8 +23,12 @@ export default class RPC { fnSetAllAddresses: (a: string[]) => void; fnSetZecPrice: (p?: number) => void; refreshTimerID?: NodeJS.Timeout; + updateTimerId?: NodeJS.Timeout; + + updateDataLock: boolean; lastBlockHeight: number; + lastTxId?: string; constructor( fnSetTotalBalance: (tb: TotalBalance) => void, @@ -43,13 +47,19 @@ export default class RPC { this.lastBlockHeight = 0; this.refreshTimerID = undefined; + this.updateTimerId = undefined; + this.updateDataLock = false; } async configure(rpcConfig: RPCConfig) { this.rpcConfig = rpcConfig; if (!this.refreshTimerID) { - this.refreshTimerID = setInterval(() => this.refresh(false), 60 * 1000); + this.refreshTimerID = setInterval(() => this.refresh(false), 3 * 60 * 1000); // 3 mins + } + + if (!this.updateTimerId) { + this.updateTimerId = setInterval(() => this.updateData(), 3 * 1000); // 3 secs } // Immediately call the refresh after configure to update the UI @@ -61,6 +71,11 @@ export default class RPC { clearInterval(this.refreshTimerID); this.refreshTimerID = undefined; } + + if (this.updateTimerId) { + clearInterval(this.updateTimerId); + this.updateTimerId = undefined; + } } static getDefaultFee(): number { @@ -96,6 +111,36 @@ export default class RPC { console.log(`Deinitialize status: ${str}`); } + async updateData() { + //console.log("Update data triggered"); + if (this.updateDataLock) { + //console.log("Update lock, returning"); + return; + } + + this.updateDataLock = true; + const latest_txid = RPC.getLastTxid(); + console.log(`Latest: ${latest_txid}, prev = ${this.lastTxId}`); + if (this.lastTxId !== latest_txid) { + const latestBlockHeight = await this.fetchInfo(); + this.lastBlockHeight = latestBlockHeight; + this.lastTxId = latest_txid; + + //console.log("Update data fetching new txns"); + + // And fetch the rest of the data. + this.fetchTotalBalance(); + this.fetchTandZTransactions(latestBlockHeight); + this.getZecPrice(); + + // Save the wallet + RPC.doSave(); + + //console.log(`Finished update data at ${latestBlockHeight}`); + } + this.updateDataLock = false; + } + async refresh(fullRefresh: boolean) { const latestBlockHeight = await this.fetchInfo(); @@ -115,16 +160,17 @@ export default class RPC { // We are synced. Cancel the poll timer clearInterval(pollerID); - // Save the wallet - RPC.doSave(); - // And fetch the rest of the data. this.fetchTotalBalance(); this.fetchTandZTransactions(latestBlockHeight); this.getZecPrice(); this.lastBlockHeight = latestBlockHeight; - // All done, set up next fetch + + // Save the wallet + RPC.doSave(); + + // All done console.log(`Finished full refresh at ${latestBlockHeight}`); } }, 1000); @@ -248,6 +294,13 @@ export default class RPC { this.fnSetAllAddresses(allAddresses); } + static getLastTxid(): string { + const lastTxid = native.litelib_execute("lasttxid", ""); + const lastTxidJSON = JSON.parse(lastTxid); + + return lastTxidJSON.last_txid; + } + static getPrivKeyAsString(address: string): string { const privKeyStr = native.litelib_execute("export", address); const privKeyJSON = JSON.parse(privKeyStr); @@ -287,6 +340,7 @@ export default class RPC { fetchTandZTransactions(latestBlockHeight: number) { const listStr = native.litelib_execute("list", ""); const listJSON = JSON.parse(listStr); + //console.log(listJSON); let txlist: Transaction[] = listJSON.map((tx: any) => { const transaction = new Transaction(); diff --git a/yarn.lock b/yarn.lock index fe5b507..002d7e8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2118,6 +2118,11 @@ dependencies: source-map "^0.6.1" +"@types/url-parse@^1.4.3": + version "1.4.3" + resolved "https://registry.yarnpkg.com/@types/url-parse/-/url-parse-1.4.3.tgz#fba49d90f834951cb000a674efee3d6f20968329" + integrity sha512-4kHAkbV/OfW2kb5BLVUuUMoumB3CP8rHqlw48aHvFy5tf9ER0AfOonBlX29l/DD68G70DmyhRlSYfQPSYpC5Vw== + "@types/verror@^1.10.3": version "1.10.4" resolved "https://registry.yarnpkg.com/@types/verror/-/verror-1.10.4.tgz#805c0612b3a0c124cf99f517364142946b74ba3b"