Monitor mempool for txns

This commit is contained in:
Aditya Kulkarni 2021-07-14 11:12:11 -07:00
parent b65a7736ee
commit ac2880bb23
6 changed files with 85 additions and 26 deletions

2
.vscode/tasks.json vendored
View File

@ -5,7 +5,7 @@
"tasks": [
{
"type": "npm",
"script": "dev",
"script": "start",
"group": { "kind": "build", "isDefault": true },
"problemMatcher": []
},

2
native/Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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<JsString> {
let to = cx.argument::<JsString>(0)?.value(&mut cx);
// fn litelib_say_hello(mut cx: FunctionContext) -> JsResult<JsString> {
// let to = cx.argument::<JsString>(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<JsBoolean> {
@ -84,10 +84,10 @@ fn litelib_initialize_new(mut cx: FunctionContext) -> JsResult<JsString> {
}
};
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<JsStr
// 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")
};
@ -157,10 +157,10 @@ fn litelib_initialize_existing(mut cx: FunctionContext) -> JsResult<JsString> {
// 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")
};

View File

@ -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();

View File

@ -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"