Categories
Bitcoin

Not sure if this is specifically an RPC problem or code change…

Been a few servers ago and client iterations since I have used this but what once pulled stats from my home node, no longer works.

The following content was written by NinjaMan35 on February 16, 2018, 11:22:39 PM in the thread Not sure if this is specifically an RPC problem or code change…. All content is owned by the author of the bitcointalk.org post. (original)


Been a few servers ago and client iterations since I have used this but what once pulled stats from my home node, no longer works.

But I know RPC is working with something else right now so not sure what to make it it. But ya, the last time I used this code, maybe a yr ago it worked fine.

Code:
include “jsonRPCClient.php”;


$username ‘you’;
$password ‘wish’;
$ip ‘id’;
$port tell;


$info rpc_command‘getinfo’ );
if( 
is_array$info) ) {
        
$required = array( ‘difficulty’‘blocks’ );
        
$numeric_data get_numeric_data$info$required );
        if( !
$numeric_data ) {
                exit( 
“get_info() failed to acquire needed data.” );
        }
        
extract$numeric_data );

        
$write_arr‘timestamp’ ] = time();
        
$write_arr‘estimated_difficulty’ ] = $difficulty;
        
$write_arr += compact‘difficulty’‘blocks’ );

        
$blocks_since_difficulty_change $blocks 2016;
        if( 
$blocks_since_difficulty_change ) {
                
$previous_difficulty_change $blocks – $blocks_since_difficulty_change;

                
$current_block_timestamp get_block_timestamp$blocks );
                
$previous_block_timestamp get_block_timestamp$previous_difficulty_change );

                if( ! 
$current_block_timestamp || ! $previous_block_timestamp ) {
                        exit( 
‘Could not get block timestamps’ );
                }

                
$average_block_time = ($current_block_timestamp – $previous_block_timestamp) / $blocks_since_difficulty_change;
                
$estimated_difficulty $difficulty 600 $average_block_time;
                
$write_arr‘estimated_difficulty’ ] = $estimated_difficulty;
        }
        
write_file$write_arr );
} else {
        exit( 
‘Could not getinfo(). getinfo() output is not an array.’ );
}

function 
rpc_command$command$arg ” ) {
        global 
$username$password$ip$port;
        static 
$rpc;
        if( ! 
$rpc ) {
                
$rpc = new jsonRPCClient“http://$username:$password@$ip:$port” );
        }
        try {
                if( ! empty( 
$arg ) ) {
                        
$ret $rpc->$command$arg );
                } else {
                        
$ret $rpc->$command();
                }
        } catch ( 
Exception $e ) {
                
trigger_error“Exception in rpc_command(). Command is $command. Error is: ” $e->getMessage() );
                
$ret false;
        }
        return 
$ret;
}

function 
get_numeric_data$info$keys ) {
        
$ret = array();
        foreach( 
$keys as $key ) {
                if( isset( 
$info$key ] ) && is_numeric$info$key ] ) ) {
                        
$ret$key ] = $info$key ];
                } else {
                        
$ret false;
                        break;
                }
        }
        return 
$ret;
}

function 
get_block_timestamp$block ) {
        if( !
is_numeric$block ) ) {
                return 
false;
        }
        
$block_hash rpc_command‘getblockhash’$block );
        if( 
$block_hash !== false && preg_match‘/^[0-9a-f]+$/i’$block_hash ) ) {
                
$block_data rpc_command‘getblock’$block_hash );
                if( 
is_array$block_data ) && isset( $block_data‘time’ ] ) && is_numeric$block_data‘time’ ] ) ) {
                        return 
$block_data‘time’ ];
                } else {
                        
trigger_error“Error in get_block_timestamp(). Time in blockdata not found or not numeric. Block is $block” );
                }
        } else {
                
trigger_error“Error in get_block_timestamp() could not getblockhash for block $block. Hash is $block_hash” );
        }
        return 
false;
}

function 
write_file$data ) {
        if( 
is_array$data  ) ) {
                
$h fopen‘getinfo.php’‘w’ );
                
fwrite$hvar_export$datatrue  ) . “;” );
                
fclose$h );
                return 
true;
        } else {
                
trigger_error‘Error in write_file(). Data is not an array.’ );
                return 
false;
        }
}

Out puts this when manually run, normally set on a cron…

[[email protected]]$ php getdata.php
Could not getinfo(). getinfo() output is not an array.
[[email protected]]$

I think I copied the entire files code ok, looks good. But ya,the last time I used this code about a yr ago it was / seemed fine and saved the required data to the secondary file I would then pull from to display on my personal sites.

Ty.

The following content was written by BitMaxz on February 16, 2018, 11:56:00 PM in the thread Not sure if this is specifically an RPC problem or code change…. All content is owned by the author of the bitcointalk.org post. (original)


Maybe this one could help recently updated 13 days ago.
https://github.com/fguillot/JsonRPC
Just test them first because never tested it..

The following content was written by achow101 on February 17, 2018, 03:35:38 AM in the thread Not sure if this is specifically an RPC problem or code change…. All content is owned by the author of the bitcointalk.org post. (original)


The getinfo RPC command which your script relies on has been removed from Bitcoin Core. You should be able to replace it with the getblockchaininfo command.

The following content was written by NinjaMan35 on February 17, 2018, 04:28:38 AM in the thread Not sure if this is specifically an RPC problem or code change…. All content is owned by the author of the bitcointalk.org post. (original)


I looked at my client again, getinfo still produces data but with the deprecated warning as part of the array now. So why the code I’m using thats not working but was about a yr ago is now not working. I don’t know but would like to find out.

The node I run is of no real signifigance, so if I end up having to keep an older client for the use of getinfo is fine with me.

By Ali Sherief

Editor-in-chief and serial coder & blogger.