Skip to content

Commit

Permalink
asd (#142)
Browse files Browse the repository at this point in the history
* for safety (#138)

* update logging (#139)

* Update finalize_block.py (#140)

* Update finalize_block.py (#141)
  • Loading branch information
crosschainer authored May 24, 2024
1 parent ccd4f0a commit 66a04aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
61 changes: 25 additions & 36 deletions src/xian/methods/finalize_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from xian.constants import ErrorCode
from loguru import logger
import traceback


def finalize_block(self, req) -> ResponseFinalizeBlock:
Expand All @@ -38,46 +39,34 @@ def finalize_block(self, req) -> ResponseFinalizeBlock:
}

for tx in req.txs:
try:
tx = decode_transaction_bytes(tx)
sender, signature, payload = unpack_transaction(tx)
if not verify(sender, payload, signature):
# Not really needed, because check_tx should catch this first, but just in case
raise Exception("Invalid Signature")
# Attach metadata to the transaction
tx["b_meta"] = self.current_block_meta
result = self.tx_processor.process_tx(tx, enabled_fees=self.enable_tx_fee)

if self.enable_tx_fee:
self.current_block_rewards[tx['b_meta']['hash']] = {
"amount": result["stamp_rewards_amount"],
"contract": result["stamp_rewards_contract"]
}
tx = decode_transaction_bytes(tx)
sender, signature, payload = unpack_transaction(tx)
if not verify(sender, payload, signature):
# Not really needed, because check_tx should catch this first, but just in case
raise Exception("Invalid Signature")
# Attach metadata to the transaction
tx["b_meta"] = self.current_block_meta
result = self.tx_processor.process_tx(tx, enabled_fees=self.enable_tx_fee)

self.nonce_storage.set_nonce_by_tx(tx)
tx_hash = result["tx_result"]["hash"]
self.fingerprint_hashes.append(tx_hash)
parsed_tx_result = json.dumps(stringify_decimals(result["tx_result"]))
logger.debug(f"Parsed tx result: {parsed_tx_result}")
if self.enable_tx_fee:
self.current_block_rewards[tx['b_meta']['hash']] = {
"amount": result["stamp_rewards_amount"],
"contract": result["stamp_rewards_contract"]
}

tx_results.append(
ExecTxResult(
code=result["tx_result"]["status"],
data=encode_str(parsed_tx_result),
gas_used=0
)
)
except Exception as e:
# Normally this cannot happen, but if it does, we need to catch it
logger.error(f"Fatal ERROR: {e}")
self.nonce_storage.set_nonce_by_tx(tx)
tx_hash = result["tx_result"]["hash"]
self.fingerprint_hashes.append(tx_hash)
parsed_tx_result = json.dumps(stringify_decimals(result["tx_result"]))
logger.debug(f"Parsed tx result: {parsed_tx_result}")

tx_results.append(
ExecTxResult(
code=ErrorCode,
data=encode_str(f"ERROR: {e}"),
gas_used=0
)
tx_results.append(
ExecTxResult(
code=result["tx_result"]["status"],
data=encode_str(parsed_tx_result),
gas_used=0
)
)

if self.static_rewards:
try:
Expand Down
21 changes: 13 additions & 8 deletions src/xian/methods/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ def query(self, req) -> ResponseQuery:

# http://localhost:26657/abci_query?path="/lint/<code>"
if path_parts[0] == "lint":
code = base64.b64decode(path_parts[1]).decode("utf-8")
stdout = StringIO()
stderr = StringIO()
reporter = Reporter(stdout, stderr)
check(code, "<string>", reporter)
stdout_output = stdout.getvalue()
stderr_output = stderr.getvalue()
result = {"stdout": stdout_output, "stderr": stderr_output}
try:
code = base64.b64decode(path_parts[1]).decode("utf-8")
stdout = StringIO()
stderr = StringIO()
reporter = Reporter(stdout, stderr)
check(code, "<string>", reporter)
stdout_output = stdout.getvalue()
stderr_output = stderr.getvalue()
result = {"stdout": stdout_output, "stderr": stderr_output}
except Exception as e:
result = {"stdout": "", "stderr": ""}

# http://localhost:26657/abci_query?path="/estimate_stamps/<encoded_txn>" BLOCK SERVICE MODE ONLY
if self.block_service_mode:
Expand Down Expand Up @@ -117,6 +120,8 @@ def query(self, req) -> ResponseQuery:
type_of_data = "None"

except Exception as err:
import traceback
traceback.print_exc()
logger.error(f"QUERY ERROR: {err}")
return ResponseQuery(code=ErrorCode, log=f"QUERY ERROR")

Expand Down

0 comments on commit 66a04aa

Please sign in to comment.