async def create_accounts(source: Builder, horizon_endpoints, accounts_num, starting_balance): """Asynchronously create accounts and return a Keypair instance for each created account.""" logging.info('creating %d accounts', accounts_num) # generate txs, squeezing as much "create account" ops as possible to each one. # when each tx is full with as much ops as it can include, sign and generate # that tx's XDR. # then, continue creating accounts using a new tx, and so on. # we stop when we create all ops required according to given accounts_num. def batch(iterable, n=1): l = len(iterable) for ndx in range(0, l, n): yield iterable[ndx:min(ndx + n, l)] kps = await generate_keypairs(accounts_num) xdrs = [] for batch_kps in batch(kps, MAX_OPS): for kp in batch_kps: source.append_create_account_op(source=source.address, destination=kp.public_address, starting_balance=str(starting_balance)) # sign with channel and root account source.sign(secret=source.keypair.seed().decode()) xdrs.append(source.gen_xdr()) # clean source builder for next transaction source.next() await send_txs_multiple_endpoints(horizon_endpoints, xdrs, expected_statuses=[200]) logging.info('created %d accounts', accounts_num) return kps
def create_whitelist_account(): """Create whitelist account for prioritizing transactions in tests.""" print('Creating whitelist account') env = KinEnvironment('LOCAL', HORIZON_ENDPOINT, PASSPHRASE) root_client = KinClient(env) root_seed = derive_root_account_seed(PASSPHRASE) builder = Builder(env.name, root_client.horizon, 100, root_seed) builder.append_create_account_op(WHITELIST_ADDRESS, str(100e5)) builder.sign() builder.submit()
def create_whitelist_account(): """Create whitelist account for prioritizing transactions in tests.""" print('Creating whitelist account') env = KinEnvironment('LOCAL', 'http://localhost:8000', 'private testnet') root_client = KinClient(env) root_seed = derive_root_account_seed('private testnet') builder = Builder(env.name, root_client.horizon, 100, root_seed) builder.append_create_account_op( 'GBR7K7S6N6C2A4I4URBXM43W7IIOK6ZZD5SAGC7LBRCUCDMICTYMK4LO', str(100e5)) builder.sign() builder.submit()
async def main(): # Create the environment local_env = Environment('LOCAL', 'http://localhost:8000', PASSPHRASE, 'http://localhost:8001') # Create a client client = KinClient(local_env) print('Client created') initial_ledger_size = get_latest_ledger(client)['max_tx_set_size'] try: # Set ledger tx size to 3 requests.get( 'http://localhost:11626/upgrades?mode=set&maxtxsize=3&upgradetime=2018-10-15T18:34:00Z' ) # Create the root account object root_account = client.kin_account( derive_root_account(PASSPHRASE).secret_seed) print('Root account object created') minimum_fee = client.get_minimum_fee() # Create an account with 0 base reserve test_account = Keypair() root_account.create_account(test_account.public_address, 0, minimum_fee) assert client.does_account_exists(test_account.public_address) print('Test account created') accounts = [Keypair() for _ in range(5)] builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=root_account.keypair.secret_seed) for keypair in accounts: builder.append_create_account_op(keypair.public_address, '100') builder.get_sequence() builder.sign() builder.submit() print('Created 5 accounts') txs = [] for index, account in enumerate(accounts, start=1): builder = Builder('LOCAL', client.horizon, fee=minimum_fee * index, secret=account.secret_seed) builder.append_manage_data_op('test', 'test'.encode()) builder.get_sequence() builder.sign() txs.append(builder) initial_ledger = get_latest_ledger(client)['sequence'] print(f'Initial ledger: {initial_ledger}') while initial_ledger == get_latest_ledger(client)['sequence']: time.sleep(0.5) first_populated_ledger = initial_ledger + 2 second_populated_ledger = initial_ledger + 3 print(f'Sending on ledger: {first_populated_ledger}') print(f'Sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') await send_txs(txs) print(f'Done sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') first_ledger_txs = client.horizon.ledger_transactions( first_populated_ledger)['_embedded']['records'] second_ledger_txs = client.horizon.ledger_transactions( second_populated_ledger)['_embedded']['records'] # First ledger should have txs where fee>=300 first_txs = sum(1 for tx in first_ledger_txs if tx['fee_paid'] >= 300) assert first_txs == 3 print('Verified first ledger') # Second ledger should have txs where fee<=200 second_txs = sum(1 for tx in second_ledger_txs if tx['fee_paid'] <= 200) assert second_txs == 2 print('Verified seconds ledger') except: raise finally: # Set tx size to what it was before requests.get( f'http://localhost:11626/upgrades?mode=set&maxtxsize={initial_ledger_size}&upgradetime=2018-10-15T18:34:00Z' )
async def main(): # Create the environment local_env = Environment('LOCAL', 'http://localhost:8000', PASSPHRASE, 'http://localhost:8001') # Create a client client = KinClient(local_env) print('Client created') initial_ledger_size = get_latest_ledger(client)['max_tx_set_size'] try: # Set ledger tx size to 3 requests.get( 'http://localhost:11626/upgrades?mode=set&maxtxsize=3&upgradetime=2018-10-15T18:34:00Z' ) # Create the root account object root_account = client.kin_account( derive_root_account(PASSPHRASE).secret_seed) print('Root account object created') minimum_fee = client.get_minimum_fee() # Create an account with 0 base reserve test_account = Keypair() root_account.create_account(test_account.public_address, 0, minimum_fee) assert client.does_account_exists(test_account.public_address) print('Test account created') # Add the account to the whitelist if not client.does_account_exists( WHITELIST_MANAGER_KEYPAIR.public_address): root_account.create_account( WHITELIST_MANAGER_KEYPAIR.public_address, 10000, 100) print('Created whitelisting account') builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=WHITELIST_MANAGER_KEYPAIR.secret_seed) builder.get_sequence() builder.append_manage_data_op(test_account.public_address, test_account._hint) builder.sign() builder.submit() print('Added account to whitelist') accounts = [Keypair() for _ in range(5)] builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=root_account.keypair.secret_seed) for keypair in accounts: builder.append_create_account_op(keypair.public_address, '100') builder.get_sequence() builder.sign() builder.submit() print('Created 5 accounts') txs = [] for account in accounts: builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=account.secret_seed) builder.append_manage_data_op('test', 'test'.encode()) builder.get_sequence() builder.sign() txs.append(builder) for tx in txs[2:]: tx.sign(test_account.secret_seed) print('Whitelisted 3 transactions') initial_ledger = get_latest_ledger(client)['sequence'] print(f'Initial ledger: {initial_ledger}') while initial_ledger == get_latest_ledger(client)['sequence']: time.sleep(0.5) first_populated_ledger = initial_ledger + 2 second_populated_ledger = initial_ledger + 3 print(f'Sending on ledger: {first_populated_ledger}') print(f'Sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') await send_txs(txs) print(f'Done sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') first_ledger_txs = client.horizon.ledger_transactions( first_populated_ledger)['_embedded']['records'] second_ledger_txs = client.horizon.ledger_transactions( second_populated_ledger)['_embedded']['records'] # First ledger should have 2 whitelisted txs, and 1 non-whitelisted whitelisted_amount = sum(1 for tx in first_ledger_txs if len(tx['signatures']) == 2) assert whitelisted_amount == 2 regular_amount = sum(1 for tx in first_ledger_txs if len(tx['signatures']) == 1) assert regular_amount == 1 # # Second ledger should have 1 whitelisted tx, and 1 non-whitelisted whitelisted_amount = sum(1 for tx in second_ledger_txs if len(tx['signatures']) == 2) assert whitelisted_amount == 1 regular_amount = sum(1 for tx in second_ledger_txs if len(tx['signatures']) == 1) assert regular_amount == 1 except: raise finally: # Set tx size to what it was before requests.get( f'http://localhost:11626/upgrades?mode=set&maxtxsize={initial_ledger_size}&upgradetime=2018-10-15T18:34:00Z' )