Skip to content

Commit 2b1e002

Browse files
committed
feat: fixed some issues in the logic, and refactors
1 parent f7c6dc0 commit 2b1e002

File tree

2 files changed

+120
-88
lines changed

2 files changed

+120
-88
lines changed

contracts/tonomy/include/tonomy/apps.hpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ namespace tonomysystem
306306
* @param origin - domain (unique)
307307
*/
308308
[[eosio::action]] void admncrtapp(
309-
name creator,
309+
name account_name,
310+
name creator,
310311
string json_data,
311312
string username,
312313
string origin);
@@ -327,6 +328,24 @@ namespace tonomysystem
327328
string origin,
328329
uint8_t plan);
329330

331+
/**
332+
* Admin: register app data for an existing account
333+
* Similar to admncrtapp but does not create the account or set resource limits.
334+
* Used when the account already exists.
335+
*
336+
* @param account_name - the app account name (must already exist)
337+
* @param creator - the creator account (used for active permission via updateauth)
338+
* @param json_data - JSON with display details
339+
* @param username - raw username (unique)
340+
* @param origin - domain (unique)
341+
*/
342+
[[eosio::action]] void adminregapp(
343+
name account_name,
344+
name creator,
345+
string json_data,
346+
string username,
347+
string origin);
348+
330349
/**
331350
* Admin: delete an app record
332351
*
@@ -362,6 +381,7 @@ namespace tonomysystem
362381

363382
using admncrtapp_action = action_wrapper<"admncrtapp"_n, &apps::admncrtapp>;
364383
using admnupdapp_action = action_wrapper<"admnupdapp"_n, &apps::admnupdapp>;
384+
using adminregapp_action = action_wrapper<"adminregapp"_n, &apps::adminregapp>;
365385
using admndelapp_action = action_wrapper<"admndelapp"_n, &apps::admndelapp>;
366386
using admnmigapp_action = action_wrapper<"admnmigapp"_n, &apps::admnmigapp>;
367387
using admnmigsc_action = action_wrapper<"admnmigsc"_n, &apps::admnmigsc>;
@@ -372,20 +392,39 @@ namespace tonomysystem
372392
*
373393
* @param username_hash - hash of the username of the account
374394
*/
375-
void check_app_username(const checksum256 &username_hash);
395+
// Helper: ensure the provided username is not already taken
396+
void check_username_is_unique(const string &username);
376397

377398
/**
378399
* Check if the app origin is already taken
379400
*
380401
* @param origin - domain associated with the app
381402
*/
382-
void check_app_origin(const string &origin);
403+
void check_app_origin_is_unique(const string &origin);
383404

384405
/**
385406
* Validate username characters against allowed set [A-Za-z0-9_-]
407+
*
408+
* @param username - raw username string
386409
*/
387410
void check_app_username_chars(const string &username);
388411

412+
/**
413+
* Update resource config and set resource limits for an account
414+
*
415+
* @param account_name - the account to set resource limits for
416+
*/
417+
void update_resource_config_and_limits(name account_name);
418+
419+
/**
420+
* Create a new account with specified owner and active authorities
421+
*
422+
* @param account_name - the new account name
423+
* @param owner_account - the account name for owner permission
424+
* @param active_account - the account name for active permission
425+
*/
426+
void create_app_account(name account_name, name owner_account, name active_account);
427+
389428
/**
390429
* Check if the raw username is already taken in appsv3
391430
*
@@ -399,5 +438,17 @@ namespace tonomysystem
399438
auto itr = idx.find(username_hash);
400439
check(itr == idx.end(), "Username already taken");
401440
}
441+
442+
/**
443+
* Register app data in appsv3 table and update authority
444+
* Common logic used by both admncrtapp and adminregapp
445+
*
446+
* @param account_name - the app account name
447+
* @param creator - the creator account for active permission
448+
* @param json_data - JSON with display details
449+
* @param username - raw username string (unique)
450+
* @param origin - domain (unique)
451+
*/
452+
void register_app_data(name account_name, name creator, const string &json_data, const string &username, const string &origin);
402453
};
403454
}

contracts/tonomy/src/apps.cpp

Lines changed: 66 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,42 @@ apps::apps(name receiver, name code, eosio::datastream<const char *> ds)
1616
_smartcontracts(receiver, receiver.value) {}
1717

1818
// Admin create app with random account name
19-
void apps::admncrtapp(name creator,
19+
void apps::admncrtapp(name account_name,
20+
name creator,
2021
string json_data,
2122
string username,
2223
string origin)
2324
{
2425
require_auth(get_self());
26+
check(is_account(creator), "Creator account does not exist");
27+
create_app_account(account_name, app_controller_account, creator);
28+
register_app_data(account_name, creator, json_data, username, origin);
29+
}
2530

31+
// Admin register app data for an existing account (without creating account)
32+
void apps::adminregapp(name account_name,
33+
name creator,
34+
string json_data,
35+
string username,
36+
string origin)
37+
{
38+
require_auth(get_self());
39+
check(is_account(account_name), "Account does not exist");
2640
check(is_account(creator), "Creator account does not exist");
41+
register_app_data(account_name, creator, json_data, username, origin);
42+
}
2743

44+
// Private helper: Register app data, account type, and update authority
45+
void apps::register_app_data(name account_name, name creator, const string &json_data, const string &username, const string &origin)
46+
{
2847
check_app_username_chars(username);
29-
30-
// Uniqueness checks for username and origin
31-
checksum256 username_hash = eosio::sha256(username.c_str(), std::strlen(username.c_str()));
32-
{
33-
auto uidx = _appsv3.get_index<"usernamehash"_n>();
34-
check(uidx.find(username_hash) == uidx.end(), "Username already taken");
35-
}
36-
checksum256 origin_hash = eosio::sha256(origin.c_str(), std::strlen(origin.c_str()));
37-
{
38-
auto oidx = _appsv3.get_index<"originhash"_n>();
39-
check(oidx.find(origin_hash) == oidx.end(), "Origin already taken");
40-
}
41-
42-
// Generate random account name from username and json_data hashes
43-
auto json_hash = eosio::sha256(json_data.c_str(), std::strlen(json_data.c_str()));
44-
const eosio::name random_name = random_account_name(username_hash, json_hash, enum_account_type::App);
45-
46-
// Create account with owner=gov.tmy, active=creator
47-
authority owner_authority = create_authority_with_account(app_controller_account);
48-
authority active_authority = create_authority_with_account(creator);
49-
active_authority.accounts.push_back({.permission = create_eosio_code_permission_level(get_self()), .weight = 1});
50-
51-
newaccount_action newaccountaction("eosio"_n, {get_self(), "active"_n});
52-
newaccountaction.send(get_self(), random_name, owner_authority, active_authority);
53-
54-
// Update resource config
55-
tonomy::resource_config_table _resource_config(get_self(), get_self().value);
56-
auto config = _resource_config.get();
57-
config.total_cpu_weight_allocated = this->initial_cpu_weight_allocation;
58-
config.total_net_weight_allocated = this->initial_net_weight_allocation;
59-
_resource_config.set(config, get_self());
60-
61-
// Set resource limits: ram=0, net=initial, cpu=initial
62-
eosio::set_resource_limits(random_name, 0, this->initial_net_weight_allocation, this->initial_cpu_weight_allocation);
48+
check_username_is_unique(username);
49+
check_app_origin_is_unique(origin);
50+
update_resource_config_and_limits(account_name);
6351

6452
// Register in appsv3
6553
_appsv3.emplace(get_self(), [&](auto &row) {
66-
row.account_name = random_name;
54+
row.account_name = account_name;
6755
row.json_data = json_data;
6856
row.version = 3;
6957
row.username = username;
@@ -74,23 +62,23 @@ void apps::admncrtapp(name creator,
7462
// Set account type
7563
tonomy::account_type_table account_type(get_self(), get_self().value);
7664
account_type.emplace(get_self(), [&](auto &row) {
77-
row.account_name = random_name;
65+
row.account_name = account_name;
7866
row.acc_type = enum_account_type::App;
7967
row.version = 1;
8068
});
8169
}
8270

83-
84-
void apps::check_app_username(const checksum256 &username_hash)
71+
void apps::check_username_is_unique(const string &username)
8572
{
73+
checksum256 username_hash = eosio::sha256(username.c_str(), std::strlen(username.c_str()));
8674
auto apps_by_username_hash_itr = _appsv3.get_index<"usernamehash"_n>();
8775
const auto username_itr = apps_by_username_hash_itr.find(username_hash);
8876
if (username_itr != apps_by_username_hash_itr.end()) {
8977
throwError("TCON1001", "This app username is already taken");
9078
}
9179
}
9280

93-
void apps::check_app_origin(const string &origin)
81+
void apps::check_app_origin_is_unique(const string &origin)
9482
{
9583
auto origin_hash = eosio::sha256(origin.c_str(), std::strlen(origin.c_str()));
9684
auto apps_by_origin_hash_itr = _appsv3.get_index<"originhash"_n>();
@@ -113,29 +101,48 @@ void apps::check_app_username_chars(const string &username)
113101
}
114102
}
115103

104+
void apps::update_resource_config_and_limits(name account_name)
105+
{
106+
// Update resource config
107+
tonomy::resource_config_table _resource_config(get_self(), get_self().value);
108+
auto config = _resource_config.get();
109+
config.total_cpu_weight_allocated += this->initial_cpu_weight_allocation;
110+
config.total_net_weight_allocated += this->initial_net_weight_allocation;
111+
_resource_config.set(config, get_self());
112+
113+
// Set resource limits: ram=0, net=initial, cpu=initial
114+
eosio::set_resource_limits(account_name, 0, this->initial_net_weight_allocation, this->initial_cpu_weight_allocation);
115+
}
116+
117+
void apps::create_app_account(name account_name, name owner_account, name active_account)
118+
{
119+
// Create account with specified owner and active authorities
120+
authority owner_authority = create_authority_with_account(owner_account);
121+
authority active_authority = create_authority_with_account(active_account);
122+
active_authority.accounts.push_back({.permission = create_eosio_code_permission_level(get_self()), .weight = 1});
123+
124+
newaccount_action newaccountaction("eosio"_n, {get_self(), "active"_n});
125+
newaccountaction.send(get_self(), account_name, owner_authority, active_authority);
126+
}
127+
116128
void apps::admnupdapp(name account_name,
117129
string json_data,
118130
string username,
119131
string origin,
120132
uint8_t plan)
121133
{
122134
require_auth(get_self());
123-
check(is_account(account_name), "Account does not exist");
124135

125136
auto itr = _appsv3.find(account_name.value);
126137
check(itr != _appsv3.end(), "App does not exist; use admncrtapp to create");
127138

128139
// validate and uniqueness checks if changed
129140
if (itr->username != username) {
130141
check_app_username_chars(username);
131-
auto uidx = _appsv3.get_index<"usernamehash"_n>();
132-
checksum256 username_hash = eosio::sha256(username.c_str(), std::strlen(username.c_str()));
133-
check(uidx.find(username_hash) == uidx.end(), "Username already taken");
142+
check_username_is_unique(username);
134143
}
135144
if (itr->origin != origin) {
136-
auto oidx = _appsv3.get_index<"originhash"_n>();
137-
checksum256 origin_hash = eosio::sha256(origin.c_str(), std::strlen(origin.c_str()));
138-
check(oidx.find(origin_hash) == oidx.end(), "Origin already taken");
145+
check_app_origin_is_unique(origin);
139146
}
140147
_appsv3.modify(itr, get_self(), [&](auto &row) {
141148
row.json_data = json_data;
@@ -276,41 +283,17 @@ void apps::appcreate(name creator,
276283
string origin)
277284
{
278285
require_auth(creator);
279-
280-
// Uniqueness checks for username and origin
281286
check_app_username_chars(username);
282-
checksum256 username_hash = eosio::sha256(username.c_str(), std::strlen(username.c_str()));
283-
{
284-
auto uidx = _appsv3.get_index<"usernamehash"_n>();
285-
check(uidx.find(username_hash) == uidx.end(), "Username already taken");
286-
}
287-
checksum256 origin_hash = eosio::sha256(origin.c_str(), std::strlen(origin.c_str()));
288-
{
289-
auto oidx = _appsv3.get_index<"originhash"_n>();
290-
check(oidx.find(origin_hash) == oidx.end(), "Origin already taken");
291-
}
287+
check_username_is_unique(username);
288+
check_app_origin_is_unique(origin);
292289

293290
// Generate random account name from username and json_data hashes
291+
checksum256 username_hash = eosio::sha256(username.c_str(), std::strlen(username.c_str()));
294292
auto json_hash = eosio::sha256(json_data.c_str(), std::strlen(json_data.c_str()));
295293
const eosio::name random_name = random_account_name(username_hash, json_hash, enum_account_type::App);
296294

297-
// Create account with owner=gov.tmy, active=creator
298-
authority owner_authority = create_authority_with_account(app_controller_account);
299-
authority active_authority = create_authority_with_account(creator);
300-
active_authority.accounts.push_back({.permission = create_eosio_code_permission_level(get_self()), .weight = 1});
301-
302-
newaccount_action newaccountaction("eosio"_n, {get_self(), "active"_n});
303-
newaccountaction.send(get_self(), random_name, owner_authority, active_authority);
304-
305-
// Update resource config
306-
tonomy::resource_config_table _resource_config(get_self(), get_self().value);
307-
auto config = _resource_config.get();
308-
config.total_cpu_weight_allocated = this->initial_cpu_weight_allocation;
309-
config.total_net_weight_allocated = this->initial_net_weight_allocation;
310-
_resource_config.set(config, get_self());
311-
312-
// Set resource limits: ram=0, net=initial, cpu=initial
313-
eosio::set_resource_limits(random_name, 0, this->initial_net_weight_allocation, this->initial_cpu_weight_allocation);
295+
create_app_account(random_name, app_controller_account, creator);
296+
update_resource_config_and_limits(random_name);
314297

315298
// Register in appsv3
316299
_appsv3.emplace(get_self(), [&](auto &row) {
@@ -342,9 +325,7 @@ void apps::appupdate(name account_name,
342325
// If username changed, ensure uniqueness
343326
if (itr->username != username) {
344327
check_app_username_chars(username);
345-
checksum256 username_hash = eosio::sha256(username.c_str(), std::strlen(username.c_str()));
346-
auto uidx = _appsv3.get_index<"usernamehash"_n>();
347-
check(uidx.find(username_hash) == uidx.end(), "Username already taken");
328+
check_username_is_unique(username);
348329
}
349330

350331
_appsv3.modify(itr, get_self(), [&](auto &row) {
@@ -359,7 +340,7 @@ void apps::appupdplan(name account_name,
359340
uint8_t plan)
360341
{
361342
// Plan updates assumed admin-governed
362-
require_auth(get_self());
343+
require_auth(account_name);
363344
auto itr = _appsv3.find(account_name.value);
364345
check(itr != _appsv3.end(), "App does not exist");
365346
_appsv3.modify(itr, get_self(), [&](auto &row) {
@@ -374,7 +355,7 @@ void apps::scdeploy(name account_name,
374355
const std::vector<char> &abi,
375356
string source_code_url)
376357
{
377-
require_auth(get_self());
358+
require_auth(account_name);
378359
// TODO: Implement smart contract deployment
379360
check(false, "scdeploy not yet implemented");
380361
}
@@ -386,23 +367,23 @@ void apps::scupdate(name account_name,
386367
const std::vector<char> &abi,
387368
string source_code_url)
388369
{
389-
require_auth(get_self());
370+
require_auth(account_name);
390371
// TODO: Implement smart contract update
391372
check(false, "scupdate not yet implemented");
392373
}
393374

394375
void apps::appaddkey(name account_name,
395376
public_key key)
396377
{
397-
require_auth(get_self());
378+
require_auth(account_name);
398379
// TODO: Implement key addition to app account's active permission
399380
check(false, "appaddkey not yet implemented");
400381
}
401382

402383
void apps::appremkey(name account_name,
403384
public_key key)
404385
{
405-
require_auth(get_self());
386+
require_auth(account_name);
406387
// TODO: Implement key removal from app account's active permission
407388
check(false, "appremkey not yet implemented");
408389
}
@@ -412,7 +393,7 @@ void apps::admnmigapp(name account_name,
412393
uint8_t plan,
413394
public_key key)
414395
{
415-
require_auth(get_self());
396+
require_auth(account_name);
416397
// TODO: Implement V2 to V3 app migration
417398
check(false, "admnmigapp not yet implemented");
418399
}

0 commit comments

Comments
 (0)