Skip to content

Commit

Permalink
Safer creation of vesting-schedules
Browse files Browse the repository at this point in the history
Since latest substrate (polkadot v0.9.12) the vesting pallet
supports multiple schedules and also checks if inserted
schedules are valid (i.e. perBlock != 0 and numBlocks != 0).

In order to catch such errors before we insert into chain, these
changes here do these checks off-chain beforehand.
  • Loading branch information
mustermeiszer committed Dec 22, 2021
1 parent f85a024 commit c0734ea
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/plugins/migration/src/migration/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,21 @@ async function transformVestingVestingInfo(fromApi: ApiPromise, toApi: ApiPromis
// * (locked/per_block): Number blocks on mainnet overall
// * snapshot_block - starting_block: Number of vested blocks
// * subtraction of the above two: How many blocks remain
// * Division by two: Take into account 12s block time
let remainingBlocks = (blockPeriodOldVesting - blocksPassedSinceVestingStart) / BigInt(2);
let remainingBlocks = (blockPeriodOldVesting - blocksPassedSinceVestingStart);
// This defines the remaining locked amount. Same as if a person has called vest once at the snapshot block.
remainingLocked = old.locked.toBigInt() - (blocksPassedSinceVestingStart * old.perBlock.toBigInt());
newPerBlock = remainingLocked / remainingBlocks;
// Ensure remaining locked is greater zero
if (remainingLocked === BigInt(0)) {
remainingLocked = BigInt(1);
}
// * Multiplication by two: Take into account 12s block time
newPerBlock = (remainingLocked / remainingBlocks) * BigInt(2);
// Ensure remaining locked is greater zero
// If we are here, this must be checked manually...
if (newPerBlock === BigInt(0)) {
const info = toApi.createType("VestingInfo", [remainingLocked, newPerBlock, atTo]);
throw Error("Invalid vesting schedule. \nStorageKey: " + completeKey.toHex() + "\n VestingInfo: " + info.toHuman());
}
newStartingBlock = atTo;

} else if ((blockPeriodOldVesting - blocksPassedSinceVestingStart) <= 0 ) {
Expand Down

0 comments on commit c0734ea

Please sign in to comment.