Bitcoin Deep Study - Perl6 HON P2P Networking - Building a node-to-node communication system as if the nodes were talking to each other

in #blockchain7 years ago

All Bitcoin's communication are done over TCP

TCP stands for Transfer Control Protocol and is part of the Internet protocol suite which also includes IP (Internet Protocol).

Mocking up Communications with Grammars

Amazingly, on Perl6 Grammars I'm able to build the node communication as if they were actually talking to each other. This is one of the reasons I consider Perl6 to be a 4th generation computer language. Here's a sketch of a messaging system based on bitcoin still not fully implemented.

unit package EC::Grammars:auth<github:bioduds>;

grammar VERACK::Greetings {
  rule TOP { <GREETING> <NODE> <VERSION> }
  token GREETING { 'Hello,' | 'Hi,' | 'Good Morning,' }
  token NODE { 'I am ' ( \d+ \. \d+ \. \d+ \. \d+ ) }
  token VERSION { 'and my version is ' ( \d+ \. \d+ \. \d+ ) }
}

class VERACK::Greetings::Actions {
  has $.timestamp;
  has $.node;
  has $.version;
  method TOP ($/) {
    self!respond;
    say "VERACK::Greetings done!";
  }
  method GREETING ($/) {
    say 'Has greeting!';
    my $proc = run 'date', :out, :err;
    $!timestamp = $proc.out.slurp: :close;
  }
  method NODE ($/) { say 'Has NODE ' ~ $/[0]; $!node = $/[0]; }
  method VERSION ($/) { say 'Has VERSION ' ~ $/[0]; $!version = $/[0]; }
  method !respond {
    say 'Responding to ' ~ $!node ~ ' with version ' ~ $!version ~ ' on ' ~ $!timestamp;
  }
}

grammar VERACK::Answer {
  rule TOP { <OK> <NODE> <VERSION> <TIMESTAMP> }
  token GREETING { 'Hello' | 'Hi' | 'Yo' }
  token NODE { 'I am ' ( \d+ \. \d+ \. \d+ \. \d+ ) }
  token VERSION { 'and my version is ' ( \d+ \. \d+ \. \d+ ) }
}

class VERACK::Answer::Actions {

}

grammar ADDR {
  rule TOP { <IDENTIFIER> <TIMESTAMP> <LINKER> <ADDRESS>+ }
  token IDENTIFIER { 'Right now, ' }
  rule TIMESTAMP { <WEEK-DAY> <MONTH> <DAY> <TIME> <PLACE> <YEAR> }
  token WEEK-DAY { \S+ }
  token MONTH { \S+ }
  token DAY { \d+ }
  token TIME { \d+ \: \d+ \: \d+ }
  token PLACE { \S+ }
  token YEAR { \d+ }
  token LINKER { 'I have' }
  rule ADDRESS { \d+ \. \d+ \. \d+ \. \d+ }
}

class ADDR::Actions {
  method TOP ($/) { say "ADDR went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method TIMESTAMP ($/) { say "Got TIMESTAMP " ~ $/; }
  method LINKER ($/) { say "Got LINKER " ~ $/; }
  method ADDRESS ($/) { say "Got ADDRESS " ~ $/; }
}

grammar INV {
  rule TOP { <IDENTIFIER> <INFO> }
  token IDENTIFIER { 'Hey, check this out: ' }
  rule INFO { ... }
}

class INV::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method INFO ($/) { say "Got INFO " ~ $/; }
}

grammar GET-DATA {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class GET-DATA::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar NOT-FOUND {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Sorry, this hasn\'t been found' }
  rule DATA { ... }
}

class NOT-FOUND::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got ID " ~ $/; }
}

grammar GET-BLOCKS {
  rule TOP { <IDENTIFIER> <BLOCKS>+ '  ' <NODE> }
  token IDENTIFIER { 'Please, ask for these blocks: ' }
  token BLOCKS { ... }
  token NODE { ... }
}

class GET-BLOCKS::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method BLOCKS ($/) { say "Asking for BLOCKS " ~ $/; }
  method NODE ($/) { say "to this NODE " ~ $/; }
}

grammar GET-HEADERS {
  rule TOP { <IDENTIFIER> <HEADERS>+ ' from ' }
  token IDENTIFIER { 'Please, get me these HEADERS: ' }
  token HEADERS { ... }
  token NODE { ... }
}

class GET-HEADERS::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method HEADERS ($/) { say "Got HEADERS " ~ $/; }
  method NODE ($/) { say "Got DATA " ~ $/; }
}

grammar TX {
  rule TOP { <IDENTIFIER> <TX> <NODE> }
  token IDENTIFIER { 'Here\'s the TX you asked: ' }
  token TX { ... }
  token NODE { ... }
}

class TX::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method TX ($/) { say "Sending TX " ~ $/; }
  method NODE ($/) { say "To this NODE " ~ $/; }
}

grammar BLOCK {
  rule TOP { <IDENTIFIER> <BLOCK> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class BLOCK::Actions {
  method TOP ($/) { say "Sending BLOCK information!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Sending DATA " ~ $/; }
}

grammar HEADERS {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class HEADERS::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar GET-ADDR {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class GET-ADDR::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar MEM-POOL {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class MEM-POOL::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar CHECK-ORDER {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class CHECK-ORDER::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar SUBMIT-ORDER {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class SUBMIT-ORDER::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar REPLY {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class REPLY::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar PING {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class PING::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar PONG {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class PONG::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar REJECT {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class REJECT::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar ALERT {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class ALERT::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar SEND-HEADERS {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class SEND-HEADERS::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

grammar FEE-FILTERS {
  rule TOP { <IDENTIFIER> <DATA> }
  token IDENTIFIER { 'Hey, here\'s the data: ' }
  rule DATA { ... }
}

class FEE-FILTERS::Actions {
  method TOP ($/) { say "INV went through!" }
  method IDENTIFIER ($/) { say "Got IDENTIFIER " ~ $/; }
  method DATA ($/) { say "Got DATA " ~ $/; }
}

REFERENCES


EDUARDO CAPANEMA
@bitworkers

Coin Marketplace

STEEM 0.29
TRX 0.11
JST 0.033
BTC 63945.57
ETH 3135.76
USDT 1.00
SBD 4.00