Skip to content

Commit

Permalink
Merge pull request #290 from xian-network/fix_bds_contracts
Browse files Browse the repository at this point in the history
Only save contracts if tx was successful
  • Loading branch information
Endogen authored Oct 1, 2024
2 parents e069f47 + 5ee483f commit 04cb9a6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/xian/methods/finalize_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ async def finalize_block(self, req) -> ResponseFinalizeBlock:

tx_events = []

# Only trigger state change events if tx was successful
# Websocket Events - Only trigger state change events if tx was successful
if result["tx_result"]["status"] == 0:

# Need to replace chars since they are reserved
translation_table = str.maketrans({'.': '_', ':': '__'})

Expand Down Expand Up @@ -103,11 +104,13 @@ async def finalize_block(self, req) -> ResponseFinalizeBlock:
)
)

# Save data to BDS - Add tx data to batch
if self.block_service_mode:
cometbft_hash = hash_bytes(tx_bytes).upper()
result["tx_result"]["hash"] = cometbft_hash
asyncio.create_task(self.bds.insert_full_data(tx | result, block_datetime))

# Save data to BDS - Process batch
if self.block_service_mode:
asyncio.create_task(self.bds.db.commit_batch_to_disk())

Expand Down
28 changes: 15 additions & 13 deletions src/xian/services/bds/bds.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ async def insert_full_data(self, tx: dict, block_time: datetime):
await self._insert_tx(tx, block_time)
logger.debug(f'Saved tx in {timer() - start_time:.3f} seconds')

# State
start_time = timer()
await self._insert_state(tx, block_time)
logger.debug(f'Saved contracts in {timer() - start_time:.3f} seconds')

# State changes
start_time = timer()
await self._insert_state_changes(tx, block_time)
logger.debug(f'Saved state changes in {timer() - start_time:.3f} seconds')

# Rewards
start_time = timer()
await self._insert_rewards(tx, block_time)
Expand All @@ -124,19 +134,11 @@ async def insert_full_data(self, tx: dict, block_time: datetime):
logger.debug(f'Saved addresses in {timer() - start_time:.3f} seconds')

# Contracts
start_time = timer()
await self._insert_contracts(tx, block_time)
logger.debug(f'Saved contracts in {timer() - start_time:.3f} seconds')

# State
start_time = timer()
await self._insert_state(tx, block_time)
logger.debug(f'Saved contracts in {timer() - start_time:.3f} seconds')

# State changes
start_time = timer()
await self._insert_state_changes(tx, block_time)
logger.debug(f'Saved state changes in {timer() - start_time:.3f} seconds')
# Only save contracts if tx was successful
if tx["tx_result"]["status"] == 0:
start_time = timer()
await self._insert_contracts(tx, block_time)
logger.debug(f'Saved contracts in {timer() - start_time:.3f} seconds')

logger.debug(f'Processed tx {tx["tx_result"]["hash"]} in {timer() - total_time:.3f} seconds')

Expand Down
1 change: 1 addition & 0 deletions src/xian/services/bds/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async def init_pool(self):
host=self.cfg.get('db_host'),
port=self.cfg.get('db_port')
)

async def execute(self, query: str, params: list = []):
"""
This is meant for INSERT, UPDATE and DELETE statements
Expand Down

0 comments on commit 04cb9a6

Please sign in to comment.