Skip to content

Commit

Permalink
feat: set custodian optinal
Browse files Browse the repository at this point in the history
  • Loading branch information
magicalne committed Apr 18, 2022
1 parent 2122b9a commit b58684a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
11 changes: 7 additions & 4 deletions agent/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from agent.sched_custodian import get_custodian

DISABLE_CUSTODIAN_STATS = 'DISABLE_CUSTODIAN_STATS'
NodeFlask = Flask(__name__)
web3_url = os.environ["WEB3_URL"]
gw_rpc_url = os.environ["GW_RPC_URL"]
Expand Down Expand Up @@ -271,6 +272,7 @@ def run(self):
global WithdrawalCapacity

while True:
logging.info("Start running")
if BlockNumber is None:
LastBlockNumber = self.gw_rpc.get_tip_number()
else:
Expand Down Expand Up @@ -301,10 +303,11 @@ def run(self):
TPS = LastBlockDetail[
"commit_transactions"] / BlockTimeDifference * 1000
one_ckb = 100_000_000
logging.info("Loading custodian stats")
CustodianStats = get_custodian(self.ckb_indexer_url,
self.gw_config,
LastBlockDetail["blocknumber"])
if DISABLE_CUSTODIAN_STATS not in os.environ:
logging.info("Loading custodian stats")
CustodianStats = get_custodian(self.ckb_indexer_url,
self.gw_config,
LastBlockDetail["blocknumber"])
logging.info("Loading deposit stats")
DepositCount, DepositCapacity = get_gw_stat_by_lock(
"deposit_lock", self.gw_rpc, LastBlockHash["last_block_hash"],
Expand Down
41 changes: 24 additions & 17 deletions agent/ckb_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,20 @@ class CustodianStats:


class CKBIndexer(object):

def __init__(self, url):
self.url = url

def get_cells(self, code_hash, args, limit, cursor):
limit = hex(limit)
headers = {"Content-Type": "application/json"}
payload = {
"id": 1,
"jsonrpc": "2.0",
"method": "get_cells",
"id":
1,
"jsonrpc":
"2.0",
"method":
"get_cells",
"params": [
{
"script": {
Expand All @@ -86,18 +90,20 @@ def get_cells(self, code_hash, args, limit, cursor):
if cursor is not None:
payload["params"].append(cursor)
try:
r = requests.post(url="%s" % (self.url), json=payload, headers=headers)
r = requests.post(url="%s" % (self.url),
json=payload,
headers=headers)

return r.json()
except Exception:
print(traceback.format_exc())

return {"result": "-1"}

def get_custodian_stats(
self, gw_config: GwConfig, last_finalized_block_numbrer
) -> CustodianStats:
custodian_script_type_hash = gw_config.get_lock_type_hash("custodian_lock")
def get_custodian_stats(self, gw_config: GwConfig,
last_finalized_block_numbrer) -> CustodianStats:
custodian_script_type_hash = gw_config.get_lock_type_hash(
"custodian_lock")
rollup_type_hash = gw_config.get_rollup_type_hash()
capacity = 0
finalized_capacity = 0
Expand All @@ -106,11 +112,11 @@ def get_custodian_stats(
cursor = None
sudt_stats = init_custodian()
limit = 1000
cnt = 0
while True:
try:
res = self.get_cells(
custodian_script_type_hash, rollup_type_hash, limit, cursor
)
res = self.get_cells(custodian_script_type_hash,
rollup_type_hash, limit, cursor)
except:
print("get_cells failed: {}".format(traceback.format_exc()))
continue
Expand Down Expand Up @@ -142,12 +148,13 @@ def get_custodian_stats(
else:
ckb_cell_count += 1

logging.info(f"loaded {cnt} page of custodian cells")
cursor = result["last_cursor"]
if cursor == "0x":
break
custodian_stats = CustodianStats(
sudt_stats, capacity, finalized_capacity, cell_count, ckb_cell_count
)
custodian_stats = CustodianStats(sudt_stats, capacity,
finalized_capacity, cell_count,
ckb_cell_count)
return custodian_stats


Expand Down Expand Up @@ -175,7 +182,7 @@ def output_data_to_int(s: str, byteorder="little", signed=False):
def init_custodian(token_dict: Dict = token_dict) -> Dict[str, SudtStats]:
res = {}
for args, detail in token_dict.items():
res[args] = SudtStats(
token=detail["name"], args=args, decimals=detail["decimals"]
)
res[args] = SudtStats(token=detail["name"],
args=args,
decimals=detail["decimals"])
return res

0 comments on commit b58684a

Please sign in to comment.