0

Thanks for stopping in :)

I break things, fiddle around, and then try to put them back together.
It is how I seem to learn best.

(One of) My recent obsession(s) has been with crypto-currency.

Since I have a little bit of knowledge working with API's through PHP and cUrl, I decided to use PHP for my fiddling.

So, I tell myself, "Why not register with a popular pool and see if I can't examine what a piece of 'work' looks like. Simple!"

I'm well aware that PHP, Json-RPC and CPU Bitcoin mining are extremely inefficient. I am not attempting to really create anything that will see the light of day; simply fiddling for fiddling's sake.

Evidently, I am just a bit ignorant on how Json-RPC and Stratum servers operate. I had thought to post this to the Bitcoin specific exchange, however I have the feeling my mistake is much more elementary than that.

The Goal: Get some work and print it to the page so I can inspect it and hopefully learn something.

My Attempt:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

function tickleElmo () {

    $feed = 'http://stratum.btcguild.com';
    $post_string = '{"method": "getblocktemplate", "params": [], "id": "0"}';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $feed);
    curl_setopt($ch, CURLOPT_PORT, 3333);
    curl_setopt($ch, CURLOPT_USERPWD, "elmoworker_1:123");
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/stratum', 'Content-length: '.strlen($post_string)));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-length: '.strlen($post_string)));

    $output = curl_exec($ch);
    curl_close($ch);

    return $output;
}

$data = tickleElmo();
var_dump($data);

?>

So, I had assumed (obviously incorrectly) that simply making a POST to the end-point would yield at least some response. I can ping the server stratum.btcguild.com just fine. Yet, via cUrl I don't get anything at all... the script simply times out and returns false.

I have tried a few different Content-type's, adding a newline character to the end of the post, the getwork and getblocktemplate methods inside the $post_string, and about 2 dozen other hack and slash alterations :(

I believe that the problem lies in my understanding of Json-RPC, as I have only previously worked with simple get API end-points in the past, but I have done my personal best to read up and find a simple explanation and have hit a road-block. I just don't have the tools to understand... :(

Any thoughts or additional sources of research/information from the intelligent folks here @stackoverflow would be greatly appreciated,

Samantha.

Samantha P
  • 543
  • 4
  • 12
  • 1
    Try adding `"jsonrpc": "2.0"` to your envelope (alongside `method` and `id`). – David-SkyMesh Jan 09 '14 at 16:33
  • [delete - this was wrong] – David-SkyMesh Jan 09 '14 at 16:34
  • I appreciate deeply your effort in posting your comment. Unfortunately, there has been no change :( Honestly, I think the issue has to do with a fundamental non-understanding on my part of Json-RPC and RPC in general. – Samantha P Jan 10 '14 at 02:55
  • I've got plenty of working code dealing with JSON-RPC in PHP/Perl/C#. For PHP, I ended up writing my own client & server stacks for JSON-RPC because *every* *single* library out there is of poor quality (please someone, correct me by showing a good one!). Nothing in your code shows a "fundamental" misunderstanding... the protocol is quite simple! The real issue is bad/inflexible servers. – David-SkyMesh Jan 10 '14 at 04:33
  • @David-SkyMesh Sorry it took me so long to respond! So, I should be getting a work response with the above request...? – Samantha P Jan 15 '14 at 08:17
  • You need to add `"jsonrpc": "2.0"` at the least, but (unless authentication is stopping you) you should get *some* sort of response. Also, some serverrs differ in whether they'll take positional (e.g: `[1,2,3]`) or named (e.g: `{ "a":1,"b":2,"c":3}`) parameters. – David-SkyMesh Jan 15 '14 at 10:23
  • Thanks so much for the help :) Would you trouble yourself to answer a general question about how Json-RPC functions? I understand the idea making a remote function/method call. But in my readings, it seems that Json-RPC also allows the endpoint(server) to send requests as well, that aren't simply a response to a client request. But a separate handshake. Where does that server request go, php://input? and how the heck do you hook into it? Really confused on that... :( – Samantha P Jan 16 '14 at 00:59
  • 1
    You're talking about "JSON-RPC notifications". How that works depends on the "transport". The typical "transport" for JSON-RPC is HTTP (or HTTPS) -- with this transport, "notifications" are either not supported, or you're required to send empty notification messages to the server in order to get the lastest "notifications" back from the server... (continued) – David-SkyMesh Jan 16 '14 at 01:46
  • 1
    There are other transports, however, (e.g: TCP or WebRTC) that allow asynchronous bi-directional communications -- with these transports you should be calling `select()` (for TCP) regularly to test if you can read from the socket, or handling incoming "notification" events (for WebRTC). Either way, you then read the message which might contain replies to your RPC method calls, or async notifications from the server. – David-SkyMesh Jan 16 '14 at 01:46

0 Answers0