Skip to content

Commit 86af431

Browse files
committed
WIP commit
1 parent b3e6167 commit 86af431

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

src/scenarios/double_tx_relay.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,70 +31,78 @@ def add_options(self, parser):
3131
help="Rate per second at which transactions are generated. (default 5 tx/s)",
3232
)
3333

34-
def run_test(self):
35-
self.log.info("Starting Double TX Relay Scenario")
36-
self.block_queue = []
37-
self.mutex = Lock()
38-
self.sent_txs = 0 # protected by mutex
39-
self.time_of_last_log = datetime.now() # protected by mutex
40-
self.failed_txs = 0 # protected by mutex
41-
self.node_details = {}
42-
self.miniwallet = MiniWallet(self.nodes[0])
34+
def generate_spendable_coins(self):
4335
num_blocks = COINBASE_MATURITY + 100
4436
self.log.info(f"Generating {num_blocks} blocks...")
4537
self.generate(self.miniwallet, num_blocks)
4638
self.log.info("Rescanning utxos")
4739
self.miniwallet.rescan_utxos()
4840

49-
split_num = 1000
41+
def split_utxos(self, num_outputs=1000) -> list:
5042
confirmed_utxos = self.miniwallet.get_utxos(confirmed_only=True)
51-
print(f"Starting with {len(confirmed_utxos)} confirmed utxos, now splitting each into 1000...")
43+
self.log.info(f"Got {len(confirmed_utxos)} confirmed utxos")
44+
self.log.info(f"Splitting each utxo into {num_outputs} utxos...")
5245
for i, utxo in enumerate(confirmed_utxos):
53-
tx = self.miniwallet.send_self_transfer_multi(from_node=self.nodes[0], utxos_to_spend=[utxo], num_outputs=split_num)
46+
tx = self.miniwallet.send_self_transfer_multi(from_node=self.nodes[0], utxos_to_spend=[utxo], num_outputs=num_outputs)
5447
self.log.info(f"Sent tx {i} of {len(confirmed_utxos)}: {tx['txid']}")
55-
self.generate(self.miniwallet, 6)
48+
self.generate(self.miniwallet, 3)
5649
self.miniwallet.rescan_utxos()
57-
50+
self.log.info("Split complete")
5851
confirmed_utxos = self.miniwallet.get_utxos(confirmed_only=True)
59-
self.log.info(f"Now got {len(confirmed_utxos)} utxos")
52+
self.log.info(f"Got {len(confirmed_utxos)} confirmed utxos")
53+
return confirmed_utxos
6054

61-
self.log.info(
62-
f"Starting process to send transactions at a rate of {self.options.txproductionrate} tx/s"
63-
)
55+
def produce_transactions(self, confirmed_utxos: list):
56+
self.log.info(f"Starting process to send anyone-can-spend transactions. Aiming for a rate of {self.options.txproductionrate} tx/s")
6457
tx_count = 0
65-
start_time = time.time()
66-
interval = 1 / self.options.txproductionrate
58+
target_interval = 1 / self.options.txproductionrate
6759
time_to_next_tx = 0
60+
61+
start_time = time.time()
62+
interval_start_time = time.time()
63+
interval_end_time = time.time()
6864
next_log_time = start_time + 10
6965

7066
while True:
7167
for node in self.nodes:
72-
time.sleep(time_to_next_tx)
7368

69+
time.sleep(time_to_next_tx)
7470
tx_start_time = time.time()
75-
if len(confirmed_utxos) == 0:
71+
72+
if not confirmed_utxos:
7673
self.generate(self.miniwallet, 1)
7774
self.miniwallet.rescan_utxos()
7875
confirmed_utxos = self.miniwallet.get_utxos(confirmed_only=True)
76+
7977
utxo = confirmed_utxos.pop()
8078
try:
8179
self.miniwallet.send_self_transfer(from_node=node, utxo_to_spend=utxo)
8280
tx_count += 1
8381
except JSONRPCException as e:
8482
self.log.warning(f"tx failed: {e}, continuing")
8583

86-
# Adjust next sleep
84+
# Adjust sleep before next send
8785
tx_end_time = time.time()
8886
tx_duration = tx_end_time - tx_start_time
89-
time_to_next_tx = max(0, interval - tx_duration)
87+
time_to_next_tx = max(0, target_interval - tx_duration)
9088

9189
current_time = time.time()
92-
elapsed_time = current_time - start_time
9390

9491
if current_time >= next_log_time:
95-
tps = tx_count / elapsed_time
92+
interval_end_time = time.time()
93+
tps = tx_count / (interval_end_time - interval_start_time)
9694
self.log.info(f"Transactions per second (TPS): {tps}")
9795
next_log_time += 10
96+
interval_start_time = time.time()
97+
tx_count = 0
98+
99+
100+
def run_test(self):
101+
self.log.info("Starting Double TX Relay Scenario")
102+
self.miniwallet = MiniWallet(self.nodes[0])
103+
self.generate_spendable_coins()
104+
confirmed_utxos = self.split_utxos(num_outputs=1000)
105+
self.produce_transactions(confirmed_utxos)
98106

99107

100108
if __name__ == "__main__":

0 commit comments

Comments
 (0)