Skip to content

Commit

Permalink
[diem genesis] some simplifications in Genesis module
Browse files Browse the repository at this point in the history
- DiemVersion::initialize takes the initial version number as input
- Set Diem version in Move code instead of Rust

Closes: aptos-labs#8494
  • Loading branch information
sblackshear authored and bors-libra committed Jun 9, 2021
1 parent 11c4566 commit 397fb33
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 102 deletions.
8 changes: 3 additions & 5 deletions language/diem-framework/modules/DiemVersion.move
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ module DiemVersion {
const EINVALID_MAJOR_VERSION_NUMBER: u64 = 0;

/// Publishes the DiemVersion config. Must be called during Genesis.
public fun initialize(
dr_account: &signer,
) {
public fun initialize(dr_account: &signer, initial_version: u64) {
DiemTimestamp::assert_genesis();
Roles::assert_diem_root(dr_account);
DiemConfig::publish_new_config<DiemVersion>(
dr_account,
DiemVersion { major: 1 },
DiemVersion { major: initial_version },
);
}
spec initialize {
Expand All @@ -32,7 +30,7 @@ module DiemVersion {

include DiemTimestamp::AbortsIfNotGenesis;
include DiemConfig::PublishNewConfigAbortsIf<DiemVersion>;
include DiemConfig::PublishNewConfigEnsures<DiemVersion>{payload: DiemVersion { major: 1 }};
include DiemConfig::PublishNewConfigEnsures<DiemVersion>{payload: DiemVersion { major: initial_version }};
}

/// Allows Diem root to update the major version to a larger version.
Expand Down
29 changes: 11 additions & 18 deletions language/diem-framework/modules/Genesis.move
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Genesis {
instruction_schedule: vector<u8>,
native_schedule: vector<u8>,
chain_id: u8,
initial_diem_version: u64,
) {
let dr_account = &dr_account;
let tc_account = &tc_account;
Expand All @@ -50,30 +51,26 @@ module Genesis {
// Currency setup
XUS::initialize(dr_account, tc_account);

XDX::initialize(
dr_account,
tc_account,
);
XDX::initialize(dr_account, tc_account);

AccountFreezing::initialize(dr_account);

TransactionFee::initialize(tc_account);

DiemSystem::initialize_validator_set(
dr_account,
);
DiemVersion::initialize(
dr_account,
);
DualAttestation::initialize(
dr_account,
);
DiemSystem::initialize_validator_set(dr_account);
DiemVersion::initialize(dr_account, initial_diem_version);
DualAttestation::initialize(dr_account);
DiemBlock::initialize_block_metadata(dr_account);

// Rotate auth keys for DiemRoot and TreasuryCompliance accounts to the given
// values
let dr_rotate_key_cap = DiemAccount::extract_key_rotation_capability(dr_account);
DiemAccount::rotate_authentication_key(&dr_rotate_key_cap, dr_auth_key);
DiemAccount::restore_key_rotation_capability(dr_rotate_key_cap);

let tc_rotate_key_cap = DiemAccount::extract_key_rotation_capability(tc_account);
DiemAccount::rotate_authentication_key(&tc_rotate_key_cap, tc_auth_key);
DiemAccount::restore_key_rotation_capability(tc_rotate_key_cap);

DiemTransactionPublishingOption::initialize(
dr_account,
initial_script_allow_list,
Expand All @@ -86,10 +83,6 @@ module Genesis {
native_schedule,
);

let tc_rotate_key_cap = DiemAccount::extract_key_rotation_capability(tc_account);
DiemAccount::rotate_authentication_key(&tc_rotate_key_cap, tc_auth_key);
DiemAccount::restore_key_rotation_capability(tc_rotate_key_cap);

// After we have called this function, all invariants which are guarded by
// `DiemTimestamp::is_operating() ==> ...` will become active and a verification condition.
// See also discussion at function specification.
Expand Down
10 changes: 4 additions & 6 deletions language/diem-framework/modules/doc/DiemVersion.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Tried to set an invalid major version for the VM. Major versions must be strictl
Publishes the DiemVersion config. Must be called during Genesis.


<pre><code><b>public</b> <b>fun</b> <a href="DiemVersion.md#0x1_DiemVersion_initialize">initialize</a>(dr_account: &signer)
<pre><code><b>public</b> <b>fun</b> <a href="DiemVersion.md#0x1_DiemVersion_initialize">initialize</a>(dr_account: &signer, initial_version: u64)
</code></pre>


Expand All @@ -83,14 +83,12 @@ Publishes the DiemVersion config. Must be called during Genesis.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="DiemVersion.md#0x1_DiemVersion_initialize">initialize</a>(
dr_account: &signer,
) {
<pre><code><b>public</b> <b>fun</b> <a href="DiemVersion.md#0x1_DiemVersion_initialize">initialize</a>(dr_account: &signer, initial_version: u64) {
<a href="DiemTimestamp.md#0x1_DiemTimestamp_assert_genesis">DiemTimestamp::assert_genesis</a>();
<a href="Roles.md#0x1_Roles_assert_diem_root">Roles::assert_diem_root</a>(dr_account);
<a href="DiemConfig.md#0x1_DiemConfig_publish_new_config">DiemConfig::publish_new_config</a>&lt;<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a>&gt;(
dr_account,
<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a> { major: 1 },
<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a> { major: initial_version },
);
}
</code></pre>
Expand All @@ -109,7 +107,7 @@ Must abort if the signer does not have the DiemRoot role [[H10]][PERMISSION].
<pre><code><b>include</b> <a href="Roles.md#0x1_Roles_AbortsIfNotDiemRoot">Roles::AbortsIfNotDiemRoot</a>{account: dr_account};
<b>include</b> <a href="DiemTimestamp.md#0x1_DiemTimestamp_AbortsIfNotGenesis">DiemTimestamp::AbortsIfNotGenesis</a>;
<b>include</b> <a href="DiemConfig.md#0x1_DiemConfig_PublishNewConfigAbortsIf">DiemConfig::PublishNewConfigAbortsIf</a>&lt;<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a>&gt;;
<b>include</b> <a href="DiemConfig.md#0x1_DiemConfig_PublishNewConfigEnsures">DiemConfig::PublishNewConfigEnsures</a>&lt;<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a>&gt;{payload: <a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a> { major: 1 }};
<b>include</b> <a href="DiemConfig.md#0x1_DiemConfig_PublishNewConfigEnsures">DiemConfig::PublishNewConfigEnsures</a>&lt;<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a>&gt;{payload: <a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a> { major: initial_version }};
</code></pre>


Expand Down
31 changes: 12 additions & 19 deletions language/diem-framework/modules/doc/Genesis.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ when executing from a fresh state.
Initializes the Diem framework.


<pre><code><b>fun</b> <a href="Genesis.md#0x1_Genesis_initialize">initialize</a>(dr_account: signer, tc_account: signer, dr_auth_key: vector&lt;u8&gt;, tc_auth_key: vector&lt;u8&gt;, initial_script_allow_list: vector&lt;vector&lt;u8&gt;&gt;, is_open_module: bool, instruction_schedule: vector&lt;u8&gt;, native_schedule: vector&lt;u8&gt;, chain_id: u8)
<pre><code><b>fun</b> <a href="Genesis.md#0x1_Genesis_initialize">initialize</a>(dr_account: signer, tc_account: signer, dr_auth_key: vector&lt;u8&gt;, tc_auth_key: vector&lt;u8&gt;, initial_script_allow_list: vector&lt;vector&lt;u8&gt;&gt;, is_open_module: bool, instruction_schedule: vector&lt;u8&gt;, native_schedule: vector&lt;u8&gt;, chain_id: u8, initial_diem_version: u64)
</code></pre>


Expand All @@ -58,6 +58,7 @@ Initializes the Diem framework.
instruction_schedule: vector&lt;u8&gt;,
native_schedule: vector&lt;u8&gt;,
chain_id: u8,
initial_diem_version: u64,
) {
<b>let</b> dr_account = &dr_account;
<b>let</b> tc_account = &tc_account;
Expand All @@ -75,30 +76,26 @@ Initializes the Diem framework.
// Currency setup
<a href="XUS.md#0x1_XUS_initialize">XUS::initialize</a>(dr_account, tc_account);

<a href="XDX.md#0x1_XDX_initialize">XDX::initialize</a>(
dr_account,
tc_account,
);
<a href="XDX.md#0x1_XDX_initialize">XDX::initialize</a>(dr_account, tc_account);

<a href="AccountFreezing.md#0x1_AccountFreezing_initialize">AccountFreezing::initialize</a>(dr_account);

<a href="TransactionFee.md#0x1_TransactionFee_initialize">TransactionFee::initialize</a>(tc_account);

<a href="DiemSystem.md#0x1_DiemSystem_initialize_validator_set">DiemSystem::initialize_validator_set</a>(
dr_account,
);
<a href="DiemVersion.md#0x1_DiemVersion_initialize">DiemVersion::initialize</a>(
dr_account,
);
<a href="DualAttestation.md#0x1_DualAttestation_initialize">DualAttestation::initialize</a>(
dr_account,
);
<a href="DiemSystem.md#0x1_DiemSystem_initialize_validator_set">DiemSystem::initialize_validator_set</a>(dr_account);
<a href="DiemVersion.md#0x1_DiemVersion_initialize">DiemVersion::initialize</a>(dr_account, initial_diem_version);
<a href="DualAttestation.md#0x1_DualAttestation_initialize">DualAttestation::initialize</a>(dr_account);
<a href="DiemBlock.md#0x1_DiemBlock_initialize_block_metadata">DiemBlock::initialize_block_metadata</a>(dr_account);

// Rotate auth keys for DiemRoot and TreasuryCompliance accounts <b>to</b> the given
// values
<b>let</b> dr_rotate_key_cap = <a href="DiemAccount.md#0x1_DiemAccount_extract_key_rotation_capability">DiemAccount::extract_key_rotation_capability</a>(dr_account);
<a href="DiemAccount.md#0x1_DiemAccount_rotate_authentication_key">DiemAccount::rotate_authentication_key</a>(&dr_rotate_key_cap, dr_auth_key);
<a href="DiemAccount.md#0x1_DiemAccount_restore_key_rotation_capability">DiemAccount::restore_key_rotation_capability</a>(dr_rotate_key_cap);

<b>let</b> tc_rotate_key_cap = <a href="DiemAccount.md#0x1_DiemAccount_extract_key_rotation_capability">DiemAccount::extract_key_rotation_capability</a>(tc_account);
<a href="DiemAccount.md#0x1_DiemAccount_rotate_authentication_key">DiemAccount::rotate_authentication_key</a>(&tc_rotate_key_cap, tc_auth_key);
<a href="DiemAccount.md#0x1_DiemAccount_restore_key_rotation_capability">DiemAccount::restore_key_rotation_capability</a>(tc_rotate_key_cap);

<a href="DiemTransactionPublishingOption.md#0x1_DiemTransactionPublishingOption_initialize">DiemTransactionPublishingOption::initialize</a>(
dr_account,
initial_script_allow_list,
Expand All @@ -111,10 +108,6 @@ Initializes the Diem framework.
native_schedule,
);

<b>let</b> tc_rotate_key_cap = <a href="DiemAccount.md#0x1_DiemAccount_extract_key_rotation_capability">DiemAccount::extract_key_rotation_capability</a>(tc_account);
<a href="DiemAccount.md#0x1_DiemAccount_rotate_authentication_key">DiemAccount::rotate_authentication_key</a>(&tc_rotate_key_cap, tc_auth_key);
<a href="DiemAccount.md#0x1_DiemAccount_restore_key_rotation_capability">DiemAccount::restore_key_rotation_capability</a>(tc_rotate_key_cap);

// After we have called this function, all invariants which are guarded by
// `<a href="DiemTimestamp.md#0x1_DiemTimestamp_is_operating">DiemTimestamp::is_operating</a>() ==&gt; ...` will become active and a verification condition.
// See also discussion at function specification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Tried to set an invalid major version for the VM. Major versions must be strictl
Publishes the DiemVersion config. Must be called during Genesis.


<pre><code><b>public</b> <b>fun</b> <a href="DiemVersion.md#0x1_DiemVersion_initialize">initialize</a>(dr_account: &signer)
<pre><code><b>public</b> <b>fun</b> <a href="DiemVersion.md#0x1_DiemVersion_initialize">initialize</a>(dr_account: &signer, initial_version: u64)
</code></pre>


Expand All @@ -83,14 +83,12 @@ Publishes the DiemVersion config. Must be called during Genesis.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="DiemVersion.md#0x1_DiemVersion_initialize">initialize</a>(
dr_account: &signer,
) {
<pre><code><b>public</b> <b>fun</b> <a href="DiemVersion.md#0x1_DiemVersion_initialize">initialize</a>(dr_account: &signer, initial_version: u64) {
<a href="DiemTimestamp.md#0x1_DiemTimestamp_assert_genesis">DiemTimestamp::assert_genesis</a>();
<a href="Roles.md#0x1_Roles_assert_diem_root">Roles::assert_diem_root</a>(dr_account);
<a href="DiemConfig.md#0x1_DiemConfig_publish_new_config">DiemConfig::publish_new_config</a>&lt;<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a>&gt;(
dr_account,
<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a> { major: 1 },
<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a> { major: initial_version },
);
}
</code></pre>
Expand All @@ -109,7 +107,7 @@ Must abort if the signer does not have the DiemRoot role [[H10]][PERMISSION].
<pre><code><b>include</b> <a href="Roles.md#0x1_Roles_AbortsIfNotDiemRoot">Roles::AbortsIfNotDiemRoot</a>{account: dr_account};
<b>include</b> <a href="DiemTimestamp.md#0x1_DiemTimestamp_AbortsIfNotGenesis">DiemTimestamp::AbortsIfNotGenesis</a>;
<b>include</b> <a href="DiemConfig.md#0x1_DiemConfig_PublishNewConfigAbortsIf">DiemConfig::PublishNewConfigAbortsIf</a>&lt;<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a>&gt;;
<b>include</b> <a href="DiemConfig.md#0x1_DiemConfig_PublishNewConfigEnsures">DiemConfig::PublishNewConfigEnsures</a>&lt;<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a>&gt;{payload: <a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a> { major: 1 }};
<b>include</b> <a href="DiemConfig.md#0x1_DiemConfig_PublishNewConfigEnsures">DiemConfig::PublishNewConfigEnsures</a>&lt;<a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a>&gt;{payload: <a href="DiemVersion.md#0x1_DiemVersion">DiemVersion</a> { major: initial_version }};
</code></pre>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ when executing from a fresh state.
Initializes the Diem framework.


<pre><code><b>fun</b> <a href="Genesis.md#0x1_Genesis_initialize">initialize</a>(dr_account: signer, tc_account: signer, dr_auth_key: vector&lt;u8&gt;, tc_auth_key: vector&lt;u8&gt;, initial_script_allow_list: vector&lt;vector&lt;u8&gt;&gt;, is_open_module: bool, instruction_schedule: vector&lt;u8&gt;, native_schedule: vector&lt;u8&gt;, chain_id: u8)
<pre><code><b>fun</b> <a href="Genesis.md#0x1_Genesis_initialize">initialize</a>(dr_account: signer, tc_account: signer, dr_auth_key: vector&lt;u8&gt;, tc_auth_key: vector&lt;u8&gt;, initial_script_allow_list: vector&lt;vector&lt;u8&gt;&gt;, is_open_module: bool, instruction_schedule: vector&lt;u8&gt;, native_schedule: vector&lt;u8&gt;, chain_id: u8, initial_diem_version: u64)
</code></pre>


Expand All @@ -58,6 +58,7 @@ Initializes the Diem framework.
instruction_schedule: vector&lt;u8&gt;,
native_schedule: vector&lt;u8&gt;,
chain_id: u8,
initial_diem_version: u64,
) {
<b>let</b> dr_account = &dr_account;
<b>let</b> tc_account = &tc_account;
Expand All @@ -75,30 +76,26 @@ Initializes the Diem framework.
// Currency setup
<a href="XUS.md#0x1_XUS_initialize">XUS::initialize</a>(dr_account, tc_account);

<a href="XDX.md#0x1_XDX_initialize">XDX::initialize</a>(
dr_account,
tc_account,
);
<a href="XDX.md#0x1_XDX_initialize">XDX::initialize</a>(dr_account, tc_account);

<a href="AccountFreezing.md#0x1_AccountFreezing_initialize">AccountFreezing::initialize</a>(dr_account);

<a href="TransactionFee.md#0x1_TransactionFee_initialize">TransactionFee::initialize</a>(tc_account);

<a href="DiemSystem.md#0x1_DiemSystem_initialize_validator_set">DiemSystem::initialize_validator_set</a>(
dr_account,
);
<a href="DiemVersion.md#0x1_DiemVersion_initialize">DiemVersion::initialize</a>(
dr_account,
);
<a href="DualAttestation.md#0x1_DualAttestation_initialize">DualAttestation::initialize</a>(
dr_account,
);
<a href="DiemSystem.md#0x1_DiemSystem_initialize_validator_set">DiemSystem::initialize_validator_set</a>(dr_account);
<a href="DiemVersion.md#0x1_DiemVersion_initialize">DiemVersion::initialize</a>(dr_account, initial_diem_version);
<a href="DualAttestation.md#0x1_DualAttestation_initialize">DualAttestation::initialize</a>(dr_account);
<a href="DiemBlock.md#0x1_DiemBlock_initialize_block_metadata">DiemBlock::initialize_block_metadata</a>(dr_account);

// Rotate auth keys for DiemRoot and TreasuryCompliance accounts <b>to</b> the given
// values
<b>let</b> dr_rotate_key_cap = <a href="DiemAccount.md#0x1_DiemAccount_extract_key_rotation_capability">DiemAccount::extract_key_rotation_capability</a>(dr_account);
<a href="DiemAccount.md#0x1_DiemAccount_rotate_authentication_key">DiemAccount::rotate_authentication_key</a>(&dr_rotate_key_cap, dr_auth_key);
<a href="DiemAccount.md#0x1_DiemAccount_restore_key_rotation_capability">DiemAccount::restore_key_rotation_capability</a>(dr_rotate_key_cap);

<b>let</b> tc_rotate_key_cap = <a href="DiemAccount.md#0x1_DiemAccount_extract_key_rotation_capability">DiemAccount::extract_key_rotation_capability</a>(tc_account);
<a href="DiemAccount.md#0x1_DiemAccount_rotate_authentication_key">DiemAccount::rotate_authentication_key</a>(&tc_rotate_key_cap, tc_auth_key);
<a href="DiemAccount.md#0x1_DiemAccount_restore_key_rotation_capability">DiemAccount::restore_key_rotation_capability</a>(tc_rotate_key_cap);

<a href="DiemTransactionPublishingOption.md#0x1_DiemTransactionPublishingOption_initialize">DiemTransactionPublishingOption::initialize</a>(
dr_account,
initial_script_allow_list,
Expand All @@ -111,10 +108,6 @@ Initializes the Diem framework.
native_schedule,
);

<b>let</b> tc_rotate_key_cap = <a href="DiemAccount.md#0x1_DiemAccount_extract_key_rotation_capability">DiemAccount::extract_key_rotation_capability</a>(tc_account);
<a href="DiemAccount.md#0x1_DiemAccount_rotate_authentication_key">DiemAccount::rotate_authentication_key</a>(&tc_rotate_key_cap, tc_auth_key);
<a href="DiemAccount.md#0x1_DiemAccount_restore_key_rotation_capability">DiemAccount::restore_key_rotation_capability</a>(tc_rotate_key_cap);

// After we have called this function, all invariants which are guarded by
// `<a href="DiemTimestamp.md#0x1_DiemTimestamp_is_operating">DiemTimestamp::is_operating</a>() ==&gt; ...` will become active and a verification condition.
// See also discussion at function specification.
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ script{
use 0x1::DiemVersion;
fun main(account: signer) {
let account = &account;
DiemVersion::initialize(account);
DiemVersion::initialize(account, 1);
}
}
// check: "Keep(ABORTED { code: 1,"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ script {
let instruction_schedule = Vector::empty();
let native_schedule = Vector::empty();
let chain_id = 0;
let initial_diem_version = 1;

DiemAccount::initialize(dr_account, x"00000000000000000000000000000000");

Expand All @@ -58,9 +59,7 @@ script {
DiemSystem::initialize_validator_set(
dr_account,
);
DiemVersion::initialize(
dr_account,
);
DiemVersion::initialize(dr_account, initial_diem_version);
DualAttestation::initialize(
dr_account,
);
Expand Down
13 changes: 4 additions & 9 deletions language/tools/move-cli/tests/testsuite/diem_smoke/genesis.move
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ script {
let instruction_schedule = Vector::empty();
let native_schedule = Vector::empty();
let chain_id = 0;
let initial_diem_version = 1;

DiemAccount::initialize(dr_account, x"00000000000000000000000000000000");

Expand All @@ -55,15 +56,9 @@ script {

TransactionFee::initialize(tc_account);

DiemSystem::initialize_validator_set(
dr_account,
);
DiemVersion::initialize(
dr_account,
);
DualAttestation::initialize(
dr_account,
);
DiemSystem::initialize_validator_set(dr_account);
DiemVersion::initialize(dr_account, initial_diem_version);
DualAttestation::initialize(dr_account);
DiemBlock::initialize_block_metadata(dr_account);

let dr_rotate_key_cap = DiemAccount::extract_key_rotation_capability(dr_account);
Expand Down
Loading

0 comments on commit 397fb33

Please sign in to comment.