| 1 | package Net::Async::Blockchain::Client::RPC; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 5 | our $VERSION = '0.002'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Net::Async::Blockchain::Client::RPC - Async RPC Client. | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | Objects of this type would not normally be constructed directly. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | Centralize all asynchronous RPC calls. | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =over 4 | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =back | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 5 | ▶ | no indirect; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 5 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 5 | ▶ | use Net::Async::HTTP; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 5 | ▶ | use JSON::MaybeUTF8 qw(encode_json_utf8 decode_json_utf8); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 5 | ▶ | use IO::Async::Notifier; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 5 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | use constant { | ||||||||||||||||||||
| 36 | # default value for the Net::Async::HTTP stall_timeout configuration. | ||||||||||||||||||||
| 37 | 5 | DEFAULT_TIMEOUT => 100, | |||||||||||||||||||
| 38 | DEFAULT_MAX_CONNECTIONS => 6 | ||||||||||||||||||||
| 39 | 5 | ▶ | }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 13 | ▶ | sub endpoint { shift->{endpoint} } | ||||||||||||||||||
|
Subroutine
endpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 42 | 13 | ▶ | sub rpc_user { shift->{rpc_user} } | ||||||||||||||||||
|
Subroutine
rpc_user: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 43 | 13 | ▶ | sub rpc_password { shift->{rpc_password} } | ||||||||||||||||||
|
Subroutine
rpc_password: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 44 | 6 | ▶ | sub timeout { shift->{timeout} // DEFAULT_TIMEOUT } | ||||||||||||||||||
|
Subroutine
timeout: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
+(shift())->{"timeout"} // 100
MC/DC100%1/1
+(shift())->{"timeout"} // 100
+(shift())->{"timeout"}
Truth table100%2/2
A
A= +(shift())->{"timeout"}
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =head2 max_connections | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | L<https://metacpan.org/pod/Net::Async::HTTP#max_connections_per_host-=%3E-INT> | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =over 4 | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =back | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | returns the configured max_connections value or DEFAULT_MAX_CONNECTIONS | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 3 | ▶ | sub max_connections { shift->{max_connections} // DEFAULT_MAX_CONNECTIONS } | ||||||||||||||||||
|
Subroutine
max_connections: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
+(shift())->{"max_connections"} // 6
MC/DC100%1/1
+(shift())->{"max_connections"} // 6
+(shift())->{"max_connections"}
Truth table100%2/2
A
A= +(shift())->{"max_connections"}
| |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =head2 http_client | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | Create an L<Net::Async::HTTP> instance, if it is already defined just return | ||||||||||||||||||||
| 63 | the object | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =over 4 | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =back | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | L<Net::Async::HTTP> | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | sub http_client { | ||||||||||||||||||||
| 74 | 13 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
http_client: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 13 | ▶ | return $self->{http_client} //= do { | ||||||||||||||||||
|
Condition66%2/3
$self->{"http_client"} //= do {
MC/DC0%0/2
$self->{"http_client"} //= do {
$self->add_child(my $http_client = "Net::Async::HTTP"->new("decode_content", 1, "stall_timeout", $self->timeout, "timeout", $self->timeout, "max_connections_per_host", $self->max_connections));
$http_client
}
$self->{"http_client"}
do {
$self->add_child(my $http_client = "Net::Async::HTTP"->new("decode_content", 1, "stall_timeout", $self->timeout, "timeout", $self->timeout, "max_connections_per_host", $self->max_connections));
$http_client
}
Truth table66%2/3
A //= B
A= $self->{"http_client"}B= do {
$self->add_child(my $http_client = "Net::Async::HTTP"->new("decode_content", 1, "stall_timeout", $self->timeout, "timeout", $self->timeout, "max_connections_per_host", $self->max_connections));
$http_client
}
| |||||||||||||||||||||
| 77 | 3 | $self->add_child( | |||||||||||||||||||
| 78 | my $http_client = Net::Async::HTTP->new( | ||||||||||||||||||||
| 79 | decode_content => 1, | ||||||||||||||||||||
| 80 | stall_timeout => $self->timeout, | ||||||||||||||||||||
| 81 | timeout => $self->timeout, | ||||||||||||||||||||
| 82 | max_connections_per_host => $self->max_connections, | ||||||||||||||||||||
| 83 | )); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 3 | $http_client; | |||||||||||||||||||
| 86 | }; | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =head2 configure | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Any additional configuration that is not described on L<IO::Async::Notifier> | ||||||||||||||||||||
| 92 | must be included and removed here. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =over 4 | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =item * C<endpoint> | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =item * C<timeout> connection timeout (seconds) | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =item * C<rpc_user> RPC user. (optional, default: undef) | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =item * C<rpc_password> RPC password. (optional, default: undef) | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =back | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | sub configure { | ||||||||||||||||||||
| 109 | 3 | ▶ | my ($self, %params) = @_; | ||||||||||||||||||
|
Subroutine
configure: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 3 | for my $k (qw(endpoint rpc_user rpc_password timeout max_connections)) { | |||||||||||||||||||
| 112 | 15 | ▶ | $self->{$k} = delete $params{$k} if exists $params{$k}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 3 | $self->SUPER::configure(%params); | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head2 _request | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | Use any argument as the method parameter for the client RPC call | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =over 4 | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =item * C<method> | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =item * C<params> (any parameter required by the RPC call) | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =back | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | L<Future> | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 13 | ▶ | async sub _request { | ||||||||||||||||||
|
Subroutine
_request: called CC=4 SCAR=14.6
| |||||||||||||||||||||
| 135 | 13 | my ($self, $method, @params) = @_; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 13 | my $obj = { | |||||||||||||||||||
| 138 | id => 1, | ||||||||||||||||||||
| 139 | method => $method, | ||||||||||||||||||||
| 140 | params => [@params], | ||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||
| 142 | # for Geth JSON-RPC spec requires the version field to be exactly "jsonrpc": "2.0" | ||||||||||||||||||||
| 143 | 13 | ▶ | $obj->{jsonrpc} = $self->jsonrpc if $self->jsonrpc; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | 13 | my @post_params = ($self->endpoint, encode_json_utf8($obj), content_type => 'application/json'); | |||||||||||||||||||
| 145 | # for ETH based, we don't require user+password. Check to send user+password if exists. | ||||||||||||||||||||
| 146 | 13 | ▶ | push @post_params, (user => $self->rpc_user) if $self->rpc_user; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 13 | ▶ | push @post_params, (pass => $self->rpc_password) if $self->rpc_password; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 13 | my $response = await $self->http_client->POST(@post_params); | |||||||||||||||||||
| 150 | 0 | return decode_json_utf8($response->decoded_content)->{result}; | |||||||||||||||||||
| 151 | 5 | } | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 5 | 1; | |||||||||||||||||||