2021-04-02 15:16:27 +02:00
# standard imports
2021-02-01 18:12:51 +01:00
import os
import sys
import logging
import argparse
import re
2021-04-02 15:16:27 +02:00
# external imports
2021-02-01 18:12:51 +01:00
import celery
2021-03-29 15:27:53 +02:00
from cic_eth_registry import CICRegistry
from chainlib . chain import ChainSpec
2021-04-02 15:16:27 +02:00
from chainlib . connection import RPCConnection
2021-04-05 17:07:09 +02:00
from chainsyncer . filter import SyncFilter
2021-02-01 18:12:51 +01:00
2021-04-02 15:16:27 +02:00
# local imports
2021-08-17 08:46:51 +02:00
import cic_eth . cli
2021-02-01 18:12:51 +01:00
from cic_eth . db import dsn_from_config
from cic_eth . db import SessionBase
from cic_eth . admin . ctrl import lock_send
2021-04-05 17:07:09 +02:00
from cic_eth . db . enum import LockEnum
from cic_eth . runnable . daemons . filters . straggler import StragglerFilter
from cic_eth . sync . retry import RetrySyncer
2021-04-06 21:11:42 +02:00
from cic_eth . stat import init_chain_stat
2021-02-01 18:12:51 +01:00
logging . basicConfig ( level = logging . WARNING )
logg = logging . getLogger ( )
2021-08-17 08:46:51 +02:00
arg_flags = cic_eth . cli . argflag_std_read
local_arg_flags = cic_eth . cli . argflag_local_sync | cic_eth . cli . argflag_local_task
argparser = cic_eth . cli . ArgumentParser ( arg_flags )
2021-04-02 15:16:27 +02:00
argparser . add_argument ( ' --batch-size ' , dest = ' batch_size ' , type = int , default = 50 , help = ' max amount of txs to resend per iteration ' )
2021-08-17 08:46:51 +02:00
argparser . add_argument ( ' --retry-delay ' , dest = ' retry_delay ' , type = int , default = 20 , help = ' seconds to wait for retrying a transaction that is marked as sent ' )
argparser . process_local_flags ( local_arg_flags )
args = argparser . parse_args ( )
2021-02-01 18:12:51 +01:00
2021-08-17 08:46:51 +02:00
extra_args = {
' retry_delay ' : ' RETRY_DELAY ' ,
' batch_size ' : ' RETRY_BATCH_SIZE ' ,
2021-02-01 18:12:51 +01:00
}
2021-08-17 08:46:51 +02:00
config = cic_eth . cli . Config . from_args ( args , arg_flags , local_arg_flags , extra_args = extra_args )
2021-03-29 15:27:53 +02:00
2021-08-17 08:46:51 +02:00
# connect to database
2021-02-01 18:12:51 +01:00
dsn = dsn_from_config ( config )
2021-04-02 15:16:27 +02:00
SessionBase . connect ( dsn , debug = config . true ( ' DATABASE_DEBUG ' ) )
2021-02-01 18:12:51 +01:00
2021-08-17 08:46:51 +02:00
chain_spec = ChainSpec . from_chain_str ( config . get ( ' CHAIN_SPEC ' ) )
2021-02-01 18:12:51 +01:00
2021-08-17 08:46:51 +02:00
# set up rpc
rpc = cic_eth . cli . RPC . from_config ( config )
conn = rpc . get_default ( )
2021-04-06 21:11:42 +02:00
2021-08-17 08:46:51 +02:00
def main ( ) :
straggler_delay = int ( config . get ( ' RETRY_DELAY ' ) )
2021-04-06 21:11:42 +02:00
loop_interval = config . get ( ' SYNCER_LOOP_INTERVAL ' )
if loop_interval == None :
stat = init_chain_stat ( conn )
loop_interval = stat . block_average ( )
2021-08-17 08:46:51 +02:00
syncer = RetrySyncer ( conn , chain_spec , cic_eth . cli . chain_interface , straggler_delay , batch_size = config . get ( ' RETRY_BATCH_SIZE ' ) )
2021-04-02 15:16:27 +02:00
syncer . backend . set ( 0 , 0 )
2021-08-17 08:46:51 +02:00
fltr = StragglerFilter ( chain_spec , queue = config . get ( ' CELERY_QUEUE ' ) )
2021-04-05 17:07:09 +02:00
syncer . add_filter ( fltr )
2021-04-06 21:11:42 +02:00
syncer . loop ( int ( loop_interval ) , conn )
2021-02-01 18:12:51 +01:00
if __name__ == ' __main__ ' :
main ( )