77
88try :
99 from sql_dict import SQLDict
10+ from trx_receipts_storage import TrxReceiptsStorage
1011except ImportError :
1112 from .sql_dict import SQLDict
13+ from .trx_receipts_storage import TrxReceiptsStorage
1214
1315
1416PARALLEL_REQUESTS = int (os .environ .get ("PARALLEL_REQUESTS" , "2" ))
@@ -40,11 +42,19 @@ def __init__(self,
4042
4143 self .evm_loader_id = evm_loader_id
4244 self .client = Client (solana_url )
43- self .transaction_receipts = SQLDict ( tablename = "known_transactions" )
45+ self .transaction_receipts = TrxReceiptsStorage ( 'transaction_receipts' , log_level )
4446 self .last_slot = start_slot
4547 self .current_slot = 0
46- self .transaction_order = []
4748 self .counter_ = 0
49+ self .max_known_tx = self .transaction_receipts .max_known_trx ()
50+ self ._move_data_from_old_table ()
51+
52+
53+ def _move_data_from_old_table (self ):
54+ if self .transaction_receipts .size () == 0 :
55+ transaction_receipts_old = SQLDict (tablename = "known_transactions" )
56+ for signature , trx in transaction_receipts_old .iteritems ():
57+ self ._add_trx (signature , trx )
4858
4959
5060 def run (self ):
@@ -53,6 +63,7 @@ def run(self):
5363 self .process_functions ()
5464 except Exception as err :
5565 logger .warning ("Got exception while indexing. Type(err):%s, Exception:%s" , type (err ), err )
66+ time .sleep (1.0 )
5667
5768
5869 def process_functions (self ):
@@ -62,82 +73,72 @@ def process_functions(self):
6273
6374 def gather_unknown_transactions (self ):
6475 poll_txs = set ()
65- ordered_txs = []
6676
6777 minimal_tx = None
6878 continue_flag = True
6979 current_slot = self .client .get_slot (commitment = "confirmed" )["result" ]
70- maximum_slot = self . last_slot
71- minimal_slot = current_slot
80+
81+ max_known_tx = self . max_known_tx
7282
7383 counter = 0
7484 while (continue_flag ):
75- opts : Dict [str , Union [int , str ]] = {}
76- if minimal_tx :
77- opts ["before" ] = minimal_tx
78- opts ["commitment" ] = "confirmed"
79- result = self .client ._provider .make_request ("getSignaturesForAddress" , self .evm_loader_id , opts )
80- logger .debug ("{:>3} get_signatures_for_address {}" .format (counter , len (result ["result" ])))
85+ results = self ._get_signatures (minimal_tx , self .max_known_tx [1 ])
86+ logger .debug ("{:>3} get_signatures_for_address {}" .format (counter , len (results )))
8187 counter += 1
8288
83- if len (result [ "result" ] ) == 0 :
84- logger .debug ("len(result['result'] ) == 0" )
89+ if len (results ) == 0 :
90+ logger .debug ("len(results ) == 0" )
8591 break
8692
87- for tx in result ["result" ]:
93+ minimal_tx = results [- 1 ]["signature" ]
94+ max_tx = (results [0 ]["slot" ], results [0 ]["signature" ])
95+ max_known_tx = max (max_known_tx , max_tx )
96+
97+ for tx in results :
8898 solana_signature = tx ["signature" ]
8999 slot = tx ["slot" ]
90100
101+ if slot < self .last_slot :
102+ continue_flag = False
103+ break
104+
91105 if solana_signature in HISTORY_START :
92106 logger .debug (solana_signature )
93107 continue_flag = False
94108 break
95109
96- ordered_txs .append (solana_signature )
97-
98- if solana_signature not in self .transaction_receipts :
110+ if not self .transaction_receipts .contains (slot , solana_signature ):
99111 poll_txs .add (solana_signature )
100112
101- if slot < minimal_slot :
102- minimal_slot = slot
103- minimal_tx = solana_signature
104-
105- if slot > maximum_slot :
106- maximum_slot = slot
107-
108- if slot < self .last_slot :
109- continue_flag = False
110- break
111-
112113 logger .debug ("start getting receipts" )
113114 pool = ThreadPool (PARALLEL_REQUESTS )
114- pool .map (self .get_tx_receipts , poll_txs )
115-
116- if len (self .transaction_order ):
117- index = 0
118- try :
119- index = ordered_txs .index (self .transaction_order [0 ])
120- except ValueError :
121- self .transaction_order = ordered_txs + self .transaction_order
122- else :
123- self .transaction_order = ordered_txs [:index ] + self .transaction_order
124- else :
125- self .transaction_order = ordered_txs
115+ pool .map (self ._get_tx_receipts , poll_txs )
126116
127- self .last_slot = maximum_slot
128117 self .current_slot = current_slot
129-
130118 self .counter_ = 0
119+ logger .debug (max_known_tx )
120+ self .max_known_tx = max_known_tx
121+
122+
123+ def _get_signatures (self , before , until ):
124+ opts : Dict [str , Union [int , str ]] = {}
125+ if until is not None :
126+ opts ["until" ] = until
127+ if before is not None :
128+ opts ["before" ] = before
129+ opts ["commitment" ] = "confirmed"
130+ result = self .client ._provider .make_request ("getSignaturesForAddress" , self .evm_loader_id , opts )
131+ return result ['result' ]
131132
132133
133- def get_tx_receipts (self , solana_signature ):
134+ def _get_tx_receipts (self , solana_signature ):
134135 # trx = None
135136 retry = True
136137
137138 while retry :
138139 try :
139140 trx = self .client .get_confirmed_transaction (solana_signature )['result' ]
140- self .transaction_receipts [ solana_signature ] = trx
141+ self ._add_trx ( solana_signature , trx )
141142 retry = False
142143 except Exception as err :
143144 logger .debug (err )
@@ -147,4 +148,16 @@ def get_tx_receipts(self, solana_signature):
147148 if self .counter_ % 100 == 0 :
148149 logger .debug (self .counter_ )
149150
150- # return (solana_signature, trx)
151+
152+ def _add_trx (self , solana_signature , trx ):
153+ if trx is not None :
154+ add = False
155+ for instruction in trx ['transaction' ]['message' ]['instructions' ]:
156+ if trx ["transaction" ]["message" ]["accountKeys" ][instruction ["programIdIndex" ]] == self .evm_loader_id :
157+ add = True
158+ if add :
159+ logger .debug ((trx ['slot' ], solana_signature ))
160+ self .transaction_receipts .add_trx (trx ['slot' ], solana_signature , trx )
161+ else :
162+ logger .debug (f"trx is None { solana_signature } " )
163+
0 commit comments