Skip to content

Commit

Permalink
WIP commit
Browse files Browse the repository at this point in the history
  • Loading branch information
willcl-ark committed Mar 5, 2024
1 parent b3e6167 commit 86af431
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions src/scenarios/double_tx_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,70 +31,78 @@ def add_options(self, parser):
help="Rate per second at which transactions are generated. (default 5 tx/s)",
)

def run_test(self):
self.log.info("Starting Double TX Relay Scenario")
self.block_queue = []
self.mutex = Lock()
self.sent_txs = 0 # protected by mutex
self.time_of_last_log = datetime.now() # protected by mutex
self.failed_txs = 0 # protected by mutex
self.node_details = {}
self.miniwallet = MiniWallet(self.nodes[0])
def generate_spendable_coins(self):
num_blocks = COINBASE_MATURITY + 100
self.log.info(f"Generating {num_blocks} blocks...")
self.generate(self.miniwallet, num_blocks)
self.log.info("Rescanning utxos")
self.miniwallet.rescan_utxos()

split_num = 1000
def split_utxos(self, num_outputs=1000) -> list:
confirmed_utxos = self.miniwallet.get_utxos(confirmed_only=True)
print(f"Starting with {len(confirmed_utxos)} confirmed utxos, now splitting each into 1000...")
self.log.info(f"Got {len(confirmed_utxos)} confirmed utxos")
self.log.info(f"Splitting each utxo into {num_outputs} utxos...")
for i, utxo in enumerate(confirmed_utxos):
tx = self.miniwallet.send_self_transfer_multi(from_node=self.nodes[0], utxos_to_spend=[utxo], num_outputs=split_num)
tx = self.miniwallet.send_self_transfer_multi(from_node=self.nodes[0], utxos_to_spend=[utxo], num_outputs=num_outputs)
self.log.info(f"Sent tx {i} of {len(confirmed_utxos)}: {tx['txid']}")
self.generate(self.miniwallet, 6)
self.generate(self.miniwallet, 3)
self.miniwallet.rescan_utxos()

self.log.info("Split complete")
confirmed_utxos = self.miniwallet.get_utxos(confirmed_only=True)
self.log.info(f"Now got {len(confirmed_utxos)} utxos")
self.log.info(f"Got {len(confirmed_utxos)} confirmed utxos")
return confirmed_utxos

self.log.info(
f"Starting process to send transactions at a rate of {self.options.txproductionrate} tx/s"
)
def produce_transactions(self, confirmed_utxos: list):
self.log.info(f"Starting process to send anyone-can-spend transactions. Aiming for a rate of {self.options.txproductionrate} tx/s")
tx_count = 0
start_time = time.time()
interval = 1 / self.options.txproductionrate
target_interval = 1 / self.options.txproductionrate
time_to_next_tx = 0

start_time = time.time()
interval_start_time = time.time()
interval_end_time = time.time()
next_log_time = start_time + 10

while True:
for node in self.nodes:
time.sleep(time_to_next_tx)

time.sleep(time_to_next_tx)
tx_start_time = time.time()
if len(confirmed_utxos) == 0:

if not confirmed_utxos:
self.generate(self.miniwallet, 1)
self.miniwallet.rescan_utxos()
confirmed_utxos = self.miniwallet.get_utxos(confirmed_only=True)

utxo = confirmed_utxos.pop()
try:
self.miniwallet.send_self_transfer(from_node=node, utxo_to_spend=utxo)
tx_count += 1
except JSONRPCException as e:
self.log.warning(f"tx failed: {e}, continuing")

# Adjust next sleep
# Adjust sleep before next send
tx_end_time = time.time()
tx_duration = tx_end_time - tx_start_time
time_to_next_tx = max(0, interval - tx_duration)
time_to_next_tx = max(0, target_interval - tx_duration)

current_time = time.time()
elapsed_time = current_time - start_time

if current_time >= next_log_time:
tps = tx_count / elapsed_time
interval_end_time = time.time()
tps = tx_count / (interval_end_time - interval_start_time)
self.log.info(f"Transactions per second (TPS): {tps}")
next_log_time += 10
interval_start_time = time.time()
tx_count = 0


def run_test(self):
self.log.info("Starting Double TX Relay Scenario")
self.miniwallet = MiniWallet(self.nodes[0])
self.generate_spendable_coins()
confirmed_utxos = self.split_utxos(num_outputs=1000)
self.produce_transactions(confirmed_utxos)


if __name__ == "__main__":
Expand Down

0 comments on commit 86af431

Please sign in to comment.