Bitcoin Forum
April 18, 2024, 10:22:48 AM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Whats the command to ping my Bitcoin node to send me X amount of blocks?  (Read 1531 times)
Xenland (OP)
Legendary
*
Offline Offline

Activity: 980
Merit: 1003


I'm not just any shaman, I'm a Sha256man


View Profile
November 06, 2012, 09:07:26 AM
 #1

 Whats the command to ping my Bitcoin node to send me X amount of blocks?
1713435768
Hero Member
*
Offline Offline

Posts: 1713435768

View Profile Personal Message (Offline)

Ignore
1713435768
Reply with quote  #2

1713435768
Report to moderator
1713435768
Hero Member
*
Offline Offline

Posts: 1713435768

View Profile Personal Message (Offline)

Ignore
1713435768
Reply with quote  #2

1713435768
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713435768
Hero Member
*
Offline Offline

Posts: 1713435768

View Profile Personal Message (Offline)

Ignore
1713435768
Reply with quote  #2

1713435768
Report to moderator
mskwik
Full Member
***
Offline Offline

Activity: 125
Merit: 100


View Profile
November 06, 2012, 03:50:34 PM
 #2

https://en.bitcoin.it/wiki/Protocol_specification#getblocks

Not really set up to give a certain number, you just give it the hash for the last block you have (or a couple of the latest) and it will send a list of anything newer (up to 500 blocks).

mskwik
Full Member
***
Offline Offline

Activity: 125
Merit: 100


View Profile
November 06, 2012, 04:00:50 PM
 #3

https://en.bitcoin.it/wiki/Protocol_specification#getblocks

Not really set up to give a certain number, you just give it the hash for the last block you have (or a couple of the latest) and it will send a list of anything newer (up to 500 blocks).

that is actual code, for the protocol, that has nothing to do with the API of the bitcoin node

That is correct, as far as I know there isn't any way to get multiple blocks out of the RPC interface (unless that has changed in recent versions) but if you connect via the P2P interface it will spit out a list fairly quickly.  Depends on your application which is easier and the original question wasn't very specific.

Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652
Merit: 2216


Chief Scientist


View Profile WWW
November 06, 2012, 04:11:40 PM
 #4

That is correct, as far as I know there isn't any way to get multiple blocks out of the RPC interface (unless that has changed in recent versions)

Recent versions support JSON-2.0 "batch" queries, so you can combine multiple RPC calls into one batch and get a list of responses.


How often do you get the chance to work on a potentially world-changing project?
mskwik
Full Member
***
Offline Offline

Activity: 125
Merit: 100


View Profile
November 06, 2012, 04:38:30 PM
 #5

That is correct, as far as I know there isn't any way to get multiple blocks out of the RPC interface (unless that has changed in recent versions)

Recent versions support JSON-2.0 "batch" queries, so you can combine multiple RPC calls into one batch and get a list of responses.


Ah, very nice.  I've been using the P2P interface to get raw block data since before the getblock RPC calls were standard (seemed preferable to vetting various getblock patches and keeping up with changing arguments for them) and even in fairly recent releases the performance seems to be much better connecting that way, but I will admit I haven't tried it again that recently.

Xenland (OP)
Legendary
*
Offline Offline

Activity: 980
Merit: 1003


I'm not just any shaman, I'm a Sha256man


View Profile
November 06, 2012, 09:20:19 PM
 #6

Thanks for the information everyone!
All this information is going towards the Bitcoin Pseudocode Client Documentation project: https://github.com/Xenland/Bitcoin-Pseudocode-Client/tree/gh-pages

Does anyone know how to query a Bitcoin node from the outside with getblocks?

I tried to curl request with my Bitcoin node in "JSON" formate and bitcoin client says unknown message or invalid message. Any BTC source code references for connecting to the p2p interface?
kjj
Legendary
*
Offline Offline

Activity: 1302
Merit: 1024



View Profile
November 06, 2012, 09:32:47 PM
 #7

To use the p2p protocol to spew blocks, you'll have to implement a fair chunk of the protocol; enough to convince your node that it is talking to another node.

If you just want to see the blocks, parsing the block files is super easy.  The only catch is that you'll need to build your own index as you go.  The satoshi client keeps an index, but it is in the BDB system, and interfacing with that is probably much more work than doing it yourself.

17Np17BSrpnHCZ2pgtiMNnhjnsWJ2TMqq8
I routinely ignore posters with paid advertising in their sigs.  You should too.
mskwik
Full Member
***
Offline Offline

Activity: 125
Merit: 100


View Profile
November 07, 2012, 03:01:14 AM
 #8

Here's a basic sample of connecting to the P2P interface in Perl, this just connects and spits out any new blocks in raw format on STDOUT and debug information about the connection on STDERR but once you've got the connection running you can send any commands through it, just need to be careful about the network byte orders that bitcoin uses.

Code:
#!/usr/bin/perl

use IO::Socket;use IO::Select;
use Digest::SHA;$|=1;

$btc=IO::Socket::INET->new(PeerAddr=>"localhost",PeerPort=>'8333',Proto=>'tcp') or die "Socket";
$sel=IO::Select->new();$sel->add($btc);

$p="\x2c\x7e\x00\x00"; # Version
$p.="\x01\x00\x00\x00\x00\x00\x00\x00"; # Services
$h=sprintf('%x',time());while(length($h)<16){$h="0".$h;}
$h=reverse($h);$p.=pack('h*',$h); # Unix Timestamp
$p.="\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"; # To IP
$p.="\x7f\x00\x00\x01\x20\x8d"; # 127.0.0.1 - Port 8333
$p.="\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"; # From IP
$p.="\x00\x00\x00\x00\x20\x8d"; # 0.0.0.0 - Port 8333
@chars=('a'..'f', 0..9);$rr=join '',map {$chars[rand @chars]} 1..16;
$p.=pack('H*',$rr); # Random Unique ID
$p.="\x00\x00\x00\x00\x00"; # No UA/Blocks

sendpacket('version',$p);
while($gotver==0){
  ($c,$p)=readpacket();
  if($c eq 'version'){$gotver=1;}
sleep(1);}
sendpacket('verack','');

while(1){
  ($c,$p)=readpacket();
  if($c eq 'inv'){
    sendpacket('getdata',$p);
  }
  if($c eq 'block'){
    $blk=unpack('H*',$p);print $blk."\n";
  }
sleep(1);}
close($btc);

sub sendpacket{my $c=shift(@_);my $p=shift(@_);
  my $o="\xf9\xbe\xb4\xd9"; # Magic for main network
  while(length($c)<12){$c.="\x00";}$o.=$c; #Command
  my $h=sprintf('%x',length($p));while(length($h)<8){$h="0".$h;}
  $h=reverse($h);$o.=pack('h*',$h); # Payload length
  $h=Digest::SHA::sha256(Digest::SHA::sha256($p));
  $h=unpack('H*',$h);$o.=pack('H*',substr($h,0,8)); # Checksum
  $o.=$p; # Payload
  print $btc $o;print STDERR "Sent $c (".length($p)." byte payload)\n";
}
sub readpacket{
  if(!$sel->can_read(1)){return 0;}
  my @x;my $x;while(1){read($btc,$x,1);$x=unpack('H*',$x);
    push(@x,$x);if($#x>3){shift(@x);}
    if(join('.',@x) eq "f9.be.b4.d9"){
      my $cmd;my $len;
      read($btc,$cmd,12);read($btc,$len,4);read($btc,$x,4);
      $x=unpack('h*',$len);$x=reverse($x);$len=hex($x);
      $x=chop($cmd);while($x eq "\x00"){$x=chop($cmd);}$cmd.=$x;
      print STDERR "Read $cmd ($len byte payload)\n";
      if($len>0){read($btc,$x,$len);return ($cmd,$x);}
      else{return ($cmd,'');}
    }
  }
}

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!