Skip to content

Commit

Permalink
[move] enhance vouch system [breaking] (0LNetworkCommunity#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
soaresa authored and 0o-de-lally committed Aug 16, 2024
1 parent 3e21576 commit 815395f
Show file tree
Hide file tree
Showing 15 changed files with 1,154 additions and 139 deletions.
32 changes: 15 additions & 17 deletions framework/cached-packages/src/libra_framework_sdk_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,18 +533,18 @@ pub enum EntryFunctionCall {
/// you may want to add people who are related to you
/// there are no known use cases for this at the moment.
VouchInsistVouchFor {
wanna_be_my_friend: AccountAddress,
friend_account: AccountAddress,
},

VouchRevoke {
its_not_me_its_you: AccountAddress,
friend_account: AccountAddress,
},

/// will only succesfully vouch if the two are not related by ancestry
/// prevents spending a vouch that would not be counted.
/// to add a vouch and ignore this check use insist_vouch
VouchVouchFor {
wanna_be_my_friend: AccountAddress,
friend_account: AccountAddress,
},
}

Expand Down Expand Up @@ -856,11 +856,9 @@ impl EntryFunctionCall {
fullnode_addresses,
),
VersionSetVersion { major } => version_set_version(major),
VouchInsistVouchFor { wanna_be_my_friend } => {
vouch_insist_vouch_for(wanna_be_my_friend)
}
VouchRevoke { its_not_me_its_you } => vouch_revoke(its_not_me_its_you),
VouchVouchFor { wanna_be_my_friend } => vouch_vouch_for(wanna_be_my_friend),
VouchInsistVouchFor { friend_account } => vouch_insist_vouch_for(friend_account),
VouchRevoke { friend_account } => vouch_revoke(friend_account),
VouchVouchFor { friend_account } => vouch_vouch_for(friend_account),
}
}

Expand Down Expand Up @@ -2329,7 +2327,7 @@ pub fn version_set_version(major: u64) -> TransactionPayload {

/// you may want to add people who are related to you
/// there are no known use cases for this at the moment.
pub fn vouch_insist_vouch_for(wanna_be_my_friend: AccountAddress) -> TransactionPayload {
pub fn vouch_insist_vouch_for(friend_account: AccountAddress) -> TransactionPayload {
TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(
AccountAddress::new([
Expand All @@ -2340,11 +2338,11 @@ pub fn vouch_insist_vouch_for(wanna_be_my_friend: AccountAddress) -> Transaction
),
ident_str!("insist_vouch_for").to_owned(),
vec![],
vec![bcs::to_bytes(&wanna_be_my_friend).unwrap()],
vec![bcs::to_bytes(&friend_account).unwrap()],
))
}

pub fn vouch_revoke(its_not_me_its_you: AccountAddress) -> TransactionPayload {
pub fn vouch_revoke(friend_account: AccountAddress) -> TransactionPayload {
TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(
AccountAddress::new([
Expand All @@ -2355,14 +2353,14 @@ pub fn vouch_revoke(its_not_me_its_you: AccountAddress) -> TransactionPayload {
),
ident_str!("revoke").to_owned(),
vec![],
vec![bcs::to_bytes(&its_not_me_its_you).unwrap()],
vec![bcs::to_bytes(&friend_account).unwrap()],
))
}

/// will only succesfully vouch if the two are not related by ancestry
/// prevents spending a vouch that would not be counted.
/// to add a vouch and ignore this check use insist_vouch
pub fn vouch_vouch_for(wanna_be_my_friend: AccountAddress) -> TransactionPayload {
pub fn vouch_vouch_for(friend_account: AccountAddress) -> TransactionPayload {
TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(
AccountAddress::new([
Expand All @@ -2373,7 +2371,7 @@ pub fn vouch_vouch_for(wanna_be_my_friend: AccountAddress) -> TransactionPayload
),
ident_str!("vouch_for").to_owned(),
vec![],
vec![bcs::to_bytes(&wanna_be_my_friend).unwrap()],
vec![bcs::to_bytes(&friend_account).unwrap()],
))
}
mod decoder {
Expand Down Expand Up @@ -3206,7 +3204,7 @@ mod decoder {
pub fn vouch_insist_vouch_for(payload: &TransactionPayload) -> Option<EntryFunctionCall> {
if let TransactionPayload::EntryFunction(script) = payload {
Some(EntryFunctionCall::VouchInsistVouchFor {
wanna_be_my_friend: bcs::from_bytes(script.args().first()?).ok()?,
friend_account: bcs::from_bytes(script.args().first()?).ok()?,
})
} else {
None
Expand All @@ -3216,7 +3214,7 @@ mod decoder {
pub fn vouch_revoke(payload: &TransactionPayload) -> Option<EntryFunctionCall> {
if let TransactionPayload::EntryFunction(script) = payload {
Some(EntryFunctionCall::VouchRevoke {
its_not_me_its_you: bcs::from_bytes(script.args().first()?).ok()?,
friend_account: bcs::from_bytes(script.args().first()?).ok()?,
})
} else {
None
Expand All @@ -3226,7 +3224,7 @@ mod decoder {
pub fn vouch_vouch_for(payload: &TransactionPayload) -> Option<EntryFunctionCall> {
if let TransactionPayload::EntryFunction(script) = payload {
Some(EntryFunctionCall::VouchVouchFor {
wanna_be_my_friend: bcs::from_bytes(script.args().first()?).ok()?,
friend_account: bcs::from_bytes(script.args().first()?).ok()?,
})
} else {
None
Expand Down
1 change: 1 addition & 0 deletions framework/libra-framework/sources/create_signer.move
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module diem_framework::create_signer {
friend ol_framework::fee_maker;
friend ol_framework::epoch_boundary;
friend ol_framework::multi_action_migration; // TODO: remove after offer migration is completed
friend ol_framework::vouch_migration; // TODO: remove after vouch migration is completed

public(friend) native fun create_signer(addr: address): signer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module ol_framework::globals {
min_blocks_per_epoch: u64,
validator_vouch_threshold: u64,
signing_threshold_pct: u64,
max_vouches_per_validator: u64,
}

const COIN_DECIMAL_PLACES: u8 = 6; // Or 10^6, 1 coin is 1_000_000 units in the database. Any human display needs to consider this scaling factor.
Expand Down Expand Up @@ -90,6 +91,11 @@ module ol_framework::globals {
get_constants().validator_vouch_threshold
}

/// Get the max number of vouches per validator
public fun get_max_vouches_per_validator(): u64 {
get_constants().max_vouches_per_validator
}

/// Get the threshold of number of signed blocks in an epoch per validator
public fun get_signing_threshold(): u64 {
get_constants().signing_threshold_pct
Expand All @@ -113,6 +119,7 @@ module ol_framework::globals {
min_blocks_per_epoch: 0,
validator_vouch_threshold: 0,
signing_threshold_pct: 3,
max_vouches_per_validator: 10,
}
};

Expand All @@ -137,6 +144,7 @@ module ol_framework::globals {
min_blocks_per_epoch: 10000,
validator_vouch_threshold: 2, // Production must be more than 1 vouch validator (at least 2)
signing_threshold_pct: 3,
max_vouches_per_validator: 10,
}
} else {
return GlobalConstants {
Expand All @@ -157,7 +165,8 @@ module ol_framework::globals {
min_blocks_per_epoch: 10000,
validator_vouch_threshold: 2, // Production must be more than 1 vouch validator (at least 2)
signing_threshold_pct: 3,
max_vouches_per_validator: 10,
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ module diem_framework::epoch_boundary {
use diem_framework::reconfiguration;
use diem_framework::transaction_fee;
use diem_framework::system_addresses;
use ol_framework::stake;
use ol_framework::jail;
use ol_framework::safe;
use ol_framework::burn;
use ol_framework::stake;
use ol_framework::vouch;
use ol_framework::rewards;
use ol_framework::testnet;
use ol_framework::fee_maker;
use ol_framework::migrations;
use ol_framework::libra_coin;
use ol_framework::slow_wallet;
use ol_framework::match_index;
Expand Down Expand Up @@ -265,6 +267,7 @@ module diem_framework::epoch_boundary {
// when new modules or structures are added by chain upgrades.
fun migrate_data(root: &signer) {
randomness::initialize(root);
migrations::execute(root);
}

// Contains all of 0L's business logic for end of epoch.
Expand Down Expand Up @@ -330,6 +333,10 @@ module diem_framework::epoch_boundary {
status.pof_thermo_increase = t_increase;
status.pof_thermo_amount = t_amount;

print(&string::utf8(b"set_vouch_price"));
let (nominal_reward, _, _, _) = proof_of_fee::get_consensus_reward();
vouch::set_vouch_price(root, nominal_reward);

print(&string::utf8(b"subsidize_from_infra_escrow"));
let (i_success, i_fee) = subsidize_from_infra_escrow(root);
status.infra_subsidize_amount = i_fee;
Expand Down
98 changes: 98 additions & 0 deletions framework/libra-framework/sources/ol_sources/migrations.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module ol_framework::migrations {
use std::vector;
use std::string;
use std::error;
use diem_framework::system_addresses;
use ol_framework::epoch_helper;

use diem_std::debug::print;

// migrations
use ol_framework::vouch_migration;

//////// CONST ////////
const EMIGRATIONS_NOT_INITIALIZED: u64 = 1;

//////// STRUCTS ////////
struct Migration has store, copy {
number: u64,
epoch: u64,
description: vector<u8>,
}

struct Migrations has key {
last_migration: u64,
history: vector<Migration>,
}

public fun execute(root: &signer) acquires Migrations {
// ensure ol_framework
system_addresses::assert_ol(root);

// execute all migrations
if (apply_migration(root, 1, b"Vouch migration initializes GivenVouches, ReceivedVouches, and drop MyVouches")) {
vouch_migration::migrate_my_vouches();
};

}

fun apply_migration(root: &signer, mig_number: u64, description: vector<u8>): bool acquires Migrations {
if (can_execute_migration(mig_number)) {
print(&string::utf8(b">>> Migration started:"));
print(&mig_number);
print(&string::utf8(description));

register_migration(root, mig_number, description);
true
} else {
false
}
}

fun can_execute_migration(mig_number: u64): bool acquires Migrations {
get_last_migration_number() < mig_number
}

fun register_migration(root: &signer, mig_number: u64, description: vector<u8>) acquires Migrations {
let epoch = epoch_helper::get_current_epoch();

if (exists<Migrations>(@ol_framework)) {
// update
let state = borrow_global_mut<Migrations>(@diem_framework);
state.last_migration = mig_number;
vector::push_back(&mut state.history, Migration {
number: mig_number,
epoch: epoch,
description: description,
});
} else {
// initialize
move_to<Migrations>(root, Migrations {
last_migration: mig_number,
history: vector[Migration {
number: mig_number,
epoch: epoch,
description: description,
}],
});
};
}

public fun get_last_migration_number(): u64 acquires Migrations {
if (!exists<Migrations>(@ol_framework)) {
return 0
};

let state = borrow_global<Migrations>(@ol_framework);
state.last_migration
}

public fun get_last_migrations_history(): (u64, u64, vector<u8>) acquires Migrations {
assert!(exists<Migrations>(@ol_framework), error::invalid_state(EMIGRATIONS_NOT_INITIALIZED));

let state = borrow_global<Migrations>(@ol_framework);
let last_migration = vector::borrow(&state.history, vector::length(&state.history) -1);
(last_migration.number, last_migration.epoch, last_migration.description)
}

}
20 changes: 14 additions & 6 deletions framework/libra-framework/sources/ol_sources/mock.move
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ module ol_framework::mock {
#[test_only]
/// mock up to 6 validators alice..frank
public fun genesis_n_vals(root: &signer, num: u64): vector<address> {
// create vals with vouches
create_vals(root, num, true);

timestamp::fast_forward_seconds(2); // or else reconfigure wont happen
stake::test_reconfigure(root, validator_universe::get_eligible_validators());

stake::get_current_validators()
}

#[test_only]
public fun create_vals(root: &signer, num: u64, with_vouches: bool) {
system_addresses::assert_ol(root);
let framework_sig = account::create_signer_for_test(@diem_framework);
ol_test_genesis(&framework_sig);
Expand All @@ -273,15 +284,12 @@ module ol_framework::mock {
validator_universe::test_register_validator(root, &pk, &pop, &sig, 100, true, true);

vouch::init(&sig);
vouch::test_set_buddies(*val, val_addr);
if (with_vouches) {
vouch::test_set_buddies(*val, val_addr);
};

i = i + 1;
};

timestamp::fast_forward_seconds(2); // or else reconfigure wont happen
stake::test_reconfigure(root, validator_universe::get_eligible_validators());

stake::get_current_validators()
}

#[test_only]
Expand Down
Loading

0 comments on commit 815395f

Please sign in to comment.