2021-06-28 07:48:36 +02:00
# SPDX-License-Identifier: GPL-3.0-or-later
# standard imports
import datetime
import sys
import os
import json
import argparse
import logging
2021-08-21 09:27:40 +02:00
# external imports
from chainlib . chain import ChainSpec
2021-06-28 07:48:36 +02:00
from hexathon import (
add_0x ,
strip_0x ,
even ,
)
import sha3
2021-10-18 14:23:54 +02:00
from funga . eth . signer import EIP155Signer
2021-06-28 07:48:36 +02:00
# local imports
2021-08-21 09:27:40 +02:00
from chainlib . eth . address import AddressChecksum
2021-06-28 07:48:36 +02:00
from chainlib . eth . chain import network_id
from chainlib . eth . block import (
block_latest ,
block_by_number ,
2021-10-25 11:24:55 +02:00
syncing ,
2021-06-28 07:48:36 +02:00
Block ,
)
from chainlib . eth . tx import count
from chainlib . eth . connection import EthHTTPConnection
from chainlib . eth . gas import (
OverrideGasOracle ,
balance ,
price ,
)
2021-08-21 09:27:40 +02:00
import chainlib . eth . cli
2022-05-12 13:23:00 +02:00
from chainlib . eth . cli . arg import (
Arg ,
ArgFlag ,
process_args ,
)
from chainlib . eth . cli . config import (
Config ,
process_config ,
)
from chainlib . eth . cli . log import process_log
2021-06-28 07:48:36 +02:00
BLOCK_SAMPLES = 10
logg = logging . getLogger ( )
2021-08-21 09:27:40 +02:00
script_dir = os . path . dirname ( os . path . realpath ( __file__ ) )
config_dir = os . path . join ( script_dir , ' .. ' , ' data ' , ' config ' )
2021-06-28 07:48:36 +02:00
2021-10-25 11:24:55 +02:00
results_translation = {
' network_id ' : ' Network Id ' ,
' block ' : ' Block ' ,
' syncing ' : ' Syncing ' ,
' gas_limit ' : ' Gas Limit ' ,
' gas_price ' : ' Gas Price ' ,
' block_time ' : ' Block time ' ,
}
2022-05-12 13:23:00 +02:00
def process_config_local ( config , arg , args , flags ) :
config . add ( args . local , ' _LOCAL ' , False )
config . add ( args . long , ' _LONG ' , False )
config . add ( args . entry , ' _ENTRY ' , False )
return config
arg_flags = ArgFlag ( )
arg = Arg ( arg_flags )
flags = arg_flags . STD_READ | arg_flags . ENV
argparser = chainlib . eth . cli . ArgumentParser ( )
argparser = process_args ( argparser , arg , flags )
2021-08-21 09:27:40 +02:00
argparser . add_argument ( ' --long ' , action = ' store_true ' , help = ' Calculate averages through sampling of blocks and txs ' )
2021-10-25 11:24:55 +02:00
argparser . add_argument ( ' --local ' , action = ' store_true ' , help = ' Include local info ' )
2022-05-12 13:23:00 +02:00
argparser . add_argument ( ' entry ' , nargs = ' ? ' , help = ' Output single item ' )
2021-08-21 09:27:40 +02:00
args = argparser . parse_args ( )
2021-06-28 07:48:36 +02:00
2022-05-12 14:58:24 +02:00
logg = process_log ( args , logg )
2022-05-12 13:23:00 +02:00
config = Config ( )
config = process_config ( config , arg , args , flags )
config = process_config_local ( config , arg , args , flags )
logg . debug ( ' config loaded: \n {} ' . format ( config ) )
2021-06-28 07:48:36 +02:00
2021-10-25 11:24:55 +02:00
if config . get ( ' _ENTRY ' ) != None :
if config . get ( ' _ENTRY ' ) not in results_translation . keys ( ) :
raise ValueError ( ' Unknown entry {} ' . format ( config . get ( ' _ENTRY ' ) ) )
2021-06-28 07:48:36 +02:00
2021-10-25 11:24:55 +02:00
rpc = chainlib . eth . cli . Rpc ( )
2021-08-21 09:27:40 +02:00
conn = rpc . connect_by_config ( config )
2021-06-28 07:48:36 +02:00
token_symbol = ' eth '
2021-08-21 09:27:40 +02:00
chain_spec = ChainSpec . from_chain_str ( config . get ( ' CHAIN_SPEC ' ) )
2021-06-28 07:48:36 +02:00
2021-08-21 09:27:40 +02:00
human = not config . true ( ' _RAW ' )
2021-06-28 07:48:36 +02:00
2021-08-21 09:27:40 +02:00
longmode = config . true ( ' _LONG ' )
2021-06-28 07:48:36 +02:00
2021-10-25 11:24:55 +02:00
def set_result ( results , k , v , w = sys . stdout ) :
kt = results_translation [ k ]
if str ( config . get ( ' _ENTRY ' ) ) == k :
w . write ( ' {} ' . format ( v ) )
return True
logg . info ( ' {} : {} \n ' . format ( kt , v ) )
results [ k ] = v
return False
2021-06-28 07:48:36 +02:00
def main ( ) :
2021-10-25 11:24:55 +02:00
results = { }
2021-08-21 09:27:40 +02:00
o = network_id ( id_generator = rpc . id_generator )
2021-06-28 07:48:36 +02:00
r = conn . do ( o )
#if human:
# n = format(n, ',')
2021-10-25 11:24:55 +02:00
if set_result ( results , ' network_id ' , r ) :
return
2021-06-28 07:48:36 +02:00
2021-08-21 09:27:40 +02:00
o = block_latest ( id_generator = rpc . id_generator )
2021-06-28 07:48:36 +02:00
r = conn . do ( o )
2022-02-12 13:30:13 +01:00
try :
n = int ( r , 16 )
except ValueError :
n = int ( r )
2021-06-28 07:48:36 +02:00
first_block_number = n
if human :
n = format ( n , ' , ' )
2021-10-25 11:24:55 +02:00
if set_result ( results , ' block ' , n ) :
return
2021-06-28 07:48:36 +02:00
2021-08-21 09:27:40 +02:00
o = block_by_number ( first_block_number , False , id_generator = rpc . id_generator )
2021-06-28 07:48:36 +02:00
r = conn . do ( o )
last_block = Block ( r )
last_timestamp = last_block . timestamp
if longmode :
aggr_time = 0.0
aggr_gas = 0
for i in range ( BLOCK_SAMPLES ) :
2021-08-21 09:27:40 +02:00
o = block_by_number ( first_block_number - i , False , id_generator = rpc . id_generator )
2021-06-28 07:48:36 +02:00
r = conn . do ( o )
block = Block ( r )
aggr_time + = last_block . timestamp - block . timestamp
gas_limit = int ( r [ ' gasLimit ' ] , 16 )
aggr_gas + = gas_limit
last_block = block
last_timestamp = block . timestamp
n = int ( aggr_gas / BLOCK_SAMPLES )
if human :
n = format ( n , ' , ' )
2021-10-25 11:24:55 +02:00
if set_result ( results , ' gas_limit ' , n ) :
return
if set_result ( results , ' block_time ' , aggr_time / BLOCK_SAMPLES ) :
return
2021-06-28 07:48:36 +02:00
2021-08-21 09:27:40 +02:00
o = price ( id_generator = rpc . id_generator )
2021-06-28 07:48:36 +02:00
r = conn . do ( o )
n = int ( r , 16 )
if human :
n = format ( n , ' , ' )
2021-10-25 11:24:55 +02:00
if set_result ( results , ' gas_price ' , n ) :
return
2021-06-28 07:48:36 +02:00
2021-10-25 11:24:55 +02:00
if config . get ( ' _LOCAL ' ) :
o = syncing ( )
2021-06-28 07:48:36 +02:00
r = conn . do ( o )
2021-10-25 11:24:55 +02:00
if set_result ( results , ' syncing ' , r ) :
return
if config . get ( ' _ENTRY ' ) != None :
raise RuntimeError ( ' entry {} ( {} ) not processed, please review the flag settings ' . format ( config . get ( ' _ENTRY ' ) , results_translation [ config . get ( ' _ENTRY ' ) ] ) )
for k in results . keys ( ) :
kt = results_translation [ k ]
sys . stdout . write ( ' {} : {} \n ' . format ( kt , results [ k ] ) )
2021-06-28 07:48:36 +02:00
if __name__ == ' __main__ ' :
main ( )