Bitcoin Forum
April 24, 2024, 07:38:03 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 3 »  All
  Print  
Author Topic: Bitcoin Binary Data Protocol, for mining, monitorblocks, etc.  (Read 25993 times)
jgarzik (OP)
Legendary
*
qt
Offline Offline

Activity: 1596
Merit: 1091


View Profile
February 15, 2011, 08:17:19 AM
Last edit: May 10, 2011, 01:54:43 AM by jgarzik
Merited by ABCbits (2)
 #1

0. The URLs
----------------------------------------------------------------------------------
URL: http://yyz.us/bitcoin/pushpool-0.4.tar.gz
Repo: https://github.com/jgarzik/pushpool


1. The Problem
----------------------------------------------------------------------------------
With the recent slashdotting and resultant influx in new users, the 'getwork' network protocol used in mining is showing some strain, particularly on the pools.  Miners request work once every 5-10 seconds using HTTP JSON-RPC, which has several glaring inefficiencies that lead to unnecessary server load:
  • HTTP/1.1 persistent connections are uncommon, possibly because bitcoind does not support them.  This results in a new TCP connection from every miner, every 5-10 seconds, to the same network host.
  • 'getwork' data is a mere 256 bytes, but HTTP headers and binary-to-hexidecimal encoding for JSON increase the payload to more than double that
  • official bitcoin client's RPC server implementation is essentially a single-threaded loop, where requests from clients B, C, and D will be stalled and ignored until client A's request is finished -- or a 30 second timeout (see -rpctimeout).  This algorithm does not tolerate a high TCP request rate from multiple threads / computers.

Several people, pool operators in particular, have a keen interest in solving these problems.  In addition, push mining (see below) has been discussed as a future alternative to the 'getwork' polling method currently employed.


2. Design goals for a solution
----------------------------------------------------------------------------------
I have written a demonstration pool server (aka a 'getwork' proxy server) that functions in a similar fashion to the recently-discussed poold.py:  large numbers of miners connect to the pool server, which proxies 'getwork' JSON-RPC requests to the official bitcoin client.  This demonstration server implements a new binary protocol that was designed to meet the following goals:

  • Persistent TCP connections, to eliminate TCP disconnect+reconnect behavior by miners
  • Network-efficient for the common use case:  one network packet for 'getwork', one network packet for the returned data.
  • Network-efficient for similar use cases (monitorblocks, monitortx) where clients connect, and then passively wait for real-time events to be delivered
  • Existing miner client workflows supported, to minimize network protocol change impact on miners
  • Support "push mining," where server delivers new work to miners unsolicited (ie. without the miner first sending a 'getwork' message)

This is not intended to replace JSON-RPC API, but to supplement it for specific use cases.  Yes, that means bitcoind will listen to three network ports: P2P network, JSON-RPC, and binary RPC (though as now, only P2P is required for operation; the servers are always optional).


3. Let's start with a protocol example: today's getwork mining
----------------------------------------------------------------------------------
The specific details of the protocol itself are in ubbp.h and protocol.h of the above URL (pushpool-0.1.1.tar.gz).  Here is an example, to provide a suitable introduction:

* TCP connection is broken up into messages.  Each message has a 64-bit header, with 8-bit opcode and 24-bit length fields.
* Miner client connects to TCP server, and issues a LOGIN message, which is compressed JSON login data + sha256 hash of (data + shared secret).
* Server responds with an OP_LOGIN_RESP msg, compressed JSON, indicating options and capabilities
* Client issues an OP_GETWORK msg (8 bytes)
* Server responds with an OP_WORK msg (264 bytes)
* Client uses its CPU/GPU to work on proof-of-work solution...
* Client issues an OP_GETWORK msg (8 bytes)
* Server responds with an OP_WORK msg (264 bytes)
* ...

The above example intentionally matches existing 'getwork' JSON-RPC miner client workflow today.  Miner clients may even support stateless operation by pipelining the OP_LOGIN and OP_GETWORK requests together, and closing the TCP connection.  Stateless operation is not recommended, but it is supported, in order to support the widest range of existing mining clients.


4.  Tomorrow's mining:  push mining
----------------------------------------------------------------------------------
When a block or tx arrives, it is preferable to begin working immediately on the new work.  From the server's perspective, this is a classic data-broadcast problem, where the server wants to broadcast N different pieces of work to N miners.  Hence, "push mining" where the server pushes new work pro-actively to the miner clients.

This new network protocol supports pushing mining, as demonstrated in this example:

* Client connects to server, issues a LOGIN message with the "send_me_work" flag set
* Server responds with OP_LOGIN_RESP msg
* Server sends a OP_WORK msg
* Server sends a OP_WORK msg
* Server sends a OP_WORK msg
* ...


5.  A similar use case:  monitorblocks
----------------------------------------------------------------------------------
Gavin Andresen has a patch in his github which provides a very useful feature:  when a new block is received (monitorblocks) or new wallet transaction (monitortx), bitcoind sends an HTTP POST to the specified URL.  Thus, monitorblocks provides real-time monitoring of the bitcoin network, and monitortx provides real-time monitoring of the local wallet.  This sort of featureset pushes data as events occur, rather than forcing a website operator to poll JSON-RPC for certain operations to complete.

Monitoring new blocks on the bitcoin network is a very easy data broadcasting problem that this binary network protocol may easily support:

* Client connects to server, issues a LOGIN message with the "send_me_blocks" flag set
* Server responds with OP_LOGIN_RESP msg
* Server sends a OP_BLOCK msg
* Server sends a OP_BLOCK msg
* Server sends a OP_BLOCK msg
...

monitortx is more complicated, because one may specify transaction-matching criteria.  But with this new protocol's support of JSON, flexibility is not a problem.


6.  A plan to proceed - this is just a rough draft
----------------------------------------------------------------------------------
I'm thinking of the following steps to proceed, given the need to coalesce several potentially parallel push-mining efforts:
  • write a pool server / proxy server that supports the new protocol (done)
  • hack existing miner clients (cpuminer, oclminer seem easy targets) to support new protocol. volunteers?
  • iterate, test, comment.  iterate, test, comment.  lather, rinse, repeat
  • Once people are happy, implement in official bitcoind
  • in parallel with any of the above efforts, update official bitcoind's rpc.cpp with a smarter httpd implementation

Let the comments begin...  hopefully someone will volunteer to mod a GPU miner to support this?



Appendix 1:  FAQ
----------------------------------------------------------------------------------
Q. Why invent a new protocol?  Why not use Google protocol buffers or XDR?

A. protobuf and XDR both require an underlying packetizing format, such as UBBP that I've presented here.  That implies the choice would be UBBP+protobuf or UBBP+JSON.  Given the bitcoin community's embrace of JSON, the latter was chosen.  JSON is actually more flexible than protobufs, because more dynamic data structures may be described using JSON.


Q.  Why did you not address glaring problems in getwork?

A.  I focused purely on a network-efficient protocol.  getwork implementation choices are outside the scope of this work.


Q.  What is the state / quality of this code release?

A.  Uh, it compiles and runs... but no clients yet exist for it.  Without a miner client for testing, it's about as useful as spitting on a fish...




Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
You can see the statistics of your reports to moderators on the "Report to moderator" pages.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
Cdecker
Hero Member
*****
Offline Offline

Activity: 489
Merit: 504



View Profile WWW
February 15, 2011, 11:48:20 AM
 #2

Sounds like a reasonable enhancement, especially since the miners will be notified of a new block and will be able to start on the new one right away Cheesy

Want to see what developers are chatting about? http://bitcoinstats.com/irc/bitcoin-dev/logs/
Bitcoin-OTC Rating
m0Ray
Sr. Member
****
Offline Offline

Activity: 868
Merit: 251


View Profile
February 15, 2011, 07:50:47 PM
 #3

Why not UDP?
jgarzik (OP)
Legendary
*
qt
Offline Offline

Activity: 1596
Merit: 1091


View Profile
February 15, 2011, 08:56:56 PM
 #4

Why not UDP?

Retransmissions imply you wind up reinventing TCP.

Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
m0Ray
Sr. Member
****
Offline Offline

Activity: 868
Merit: 251


View Profile
February 15, 2011, 10:10:55 PM
 #5

Why not UDP?
Retransmissions imply you wind up reinventing TCP.
Is retransmission really required everywhere? I think it is useful only during login and result reporting and only on miner side. All other activity does not require TCP features.
I think it can be like this:

Code:
Miner->Server: LOGIN username password
Server->Miner: OK keepalive_timeout
If miner does not receive an answer in reasonable time, it resends login request.
On login error server can simply ignore the request or send some REJECT messages.
On success the server records miner IP and port number and adds it to miner identification table together with current timestamp.
Then:
Code:
Server->Miner: WORK ....

Once during keepalive_timeout:
Code:
Miner->Server: ALIVE
or
Code:
Miner->Server: RESULT ...
Server->Miner: OK keepalive_timeout
Server must update timestamp in miner id table then.
Miner resends RESULT message if it does not receive an answer in reasonable time.

Server will remove miner id records from its table after 4*keepalive_timeout.
Note that server can adapt keepalive_timeout for certain miners using statistical data on the fly.

This will reduce traffic to minimum, IMHO.
jgarzik (OP)
Legendary
*
qt
Offline Offline

Activity: 1596
Merit: 1091


View Profile
February 15, 2011, 10:33:56 PM
 #6

Why not UDP?
Retransmissions imply you wind up reinventing TCP.
Is retransmission really required everywhere? I think it is useful only during login and result reporting and only on miner side. All other activity does not require TCP features.

A miner does not want to lose a WORK msg, GETWORK msg, nor have their solution lost.  Every single message -- LOGIN, GETWORK, WORK, ... -- must be retransmitted or retried by one side or the other.

But that's just one of many disadvantages of UDP.  TCP is also better supported by most programming language libs, and is more firewall- and NAT-friendly.

Having implemented many UDP servers of various sorts -- financial data feeds, gaming servers, and cloud computing coordinators -- you really do wind up reinventing TCP while attempting to simply have a robust UDP implementation.

Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
m0Ray
Sr. Member
****
Offline Offline

Activity: 868
Merit: 251


View Profile
February 15, 2011, 10:50:49 PM
Last edit: February 16, 2011, 01:15:55 AM by m0Ray
 #7

1) Neither login request nor solution are not lost in my proposal.
2) I don't think that loss of WORK message will seriously impact the performance. If I understand the principle, new WORK will be broadcasted with every transaction received by server. And it's often enough.
3) I mean only "push" protocol, which does not use GETWORK.
4) UDP, as far as I know, is the same NAT-friendly as TCP. In both cases NAT box just maps the source port.
5) It is not widely supported by libs only because it is very simple.
6) Some features of TCP are really overhead when building a low-latency service. So it is sometimes better to reimplement some TCP features than use its full version. For example, in this case we don't need an acknowledge for every message.

And... the SYN flood...
comboy
Sr. Member
****
Offline Offline

Activity: 247
Merit: 252



View Profile
February 16, 2011, 02:04:24 AM
 #8

I just want to say I also think UDP is a weird/bad idea here.

And even though I have not yet time to dive in, solution looks very nice.

Variance is a bitch!
jgarzik (OP)
Legendary
*
qt
Offline Offline

Activity: 1596
Merit: 1091


View Profile
February 16, 2011, 02:43:23 AM
 #9

1) Neither login request nor solution are not lost in my proposal.

If they cannot be lost, then by definition they must be retransmitted.  And you must build logic to determine how often to retransmit.  When to stop retransmitting and give up.  Reinventing TCP, in other words.

Quote
2) I don't think that loss of WORK message will seriously impact the performance. If I understand the principle, new WORK will be broadcasted with every transaction received by server. And it's often enough.
3) I mean only "push" protocol, which does not use GETWORK.

The loss of a WORK message can mean the loss of money, due to not working on the latest block etc.  No miner will stand for this, therefore, WORK must be acknowledged by client, and retransmitted by server.  TCP does this for us automatically.

Quote
4) UDP, as far as I know, is the same NAT-friendly as TCP. In both cases NAT box just maps the source port.

UDP has no notion of connections, so a heavily loaded NAT box must rely on timeouts and other hacks, unlike TCP.  But in focusing on NAT you ignored "firewall";   TCP far more readily passes through firewalls than UDP.  I've seen this at plenty of large corporate sites especially.  They'll do a local DNS server, and no UDP traffic will traverse the firewall into the outside world.  If you want universality, UDP is not the way to go.  TCP is simply more likely to succeed.


Quote
6) Some features of TCP are really overhead when building a low-latency service. So it is sometimes better to reimplement some TCP features than use its full version. For example, in this case we don't need an acknowledge for every message.

Only if you don't mind losing money Smiley


Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
bitcoinex
Sr. Member
****
Offline Offline

Activity: 350
Merit: 252


probiwon.com


View Profile WWW
February 16, 2011, 01:00:30 PM
 #10

What about increase time interval between requests? And ban for who will be use the frequent requests.

New bitcoin lottery: probiwon.com
- Moжeт, ты eщё и в Heвидимyю Pyкy Pынкa вepyeшь? - Зaчeм жe вepoвaть в тo, чтo мoжнo нaблюдaть нeпocpeдcтвeннo?
burtyb
Newbie
*
Offline Offline

Activity: 45
Merit: 0



View Profile WWW
February 16, 2011, 02:21:39 PM
 #11

What about increase time interval between requests? And ban for who will be use the frequent requests.

That might cause problems for those using a high number of CPU cores though?
jgarzik (OP)
Legendary
*
qt
Offline Offline

Activity: 1596
Merit: 1091


View Profile
February 16, 2011, 05:06:40 PM
 #12

What about increase time interval between requests? And ban for who will be use the frequent requests.

This is not a network protocol issue.  You can easily add these rules to your pool server or bitcoind, once the protocol is deployed.

Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
slush
Legendary
*
Offline Offline

Activity: 1386
Merit: 1097



View Profile WWW
February 16, 2011, 06:23:13 PM
 #13

I just read the sources and mixing binary protocol with json compressed data looks weird for me. Why not simply use (compressed) json RPC over TCP and define only RPC methods? This should be way easier to implementation in any language, more standard, readable etc. But it still enable push features and will be more efficient because we get rid of HTTP overhead. Please don't reinvent the wheel.

Maybe I'm too high-level oriented, but encapsulating JSON RPC into proprietary binary protocol is very unusual.

For example, method 'login' should look like {id:'xxx',method:'login',params:['username','sha256 of username+password']}. One command can be finished by new line, or better, almost every language has support for streaming JSON (well, I know Java and Python libraries), because it is very easy to detect that message is complete.

jgarzik (OP)
Legendary
*
qt
Offline Offline

Activity: 1596
Merit: 1091


View Profile
February 16, 2011, 06:31:33 PM
 #14

I just read the sources and mixing binary protocol with json compressed data looks weird for me. Why not simply use (compressed) json RPC over TCP and define only RPC methods?

Because sending work as compressed JSON involves
  • encoding binary data to hexidecimal
  • storing that hexidecimal string in JSON structure
  • compressing JSON
  • (sent to client)
  • decompressing JSON
  • receiving pointer to hex string
  • decoding hex string to binary data

It is obviously more simple -- less CPU usage and less bandwidth usage -- to send binary work data directly.  Remember, binary data is the common case for mining.

JSON is in the protocol for flexible feature negotiation and configuration.  But we must to avoid today's binary->text->compressed->text->binary redundant data encoding, because the miners work on binary data.

Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
slush
Legendary
*
Offline Offline

Activity: 1386
Merit: 1097



View Profile WWW
February 16, 2011, 06:52:59 PM
 #15

    Because sending work as compressed JSON involves
    • encoding binary data to hexidecimal

    Absolutely marginal overhead. Average CPU core can encode megabytes of data to hexadecimal per second.

    Quote
    • storing that hexidecimal string in JSON structure

    So 2x more data (two bytes for one raw byte) for payload itself. For one mining job, only few bytes is really required, most of current data sending to client are not used (source: m0mchil). Much more effective way is to change job payload itself.

    Quote
    • compressing JSON

    Which is also in your proposal, for storing message payload. Again, I don't see real trouble here.

    Quote
    • receiving pointer to hex string

    That's why I told I'm probably too highlevel. I really don't care about finding pointer in hex string. It is much more cost effective to leave this job on computers and high level libraries than fiddling with bits on low level protocol. Don't forget that this protocol have to be reimplemented in many languages, so using standard protocol, you save tens of hours of labour for programming and bug fixing.

    Quote
    It is obviously more simple -- less CPU usage and less bandwidth usage -- to send binary work data directly.  Remember, binary data is the common case for mining.

    You are right that raw binary protocol is really the most effective. But let's find some reasonable level of optimizations. It does not need to be _perfect_. It need to be effective AND easy to handle/debug. Don't forget that you are optimizing nanoseconds of CPU job and then perform one SQL request, which is 100x slower than any protocol parsing.

    Rough calculation:

    Now:
    --------
    One request: 300 bytes of HTTP request, 700 bytes of data ==> ~1 kB of data every 5 seconds for each worker. It is 12kB per minute per worker.

    Json over TCP:
    --------
    One request: (approx) 20 bytes of request, 300? bytes of response EVERY MINUTE ==> 320 bytes per minute per worker.

    By very simple optimization, you cut bandwidth to 2.5% of original size. Without any binary fiddling and proprietary stuff. How many % will be the savings between Json over TCP and binary over TCP?[/list]

    slush
    Legendary
    *
    Offline Offline

    Activity: 1386
    Merit: 1097



    View Profile WWW
    February 16, 2011, 07:05:24 PM
     #16

    One request: (approx) 20 bytes of request, 300? bytes of response EVERY MINUTE ==> 320 bytes per minute per worker.

    Well, I know that I'm again mixing protocol and getwork implementation. But there is no big point in supporting getwork over tcp and still sending job every 5 seconds. So I'm talking about real situation, about using TCP protocol and real pushwork implementation at once.

    jgarzik (OP)
    Legendary
    *
    qt
    Offline Offline

    Activity: 1596
    Merit: 1091


    View Profile
    February 16, 2011, 07:16:51 PM
     #17

    Because sending work as compressed JSON involves
    • encoding binary data to hexidecimal

    Absolutely marginal overhead. Average CPU core can encode megabytes of data to hexadecimal per second.

    Quote
    • storing that hexidecimal string in JSON structure

    So 2x more data (two bytes for one raw byte) for payload itself. For one mining job, only few bytes is really required, most of current data sending to client are not used (source: m0mchil). Much more effective way is to change job payload itself.

    Quote
    • compressing JSON

    Which is also in your proposal, for storing message payload. Again, I don't see real trouble here.

    Quote
    • receiving pointer to hex string

    That's why I told I'm probably too highlevel. I really don't care about finding pointer in hex string. It is much more cost effective to leave this job on computers and high level libraries than fiddling with bits on low level protocol. Don't forget that this protocol have to be reimplemented in many languages, so using standard protocol, you save tens of hours of labour for programming and bug fixing.

    You are being too literal.  Even python must do this step:  work = json_result['data']


    Quote
    Quote
    It is obviously more simple -- less CPU usage and less bandwidth usage -- to send binary work data directly.  Remember, binary data is the common case for mining.

    You are right that raw binary protocol is really the most effective. But let's find some reasonable level of optimizations. It does not need to be _perfect_. It need to be effective AND easy to handle/debug. Don't forget that you are optimizing nanoseconds of CPU job and then perform one SQL request, which is 100x slower than any protocol parsing.

    Doing all the extra, pointless work of binary->text->compression->text->binary also increases the chances for programmer error.

    Once you have a binary, packetized protocol, the easiest, least error-prone thing to do is receive (or create, in bitcoind's case) a raw binary packet, and pass that directly to a connected miner.


    Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
    Visit bloq.com / metronome.io
    Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
    jgarzik (OP)
    Legendary
    *
    qt
    Offline Offline

    Activity: 1596
    Merit: 1091


    View Profile
    February 16, 2011, 07:20:49 PM
     #18

    One request: (approx) 20 bytes of request, 300? bytes of response EVERY MINUTE ==> 320 bytes per minute per worker.

    Well, I know that I'm again mixing protocol and getwork implementation. But there is no big point in supporting getwork over tcp and still sending job every 5 seconds. So I'm talking about real situation, about using TCP protocol and real pushwork implementation at once.

    The protocol supports multiple use cases:
    • getwork polling (ie. how every single miner is written today).  C:GETWORK  S:WORK  C:GETWORK  S:WORK ...
    • push mining   C:CONFIG(push mining)  S:WORK  S:WORK  S:WORK  S:WORK  ...
    • monitorblocks   C:CONFIG(monitor blocks)   S:BLOCK   S:BLOCK   S:BLOCK   ...

    The protocol supports LAN or WAN, bitcoind or pool server.

    If the miner client prefers polling over push mining, they may choose to do so.




    Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
    Visit bloq.com / metronome.io
    Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
    slush
    Legendary
    *
    Offline Offline

    Activity: 1386
    Merit: 1097



    View Profile WWW
    February 16, 2011, 07:33:23 PM
     #19

    You are being too literal.  Even python must do this step:  work = json_result['data']

    OK, I didn't told that the pointer lookup is not needed/performed inside, automatically. It is just done by standard libraries presented in every language and there is no need to implement binary stuff again.

    Quote
    Doing all the extra, pointless work of binary->text->compression->text->binary also increases the chances for programmer error.

    Well, we are talking about personal opinions. My opinion is that high level programming is much easier and error prone than low level implementation. Thanks to this attitude, we're programming in high level languages and not in assembler.

    And you still does not give the calculation of bandwidth savings against JSON RPC over TCP.

    Quote
    Once you have a binary, packetized protocol, the easiest

    Correct. But I hope you don't want to say that

    import json
    json.decode(sock.read())

    is harder to do than creating own parsing library for every language (C, Python, Java, PHP?) and unpacking binary data, right?

    I don't want to be personal in any way, I think it's great that you open this topic. I'm just finding some equilibrium between hardcore lowlevel stuff and almost standard protocol implemented anywhere. I'm simply not convicted that this protocol need such heavy overoptimization.

    adv
    Full Member
    ***
    Offline Offline

    Activity: 168
    Merit: 100


    View Profile
    February 16, 2011, 07:36:15 PM
     #20

    What about increase time interval between requests? And ban for who will be use the frequent requests.
    What about many NATed users?
    I think you say "they must use different username/password". But they dont MUST do this and may write miner, that use different accounts for frequently requests...

    U may thank me here: 14Js1ng1SvYBPgUJnjNAEPYH4d6SHF79UF
    Pages: [1] 2 3 »  All
      Print  
     
    Jump to:  

    Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!