Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initial vesting #3606

Merged
merged 4 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions massa-execution-worker/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use massa_models::{amount::Amount, slot::Slot};
use massa_pos_exports::SelectorController;
use massa_sc_runtime::{Interface, Response, RuntimeModule};
use massa_storage::Storage;
use massa_time::MassaTime;
use parking_lot::{Mutex, RwLock};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::sync::Arc;
Expand Down Expand Up @@ -430,7 +431,7 @@ impl ExecutionState {
if let Some(vesting_range) = self.find_vesting_range(&buyer_addr, &current_slot) {
let rolls = self.get_final_and_candidate_rolls(&buyer_addr);
// (candidate_rolls + amount to buy)
let max_rolls = rolls.1 + roll_count;
let max_rolls = rolls.1.saturating_add(*roll_count);
if max_rolls > vesting_range.max_rolls {
return Err(ExecutionError::VestingError(format!(
"vesting_max_rolls={} with value max_rolls={} ",
Expand Down Expand Up @@ -1436,24 +1437,48 @@ impl ExecutionState {
))
})?;

let get_slot_at_timestamp = |config: &ExecutionConfig, timestamp: MassaTime| {
match massa_models::timeslots::get_latest_block_slot_at_timestamp(
config.thread_count,
config.t0,
config.genesis_timestamp,
timestamp,
) {
Ok(opts) => Ok(opts.unwrap()),
Err(_) => Err(ExecutionError::InitVestingError(format!(
"can no get the slot at timestamp : {}",
timestamp
))),
}
};

for v in hashmap.values_mut() {
*v = v
.windows(2)
.map(|elements| {
let (mut prev, next) = (elements[0], elements[1]);
let end_slot =
next.start_slot
.get_prev_slot(config.thread_count)
.map_err(|e| {
ExecutionError::InitVestingError(format!(
"error on get prev slot for init vesting : {}",
e
))
})?;
prev.end_slot = end_slot;
Ok(prev)
})
.collect::<Result<Vec<VestingRange>, ExecutionError>>()?;
if v.len().eq(&1) {
return Err(ExecutionError::InitVestingError(
"vesting file should has more one element".to_string(),
));
} else {
*v = v
.windows(2)
.map(|elements| {
let (mut prev, next) = (elements[0], elements[1]);

// retrieve the start_slot
if prev.timestamp.eq(&MassaTime::from(0)) {
// first range with timestamp = 0
prev.start_slot = Slot::min();
} else {
prev.start_slot = get_slot_at_timestamp(config, prev.timestamp)?;
}

// retrieve the end_slot
let next_range_slot = get_slot_at_timestamp(config, next.timestamp)?;
prev.end_slot = next_range_slot.get_prev_slot(config.thread_count)?;

Ok(prev)
})
.collect::<Result<Vec<VestingRange>, ExecutionError>>()?;
}
}

Ok(hashmap)
Expand Down
34 changes: 10 additions & 24 deletions massa-execution-worker/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use massa_models::{
use massa_pos_exports::SelectorConfig;
use massa_pos_worker::start_selector_worker;
use massa_signature::KeyPair;
use massa_time::MassaTime;
use parking_lot::RwLock;
use std::str::FromStr;
use std::{
Expand Down Expand Up @@ -174,40 +175,25 @@ pub fn get_initials_vesting(with_value: bool) -> NamedTempFile {

if with_value {
let vesting1 = VestingRange {
start_slot: Slot {
period: 1,
thread: 0,
},
end_slot: Slot {
period: 0,
thread: 0,
},
start_slot: Slot::min(),
end_slot: Slot::min(),
timestamp: MassaTime::from(0),
min_balance: Amount::from_str("200000").unwrap(),
max_rolls: 150,
};

let vesting2 = VestingRange {
start_slot: Slot {
period: 40,
thread: 0,
},
end_slot: Slot {
period: 0,
thread: 0,
},
start_slot: Slot::min(),
end_slot: Slot::min(),
timestamp: MassaTime::from(1678457600000),
min_balance: Amount::from_str("150000").unwrap(),
max_rolls: 130,
};

let vesting3 = VestingRange {
start_slot: Slot {
period: 500,
thread: 0,
},
end_slot: Slot {
period: 0,
thread: 0,
},
start_slot: Slot::min(),
end_slot: Slot::min(),
timestamp: MassaTime::from(1679321600000),
min_balance: Amount::from_str("80000").unwrap(),
max_rolls: 80,
};
Expand Down
4 changes: 2 additions & 2 deletions massa-models/src/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ impl Slot {
}

/// returns the maximal slot
pub const fn max() -> Slot {
pub const fn max(thread_count: u8) -> Slot {
Slot {
period: u64::MAX,
thread: u8::MAX,
thread: thread_count,
}
}

Expand Down
15 changes: 7 additions & 8 deletions massa-models/src/vesting_range.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
use crate::amount::Amount;
use crate::slot::Slot;
use massa_time::MassaTime;
use serde::{Deserialize, Serialize};

/// Represent a vesting range
#[derive(Clone, Copy, Deserialize, Serialize, Debug)]
pub struct VestingRange {
/// start slot of range
/// use "slot" field in the initial_vesting.json file
#[serde(rename(deserialize = "slot", serialize = "slot"))]
#[serde(default = "Slot::min")]
#[serde(skip_serializing)]
pub start_slot: Slot,

/// end slot for the range
/// Init with 0,0 and calculate on load
#[serde(default = "init_end_slot_range")]
#[serde(default = "Slot::min")]
#[serde(skip_serializing)]
pub end_slot: Slot,

/// timestamp to get the start slot
pub timestamp: MassaTime,

/// minimal balance for specific range
pub min_balance: Amount,

/// max rolls for specific range
pub max_rolls: u64,
}

/// init the end_slot on startup
fn init_end_slot_range() -> Slot {
Slot::new(0, 0)
}
50 changes: 22 additions & 28 deletions massa-node/base_config/initial_vesting.json
Original file line number Diff line number Diff line change
@@ -1,62 +1,56 @@
{
"AU1LQrXPJ3DVL8SFRqACk31E9MVxBcmCATFiRdpEmgztGxWAx48D": [
{
"slot": {
"period": 0,
"thread": 0
},
"timestamp": 0,
"min_balance": "100000",
"max_rolls": 40
},
{
"timestamp": 1678025600000,
"min_balance": "100000",
"max_rolls": 45
},
{
"slot": {
"period": 27000,
"thread": 0
},
"timestamp": 1678457600000,
"min_balance": "50000",
"max_rolls": 50
},
{
"slot": {
"period": 54000,
"thread": 0
},
"timestamp": 1679321600000,
"min_balance": "0",
"max_rolls": 18446744073709551615
}
],
"AU1DHJY6zd6oKJPos8gQ6KYqmsTR669wes4ZhttLD9gE7PYUF3Rs": [
{
"slot": {
"period": 0,
"thread": 0
},
"timestamp": 0,
"min_balance": "100000",
"max_rolls": 5
},
{
"timestamp": 1678457600000,
"min_balance": "100000",
"max_rolls": 10
},
{
"slot": {
"period": 27000,
"thread": 0
},
"timestamp": 1679321600000,
"min_balance": "0",
"max_rolls": 18446744073709551615
}
],
"AU12spryWgt7VUoT9LRi8Roe5WVgUPLeaQ6GXMd9ABUQyfv62Ab18": [
{
"slot": {
"period": 0,
"thread": 0
},
"timestamp": 0,
"min_balance": "1000000000",
"max_rolls": 0
},
{
"timestamp": 1679321600000,
"min_balance": "1000000000",
"max_rolls": 0
},
{
"slot": {
"period": 1000000,
"thread": 0
},
"timestamp": 1692537200000,
"min_balance": "1000000000",
"max_rolls": 0
}
Expand Down