Added info in batch webhooks

This commit is contained in:
kexkey
2020-08-07 17:51:34 -04:00
parent 9af47b9940
commit d91f1af52f
4 changed files with 40 additions and 14 deletions

View File

@@ -48,6 +48,7 @@ CREATE TABLE tx (
blockhash TEXT, blockhash TEXT,
blockheight INTEGER, blockheight INTEGER,
blocktime INTEGER, blocktime INTEGER,
conf_target INTEGER,
raw_tx TEXT, raw_tx TEXT,
inserted_ts INTEGER DEFAULT CURRENT_TIMESTAMP inserted_ts INTEGER DEFAULT CURRENT_TIMESTAMP
); );

View File

@@ -14,3 +14,5 @@ ALTER TABLE recipient ADD COLUMN label INTEGER REFERENCES batcher;
ALTER TABLE recipient ADD COLUMN calledback INTEGER DEFAULT FALSE; ALTER TABLE recipient ADD COLUMN calledback INTEGER DEFAULT FALSE;
ALTER TABLE recipient ADD COLUMN calledback_ts INTEGER; ALTER TABLE recipient ADD COLUMN calledback_ts INTEGER;
CREATE INDEX idx_recipient_label ON recipient (label); CREATE INDEX idx_recipient_label ON recipient (label);
ALTER TABLE tx ADD COLUMN conf_target INTEGER DEFAULT NULL;

View File

@@ -448,7 +448,7 @@ batchspend() {
row=$(sql "SELECT COUNT(id), COALESCE(MIN(inserted_ts), 0), COALESCE(SUM(amount), 0.00000000) FROM recipient WHERE tx_id IS NULL AND batcher_id=${batcher_id}") row=$(sql "SELECT COUNT(id), COALESCE(MIN(inserted_ts), 0), COALESCE(SUM(amount), 0.00000000) FROM recipient WHERE tx_id IS NULL AND batcher_id=${batcher_id}")
# Let's insert the txid in our little DB -- then we'll already have it when receiving confirmation # Let's insert the txid in our little DB -- then we'll already have it when receiving confirmation
id_inserted=$(sql "INSERT OR IGNORE INTO tx (txid, hash, confirmations, timereceived, fee, size, vsize, is_replaceable, raw_tx) VALUES (\"${txid}\", ${tx_hash}, 0, ${tx_ts_firstseen}, ${fees}, ${tx_size}, ${tx_vsize}, ${tx_replaceable}, readfile('batchspend-rawtx-${txid}.blob')); SELECT LAST_INSERT_ROWID();") id_inserted=$(sql "INSERT OR IGNORE INTO tx (txid, hash, confirmations, timereceived, fee, size, vsize, is_replaceable, conf_target, raw_tx) VALUES (\"${txid}\", ${tx_hash}, 0, ${tx_ts_firstseen}, ${fees}, ${tx_size}, ${tx_vsize}, ${tx_replaceable}, ${conf_target}, readfile('batchspend-rawtx-${txid}.blob')); SELECT LAST_INSERT_ROWID();")
returncode=$? returncode=$?
trace_rc ${returncode} trace_rc ${returncode}
if [ "${returncode}" -eq 0 ]; then if [ "${returncode}" -eq 0 ]; then
@@ -469,13 +469,13 @@ batchspend() {
trace "[batchspend] total=${total}" trace "[batchspend] total=${total}"
response='{"result":{"batcherId":'${batcher_id}',"confTarget":'${conf_target}',"nbOutputs":'${count}',"oldest":"'${oldest}'","total":'${total} response='{"result":{"batcherId":'${batcher_id}',"confTarget":'${conf_target}',"nbOutputs":'${count}',"oldest":"'${oldest}'","total":'${total}
response="${response},\"txid\":\"${txid}\",\"hash\":${tx_hash},\"details\":{\"firstseen\":${tx_ts_firstseen},\"size\":${tx_size},\"vsize\":${tx_vsize},\"replaceable\":${tx_replaceable},\"fee\":${fees}},\"outputs\":[${webhooks_data}]}" response="${response},\"status\":\"accepted\",\"txid\":\"${txid}\",\"hash\":${tx_hash},\"details\":{\"firstseen\":${tx_ts_firstseen},\"size\":${tx_size},\"vsize\":${tx_vsize},\"replaceable\":${tx_replaceable},\"fee\":${fees}},\"outputs\":[${webhooks_data}]}"
response="${response},\"error\":null}" response="${response},\"error\":null}"
# Delete the temp file containing the raw tx (see above) # Delete the temp file containing the raw tx (see above)
rm batchspend-rawtx-${txid}.blob rm batchspend-rawtx-${txid}.blob
batch_webhooks "[${webhooks_data}]" '"batcherId":'${batcher_id}',"txid":"'${txid}'","hash":'${tx_hash}',"details":{"firstseen":'${tx_ts_firstseen}',"size":'${tx_size}',"vsize":'${tx_vsize}',"replaceable":'${tx_replaceable}',"fee":'${fees}'}' batch_webhooks "[${webhooks_data}]" '"batcherId":'${batcher_id}',"confTarget":'${conf_target}',"nbOutputs":'${count}',"oldest":"'${oldest}'","total":'${total}',"status":"accepted","txid":"'${txid}'","hash":'${tx_hash}',"details":{"firstseen":'${tx_ts_firstseen}',"size":'${tx_size}',"vsize":'${tx_vsize}',"replaceable":'${tx_replaceable}',"fee":'${fees}'}'
else else
local message=$(echo "${data}" | jq -e ".error.message") local message=$(echo "${data}" | jq -e ".error.message")
@@ -503,8 +503,14 @@ batch_check_webhooks() {
local tx_vsize local tx_vsize
local tx_replaceable local tx_replaceable
local fees local fees
local conf_target
local row
local count
local oldest
local total
local tx_id
local batching=$(sql "SELECT address, amount, r.id, webhook_url, b.id, t.txid, t.hash, t.timereceived, t.fee, t.size, t.vsize, t.is_replaceable FROM recipient r, batcher b, tx t WHERE r.batcher_id=b.id AND r.tx_id=t.id AND NOT calledback AND tx_id IS NOT NULL AND webhook_url IS NOT NULL") local batching=$(sql "SELECT address, amount, r.id, webhook_url, b.id, t.txid, t.hash, t.timereceived, t.fee, t.size, t.vsize, t.is_replaceable, t.conf_target, t.id FROM recipient r, batcher b, tx t WHERE r.batcher_id=b.id AND r.tx_id=t.id AND NOT calledback AND tx_id IS NOT NULL AND webhook_url IS NOT NULL")
trace "[batch_check_webhooks] batching=${batching}" trace "[batch_check_webhooks] batching=${batching}"
local IFS=$'\n' local IFS=$'\n'
@@ -527,18 +533,35 @@ batch_check_webhooks() {
trace "[batch_check_webhooks] tx_hash=${tx_hash}" trace "[batch_check_webhooks] tx_hash=${tx_hash}"
tx_ts_firstseen=$(echo "${row}" | cut -d '|' -f8) tx_ts_firstseen=$(echo "${row}" | cut -d '|' -f8)
trace "[batch_check_webhooks] tx_ts_firstseen=${tx_ts_firstseen}" trace "[batch_check_webhooks] tx_ts_firstseen=${tx_ts_firstseen}"
tx_size=$(echo "${row}" | cut -d '|' -f9) fees=$(echo "${row}" | cut -d '|' -f9)
trace "[batch_check_webhooks] tx_size=${tx_size}"
tx_vsize=$(echo "${row}" | cut -d '|' -f10)
trace "[batch_check_webhooks] tx_vsize=${tx_vsize}"
tx_replaceable=$(echo "${row}" | cut -d '|' -f11)
trace "[batch_check_webhooks] tx_replaceable=${tx_replaceable}"
fees=$(echo "${row}" | cut -d '|' -f12)
trace "[batch_check_webhooks] fees=${fees}" trace "[batch_check_webhooks] fees=${fees}"
tx_size=$(echo "${row}" | cut -d '|' -f10)
trace "[batch_check_webhooks] tx_size=${tx_size}"
tx_vsize=$(echo "${row}" | cut -d '|' -f11)
trace "[batch_check_webhooks] tx_vsize=${tx_vsize}"
tx_replaceable=$(echo "${row}" | cut -d '|' -f12)
trace "[batch_check_webhooks] tx_replaceable=${tx_replaceable}"
conf_target=$(echo "${row}" | cut -d '|' -f13)
trace "[batch_check_webhooks] conf_target=${conf_target}"
tx_id=$(echo "${row}" | cut -d '|' -f14)
trace "[batch_check_webhooks] tx_id=${tx_id}"
webhooks_data="{\"outputId\":${recipient_id},\"address\":\"${address}\",\"amount\":${amount},\"webhookUrl\":\"${webhook_url}\"}" webhooks_data="{\"outputId\":${recipient_id},\"address\":\"${address}\",\"amount\":${amount},\"webhookUrl\":\"${webhook_url}\"}"
batch_webhooks "[${webhooks_data}]" '"batcherId":'${batcher_id}',"txid":"'${txid}'","hash":"'${tx_hash}'","details":{"firstseen":'${tx_ts_firstseen}',"size":'${tx_size}',"vsize":'${tx_vsize}',"replaceable":'${tx_replaceable}',"fee":'${fees}'}' # I know this query for each output is not very efficient, but this function should not execute often, only in case of
# failed callbacks on batches...
# Get the info on the batch
row=$(sql "SELECT COUNT(id), COALESCE(MIN(inserted_ts), 0), COALESCE(SUM(amount), 0.00000000) FROM recipient r WHERE tx_id=\"${tx_id}\"")
# Use the selected row above
count=$(echo "${row}" | cut -d '|' -f1)
trace "[batchspend] count=${count}"
oldest=$(echo "${row}" | cut -d '|' -f2)
trace "[batchspend] oldest=${oldest}"
total=$(echo "${row}" | cut -d '|' -f3)
trace "[batchspend] total=${total}"
batch_webhooks "[${webhooks_data}]" '"batcherId":'${batcher_id}',"confTarget":'${conf_target}',"nbOutputs":'${count}',"oldest":"'${oldest}'","total":'${total}',"status":"accepted","txid":"'${txid}'","hash":"'${tx_hash}'","details":{"firstseen":'${tx_ts_firstseen}',"size":'${tx_size}',"vsize":'${tx_vsize}',"replaceable":'${tx_replaceable}',"fee":'${fees}'}'
done done
} }

View File

@@ -69,7 +69,7 @@ spend() {
######################################################################################################## ########################################################################################################
# Let's insert the txid in our little DB -- then we'll already have it when receiving confirmation # Let's insert the txid in our little DB -- then we'll already have it when receiving confirmation
sql "INSERT OR IGNORE INTO tx (txid, hash, confirmations, timereceived, fee, size, vsize, is_replaceable, raw_tx) VALUES (\"${txid}\", ${tx_hash}, 0, ${tx_ts_firstseen}, ${fees}, ${tx_size}, ${tx_vsize}, ${tx_replaceable}, readfile('spend-rawtx-${txid}-$$.blob'))" sql "INSERT OR IGNORE INTO tx (txid, hash, confirmations, timereceived, fee, size, vsize, is_replaceable, conf_target, raw_tx) VALUES (\"${txid}\", ${tx_hash}, 0, ${tx_ts_firstseen}, ${fees}, ${tx_size}, ${tx_vsize}, ${tx_replaceable}, ${conf_target}, readfile('spend-rawtx-${txid}-$$.blob'))"
trace_rc $? trace_rc $?
id_inserted=$(sql "SELECT id FROM tx WHERE txid=\"${txid}\"") id_inserted=$(sql "SELECT id FROM tx WHERE txid=\"${txid}\"")
trace_rc $? trace_rc $?
@@ -77,7 +77,7 @@ spend() {
trace_rc $? trace_rc $?
data="{\"status\":\"accepted\"" data="{\"status\":\"accepted\""
data="${data},\"txid\":\"${txid}\",\"hash\":\"${tx_hash}\",\"details\":{\"address\":\"${address}\",\"amount\":${amount},\"firstseen\":${tx_ts_firstseen},\"size\":${tx_size},\"vsize\":${tx_vsize},\"replaceable\":${tx_replaceable},\"fee\":${fees},\"subtractfeefromamount\":${subtractfeefromamount}}}" data="${data},\"txid\":\"${txid}\",\"hash\":${tx_hash},\"details\":{\"address\":\"${address}\",\"amount\":${amount},\"firstseen\":${tx_ts_firstseen},\"size\":${tx_size},\"vsize\":${tx_vsize},\"replaceable\":${tx_replaceable},\"fee\":${fees},\"subtractfeefromamount\":${subtractfeefromamount}}}"
# Delete the temp file containing the raw tx (see above) # Delete the temp file containing the raw tx (see above)
rm spend-rawtx-${txid}-$$.blob rm spend-rawtx-${txid}-$$.blob