Given a list of blockchain transactions with timestamps and prices, what is the output of the code that detects if a transaction was front-run?
transactions = [
{'tx_id': 'a1', 'timestamp': 100, 'price': 10},
{'tx_id': 'b2', 'timestamp': 101, 'price': 12},
{'tx_id': 'c3', 'timestamp': 102, 'price': 11}
]
front_run_detected = False
for i in range(1, len(transactions)):
if transactions[i]['price'] < transactions[i-1]['price']:
front_run_detected = True
break
print(front_run_detected)Look for a price drop after a higher price transaction.
The code checks if any transaction has a lower price than the previous one, indicating a possible front-run. Here, price drops from 12 to 11, so it prints True.
Which statement best describes the impact of front-running on blockchain transaction ordering?
Think about how attackers gain advantage by reordering.
Front-running involves reordering transactions so the attacker profits by acting before others.
Why does the following code fail to detect front-running correctly?
txs = [{'id': 'x1', 'price': 5}, {'id': 'x2', 'price': 7}, {'id': 'x3', 'price': 6}]
front_run = False
for i in range(len(txs)):
if txs[i]['price'] < txs[i-1]['price']:
front_run = True
break
print(front_run)Check the index used in the loop and how it accesses previous elements.
The loop starts at 0, so txs[i-1] accesses txs[-1] (last element) on first iteration, causing incorrect logic.
Which option contains the syntax error preventing the code from running?
def detect_front_run(txs): for i in range(1, len(txs)) if txs[i]['price'] < txs[i-1]['price']: return True return False
Look carefully at the for loop syntax.
Python requires a colon ':' at the end of for loop declarations.
Given a batch of transactions with prices, how many front-run opportunities exist where a transaction is immediately followed by a lower price?
transactions = [
{'tx_id': 't1', 'price': 20},
{'tx_id': 't2', 'price': 25},
{'tx_id': 't3', 'price': 22},
{'tx_id': 't4', 'price': 30},
{'tx_id': 't5', 'price': 28}
]
count = 0
for i in range(1, len(transactions)):
if transactions[i]['price'] < transactions[i-1]['price']:
count += 1
print(count)Count how many times price drops compared to previous transaction.
Price drops occur from 25 to 22 and from 30 to 28, so count is 2.