mappum / bitcoin-net Goto Github PK
View Code? Open in Web Editor NEW:globe_with_meridians: Bitcoin networking that works in Node and the browser
:globe_with_meridians: Bitcoin networking that works in Node and the browser
When I run the sample I'm getting flooded with
connected to peer 192.168.1.140
Does this even work anymore?
Issue: when only one webSeed url and only one p2p peer is set several peer objects are created sending over one websockets object resulting in repeated request rejects from the p2p peer.
The situation shows itself as several 'version' messages are sent by each peer over the same single websocket connection so the chain node accepts only first version and rejects subsequent and the client prints bitcoin-net:peergroup peer connection error: Peer handshake timed out
messages in console.
In the browser debug I could see that WebSocketStream() function, creating the ws object, is called only once but PeerGroup _OnConnection(), creating peers, is called three times. It looks like we have 3 peers but only one ws object resulting in mixing up exchanges with the chain node
The tests are not yet very thorough, and live tests which connect to real nodes should be used sparingly (should be using mock connections).
It seems that very many peers are timing out when responding with blocks
messages when we request many of them via BlockStream
. I'm not sure if we're hitting some Bitcoin Core rate limiting, or if a bug is preventing us from seeing the response.
I'm interested in caching the results of a particular transaction stream. This will let me read the transactions off disk, and then start looking at new transactions at the point where my disk cache stops. I'm currently doing this in a pretty simple way, by just caching each transaction that comes out of the transaction stream. What I'd like to do is to also cache that a particular block doesn't contain any matching transactions. Right now this seems a little tricky because the transaction stream doesn't tell us when it sees a block without transactions. I can think of a few ways to do this, but do you have any recommendations for the "right" way to approach this?
One thing I was thinking about is that the transaction stream could have an option where it can output an object indicating that it has finished looking at a particular block. This may be a bit too closely tailored to my use case, though.
Another idea is to have a stream that reads from both the block stream and the transaction stream and kind of zips their outputs together into a single stream. This might be a little bit more work but it could be better-factored.
When I run the tests w/ the Bitcoin testnet params, I get the error below.
It looks like some values in the test are hardcoded for the Bitcoin mainnet. However, I think that there might be actual functionality issues as well, because burnie seems to not work with bitcoin-net@4.
✓ PeerGroup constructor
✓ invalid params
✓ valid params, no options
✓ valid params, with options
✓ connect
✓ peer methods
✓ randomPeer
⨯ getBlocks
not ok 19 no error
---
operator: error
expected: |-
undefined
actual: |-
{ [Error: Request timed out] timeout: true }
at: Timeout._onTimeout (/Users/paul/repos/bitcoin-net/lib/peer.js:9:23770)
stack: |-
Error: Request timed out
at Timeout._onTimeout (/Users/paul/repos/bitcoin-net/lib/peer.js:9:23640)
at tryOnTimeout (timers.js:224:11)
at Timer.listOnTimeout (timers.js:198:5)
...
not ok 20 result is array
---
operator: ok
expected: true
actual: false
at: Timeout._onTimeout (/Users/paul/repos/bitcoin-net/lib/peer.js:9:23770)
...
/Users/paul/repos/bitcoin-net/test/build/peerGroup.js:101
t.equal(res.length, 1, 'result is correct length');
^
TypeError: Cannot read property 'length' of undefined
at /Users/paul/repos/bitcoin-net/test/build/peerGroup.js:101:20
at Timeout._onTimeout (/Users/paul/repos/bitcoin-net/lib/peer.js:9:23770)
at tryOnTimeout (timers.js:224:11)
at Timer.listOnTimeout (timers.js:198:5)
------------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
------------------------|----------|----------|----------|----------|----------------|
bitcoin-net/ | 100 | 100 | 100 | 100 | |
index.js | 100 | 100 | 100 | 100 | |
bitcoin-net/lib/ | 52.11 | 34.26 | 46.9 | 52.29 | |
bridge.js | 43.94 | 33.33 | 33.33 | 29.27 |... 56,59,60,67 |
peer.js | 59.38 | 42.07 | 63.27 | 60.36 |... 462,463,464 |
peerGroup.js | 50 | 30.81 | 41.94 | 51.6 |... 537,538,540 |
protocolTransforms.js | 32.86 | 4.17 | 25 | 32.86 |... 103,115,116 |
utils.js | 72.73 | 71.43 | 66.67 | 80 | 11,12 |
------------------------|----------|----------|----------|----------|----------------|
All files | 52.17 | 34.26 | 46.9 | 52.36 | |
------------------------|----------|----------|----------|----------|----------------|
not ok 21 no plan found
⨯ fail 3
npm ERR! Test failed. See above for more details.
block
argument is object with data fields, but without any specific methods
Line 189 in c842a23
example.js
let { Peer } = require('bitcoin-net')
const net = require("net")
const socket = net.connect({ port: 8333, host: 'vilha.info' }, () => {
const peer = new Peer({ magic: 0xd9b4bef9, defaultPort: 8333 }, { socket })
peer.once('ready', () => {
peer.getBlocks([Buffer.from('0000000000000000002b0fcdc0bdedcc71fcce092633885628c3b50d43200002', 'hex').reverse()],
(err, headers) => {
console.log(headers)
})
})
})
package.json
"dependencies": {
"bitcoin-inventory": "^1.0.2",
"bitcoin-merkle-proof": "0.0.6",
"bitcoin-net": "^5.0.0",
"bitcoin-protocol": "^3.0.1",
"blockchain-spv": "^3.1.6",
"crypto": "^1.0.1",
"debug": "^2.2.0",
"event-cleanup": "^1.0.3",
"object-assign": "^4.1.0",
"through2": "^2.0.1",
"webcoin-bitcoin": "^3.1.4"
}
Now since I'm getting connections and figured out how to use the debug command I see a lot of pingpongs and the blocks.
Shouldn't I also get the "inv" for all transactions? How can I listen to (and send back raw) transactions?
We'll need an API for setting Bloom filters on our peers. However, this module is getting pretty large, so it might make sense to put that in its own module which is enabled on a PeerGroup
by setting the filter for each peer.
inv.on('tx', (tx) => {bitcoinjs.TransactionBuilder.fromTransaction(tx)}
is failing on the outs.value in tx with:
Expected UInt53, got BN
The README docs are still missing some info:
As far as I can tell, the close
method of PeerGroup
does successfully close it, but the callback is called before it's really closed. Specifically, it looks like TCP connections can still be opened after close
is called. Maybe we could count the number of open TCP connections and only call the callback after count goes to zero?
simple-block-stream closing... +31ms
bitcoin-net:peergroup close called: peers.length = 0 +1ms
// At this point, PeerGroup calls the callback
simple-block-stream closed +0ms
simple-block-stream streaming from 278336 73a362d87ddf3cd111a53532dc711d2869054064fd38032a0000000000000000 +8ms
simple-block-stream starting txs... +1ms
bitcoin-net:peergroup _connectTCP: tcp://96.2.103.25:8333 +5ms
bitcoin-net:peergroup _connectTCP: tcp://108.44.238.26:8333 +3ms
bitcoin-net:peergroup _connectTCP: tcp://122.133.113.149:8333 +1ms
bitcoin-net:peergroup _connectTCP: tcp://169.255.57.31:8333 +0ms
bitcoin-net:peergroup _connectTCP: tcp://124.82.29.181:8333 +1ms
bitcoin-net:peergroup _connectTCP: tcp://46.17.103.64:8333 +29ms
bitcoin-net:peergroup _connectTCP: tcp://37.146.53.88:8333 +25ms
bitcoin-net:peergroup _connectTCP: tcp://103.255.25.89:8333 +9ms
bitcoin-net:peergroup discovery connection error: Connection timed out +8s
In the sample it should be
new PeerGroup.PeerGroup(params)
and
peer.ping(() => console.log('received pong response'))
and
peer.socket._sockname.address
The browser debug is flooding (and crashing the browser) with
bitcoin-net:peergroup discovery connection error: Peer does not have any peers to exchange
It will be important to use an authenticated transport for peer-exchange
seeds, to prevent MITM attacks which could allow attackers to control all of a client's peers.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.