57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
|
import logging
|
||
|
import random
|
||
|
import time
|
||
|
from threading import Thread
|
||
|
|
||
|
from aucoin.core import Core
|
||
|
from aucoin.database import session_scope
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
class Stressor(object):
|
||
|
def __init__(self, core: Core, receiver_address=None):
|
||
|
self.core = core
|
||
|
self.receiver_address = receiver_address
|
||
|
|
||
|
logger.info("Starting stressor")
|
||
|
|
||
|
Thread(target=self.sender, daemon=True).start()
|
||
|
|
||
|
def sender(self):
|
||
|
# Receiver address is either provided by parameter or chosen at random. We set it here (in the thread) instead
|
||
|
# of in the constructor because random_address() might loop.
|
||
|
receiver_address = self.receiver_address or self.random_address()
|
||
|
logger.info("Chosen receiver address: %s", receiver_address.hex())
|
||
|
|
||
|
while True:
|
||
|
time.sleep(1)
|
||
|
|
||
|
with self.core.lock:
|
||
|
balance = sum(self.core.wallet.balance) # sum of confirmed and unconfirmed balance
|
||
|
|
||
|
while balance >= 110:
|
||
|
amount = random.randint(10, 100)
|
||
|
fee = random.randint(0, 10)
|
||
|
|
||
|
logger.info("Sending %s auc to %s with %s auc fee", amount, receiver_address.hex(), fee)
|
||
|
self.core.wallet.make_transaction(receiver_address, amount, fee)
|
||
|
|
||
|
balance -= amount + fee
|
||
|
|
||
|
def random_address(self):
|
||
|
logger.debug("Choosing random receiver address..")
|
||
|
# Loop until we get an address because the blockchain might be empty (or only contain our addresses), in which
|
||
|
# case we need to keep trying until somebody else mines a block.
|
||
|
receiver_address = None
|
||
|
while receiver_address is None:
|
||
|
with session_scope() as session:
|
||
|
# Known addresses are all addresses on the chain minus the address of the genesis block and ourselves
|
||
|
known_addresses = self.core.blockchain.known_addresses(session) - {bytes(32)} - self.core.wallet.addresses
|
||
|
|
||
|
if known_addresses:
|
||
|
return random.choice(tuple(known_addresses)) # random.choice doesn't work with sets
|
||
|
|
||
|
time.sleep(1)
|
||
|
continue
|