Rehabilitate traffic generator script
This commit is contained in:
76
apps/contract-migration/scripts/cic_eth/traffic/cmd/cache.py
Normal file
76
apps/contract-migration/scripts/cic_eth/traffic/cmd/cache.py
Normal file
@@ -0,0 +1,76 @@
|
||||
# external imports
|
||||
from chainlib.jsonrpc import JSONRPCException
|
||||
from eth_erc20 import ERC20
|
||||
from eth_accounts_index import AccountsIndex
|
||||
from eth_token_index import TokenUniqueSymbolIndex
|
||||
|
||||
class ERC20Token:
|
||||
|
||||
def __init__(self, chain_spec, address, conn):
|
||||
self.__address = address
|
||||
|
||||
c = ERC20(chain_spec)
|
||||
o = c.symbol(address)
|
||||
r = conn.do(o)
|
||||
self.__symbol = c.parse_symbol(r)
|
||||
|
||||
o = c.decimals(address)
|
||||
r = conn.do(o)
|
||||
self.__decimals = c.parse_decimals(r)
|
||||
|
||||
|
||||
def symbol(self):
|
||||
return self.__symbol
|
||||
|
||||
|
||||
def decimals(self):
|
||||
return self.__decimals
|
||||
|
||||
|
||||
class IndexCache:
|
||||
|
||||
def __init__(self, chain_spec, address):
|
||||
self.address = address
|
||||
self.chain_spec = chain_spec
|
||||
|
||||
|
||||
def parse(self, r):
|
||||
return r
|
||||
|
||||
|
||||
def get(self, conn):
|
||||
entries = []
|
||||
i = 0
|
||||
while True:
|
||||
o = self.o.entry(self.address, i)
|
||||
try:
|
||||
r = conn.do(o)
|
||||
entries.append(self.parse(r, conn))
|
||||
except JSONRPCException:
|
||||
return entries
|
||||
i += 1
|
||||
|
||||
|
||||
class AccountRegistryCache(IndexCache):
|
||||
|
||||
def __init__(self, chain_spec, address):
|
||||
super(AccountRegistryCache, self).__init__(chain_spec, address)
|
||||
self.o = AccountsIndex(chain_spec)
|
||||
self.get_accounts = self.get
|
||||
|
||||
|
||||
def parse(self, r, conn):
|
||||
return self.o.parse_account(r)
|
||||
|
||||
|
||||
class TokenRegistryCache(IndexCache):
|
||||
|
||||
def __init__(self, chain_spec, address):
|
||||
super(TokenRegistryCache, self).__init__(chain_spec, address)
|
||||
self.o = TokenUniqueSymbolIndex(chain_spec)
|
||||
self.get_tokens = self.get
|
||||
|
||||
|
||||
def parse(self, r, conn):
|
||||
token_address = self.o.parse_entry(r)
|
||||
return ERC20Token(self.chain_spec, token_address, conn)
|
||||
@@ -163,9 +163,9 @@ class TrafficProvisioner:
|
||||
"""Aux parameter template to be passed to the traffic generator module"""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.tokens = self.oracles['token'].get_tokens()
|
||||
self.accounts = self.oracles['account'].get_accounts()
|
||||
def __init__(self, conn):
|
||||
self.tokens = self.oracles['token'].get_tokens(conn)
|
||||
self.accounts = self.oracles['account'].get_accounts(conn)
|
||||
self.aux = copy.copy(self.default_aux)
|
||||
self.__balances = {}
|
||||
for a in self.accounts:
|
||||
@@ -277,13 +277,14 @@ class TrafficSyncHandler:
|
||||
:type traffic_router: TrafficRouter
|
||||
:raises Exception: Any Exception redis may raise on connection attempt.
|
||||
"""
|
||||
def __init__(self, config, traffic_router):
|
||||
def __init__(self, config, traffic_router, conn):
|
||||
self.traffic_router = traffic_router
|
||||
self.redis_channel = str(uuid.uuid4())
|
||||
self.pubsub = self.__connect_redis(self.redis_channel, config)
|
||||
self.traffic_items = {}
|
||||
self.config = config
|
||||
self.init = False
|
||||
self.conn = conn
|
||||
|
||||
|
||||
# connects to redis
|
||||
@@ -307,7 +308,7 @@ class TrafficSyncHandler:
|
||||
:param tx_index: Syncer block transaction index at time of call.
|
||||
:type tx_index: number
|
||||
"""
|
||||
traffic_provisioner = TrafficProvisioner()
|
||||
traffic_provisioner = TrafficProvisioner(self.conn)
|
||||
traffic_provisioner.add_aux('redis_channel', self.redis_channel)
|
||||
|
||||
refresh_accounts = None
|
||||
@@ -343,7 +344,7 @@ class TrafficSyncHandler:
|
||||
sender = traffic_provisioner.accounts[sender_index]
|
||||
#balance_full = balances[sender][token_pair[0].symbol()]
|
||||
if len(sender_indices) == 1:
|
||||
sender_indices[m] = sender_sender_indices[len(senders)-1]
|
||||
sender_indices[sender_index] = sender_indices[len(sender_indices)-1]
|
||||
sender_indices = sender_indices[:len(sender_indices)-1]
|
||||
|
||||
balance_full = traffic_provisioner.balance(sender, token_pair[0])
|
||||
@@ -351,7 +352,14 @@ class TrafficSyncHandler:
|
||||
recipient_index = random.randint(0, len(traffic_provisioner.accounts)-1)
|
||||
recipient = traffic_provisioner.accounts[recipient_index]
|
||||
|
||||
logg.debug('trigger item {} tokens {} sender {} recipient {} balance {}')
|
||||
logg.debug('trigger item {} tokens {} sender {} recipient {} balance {}'.format(
|
||||
traffic_item,
|
||||
token_pair,
|
||||
sender,
|
||||
recipient,
|
||||
balance_full,
|
||||
)
|
||||
)
|
||||
(e, t, balance_result,) = traffic_item.method(
|
||||
token_pair,
|
||||
sender,
|
||||
@@ -359,7 +367,6 @@ class TrafficSyncHandler:
|
||||
balance_full,
|
||||
traffic_provisioner.aux,
|
||||
block_number,
|
||||
tx_index,
|
||||
)
|
||||
traffic_provisioner.update_balance(sender, token_pair[0], balance_result)
|
||||
sender_indices.append(recipient_index)
|
||||
|
||||
Reference in New Issue
Block a user