Now possible to watch same entity from multiple clients, unwatchtxid

This commit is contained in:
kexkey
2020-08-25 14:25:38 -04:00
parent bc9f36fb11
commit 22eab0c109
8 changed files with 193 additions and 24 deletions

View File

@@ -17,6 +17,7 @@ action_getactivewatchesbyxpub=watcher
action_getactivewatchesbylabel=watcher
action_getactivexpubwatches=watcher
action_watchtxid=watcher
action_unwatchtxid=watcher
action_getactivewatches=watcher
action_get_txns_by_watchlabel=watcher
action_get_unused_addresses_by_watchlabel=watcher

View File

@@ -22,6 +22,7 @@ action_getactivexpubwatches=watcher
action_get_txns_by_watchlabel=watcher
action_get_unused_addresses_by_watchlabel=watcher
action_watchtxid=watcher
action_unwatchtxid=watcher
action_getactivewatches=watcher
action_getbestblockhash=watcher
action_getbestblockinfo=watcher

View File

@@ -14,7 +14,7 @@ CREATE TABLE watching_by_pub32 (
CREATE TABLE watching (
id INTEGER PRIMARY KEY AUTOINCREMENT,
address TEXT UNIQUE,
address TEXT,
watching INTEGER DEFAULT FALSE,
callback0conf TEXT,
calledback0conf INTEGER DEFAULT FALSE,
@@ -26,6 +26,8 @@ CREATE TABLE watching (
event_message TEXT,
inserted_ts INTEGER DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_watching_address ON watching (address);
CREATE UNIQUE INDEX idx_watching_01 ON watching (address, callback0conf, callback1conf);
CREATE TABLE watching_tx (
watching_id INTEGER REFERENCES watching,
@@ -86,7 +88,7 @@ INSERT INTO batcher (id, label, conf_target, feerate) VALUES (1, "default", 6, N
CREATE TABLE watching_by_txid (
id INTEGER PRIMARY KEY AUTOINCREMENT,
txid TEXT UNIQUE,
txid TEXT,
watching INTEGER DEFAULT FALSE,
callback1conf TEXT,
calledback1conf INTEGER DEFAULT FALSE,
@@ -96,6 +98,7 @@ CREATE TABLE watching_by_txid (
inserted_ts INTEGER DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_watching_by_txid_txid ON watching_by_txid (txid);
CREATE UNIQUE INDEX idx_watching_by_txid_1x ON watching_by_txid (txid, callback1conf, callbackxconf);
CREATE TABLE stamp (
id INTEGER PRIMARY KEY AUTOINCREMENT,

View File

@@ -1,3 +1,6 @@
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
CREATE TABLE batcher (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -16,3 +19,57 @@ ALTER TABLE recipient ADD COLUMN calledback_ts INTEGER;
CREATE INDEX idx_recipient_label ON recipient (label);
ALTER TABLE tx ADD COLUMN conf_target INTEGER DEFAULT NULL;
ALTER TABLE watching RENAME TO watching_20200610;
CREATE TABLE watching (
id INTEGER PRIMARY KEY AUTOINCREMENT,
address TEXT,
watching INTEGER DEFAULT FALSE,
callback0conf TEXT,
calledback0conf INTEGER DEFAULT FALSE,
callback1conf TEXT,
calledback1conf INTEGER DEFAULT FALSE,
imported INTEGER DEFAULT FALSE,
watching_by_pub32_id INTEGER REFERENCES watching_by_pub32,
pub32_index INTEGER,
event_message TEXT,
inserted_ts INTEGER DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO watching SELECT * FROM watching_20200610;
DROP INDEX IF EXISTS idx_watching_address;
CREATE INDEX idx_watching_address ON watching (address);
DROP INDEX IF EXISTS idx_watching_01;
CREATE UNIQUE INDEX idx_watching_01 ON watching (address, callback0conf, callback1conf);
--DROP TABLE watching20200610;
ALTER TABLE watching_by_txid RENAME TO watching_by_txid_20200610;
CREATE TABLE watching_by_txid (
id INTEGER PRIMARY KEY AUTOINCREMENT,
txid TEXT,
watching INTEGER DEFAULT FALSE,
callback1conf TEXT,
calledback1conf INTEGER DEFAULT FALSE,
callbackxconf TEXT,
calledbackxconf INTEGER DEFAULT FALSE,
nbxconf INTEGER,
inserted_ts INTEGER DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO watching_by_txid SELECT * FROM watching_by_txid_20200610;
DROP INDEX IF EXISTS idx_watching_by_txid_txid;
CREATE INDEX idx_watching_by_txid_txid ON watching_by_txid (txid);
DROP INDEX IF EXISTS idx_watching_by_txid_1x;
CREATE UNIQUE INDEX idx_watching_by_txid_1x ON watching_by_txid (txid, callback1conf, callbackxconf);
--DROP TABLE watching_by_txid_20200610;
COMMIT;
PRAGMA foreign_keys=on;

View File

@@ -23,7 +23,7 @@ do_callbacks_txid() {
build_callback_txid ${row}
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq 0 ]; then
if [ "${returncode}" -eq "0" ]; then
id=$(echo "${row}" | cut -d '|' -f1)
sql "UPDATE watching_by_txid SET calledback1conf=1 WHERE id=\"${id}\""
trace_rc $?
@@ -39,7 +39,8 @@ do_callbacks_txid() {
do
build_callback_txid ${row}
returncode=$?
if [ "${returncode}" -eq 0 ]; then
trace_rc ${returncode}
if [ "${returncode}" -eq "0" ]; then
id=$(echo "${row}" | cut -d '|' -f1)
sql "UPDATE watching_by_txid SET calledbackxconf=1, watching=0 WHERE id=\"${id}\""
trace_rc $?
@@ -136,6 +137,9 @@ build_callback_txid() {
trace "[build_callback_txid] Number of confirmations for tx is not enough to call back."
return 1
fi
else
trace "[build_callback_txid] Couldn't get tx from the Bitcoin node."
return 1
fi
}

View File

@@ -100,8 +100,35 @@ main() {
;;
unwatch)
# curl (GET) 192.168.111.152:8080/unwatch/2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp
# or
# POST http://192.168.111.152:8080/unwatch
# BODY {"address":"2N8DcqzfkYi8CkYzvNNS5amoq3SbAcQNXKp","unconfirmedCallbackURL":"192.168.111.233:1111/callback0conf","confirmedCallbackURL":"192.168.111.233:1111/callback1conf"}
# or
# BODY {"id":3124}
response=$(unwatchrequest "${line}")
# args:
# - address: string, required
# - unconfirmedCallbackURL: string, optional
# - confirmedCallbackURL: string, optional
# or
# - id: the id returned by the watch
local address
local unconfirmedCallbackURL
local confirmedCallbackURL
local watchid
# Let's make it work even for a GET request (equivalent to a POST with empty json object body)
if [ "$http_method" = "POST" ]; then
address=$(echo "${line}" | jq -r ".address")
unconfirmedCallbackURL=$(echo "${line}" | jq ".unconfirmedCallbackURL")
confirmedCallbackURL=$(echo "${line}" | jq ".confirmedCallbackURL")
watchid=$(echo "${line}" | jq ".id")
else
address=$(echo "${line}" | cut -d ' ' -f2 | cut -d '/' -f3)
fi
response=$(unwatchrequest "${watchid}" "${address}" "${unconfirmedCallbackURL}" "${confirmedCallbackURL}")
response_to_client "${response}" ${?}
break
;;
@@ -158,6 +185,28 @@ main() {
response_to_client "${response}" ${?}
break
;;
unwatchtxid)
# POST http://192.168.111.152:8080/unwatchtxid
# BODY {"txid":"b081ca7724386f549cf0c16f71db6affeb52ff7a0d9b606fb2e5c43faffd3387","unconfirmedCallbackURL":"192.168.111.233:1111/callback0conf","confirmedCallbackURL":"192.168.111.233:1111/callback1conf"}
# or
# BODY {"id":3124}
# args:
# - txid: string, required
# - unconfirmedCallbackURL: string, optional
# - confirmedCallbackURL: string, optional
# or
# - id: the id returned by watchtxid
local txid=$(echo "${line}" | jq -r ".txid")
local unconfirmedCallbackURL=$(echo "${line}" | jq ".unconfirmedCallbackURL")
local confirmedCallbackURL=$(echo "${line}" | jq ".confirmedCallbackURL")
local watchid=$(echo "${line}" | jq ".id")
response=$(unwatchtxidrequest "${watchid}" "${txid}" "${unconfirmedCallbackURL}" "${confirmedCallbackURL}")
response_to_client "${response}" ${?}
break
;;
getactivewatches)
# curl (GET) 192.168.111.152:8080/getactivewatches

View File

@@ -6,16 +6,27 @@
unwatchrequest() {
trace "Entering unwatchrequest()..."
local request=${1}
local address=$(echo "${request}" | cut -d ' ' -f2 | cut -d '/' -f3)
local watchid=${1}
local address=${2}
local unconfirmedCallbackURL=${3}
local confirmedCallbackURL=${4}
local returncode
trace "[unwatchrequest] Unwatch request on address ${address}"
trace "[unwatchrequest] Unwatch request id ${watchid} on address ${address} with url0conf ${unconfirmedCallbackURL} and url1conf ${confirmedCallbackURL}"
sql "UPDATE watching SET watching=0 WHERE address=\"${address}\""
if [ "${watchid}" != "null" ]; then
sql "UPDATE watching SET watching=0 WHERE id=${watchid}"
returncode=$?
trace_rc ${returncode}
data="{\"event\":\"unwatch\",\"address\":\"${address}\"}"
data="{\"event\":\"unwatch\",\"id\":${watchid}}"
else
sql "UPDATE watching SET watching=0 WHERE address='${address}' AND callback0conf=${unconfirmedCallbackURL} AND callback1conf=${confirmedCallbackURL}"
returncode=$?
trace_rc ${returncode}
data="{\"event\":\"unwatch\",\"address\":\"${address}\",\"unconfirmedCallbackURL\":${unconfirmedCallbackURL},\"confirmedCallbackURL\":${confirmedCallbackURL}}"
fi
trace "[unwatchrequest] responding=${data}"
echo "${data}"
@@ -80,3 +91,34 @@ unwatchpub32labelrequest() {
return ${returncode}
}
unwatchtxidrequest() {
trace "Entering unwatchtxidrequest()..."
local watchid=${1}
local txid=${2}
local unconfirmedCallbackURL=${3}
local confirmedCallbackURL=${4}
local returncode
trace "[unwatchtxidrequest] Unwatch request id ${watchid} on txid ${txid} with url0conf ${unconfirmedCallbackURL} and url1conf ${confirmedCallbackURL}"
if [ "${watchid}" != "null" ]; then
sql "UPDATE watching_by_txid SET watching=0 WHERE id=${watchid}"
returncode=$?
trace_rc ${returncode}
data="{\"event\":\"unwatchtxid\",\"id\":${watchid}}"
else
sql "UPDATE watching_by_txid SET watching=0 WHERE txid='${txid}' AND callback0conf=${unconfirmedCallbackURL} AND callback1conf=${confirmedCallbackURL}"
returncode=$?
trace_rc ${returncode}
data="{\"event\":\"unwatchtxid\",\"txid\":\"${txid}\",\"unconfirmedCallbackURL\":${unconfirmedCallbackURL},\"confirmedCallbackURL\":${confirmedCallbackURL}}"
fi
trace "[unwatchtxidrequest] responding=${data}"
echo "${data}"
return ${returncode}
}

View File

@@ -52,12 +52,14 @@ watchrequest() {
imported=0
fi
sql "INSERT INTO watching (address, watching, callback0conf, callback1conf, imported, event_message) VALUES (\"${address}\", 1, ${cb0conf_url}, ${cb1conf_url}, ${imported}, ${event_message}) ON CONFLICT(address) DO UPDATE SET watching=1, callback0conf=excluded.callback0conf, calledback0conf=0, callback1conf=excluded.callback1conf, calledback1conf=0, event_message=excluded.event_message"
# sql "INSERT INTO watching (address, watching, callback0conf, callback1conf, imported, event_message) VALUES (\"${address}\", 1, ${cb0conf_url}, ${cb1conf_url}, ${imported}, ${event_message}) ON CONFLICT(address) DO UPDATE SET watching=1, callback0conf=excluded.callback0conf, calledback0conf=0, callback1conf=excluded.callback1conf, calledback1conf=0, event_message=excluded.event_message"
sql "INSERT INTO watching (address, watching, callback0conf, callback1conf, imported, event_message) VALUES (\"${address}\", 1, ${cb0conf_url}, ${cb1conf_url}, ${imported}, ${event_message}) ON CONFLICT DO UPDATE watching SET watching=1, event_message=${event_message}, calledback0conf=0, calledback1conf=0"
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -eq 0 ]; then
inserted=1
id_inserted=$(sql "SELECT id FROM watching WHERE address='${address}'")
id_inserted=$(sql "SELECT id FROM watching WHERE address='${address}' AND callback0conf=${cb0conf_url} AND callback1conf=${cb1conf_url}")
trace "[watchrequest] id_inserted: ${id_inserted}"
else
inserted=0
@@ -78,15 +80,15 @@ watchrequest() {
result="{\"id\":\"${id_inserted}\",
\"event\":\"watch\",
\"imported\":\"${imported}\",
\"inserted\":\"${inserted}\",
\"imported\":${imported},
\"inserted\":${inserted},
\"address\":\"${address}\",
\"unconfirmedCallbackURL\":${cb0conf_url},
\"confirmedCallbackURL\":${cb1conf_url},
\"estimatesmartfee2blocks\":\"${fees2blocks}\",
\"estimatesmartfee6blocks\":\"${fees6blocks}\",
\"estimatesmartfee36blocks\":\"${fees36blocks}\",
\"estimatesmartfee144blocks\":\"${fees144blocks}\",
\"estimatesmartfee2blocks\":${fees2blocks},
\"estimatesmartfee6blocks\":${fees6blocks},
\"estimatesmartfee36blocks\":${fees36blocks},
\"estimatesmartfee144blocks\":${fees144blocks},
\"eventMessage\":${event_message}}"
trace "[watchrequest] responding=${result}"
@@ -270,7 +272,8 @@ insert_watches() {
inserted_values="${inserted_values})"
done
sql "INSERT INTO watching (address, watching, callback0conf, callback1conf, imported, watching_by_pub32_id, pub32_index) VALUES ${inserted_values} ON CONFLICT(address) DO UPDATE SET watching=1, callback0conf=excluded.callback0conf, calledback0conf=0, callback1conf=excluded.callback1conf, calledback1conf=0"
# sql "INSERT INTO watching (address, watching, callback0conf, callback1conf, imported, watching_by_pub32_id, pub32_index) VALUES ${inserted_values} ON CONFLICT(address) DO UPDATE SET watching=1, callback0conf=excluded.callback0conf, calledback0conf=0, callback1conf=excluded.callback1conf, calledback1conf=0"
sql "INSERT INTO watching (address, watching, callback0conf, callback1conf, imported, watching_by_pub32_id, pub32_index) VALUES ${inserted_values} ON CONFLICT DO UPDATE watching SET watching=1, event_message=${event_message}, calledback0conf=0, calledback1conf=0"
returncode=$?
trace_rc ${returncode}
@@ -313,7 +316,7 @@ extend_watchers() {
# we want to extend the watched addresses to 166 if our gap is 100 (default).
trace "[extend_watchers] We have addresses to add to watchers!"
watchpub32 "${label}" "${pub32}" "${derivation_path}" $((${last_imported_n} + 1)) "${callback0conf}" "${callback1conf}" ${upgrade_to_n} > /dev/null
watchpub32 "${label}" "${pub32}" "${derivation_path}" "$((${last_imported_n} + 1))" "${callback0conf}" "${callback1conf}" "${upgrade_to_n}" > /dev/null
returncode=$?
trace_rc ${returncode}
else
@@ -342,12 +345,21 @@ watchtxidrequest() {
local result
trace "[watchtxidrequest] Watch request on txid (${txid}), cb 1-conf (${cb1conf_url}) and cb x-conf (${cbxconf_url}) on ${nbxconf} confirmations."
sql "INSERT OR IGNORE INTO watching_by_txid (txid, watching, callback1conf, callbackxconf, nbxconf) VALUES (${txid}, 1, ${cb1conf_url}, ${cbxconf_url}, ${nbxconf})"
# sql "INSERT OR IGNORE INTO watching_by_txid (txid, watching, callback1conf, callbackxconf, nbxconf) VALUES (${txid}, 1, ${cb1conf_url}, ${cbxconf_url}, ${nbxconf})"
sql "INSERT INTO watching_by_txid (txid, watching, callback1conf, callbackxconf, nbxconf) VALUES (${txid}, 1, ${cb1conf_url}, ${cbxconf_url}, ${nbxconf})"
returncode=$?
trace_rc ${returncode}
if [ "${returncode}" -ne "0" ]; then
trace "[watchtxidrequest] txid with urls already being watched, updating with new values based on supplied txid, 1confurl and xconfurl..."
sql "UPDATE watching_by_txid SET watching=1, nbxconf=${nbxconf}, calledback1conf=0, calledbackxconf=0 WHERE txid=${txid} AND callback1conf=${cb1conf_url} AND callbackxconf=${cbxconf_url}"
returncode=$?
trace_rc ${returncode}
fi
if [ "${returncode}" -eq 0 ]; then
inserted=1
id_inserted=$(sql "SELECT id FROM watching_by_txid WHERE txid=${txid}")
id_inserted=$(sql "SELECT id FROM watching_by_txid WHERE txid=${txid} AND callback1conf=${cb1conf_url} AND callbackxconf=${cbxconf_url}")
trace "[watchtxidrequest] id_inserted: ${id_inserted}"
else
inserted=0