This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathunlock.ts
102 lines (95 loc) · 3.17 KB
/
unlock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { CommandExecuteContext } from '../../../state_machine';
import { BaseCommand } from '../../base_command';
import { EMPTY_KEY, MODULE_NAME_POS } from '../constants';
import { ValidatorStore } from '../stores/validator';
import { GenesisDataStore } from '../stores/genesis';
import { StakerStore } from '../stores/staker';
import {
TokenMethod,
TokenID,
UnlockCommandDependencies,
PunishmentLockingPeriods,
} from '../types';
import { isPunished, isCertificateGenerated, hasWaited } from '../utils';
export class UnlockCommand extends BaseCommand {
private _tokenMethod!: TokenMethod;
private _posTokenID!: TokenID;
private _roundLength!: number;
private _punishmentLockingPeriods!: PunishmentLockingPeriods;
public addDependencies(args: UnlockCommandDependencies) {
this._tokenMethod = args.tokenMethod;
}
public init(args: {
posTokenID: TokenID;
roundLength: number;
punishmentLockingPeriods: PunishmentLockingPeriods;
}) {
this._posTokenID = args.posTokenID;
this._roundLength = args.roundLength;
this._punishmentLockingPeriods = args.punishmentLockingPeriods;
}
public async execute(context: CommandExecuteContext): Promise<void> {
const {
transaction: { senderAddress },
getMethodContext,
header: { height, aggregateCommit },
} = context;
const validatorSubstore = this.stores.get(ValidatorStore);
const stakerSubstore = this.stores.get(StakerStore);
const stakerData = await stakerSubstore.get(context, senderAddress);
const ineligibleUnlocks = [];
const genesisDataStore = this.stores.get(GenesisDataStore);
const genesisData = await genesisDataStore.get(context, EMPTY_KEY);
const { height: genesisHeight } = genesisData;
for (const unlockObject of stakerData.pendingUnlocks) {
const { reportMisbehaviorHeights } = await validatorSubstore.get(
context,
unlockObject.validatorAddress,
);
if (
hasWaited(unlockObject, senderAddress, height, this._punishmentLockingPeriods) &&
!isPunished(
unlockObject,
reportMisbehaviorHeights,
senderAddress,
height,
this._punishmentLockingPeriods,
) &&
isCertificateGenerated({
unlockObject,
genesisHeight,
maxHeightCertified: aggregateCommit.height,
roundLength: this._roundLength,
})
) {
await this._tokenMethod.unlock(
getMethodContext(),
senderAddress,
MODULE_NAME_POS,
this._posTokenID,
unlockObject.amount,
);
continue;
}
ineligibleUnlocks.push(unlockObject);
}
if (stakerData.pendingUnlocks.length === ineligibleUnlocks.length) {
throw new Error('No eligible staker data was found for unlocking');
}
stakerData.pendingUnlocks = ineligibleUnlocks;
await stakerSubstore.set(context, senderAddress, stakerData);
}
}