Simplify genesis state; #17
This commit is contained in:
parent
36ba2e1d45
commit
c14ac442f5
5 changed files with 66 additions and 77 deletions
2
docs
2
docs
|
|
@ -1 +1 @@
|
||||||
Subproject commit 97435c1a622e41e0a5fc1be72aaadea62e1b7adb
|
Subproject commit 66a8825f937af9b810b63a3015521e29027ac5d4
|
||||||
|
|
@ -286,7 +286,7 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
const asset_object& core_asset =
|
const asset_object& core_asset =
|
||||||
create<asset_object>( [&]( asset_object& a ) {
|
create<asset_object>( [&]( asset_object& a ) {
|
||||||
a.symbol = GRAPHENE_SYMBOL;
|
a.symbol = GRAPHENE_SYMBOL;
|
||||||
a.options.max_supply = GRAPHENE_MAX_SHARE_SUPPLY;
|
a.options.max_supply = genesis_state.max_core_supply;
|
||||||
a.precision = GRAPHENE_BLOCKCHAIN_PRECISION_DIGITS;
|
a.precision = GRAPHENE_BLOCKCHAIN_PRECISION_DIGITS;
|
||||||
a.options.flags = 0;
|
a.options.flags = 0;
|
||||||
a.options.issuer_permissions = 0;
|
a.options.issuer_permissions = 0;
|
||||||
|
|
@ -356,7 +356,7 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
|
|
||||||
// Helper function to get asset ID by symbol
|
// Helper function to get asset ID by symbol
|
||||||
const auto& assets_by_symbol = get_index_type<asset_index>().indices().get<by_symbol>();
|
const auto& assets_by_symbol = get_index_type<asset_index>().indices().get<by_symbol>();
|
||||||
auto get_asset_id = [&assets_by_symbol](const string& symbol) {
|
const auto get_asset_id = [&assets_by_symbol](const string& symbol) {
|
||||||
auto itr = assets_by_symbol.find(symbol);
|
auto itr = assets_by_symbol.find(symbol);
|
||||||
FC_ASSERT(itr != assets_by_symbol.end(),
|
FC_ASSERT(itr != assets_by_symbol.end(),
|
||||||
"Unable to find asset '${sym}'. Did you forget to add a record for it to initial_assets?",
|
"Unable to find asset '${sym}'. Did you forget to add a record for it to initial_assets?",
|
||||||
|
|
@ -364,19 +364,18 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
return itr->get_id();
|
return itr->get_id();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
map<asset_id_type, share_type> total_supplies;
|
||||||
|
|
||||||
// Create initial assets
|
// Create initial assets
|
||||||
for( const genesis_state_type::initial_asset_type& asset : genesis_state.initial_assets )
|
for( const genesis_state_type::initial_asset_type& asset : genesis_state.initial_assets )
|
||||||
{
|
{
|
||||||
|
asset_id_type new_asset_id = get_index_type<asset_index>().get_next_id();
|
||||||
asset_dynamic_data_id_type dynamic_data_id;
|
asset_dynamic_data_id_type dynamic_data_id;
|
||||||
optional<asset_bitasset_data_id_type> bitasset_data_id;
|
optional<asset_bitasset_data_id_type> bitasset_data_id;
|
||||||
if( asset.bitasset_opts.valid() )
|
if( asset.is_bitasset )
|
||||||
{
|
{
|
||||||
share_type total_allocated;
|
|
||||||
asset_id_type new_asset_id = get_index_type<asset_index>().get_next_id();
|
|
||||||
asset_id_type collateral_asset_id = get_asset_id(asset.bitasset_opts->backing_asset_symbol);
|
|
||||||
|
|
||||||
int collateral_holder_number = 0;
|
int collateral_holder_number = 0;
|
||||||
for( const auto& collateral_rec : asset.bitasset_opts->collateral_records )
|
for( const auto& collateral_rec : asset.collateral_records )
|
||||||
{
|
{
|
||||||
account_create_operation cop;
|
account_create_operation cop;
|
||||||
cop.name = asset.symbol + "-collateral-holder-" + std::to_string(collateral_holder_number);
|
cop.name = asset.symbol + "-collateral-holder-" + std::to_string(collateral_holder_number);
|
||||||
|
|
@ -391,30 +390,23 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
c.collateral = collateral_rec.collateral;
|
c.collateral = collateral_rec.collateral;
|
||||||
c.debt = collateral_rec.debt;
|
c.debt = collateral_rec.debt;
|
||||||
c.call_price = price::call_price(chain::asset(c.debt, new_asset_id),
|
c.call_price = price::call_price(chain::asset(c.debt, new_asset_id),
|
||||||
chain::asset(c.collateral, collateral_asset_id),
|
chain::asset(c.collateral, core_asset.id),
|
||||||
asset.bitasset_opts->maintenance_collateral_ratio);
|
GRAPHENE_DEFAULT_MAINTENANCE_COLLATERAL_RATIO);
|
||||||
});
|
});
|
||||||
|
|
||||||
total_allocated += collateral_rec.debt;
|
total_supplies[ 0 ] += collateral_rec.collateral;
|
||||||
}
|
}
|
||||||
|
|
||||||
bitasset_data_id = create<asset_bitasset_data_object>([&](asset_bitasset_data_object& b) {
|
bitasset_data_id = create<asset_bitasset_data_object>([&](asset_bitasset_data_object& b) {
|
||||||
b.options.feed_lifetime_sec = asset.bitasset_opts->feed_lifetime_sec;
|
b.options.short_backing_asset = core_asset.id;
|
||||||
b.options.minimum_feeds = asset.bitasset_opts->minimum_feeds;
|
|
||||||
b.options.force_settlement_delay_sec = asset.bitasset_opts->force_settlement_delay_sec;
|
|
||||||
b.options.force_settlement_offset_percent = asset.bitasset_opts->force_settlement_offset_percent;
|
|
||||||
b.options.maximum_force_settlement_volume = asset.bitasset_opts->maximum_force_settlement_volume;
|
|
||||||
b.options.short_backing_asset = get_asset_id(asset.bitasset_opts->backing_asset_symbol);
|
|
||||||
}).id;
|
}).id;
|
||||||
|
}
|
||||||
|
|
||||||
dynamic_data_id = create<asset_dynamic_data_object>([&](asset_dynamic_data_object& d) {
|
dynamic_data_id = create<asset_dynamic_data_object>([&](asset_dynamic_data_object& d) {
|
||||||
d.current_supply = total_allocated;
|
d.accumulated_fees = asset.accumulated_fees;
|
||||||
d.accumulated_fees = asset.initial_accumulated_fees;
|
}).id;
|
||||||
}).id;
|
|
||||||
} else
|
total_supplies[ new_asset_id ] += asset.accumulated_fees;
|
||||||
dynamic_data_id = create<asset_dynamic_data_object>([&](asset_dynamic_data_object& d) {
|
|
||||||
d.accumulated_fees = asset.initial_accumulated_fees;
|
|
||||||
}).id;
|
|
||||||
|
|
||||||
create<asset_object>([&](asset_object& a) {
|
create<asset_object>([&](asset_object& a) {
|
||||||
a.symbol = asset.symbol;
|
a.symbol = asset.symbol;
|
||||||
|
|
@ -422,10 +414,6 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
a.precision = asset.precision;
|
a.precision = asset.precision;
|
||||||
a.issuer = get_account_id(asset.issuer_name);
|
a.issuer = get_account_id(asset.issuer_name);
|
||||||
a.options.max_supply = asset.max_supply;
|
a.options.max_supply = asset.max_supply;
|
||||||
a.options.market_fee_percent = asset.market_fee_percent;
|
|
||||||
a.options.max_market_fee = asset.max_market_fee;
|
|
||||||
a.options.issuer_permissions = asset.issuer_permissions;
|
|
||||||
a.options.flags = asset.flags;
|
|
||||||
|
|
||||||
a.dynamic_asset_data_id = dynamic_data_id;
|
a.dynamic_asset_data_id = dynamic_data_id;
|
||||||
a.bitasset_data_id = bitasset_data_id;
|
a.bitasset_data_id = bitasset_data_id;
|
||||||
|
|
@ -436,19 +424,22 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
share_type total_allocation;
|
share_type total_allocation;
|
||||||
for( const auto& handout : genesis_state.initial_balances )
|
for( const auto& handout : genesis_state.initial_balances )
|
||||||
{
|
{
|
||||||
create<balance_object>([&handout,&assets_by_symbol,total_allocation](balance_object& b) {
|
const auto asset_id = get_asset_id(handout.asset_symbol);
|
||||||
b.balance = asset(handout.amount, assets_by_symbol.find(handout.asset_symbol)->get_id());
|
create<balance_object>([&handout,&get_asset_id,total_allocation,asset_id](balance_object& b) {
|
||||||
|
b.balance = asset(handout.amount, asset_id);
|
||||||
b.owner = handout.owner;
|
b.owner = handout.owner;
|
||||||
});
|
});
|
||||||
total_allocation += handout.amount;
|
|
||||||
|
total_supplies[ asset_id ] += handout.amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create initial vesting balances
|
// Create initial vesting balances
|
||||||
for( const genesis_state_type::initial_vesting_balance_type& vest : genesis_state.initial_vesting_balances )
|
for( const genesis_state_type::initial_vesting_balance_type& vest : genesis_state.initial_vesting_balances )
|
||||||
{
|
{
|
||||||
|
const auto asset_id = get_asset_id(vest.asset_symbol);
|
||||||
create<balance_object>([&](balance_object& b) {
|
create<balance_object>([&](balance_object& b) {
|
||||||
b.owner = vest.owner;
|
b.owner = vest.owner;
|
||||||
b.balance = asset(vest.amount, assets_by_symbol.find(vest.asset_symbol)->get_id());
|
b.balance = asset(vest.amount, asset_id);
|
||||||
|
|
||||||
linear_vesting_policy policy;
|
linear_vesting_policy policy;
|
||||||
policy.begin_timestamp = vest.begin_timestamp;
|
policy.begin_timestamp = vest.begin_timestamp;
|
||||||
|
|
@ -458,17 +449,33 @@ void database::init_genesis(const genesis_state_type& genesis_state)
|
||||||
|
|
||||||
b.vesting_policy = std::move(policy);
|
b.vesting_policy = std::move(policy);
|
||||||
});
|
});
|
||||||
total_allocation += vest.amount;
|
|
||||||
|
total_supplies[ asset_id ] += vest.amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set current supply based on allocations, if they happened
|
if( total_supplies[ 0 ] > 0 )
|
||||||
if( total_allocation > 0 )
|
|
||||||
{
|
{
|
||||||
modify(dyn_asset, [total_allocation](asset_dynamic_data_object& d) {
|
adjust_balance(GRAPHENE_COMMITTEE_ACCOUNT, -get_balance(GRAPHENE_COMMITTEE_ACCOUNT,{}));
|
||||||
d.current_supply = total_allocation;
|
|
||||||
});
|
|
||||||
adjust_balance(GRAPHENE_COMMITTEE_ACCOUNT, -get_balance(GRAPHENE_COMMITTEE_ACCOUNT,{}));
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
total_supplies[ 0 ] = GRAPHENE_MAX_SHARE_SUPPLY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save tallied supplies
|
||||||
|
for( const auto& item : total_supplies )
|
||||||
|
{
|
||||||
|
const auto asset_id = item.first;
|
||||||
|
const auto total_supply = item.second;
|
||||||
|
|
||||||
|
modify( get( asset_id ), [ & ]( asset_object& asset ) {
|
||||||
|
modify( get( asset.dynamic_asset_data_id ), [ & ]( asset_dynamic_data_object& asset_data ) {
|
||||||
|
asset_data.current_supply = total_supply;
|
||||||
|
} );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Assert that bitasset debt = supply
|
||||||
|
|
||||||
// Create initial witnesses
|
// Create initial witnesses
|
||||||
std::for_each(genesis_state.initial_witness_candidates.begin(), genesis_state.initial_witness_candidates.end(),
|
std::for_each(genesis_state.initial_witness_candidates.begin(), genesis_state.initial_witness_candidates.end(),
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,7 @@ namespace graphene { namespace chain {
|
||||||
/// Ticker symbol for this asset, i.e. "USD"
|
/// Ticker symbol for this asset, i.e. "USD"
|
||||||
string symbol;
|
string symbol;
|
||||||
/// Maximum number of digits after the decimal point (must be <= 12)
|
/// Maximum number of digits after the decimal point (must be <= 12)
|
||||||
uint8_t precision;
|
uint8_t precision = 0;
|
||||||
/// ID of the account which issued this asset.
|
/// ID of the account which issued this asset.
|
||||||
account_id_type issuer;
|
account_id_type issuer;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,36 +26,23 @@ struct genesis_state_type {
|
||||||
bool is_lifetime_member = false;
|
bool is_lifetime_member = false;
|
||||||
};
|
};
|
||||||
struct initial_asset_type {
|
struct initial_asset_type {
|
||||||
|
struct initial_collateral_position {
|
||||||
|
address owner;
|
||||||
|
share_type collateral;
|
||||||
|
share_type debt;
|
||||||
|
};
|
||||||
|
|
||||||
string symbol;
|
string symbol;
|
||||||
|
string issuer_name;
|
||||||
|
|
||||||
string description;
|
string description;
|
||||||
uint8_t precision;
|
uint8_t precision;
|
||||||
string issuer_name;
|
|
||||||
share_type max_supply;
|
share_type max_supply;
|
||||||
uint16_t market_fee_percent;
|
share_type accumulated_fees;
|
||||||
share_type max_market_fee;
|
|
||||||
uint16_t issuer_permissions;
|
|
||||||
uint16_t flags;
|
|
||||||
|
|
||||||
struct initial_bitasset_options {
|
bool is_bitasset = false;
|
||||||
uint32_t feed_lifetime_sec;
|
vector<initial_collateral_position> collateral_records;
|
||||||
uint8_t minimum_feeds;
|
|
||||||
uint32_t force_settlement_delay_sec;
|
|
||||||
uint16_t force_settlement_offset_percent;
|
|
||||||
uint16_t maximum_force_settlement_volume;
|
|
||||||
string backing_asset_symbol;
|
|
||||||
|
|
||||||
struct initial_collateral_position {
|
|
||||||
address owner;
|
|
||||||
share_type collateral;
|
|
||||||
share_type debt;
|
|
||||||
};
|
|
||||||
|
|
||||||
uint16_t maintenance_collateral_ratio;
|
|
||||||
vector<initial_collateral_position> collateral_records;
|
|
||||||
};
|
|
||||||
optional<initial_bitasset_options> bitasset_opts;
|
|
||||||
|
|
||||||
share_type initial_accumulated_fees;
|
|
||||||
};
|
};
|
||||||
struct initial_balance_type {
|
struct initial_balance_type {
|
||||||
address owner;
|
address owner;
|
||||||
|
|
@ -86,6 +73,7 @@ struct genesis_state_type {
|
||||||
};
|
};
|
||||||
|
|
||||||
time_point_sec initial_timestamp;
|
time_point_sec initial_timestamp;
|
||||||
|
share_type max_core_supply = GRAPHENE_MAX_SHARE_SUPPLY;
|
||||||
chain_parameters initial_parameters;
|
chain_parameters initial_parameters;
|
||||||
vector<initial_account_type> initial_accounts;
|
vector<initial_account_type> initial_accounts;
|
||||||
vector<initial_asset_type> initial_assets;
|
vector<initial_asset_type> initial_assets;
|
||||||
|
|
@ -101,15 +89,10 @@ struct genesis_state_type {
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_account_type, (name)(owner_key)(active_key)(is_lifetime_member))
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_account_type, (name)(owner_key)(active_key)(is_lifetime_member))
|
||||||
|
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_asset_type,
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_asset_type,
|
||||||
(symbol)(description)(precision)(issuer_name)(max_supply)(market_fee_percent)
|
(symbol)(issuer_name)(description)(precision)(max_supply)(accumulated_fees)(is_bitasset)(collateral_records))
|
||||||
(issuer_permissions)(flags)(bitasset_opts)(initial_accumulated_fees))
|
|
||||||
|
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_asset_type::initial_bitasset_options,
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_asset_type::initial_collateral_position,
|
||||||
(feed_lifetime_sec)(minimum_feeds)(force_settlement_delay_sec)(force_settlement_offset_percent)
|
(owner)(collateral)(debt))
|
||||||
(maximum_force_settlement_volume)(backing_asset_symbol)(maintenance_collateral_ratio)(collateral_records))
|
|
||||||
|
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_asset_type::initial_bitasset_options::initial_collateral_position,
|
|
||||||
(collateral)(debt))
|
|
||||||
|
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_balance_type,
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_balance_type,
|
||||||
(owner)(asset_symbol)(amount))
|
(owner)(asset_symbol)(amount))
|
||||||
|
|
@ -124,6 +107,6 @@ FC_REFLECT(graphene::chain::genesis_state_type::initial_committee_member_type, (
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type::initial_worker_type, (owner_name)(daily_pay))
|
FC_REFLECT(graphene::chain::genesis_state_type::initial_worker_type, (owner_name)(daily_pay))
|
||||||
|
|
||||||
FC_REFLECT(graphene::chain::genesis_state_type,
|
FC_REFLECT(graphene::chain::genesis_state_type,
|
||||||
(initial_timestamp)(initial_parameters)(initial_accounts)(initial_assets)(initial_balances)
|
(initial_timestamp)(max_core_supply)(initial_parameters)(initial_accounts)(initial_assets)(initial_balances)
|
||||||
(initial_vesting_balances)(initial_active_witnesses)(initial_witness_candidates)
|
(initial_vesting_balances)(initial_active_witnesses)(initial_witness_candidates)
|
||||||
(initial_committee_candidates)(initial_worker_candidates))
|
(initial_committee_candidates)(initial_worker_candidates))
|
||||||
|
|
|
||||||
|
|
@ -407,7 +407,6 @@ BOOST_FIXTURE_TEST_CASE( tapos_rollover, database_fixture )
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ACTORS((alice)(bob));
|
ACTORS((alice)(bob));
|
||||||
const auto& core = asset_id_type()(db);
|
|
||||||
|
|
||||||
BOOST_TEST_MESSAGE( "Give Alice some money" );
|
BOOST_TEST_MESSAGE( "Give Alice some money" );
|
||||||
transfer(committee_account, alice_id, asset(10000));
|
transfer(committee_account, alice_id, asset(10000));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue