-
Notifications
You must be signed in to change notification settings - Fork 647
/
pox_CommandModel.ts
212 lines (186 loc) · 7.34 KB
/
pox_CommandModel.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import fc from "fast-check";
import { Simnet } from "@hirosystems/clarinet-sdk";
import {
ClarityValue,
cvToValue,
StacksPrivateKey,
} from "@stacks/transactions";
import { StackingClient } from "@stacks/stacking";
import {
FIRST_BURNCHAIN_BLOCK_HEIGHT,
REWARD_CYCLE_LENGTH,
} from "./pox_Commands";
export type StxAddress = string;
export type BtcAddress = string;
export type CommandTag = string;
export class Stub {
readonly wallets: Map<StxAddress, Wallet>;
readonly statistics: Map<string, number>;
readonly stackers: Map<StxAddress, Stacker>;
stackingMinimum: number;
nextRewardSetIndex: number;
lastRefreshedCycle: number;
burnBlockHeight: number;
constructor(
wallets: Map<StxAddress, Wallet>,
stackers: Map<StxAddress, Stacker>,
statistics: Map<CommandTag, number>,
) {
this.wallets = wallets;
this.statistics = statistics;
this.stackers = stackers;
this.stackingMinimum = 0;
this.nextRewardSetIndex = 0;
this.lastRefreshedCycle = 0;
this.burnBlockHeight = 0;
}
trackCommandRun(commandName: string) {
const count = this.statistics.get(commandName) || 0;
this.statistics.set(commandName, count + 1);
}
reportCommandRuns() {
console.log("Command run method execution counts:");
this.statistics.forEach((count, commandName) => {
console.log(`${commandName}: ${count}`);
});
}
refreshStateForNextRewardCycle(real: Real) {
const burnBlockHeightResult = real.network.runSnippet("burn-block-height");
const burnBlockHeight = Number(
cvToValue(burnBlockHeightResult as ClarityValue),
);
const lastRefreshedCycle = this.lastRefreshedCycle;
const currentRewCycle = Math.floor(
(Number(burnBlockHeight) - FIRST_BURNCHAIN_BLOCK_HEIGHT) /
REWARD_CYCLE_LENGTH,
);
// The `this.burnBlockHeight` instance member is used for logging purposes.
// However, it's not used in the actual implementation of the model and all
// usages below use the `burnBlockHeight` local variable.
this.burnBlockHeight = burnBlockHeight;
if (lastRefreshedCycle < currentRewCycle) {
this.nextRewardSetIndex = 0;
this.wallets.forEach((w) => {
let updatedAmountToCommit = 0;
const wallet = this.stackers.get(w.stxAddress)!;
// Get the wallet's ex-delegators by comparing their delegatedUntilBurnHt
// to the current burn block height (only if the wallet is a delegatee).
const expiredDelegators = wallet.poolMembers.filter((stackerAddress) =>
this.stackers.get(stackerAddress)!.delegatedUntilBurnHt <
burnBlockHeight
);
// Get the operator's pool stackers that no longer have partially commited
// STX for the next reward cycle by comparing their unlock height to
// the next reward cycle's first block (only if the wallet is an operator).
const stackersToRemoveAmountToCommit = wallet.lockedAddresses.filter((
stackerAddress,
) =>
this.stackers.get(stackerAddress)!.unlockHeight <=
burnBlockHeight + REWARD_CYCLE_LENGTH
);
// Get the operator's ex-pool stackers by comparing their unlockHeight to
// the current burn block height (only if the wallet is an operator).
const expiredStackers = wallet.lockedAddresses.filter(
(stackerAddress) =>
this.stackers.get(stackerAddress)!.unlockHeight <=
burnBlockHeight,
);
// For each remaining pool stacker (if any), increase the operator's
// amountToCommit (partial-stacked) for the next cycle by the
// stacker's amountLocked.
wallet.lockedAddresses.forEach((stacker) => {
const stackerWallet = this.stackers.get(stacker)!;
updatedAmountToCommit += stackerWallet?.amountLocked;
});
// Update the operator's amountToCommit (partial-stacked).
wallet.amountToCommit = updatedAmountToCommit;
// Remove the expired delegators from the delegatee's poolMembers list.
expiredDelegators.forEach((expDelegator) => {
const expDelegatorIndex = wallet.poolMembers.indexOf(expDelegator);
wallet.poolMembers.splice(expDelegatorIndex, 1);
});
// Remove the expired stackers from the operator's lockedAddresses list.
expiredStackers.forEach((expStacker) => {
const expStackerIndex = wallet.lockedAddresses.indexOf(expStacker);
wallet.lockedAddresses.splice(expStackerIndex, 1);
});
// For each pool stacker that no longer have partially commited STX for
// the next reward cycle, decrement the operator's amountToCommit
// (partial-stacked) by the stacker's amountLocked.
stackersToRemoveAmountToCommit.forEach((expStacker) => {
const expStackerWallet = this.stackers.get(expStacker)!;
wallet.amountToCommit -= expStackerWallet.amountLocked;
});
// Check the wallet's stack expiry and update the state accordingly.
if (
wallet.unlockHeight > 0 && wallet.unlockHeight <= burnBlockHeight
) {
wallet.isStacking = false;
wallet.isStackingSolo = false;
wallet.amountUnlocked += wallet.amountLocked;
wallet.amountLocked = 0;
wallet.unlockHeight = 0;
wallet.firstLockedRewardCycle = 0;
} // If the wallet is solo stacking and its stack won't expire in the
// next reward cycle, increment the model's nextRewardSetIndex (the
// next empty reward slot)
else if (
wallet.unlockHeight > 0 &&
wallet.unlockHeight > burnBlockHeight + REWARD_CYCLE_LENGTH &&
wallet.isStackingSolo
) {
this.nextRewardSetIndex++;
}
wallet.committedRewCycleIndexes = [];
});
this.lastRefreshedCycle = currentRewCycle;
}
}
}
export type Real = {
network: Simnet;
};
export type Wallet = {
label: string;
stxAddress: string;
btcAddress: string;
signerPrvKey: StacksPrivateKey;
signerPubKey: string;
stackingClient: StackingClient;
};
export type Stacker = {
ustxBalance: number;
isStacking: boolean;
isStackingSolo: boolean;
hasDelegated: boolean;
lockedAddresses: StxAddress[];
amountToCommit: number;
poolMembers: StxAddress[];
delegatedTo: StxAddress;
delegatedMaxAmount: number;
delegatedUntilBurnHt: number;
delegatedPoxAddress: BtcAddress;
amountLocked: number;
amountUnlocked: number;
unlockHeight: number;
firstLockedRewardCycle: number;
allowedContractCaller: StxAddress;
callerAllowedBy: StxAddress[];
committedRewCycleIndexes: number[];
};
export type PoxCommand = fc.Command<Stub, Real>;
export const logCommand = (...items: (string | undefined)[]) => {
// Ensure we only render up to the first 10 items for brevity.
const renderItems = items.slice(0, 10);
const columnWidth = 23; // Standard width for each column after the first two.
const halfColumns = Math.floor(columnWidth / 2);
// Pad columns to their widths: half for the first two, full for the rest.
const prettyPrint = renderItems.map((content, index) =>
// Check if the index is less than 2 (i.e., first two items).
content
? (index < 2 ? content.padEnd(halfColumns) : content.padEnd(columnWidth))
: (index < 2 ? "".padEnd(halfColumns) : "".padEnd(columnWidth))
);
prettyPrint.push("\n");
process.stdout.write(prettyPrint.join(""));
};