-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_injector.py
executable file
·242 lines (214 loc) · 7.49 KB
/
account_injector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python3
import logging
import argparse
import os
import colorlog
import inspect
import eospy.cleos
import pandas
SCRIPT_PATH = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
parser = argparse.ArgumentParser()
parser.add_argument("-v", '--verbose', action="store_true",
dest="verbose", help='Print logged info to screen')
parser.add_argument("-d", '--debug', action="store_true",
dest="debug", help='Print debug info')
parser.add_argument('-l', '--log_file', default='{}.log'.format(
os.path.basename(__file__).split('.')[0]), help='Log file')
parser.add_argument('-s', '--snapshot_file',
default='{}/eosmetal_telos_snapshot.csv'.format(SCRIPT_PATH), help='Snapshot file')
parser.add_argument('-u', '--api_endpoint',
default='http://127.0.0.1:8888', help='EOSIO API endpoint URI')
parser.add_argument('-b', '--batch_size', type=int,
default=200, help='Number of actions per transaction')
args = parser.parse_args()
VERBOSE = args.verbose
DEBUG = args.debug
SNAPSHOT_FILE = args.snapshot_file
LOG_FILE = args.log_file
API_ENDPOINT = args.api_endpoint
BATCH_SIZE = int(args.batch_size)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = colorlog.ColoredFormatter(
'%(log_color)s%(asctime)s - %(levelname)s - %(message)s%(reset)s')
if DEBUG:
logger.setLevel(logging.DEBUG)
if VERBOSE:
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger.addHandler(ch)
fh = logging.FileHandler(LOG_FILE)
fh.setFormatter(formatter)
logger.addHandler(fh)
RAM_KB = 4
SYMBOL = "TLOS"
KEY = "5JRiK3ctuSgPwEsFvY1FeCxW6VHYcGo3h28YkyJYBnEBvtgrhPd"
SCRIPT_PATH = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
cleos = eospy.cleos.Cleos(url=API_ENDPOINT)
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
def get_account_creation_actions(account, balance, key):
# Create new account tx
owner_active_auth = {
"threshold": 1,
"keys": [{
"key": key,
"weight": 1
}],
"accounts": [],
"waits": []
}
newaccount_payload = {
'creator' : 'eosio',
'name' : account,
'owner': owner_active_auth,
'active': owner_active_auth
}
newaccount_data = cleos.abi_json_to_bin('eosio', 'newaccount', newaccount_payload)
newaccount_action = {
'account' : 'eosio',
'name' : 'newaccount',
'authorization' : [
{
'actor' : 'eosio',
'permission' : 'active'
} ],
'data' : newaccount_data['binargs']
}
# Create buy ram tx
buyram_payload = {
'payer':'eosio',
'receiver':account,
'bytes': RAM_KB*1024
}
buyram_data = cleos.abi_json_to_bin('eosio', 'buyrambytes', buyram_payload)
buyram_action = {
'account' : 'eosio',
'name' : 'buyrambytes',
'authorization' : [
{
'actor' : 'eosio',
'permission' : 'active'
} ],
'data' : buyram_data['binargs']
}
# Create delegatebw tx
f_balance = float(balance)
f_balance = float(balance)
if f_balance < 3:
liquid = 0.1
elif f_balance <= 11:
liquid = 2.0
else:
liquid = 10.0
remainder = f_balance - liquid
delegate_cpu = round(remainder / 2, 4)
delegate_net = remainder - delegate_cpu
delegate_payload = {
'from': 'eosio',
'receiver': account,
'stake_net_quantity': '{:.4f} {}'.format(delegate_net, SYMBOL),
'stake_cpu_quantity': '{:.4f} {}'.format(delegate_cpu, SYMBOL),
'transfer': True
}
delegate_data = cleos.abi_json_to_bin('eosio', 'delegatebw', delegate_payload)
delegate_action = {
'account' : 'eosio',
'name' : 'delegatebw',
'authorization' : [
{
'actor' : 'eosio',
'permission' : 'active'
} ],
'data' : delegate_data['binargs']
}
# Create transfer tx
transfer_payload = {
"from": "eosio",
"to": account,
"quantity": '{:.4f} {}'.format(liquid, SYMBOL),
"memo": "transfer genesis balance to {}".format(account)
}
transfer_data = cleos.abi_json_to_bin('eosio.token', 'transfer', transfer_payload)
transfer_action = {
"account": "eosio.token",
"name": "transfer",
"authorization": [
{
"actor": "eosio",
"permission": "active"
}],
"data": transfer_data['binargs']}
return (newaccount_action, buyram_action, delegate_action, transfer_action)
def get_chain_params():
return cleos.get_table('eosio', 'eosio', 'global')['rows'][0]
def set_chain_params(params):
set_params_payload = {
'params': params
}
set_params_data = cleos.abi_json_to_bin('eosio', 'setparams', set_params_payload)
set_params_action = {
'account' : 'eosio',
'name' : 'setparams',
'authorization' : [
{
'actor' : 'eosio',
'permission' : 'active'
} ],
'data' : set_params_data['binargs']
}
trx = {"actions": [set_params_action]}
return cleos.push_transaction(trx, KEY, broadcast=True)
def main():
try:
telos_genesis = pandas.read_csv(SNAPSHOT_FILE, dtype=str, names=['eth_address',
'eos_account', 'eos_key', 'balance'])
except Exception as e:
logger.critical(
'Error loading snapshot at {}: {}'.format(SNAPSHOT_FILE, e))
exit(1)
logging.info('Creating accounts')
created_accounts = 0
num_accounts = len(telos_genesis.index)
#Set chain params to max performance
logger.info('Setting chain params to max performance')
global_params = get_chain_params()
max_block_cpu_usage = global_params['max_block_cpu_usage']
max_transaction_cpu_usage = global_params['max_transaction_cpu_usage']
global_params['max_block_cpu_usage'] = 100000000
global_params['max_transaction_cpu_usage'] = 99999899
try:
set_chain_params(global_params)
except Exception as e:
logger.critical('Error setting chain params: {}'.format(e))
quit()
#Create accounts
for i in chunker(telos_genesis, BATCH_SIZE):
actions = []
for _, row in i.iterrows():
actions.extend(get_account_creation_actions(
row['eos_account'], row['balance'], row['eos_key']))
#print(actions)
trx = {"actions": actions}
try:
resp = cleos.push_transaction(trx, KEY, broadcast=True)
except Exception as e:
logger.critical('Error creating accounts: {}'.format(e))
quit()
#logger.info(resp)
created_accounts += BATCH_SIZE
logger.info('Created {} accounts of {}'.format(created_accounts, num_accounts))
#Setting back chain params to original values
logger.info('Setting back chain params to original values')
global_params['max_block_cpu_usage'] = max_block_cpu_usage
global_params['max_transaction_cpu_usage'] = max_transaction_cpu_usage
try:
set_chain_params(global_params)
except Exception as e:
logger.critical('Error setting back chain params: {}'.format(e))
quit()
logger.info('Injection finished')
if __name__ == "__main__":
main()