Skip to content

Commit

Permalink
fix: agent query l1 tx too early
Browse files Browse the repository at this point in the history
  • Loading branch information
magicalne committed Sep 26, 2022
1 parent 7151865 commit 6c8998e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
38 changes: 20 additions & 18 deletions agent/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_gw_stat_by_lock(lock_name, gw_rpc: GodwokenRpc, block_hash,
lock_type_hash = gw_config.get_lock_type_hash(lock_name)
res = gw_rpc.gw_get_block_committed_info(block_hash)
if res is None or res is None:
return (0, 0)
raise Exception(f"Block hash: {block_hash} isn't committed.")
tx = res["transaction_hash"]
res = ckb_rpc.get_transaction(tx)
inputs = res["transaction"]["inputs"]
Expand Down Expand Up @@ -105,12 +105,16 @@ def update_metrics(tip_number, ping, last_block_hash: str,
"""
def reset_metrics():
with TaskLock:
global BlockNumber
global CommitTransacionCount
global DepositDict
global WithdrawalDict
CommitTransacionCount = {}
DepositDict = {}
WithdrawalDict = {}
if BlockNumber is not None:
last_block_num = BlockNumber - 1
CommitTransacionCount.pop(last_block_num, None)
DepositDict.pop(last_block_num, None)
WithdrawalDict.pop(last_block_num, None)
logging.info(f"Reset metrics for block: {last_block_num}")

"""
General metrics job.
Expand Down Expand Up @@ -142,7 +146,6 @@ def __init__(self):
elif net_env == "mainnet_v1":
self.gw_config = mainnet_v1_config()
else:
logging.info("use devnet")
rollup_result_path = os.environ["ROLLUP_RESULT_PATH"]
scripts_result_path = os.environ["SCRIPTS_RESULT_PATH"]
self.gw_config = devnet_config(rollup_result_path,
Expand All @@ -153,25 +156,27 @@ def __init__(self):
)
self.gw_config = testnet_config()

"""
Always try to get new tip.
"""
def run(self):
global BlockNumber

while True:
sleep(1)
logging.info("Start running")
try:
tip_number = self.gw_rpc.get_tip_number()
except:
logging.exception("Cannot get tip number")
continue
logging.debug("Start running")
if BlockNumber is None:
try:
tip_number = self.gw_rpc.get_tip_number()
except:
logging.exception("Cannot get tip number")
continue
else:
tip_number = BlockNumber

try:
ping = self.gw_rpc.ping()

block = self.gw_rpc.get_block_by_number(hex(tip_number))
if block is None:
continue
last_block_hash = block['hash']
last_block_ts = convert_int(block['raw']['timestamp'])
tx_cnt = len(block['transactions'])
Expand All @@ -188,7 +193,6 @@ def run(self):
except:
logging.exception("get block info failed")
continue
logging.info("Loading deposit stats")
try:
deposit = get_gw_stat_by_lock(
"deposit_lock", self.gw_rpc,
Expand All @@ -197,7 +201,6 @@ def run(self):
except:
logging.exception("Failed to get deposit stats")
continue
logging.info("Loading withdrawal stats")
try:
withdrawal = get_gw_stat_by_lock(
"withdrawal_lock", self.gw_rpc,
Expand All @@ -207,7 +210,7 @@ def run(self):
logging.exception("Failed to get withdrawal stats")
continue
update_metrics(
tip_number,
tip_number+1,
ping,
last_block_hash,
last_block_ts,
Expand All @@ -231,7 +234,6 @@ def run(self):
global CustodianStats
while True:
sleep(10)
logging.info("Loading custodian stats")
try:
CustodianStats = get_custodian(
self.ckb_indexer_url, self.gw_config,
Expand Down
1 change: 0 additions & 1 deletion agent/ckb_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ def get_custodian_stats(self, gw_config: GwConfig,
else:
ckb_cell_count += 1

logging.info(f"loaded {cnt} page of custodian cells")
cursor = result["last_cursor"]
if cursor == "0x":
break
Expand Down
1 change: 0 additions & 1 deletion agent/godwoken_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def get_block(self, block_hash):

def get_tip_number(self):
tip_block_hash = self.get_tip_block_hash()
print(f'tip block hash: {tip_block_hash}')
tip = self.get_block(tip_block_hash)
tip_number = tip['block']['raw']['number']
return convert_int(tip_number)
Expand Down
4 changes: 2 additions & 2 deletions agent/json_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def submit(self, method: str, params: List):
}
try:
r = requests.post(url="%s" % (self.url), json=payload, headers=headers)
logging.info(f"Access {self.url} {method} status: {r.status_code}")
logging.debug(f"Access {self.url} {method} status: {r.status_code}")
r.raise_for_status()
return r.json()["result"]
except Exception as e:
logging.error(f"Submit request to {self.url}, method: {method}, params: {params}", exc_info = e)
logging.warn(f"Submit request to {self.url}, method: {method}, params: {params}", exc_info = e)
raise RPCException from None


0 comments on commit 6c8998e

Please sign in to comment.