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

feat: deprecating schedules array in favour of single schedule #4

Merged
merged 7 commits into from
Sep 25, 2024

Conversation

wei3erHase
Copy link
Member

@wei3erHase wei3erHase commented Sep 19, 2024

The original codebase https://github.com/Bonfida/token-vesting supports an array of Dates to define a Vesting Contract. In that way, it can say: "unlock 10 at May 10, 10 at May 20, ..." and so on.

The intended behaviour is to have only 1 Date per vesting / lockup, this PR replaces the Schedules<> array for a single Schedule, and deprecates the variable number_of_schedules

Comment on lines 90 to 97

pub fn unpack_schedules(input: &[u8]) -> Result<Vec<VestingSchedule>, ProgramError> {
let number_of_schedules = input.len() / VestingSchedule::LEN;
let mut output: Vec<VestingSchedule> = Vec::with_capacity(number_of_schedules);
let mut offset = 0;
for _ in 0..number_of_schedules {
output.push(VestingSchedule::unpack_from_slice(
&input[offset..offset + VestingSchedule::LEN],
)?);
offset += VestingSchedule::LEN;
}
pub fn unpack_schedule(input: &[u8]) -> Result<VestingSchedule, ProgramError> {
let offset = 0;
let output: VestingSchedule = VestingSchedule::unpack_from_slice(
&input[offset..offset + VestingSchedule::LEN],
)?;
Ok(output)
}
Copy link
Member

@0xng 0xng Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offset seems unnecessary now, can you do something like this?

pub fn unpack_schedule(input: &[u8]) -> Result<VestingSchedule, ProgramError> {
    VestingSchedule::unpack_from_slice(&input[..VestingSchedule::LEN])
}

the validation of the len happens inside the unpack_from_slice function, i cant' comment on it now, but it does src < 16 and 16 is the LEN, I recommend using the LEN const there to avoid magic numbers. same happens in this function but when used with the Header

Comment on lines 99 to 102
pub fn pack_schedule_into_slice(schedule: VestingSchedule, target: &mut [u8]) {
let offset = 0;
schedule.pack_into_slice(&mut target[offset..]);
}
Copy link
Member

@0xng 0xng Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is similar to the other one, some validation perhaps can be added so the packing doesn't fail (although the validation may happen in the pack_into_slice fn), and the offset is 0 so it can be removed and simplified. target is already a mutable reference, so not sure if you need to cast it without the offset, but not 100% certain as the mutable reference will transform the original variable.

pub fn pack_schedule_into_slice(schedule: VestingSchedule, target: &mut [u8]) {
    schedule.pack_into_slice(target);
}

either way, proper validation is the main thing i would check for. for example, it seems the pack_into_slice function requires 16 bytes, but we have no guarantee that target fits the required length.

edit: this seems to be somewhat checked in another file which seems to be the main function, either way, i will leave the comment just in case

release_time: schedule.release_time,
amount: schedule.amount,
};
state_schedule.pack_into_slice(&mut data[offset..]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think it would be more explicit to remove offset variable and just use VettingScheduleHeader::LEN or rename offset to header_offset so it's clear this starts packing what's after the header?

@wei3erHase wei3erHase marked this pull request as ready for review September 25, 2024 14:01
@wei3erHase wei3erHase merged commit abec526 into dev Sep 25, 2024
2 checks passed
@wei3erHase wei3erHase deleted the feat/single-schedule branch September 25, 2024 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants