Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/async walletnotify #266

Merged
merged 9 commits into from
Jun 17, 2022

Conversation

phillamy
Copy link
Contributor

First commit usng MQTT to send events from core to Proxy. Event are filtrer t send watching wallets tx only.

Next: remove code in confirmation and use data received in sub message

@phillamy phillamy closed this May 10, 2022
@phillamy phillamy deleted the features/async-walletnotify branch May 10, 2022 23:43
@phillamy phillamy restored the features/async-walletnotify branch May 10, 2022 23:46
@phillamy phillamy reopened this May 10, 2022
@phillamy phillamy changed the base branch from master to dev May 10, 2022 23:53
@phillamy
Copy link
Contributor Author

phillamy commented May 16, 2022

I added coreutils in Dockerfile to use the most recent version of base64.

The date command changed breaking this. -D Option is not an option anymore:
inserted_ts=$(date -d "$(echo "${watching}" | cut -d '|' -f2)" -D '%Y-%m-%d %H:%M:%S' +"%s")

Line replaced by (in manage_missed_conf.sh):
inserted_ts=$(date -d "$(echo "${watching}" | cut -d '|' -f2)" +%s)

@phillamy
Copy link
Contributor Author

Core will filter out transactions based on the wallet and will only send trx concerning the "watching" wallets. Core sends the trx details to the broker since it had to do a "gettransaction" to identify the wallet. The proxy will not have to do this anymore

@phillamy
Copy link
Contributor Author

I had to build a new core image called "async-blocknotify". It brings a script that gets called by walletnotify=...

In setup.sh, you can see BITCOIN_VERSION="async-blocknotify"

@Kexkey
Copy link
Collaborator

Kexkey commented Jun 8, 2022

I had to build a new core image called "async-blocknotify". It brings a script that gets called by walletnotify=...

In setup.sh, you can see BITCOIN_VERSION="async-blocknotify"

I don't understand why you had to build a new Bitcoin Core image? The templates' bitcoin/bitcoin.conf file has the new walletnotify instruction, no?

@Kexkey
Copy link
Collaborator

Kexkey commented Jun 8, 2022

I had to build a new core image called "async-blocknotify". It brings a script that gets called by walletnotify=...
In setup.sh, you can see BITCOIN_VERSION="async-blocknotify"

I don't understand why you had to build a new Bitcoin Core image? The templates' bitcoin/bitcoin.conf file has the new walletnotify instruction, no?

Ah it's probably to include the mosquitto package to be able to publish the message...

tx=$(bitcoin-cli -rpcwallet=$wallet gettransaction $txid true)

if [ -n "$tx" ]; then
echo "[walletnotify-$pid] Found [$txid] in wallet [$wallet]"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insert something like tx=$(echo $tx | jq -Mc) here to remove unnecessary json formatting

if [ -n "$tx" ]; then
echo "[walletnotify-$pid] Found [$txid] in wallet [$wallet]"
echo "[walletnotify-$pid] mosquitto_pub -h broker -t confirmation -m \"$tx\" "
mosquitto_pub -h broker -t confirmation -m $(echo $tx | base64 -w 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename the topic bitcoin_watching_walletnotify to make it clear that it's triggered by Bitcoin Core's walletnotify on a watching wallet. Especially since in confirmation(), we publish something on tx_confirmation topic after making sure the related address is still being watched by CN.

#!/bin/sh

walletnotify(){
local pid=$(cut -d' ' -f4 < /proc/self/stat)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use $$ to get the PID

dist/setup.sh Outdated
@@ -885,7 +891,7 @@ PROXYCRON_VERSION="v0.9.0-dev"
OTSCLIENT_VERSION="v0.9.0-dev"
PYCOIN_VERSION="v0.9.0-dev"
CYPHERAPPS_VERSION="dev"
BITCOIN_VERSION="v22.0"
BITCOIN_VERSION="async-blocknotify"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll publish v22.0-mosquitto to Docker's hub, we can use it until I publish v23 that will include mosquitto by default, and will be used for CN v0.9.0.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default walletnotify.sh in the image will be a no-op


while true # Keep an infinite loop to reconnect when connection lost/broker unavailable
do
mosquitto_sub -h broker -t confirmation | while read -r message
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

topic name here as well

confirmation() {
(
flock -x 201

trace "Entering confirmation()..."

local returncode
local txid=${1}
local tx_details=$(echo ${1} | base64 -d)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, we got tx_details needed to have wallet-specific information about the tx and we got tx_raw_details needed to get some other information not found in gettransaction but found in getrawtransaction.

Here, we are receiving tx_details (result of gettransaction) but a few lines below we are calling getrawtransaction to get the rest of the details (into tx_raw_details).

I would like that we call getrawtransaction on the Bitcoin Core container's side in the walletnotify script and include the result in the published message. So we would have something like {"gettransaction":{...},"getrawtransaction":{...}} in the base64 message.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...instead of calling get_rawtransaction from the proxy.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh wait a minute. We can use gettransaction txid true true on the node to get everything we need, including the rawtransaction info (in the decoded field)! Let's use it in walletnotify.sh and publish the result as message!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIce, I did not see the third argument on chainquery.com, it's not up to date (second time !!). I'll add this !!

tx_blockhash="$(echo "${tx_details}" | jq -r '.result.blockhash')"
tx_blockheight=$(get_block_info ${tx_blockhash} | jq '.result.height')
tx_blockhash="$(echo "${tx_details}" | jq -r '.blockhash')"
tx_blockheight=$(get_block_info ${tx_blockhash} | jq '.height')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We calling the Bitcoin Core container here to get block height... Do you think it's a good idea to do this stuff in the walletnotify.sh script in the Bitcoin container and include it in the message? Instead of calling it here from the proxy...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey we have the blockheight and blockhash fields in gettransaction's result! It's not even needed anymore to call getblockinfo...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

trace "[confirmation] tx_blockhash=${tx_blockhash}"
if [ "${tx_blockhash}" = "null" ]; then
trace "[confirmation] probably being called by executecallbacks without any confirmations since the last time we checked"
else
local tx_blockheight=$(get_block_info "${tx_blockhash}" | jq '.result.height')
local tx_blocktime=$(echo "${tx_details}" | jq '.result.blocktime')
local tx_blockheight=$(get_block_info "${tx_blockhash}" | jq '.height')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, we would already have this info in the published message...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get the info from the gettransaction result!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - no more calls to core

@@ -111,7 +111,8 @@ manage_missed_conf() {

latesttxid=$(echo "${received_address}" | jq -r ".txids | last")
trace "[manage_missed_conf] latesttxid=${latesttxid}"
data='{"method":"gettransaction","params":["'${latesttxid}'"]}'
data="{\"method\":\"gettransaction\",\"params\":[\"${latesttxid}\",true]}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, watching01.dat and xpubwatching01.dat are not created as watch-only wallets, so the "true" arg wasn't needed... But I think it's a good idea to add it, just in case. After all, this is a watching wallet...

@@ -126,7 +127,7 @@ manage_missed_conf() {
trace "[manage_missed_conf] Broadcast or mined after watch, we missed it!"
# We skip the callbacks because do_callbacks is called right after in
# requesthandler.executecallbacks (where we're from)
confirmation "${latesttxid}" "true"
confirmation $(echo "${tx}" | jq .result | base64 -w 0) "true"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use jq -Mc to remove formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@phillamy phillamy marked this pull request as ready for review June 14, 2022 16:41
@Kexkey Kexkey merged commit bcc0a36 into SatoshiPortal:dev Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants