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

Implement spvnode.sendTX() and spvnode.relay() #417

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

OrfeasLitos
Copy link
Contributor

@OrfeasLitos OrfeasLitos commented Feb 17, 2018

The definitions of these two functions were not implemented previously, they both just called this.broadcast(). Now sendTX() adds the tx to the txFilter of the pool and emits the tx; relay() calls sendTX() and silences errors, just like for the full node.

@OrfeasLitos OrfeasLitos force-pushed the spv-send-relay branch 2 times, most recently from 900da26 to a54dd34 Compare February 17, 2018 22:11
@OrfeasLitos OrfeasLitos changed the base branch from master to wallet-rewrite February 17, 2018 22:11
@OrfeasLitos OrfeasLitos force-pushed the spv-send-relay branch 2 times, most recently from b3d9585 to c63e666 Compare February 17, 2018 22:24
nodech
nodech previously requested changes Feb 17, 2018
lib/node/spvnode.js Outdated Show resolved Hide resolved
lib/node/spvnode.js Outdated Show resolved Hide resolved
lib/node/spvnode.js Outdated Show resolved Hide resolved
@OrfeasLitos
Copy link
Contributor Author

All done

Copy link
Member

@tuxcanfly tuxcanfly left a comment

Choose a reason for hiding this comment

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

Tested OK.

Earlier a transaction relayed by us was only added to the filter if a peer relayed it back.

Sidenote: this cannot be easily tested without being able to configure wallet port (see #418)

@bucko13
Copy link
Contributor

bucko13 commented Feb 19, 2018

What's the reasoning for no longer returning promises?

lib/node/spvnode.js Outdated Show resolved Hide resolved
@OrfeasLitos
Copy link
Contributor Author

I did not return following the example of the full node:

async sendTX(tx) {
and
async relay(tx) {
.
Should I add the returns? Should the full node be changed as well?

@nodech
Copy link
Member

nodech commented Feb 19, 2018

Nope, I thought It would break the API. then you need to add async keyword to sendTX and await this.broadcast

@nodech
Copy link
Member

nodech commented Feb 19, 2018

When it returned Promise, it would work with other async/await. This will just break.

@OrfeasLitos
Copy link
Contributor Author

Done

}

/**
* Broadcast a transaction. Silence errors.
* @param {TX} tx
* @returns {Promise}
Copy link
Member

Choose a reason for hiding this comment

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

you still need this returns in both methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can do it, but won't this make the spv node have a different API from the full node? Is there a reason for this difference?

Copy link
Member

Choose a reason for hiding this comment

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

fullnode lacks this line in docs. Every async method returns Promise.

@OrfeasLitos
Copy link
Contributor Author

Now it should be ok?

@OrfeasLitos OrfeasLitos force-pushed the spv-send-relay branch 2 times, most recently from 4fd1cd2 to e426e43 Compare February 21, 2018 11:03
OrfeasLitos added a commit to OrfeasLitos/TrustIsRisk.js that referenced this pull request Mar 3, 2018
This note contained some provisional bug fixes made to
bcoin/lib/node/spvnode.js, along with the suggestions by the bcoin devs
on how to correctly fix those bugs. Since PR
bcoin-org/bcoin#417 has been published, this
note is redundant.
@braydonf
Copy link
Contributor

braydonf commented Jun 28, 2018

The fullnode sendTx method doesn't await for this.broadcast(tx) and only when trying to the mempool. Was that the intention? Following it through and an await for broadcast() would resolve as soon as one of the peers requests the data for the transaction from the inventory announcement message.

@OrfeasLitos
Copy link
Contributor Author

@braydonf maybe this comment is more relevant in #420, since the fullnode sendTX() is most recently changed there and is not changed here.

This is intentional. When adding to the mempool, we need to see whether the tx is an orphan, so we have to await for addTX() to return. Note that this procedure does not need any network interaction, thus the resolution should happen quickly.

As you said, the resolution of await this.broadcast(tx) depends on network factors, such as whether any peer is connected and whether any peer sends a getdata for this particular tx. In case no peer is connected, sendTX() may complete in an indefinite amount of time. We wouldn't want to wait for too long and anyway from a semantic point of view, as long as sendTX() adds the broadcast job to the array of jobs, its mission is complete, why wait even more?

@braydonf
Copy link
Contributor

Right, so there is an await this.pool.broadcast with the error being emitted from the node from there.

@OrfeasLitos
Copy link
Contributor Author

OrfeasLitos commented Jun 28, 2018

You're referring to this:

await this.pool.broadcast(item);
right?

@braydonf
Copy link
Contributor

Yep, and the same exists in spvnode.js

@OrfeasLitos
Copy link
Contributor Author

@nodar-chkuaselidze Can we merge either this or #697? Also #681 is ready and we need them for our application.

* @param {TX} tx
* @returns {Promise}
*/

sendTX(tx) {
return this.broadcast(tx);
async sendTX(tx) {
Copy link
Member

Choose a reason for hiding this comment

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

To protect users from getting banned by sending an invalid tx, would it make sense to first check tx.verify() before anything else?

Copy link
Contributor Author

@OrfeasLitos OrfeasLitos Feb 18, 2019

Choose a reason for hiding this comment

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

Fixed following the full node example:

const [valid, reason, score] = tx.checkSanity();
if (!valid)
throw new VerifyError(tx, 'invalid', reason, score);

bcoin/lib/node/fullnode.js

Lines 297 to 303 in f585d86

} catch (err) {
if (err.type === 'VerifyError' && err.score === 0) {
this.error(err);
this.logger.warning('Verification failed for tx: %h.', tx.hash());
this.logger.warning('Attempting to broadcast anyway...');
this.broadcast(tx);
return;

@OrfeasLitos OrfeasLitos force-pushed the spv-send-relay branch 2 times, most recently from 75a5e7c to 1f65e4a Compare February 18, 2019 22:14
@codecov-io
Copy link

codecov-io commented Feb 18, 2019

Codecov Report

Merging #417 into master will decrease coverage by 42.24%.
The diff coverage is 41.17%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #417       +/-   ##
==========================================
- Coverage   61.94%   19.7%   -42.25%     
==========================================
  Files         156     155        -1     
  Lines       26084   26071       -13     
==========================================
- Hits        16157    5136    -11021     
- Misses       9927   20935    +11008
Impacted Files Coverage Δ
lib/net/pool.js 16.49% <0%> (-14.95%) ⬇️
lib/node/spvnode.js 54.9% <53.84%> (+21.93%) ⬆️
lib/workers/worker.js 0% <0%> (-100%) ⬇️
lib/hd/words/chinese-traditional.js 0% <0%> (-100%) ⬇️
lib/hd/words/french.js 0% <0%> (-100%) ⬇️
lib/hd/words/japanese.js 0% <0%> (-100%) ⬇️
lib/hd/words/chinese-simplified.js 0% <0%> (-100%) ⬇️
lib/hd/words/italian.js 0% <0%> (-100%) ⬇️
lib/script/scripterror.js 6.66% <0%> (-93.34%) ⬇️
lib/blockstore/file.js 5.45% <0%> (-90.46%) ⬇️
... and 101 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2148e6b...518be85. Read the comment docs.

@OrfeasLitos OrfeasLitos changed the base branch from wallet-rewrite to master February 18, 2019 22:15
@OrfeasLitos
Copy link
Contributor Author

Also rebased and added tests for TX emission

@OrfeasLitos
Copy link
Contributor Author

OrfeasLitos commented Apr 4, 2019

@pinheadmz Can you clarify what changes you propose in this comment?

@pinheadmz
Copy link
Member

Ok so since #631 has come up again, I gave this PR another review and I think it is a good solution to a long-standing issue. I rebased on master here: https://github.com/pinheadmz/bcoin/commits/spv-send-relay-2 and fixed a few typos (rename test to SPV Node and replace util/assert with bsert). I was able to confirm this branch solves #631 the following way:

  1. Launch full node:
$ bcoin --network=regtest --no-wallet --bip37
  1. Launch and connect SPV node with unique ports:
$  bcoin --spv --prefix=~/.bcoin/rtspv --port=11111 --http-port=22222 \
 --wallet-port=33333 --wallet-http-port=44444 --only=127.0.0.1
  1. Get SPV wallet address and generate coins to it from full node.

  2. Make and sign but do not broadcast a transaction from the SPV node:

$ bwallet-cli --http-port=44444 mktx n1n4wbxax1Q6H1zFmuazJBxvNwv1sVk4d8 0.01
  1. In a new terminal, set up a listener for all wallet events from the SPV node:
$ bwallet-cli --http-port=44444 listen
  1. Broadcast the transaction hex result from (4) FROM the SPV node:
$ bcoin-cli --http-port=22222 broadcast \
0100000001a7cb2a20b0db5ccfebdcacd8270...

Results:

On master branch, the wallet does not emit any events. On spv-send-relay-2, you catch the usual events TX and Balance

test/spvnode-test.js Outdated Show resolved Hide resolved
test/spvnode-test.js Outdated Show resolved Hide resolved
@OrfeasLitos OrfeasLitos force-pushed the spv-send-relay branch 2 times, most recently from a94d6d1 to 161d76d Compare November 8, 2019 10:51
Copy link
Member

@pinheadmz pinheadmz left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this, @OrfeasLitos and your patience in review!

@pinheadmz pinheadmz added ready for review Ready to be reviewed spv labels Nov 8, 2019
@tuxcanfly tuxcanfly self-requested a review November 16, 2019 05:19
Copy link
Member

@tuxcanfly tuxcanfly left a comment

Choose a reason for hiding this comment

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

Tested OK. Fixed minor nits.

@tuxcanfly
Copy link
Member

@OrfeasLitos only minor nit. Please use standard commit messages e.g:

spvnode: implement sendTX and relay; emit "tx"

test: add node sendTX test case for "tx" event

Apologies for having this linger for so long.

@tuxcanfly
Copy link
Member

Also, either squash the commits or move spvnode-test.js to the commit last commit. Because, without the fix, the spv test will fail and break the build at that commit.

test/spvnode-test.js Show resolved Hide resolved
test/spvnode-test.js Show resolved Hide resolved
tuxcanfly and others added 2 commits November 16, 2019 16:16
Co-authored-by: Christopher Jeffrey <chjjeffrey@gmail.com>
The definitions of these two functions were not implemented previously,
they both just called this.broadcast(). Now sendTX() broadcasts only if
it wouldn't hurt its score and emits the tx only if it is watched;
relay() calls sendTX() and silences errors, just like for the full node.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for review Ready to be reviewed spv
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants