Bitcoin Forum
May 06, 2024, 01:08:03 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Not sure if this is specifically an RPC problem or code change...  (Read 121 times)
NinjaMan35 (OP)
Jr. Member
*
Offline Offline

Activity: 134
Merit: 3


View Profile
February 16, 2018, 11:22:39 PM
 #1

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:
<?php
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$h"<?php \$bitcoind_getinfo = " var_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...

[not@here]$ php getdata.php
Could not getinfo(). getinfo() output is not an array.
[not@here]$

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.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
BitMaxz
Legendary
*
Offline Offline

Activity: 3248
Merit: 2965


Block halving is coming.


View Profile WWW
February 16, 2018, 11:56:00 PM
 #2

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

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6581


Just writing some code


View Profile WWW
February 17, 2018, 03:35:38 AM
 #3

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.

NinjaMan35 (OP)
Jr. Member
*
Offline Offline

Activity: 134
Merit: 3


View Profile
February 17, 2018, 04:28:38 AM
 #4

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.
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6581


Just writing some code


View Profile WWW
February 17, 2018, 04:31:26 AM
 #5

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.
The data type of getinfo is a JSON object, but your script expects a JSON array. It must have changed data types since your script was written.

NinjaMan35 (OP)
Jr. Member
*
Offline Offline

Activity: 134
Merit: 3


View Profile
February 17, 2018, 04:55:21 AM
 #6

Not worth trying to figure out what client would work. That being said, anyone know where to begin to make it work again while the getinfo is still available ?

Ty.
Pages: [1]
  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!