-
Notifications
You must be signed in to change notification settings - Fork 2
/
stake_instruction.c
585 lines (479 loc) · 22.4 KB
/
stake_instruction.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#include "common_byte_strings.h"
#include "instruction.h"
#include "sol/parser.h"
#include "sol/transaction_summary.h"
#include "stake_instruction.h"
#include "util.h"
#include <stdbool.h>
#include <string.h>
const Pubkey stake_program_id = {{PROGRAM_ID_STAKE}};
static int parse_stake_instruction_kind(Parser* parser, enum StakeInstructionKind* kind) {
uint32_t maybe_kind;
BAIL_IF(parse_u32(parser, &maybe_kind));
switch (maybe_kind) {
case StakeInitialize:
case StakeAuthorize:
case StakeDelegate:
case StakeSplit:
case StakeWithdraw:
case StakeDeactivate:
case StakeSetLockup:
case StakeMerge:
case StakeAuthorizeWithSeed:
case StakeInitializeChecked:
case StakeAuthorizeChecked:
case StakeAuthorizeCheckedWithSeed:
case StakeSetLockupChecked:
*kind = (enum StakeInstructionKind) maybe_kind;
return 0;
}
return 1;
}
static int parse_stake_authorize(Parser* parser, enum StakeAuthorize* authorize) {
uint32_t maybe_authorize;
BAIL_IF(parse_u32(parser, &maybe_authorize));
switch (maybe_authorize) {
case StakeAuthorizeStaker:
case StakeAuthorizeWithdrawer:
*authorize = (enum StakeAuthorize) maybe_authorize;
return 0;
}
return 1;
}
// Returns 0 and populates StakeDelegateInfo if provided a MessageHeader
// and a delegate instruction, otherwise non-zero.
static int parse_delegate_stake_instruction(const Instruction* instruction,
const MessageHeader* header,
StakeDelegateInfo* info) {
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->stake_pubkey));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->vote_pubkey));
// Skip clock sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
// Skip stake history sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
// Skip stake config account
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->authorized_pubkey));
return 0;
}
static int parse_stake_initialize_instruction(Parser* parser,
const Instruction* instruction,
const MessageHeader* header,
StakeInitializeInfo* info) {
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->account));
// Skip rent sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
BAIL_IF(parse_pubkey(parser, &info->stake_authority));
BAIL_IF(parse_pubkey(parser, &info->withdraw_authority));
// Lockup
BAIL_IF(parse_i64(parser, &info->lockup.unix_timestamp));
BAIL_IF(parse_u64(parser, &info->lockup.epoch));
BAIL_IF(parse_pubkey(parser, &info->lockup.custodian));
info->lockup.present = StakeLockupHasAll;
return 0;
}
static int parse_stake_initialize_checked_instruction(Parser* parser,
const Instruction* instruction,
const MessageHeader* header,
StakeInitializeInfo* info) {
UNUSED(parser);
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->account));
// Skip rent sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->stake_authority));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->withdraw_authority));
// No lockup on checked instructions
info->lockup.present = StakeLockupHasNone;
return 0;
}
static int parse_stake_withdraw_instruction(Parser* parser,
const Instruction* instruction,
const MessageHeader* header,
StakeWithdrawInfo* info) {
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->account));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->to));
// Skip clock sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
// Skip stake history sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->authority));
BAIL_IF(parse_u64(parser, &info->lamports));
return 0;
}
static int parse_stake_authorize_instruction(Parser* parser,
const Instruction* instruction,
const MessageHeader* header,
StakeAuthorizeInfo* info) {
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->account));
// Skip clock sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->authority));
// Custodian is optional, don't BAIL_IF()
instruction_accounts_iterator_next(&it, &info->custodian);
BAIL_IF(parse_pubkey(parser, &info->new_authority));
BAIL_IF(parse_stake_authorize(parser, &info->authorize));
return 0;
}
static int parse_stake_authorize_checked_instruction(Parser* parser,
const Instruction* instruction,
const MessageHeader* header,
StakeAuthorizeInfo* info) {
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->account));
// Skip clock sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->authority));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->new_authority));
// Custodian is optional, don't BAIL_IF()
instruction_accounts_iterator_next(&it, &info->custodian);
BAIL_IF(parse_stake_authorize(parser, &info->authorize));
return 0;
}
static int parse_stake_deactivate_instruction(Parser* parser,
const Instruction* instruction,
const MessageHeader* header,
StakeDeactivateInfo* info) {
UNUSED(parser);
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->account));
// Skip clock sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->authority));
return 0;
}
static int parse_stake_lockupargs(Parser* parser, StakeLockup* lockup, bool parse_custodian) {
// LockupArgs
enum StakeLockupPresent present = StakeLockupHasNone;
enum Option option;
BAIL_IF(parse_option(parser, &option));
if (option == OptionSome) {
BAIL_IF(parse_i64(parser, &lockup->unix_timestamp));
present |= StakeLockupHasTimestamp;
}
BAIL_IF(parse_option(parser, &option));
if (option == OptionSome) {
BAIL_IF(parse_u64(parser, &lockup->epoch));
present |= StakeLockupHasEpoch;
}
if (parse_custodian) {
BAIL_IF(parse_option(parser, &option));
if (option == OptionSome) {
BAIL_IF(parse_pubkey(parser, &lockup->custodian));
present |= StakeLockupHasCustodian;
}
}
lockup->present = present;
return 0;
}
static int parse_stake_set_lockup_instruction(Parser* parser,
const Instruction* instruction,
const MessageHeader* header,
StakeSetLockupInfo* info) {
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->account));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->custodian));
BAIL_IF(parse_stake_lockupargs(parser, &info->lockup, true));
return 0;
}
static int parse_stake_set_lockup_checked_instruction(Parser* parser,
const Instruction* instruction,
const MessageHeader* header,
StakeSetLockupInfo* info) {
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->account));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->custodian));
BAIL_IF(parse_stake_lockupargs(parser, &info->lockup, false));
// Custodian is optional
if (instruction_accounts_iterator_next(&it, &info->lockup.custodian) == 0) {
info->lockup.present = info->lockup.present | StakeLockupHasCustodian;
}
return 0;
}
static int parse_stake_split_instruction(Parser* parser,
const Instruction* instruction,
const MessageHeader* header,
StakeSplitInfo* info) {
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->account));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->split_account));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->authority));
BAIL_IF(parse_u64(parser, &info->lamports));
return 0;
}
static int parse_stake_merge_instruction(const Instruction* instruction,
const MessageHeader* header,
StakeMergeInfo* info) {
InstructionAccountsIterator it;
instruction_accounts_iterator_init(&it, header, instruction);
BAIL_IF(instruction_accounts_iterator_next(&it, &info->destination));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->source));
// Skip clock sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
// Skip stake history sysvar
BAIL_IF(instruction_accounts_iterator_next(&it, NULL));
BAIL_IF(instruction_accounts_iterator_next(&it, &info->authority));
return 0;
}
int parse_stake_instructions(const Instruction* instruction,
const MessageHeader* header,
StakeInfo* info) {
Parser parser = {instruction->data, instruction->data_length};
BAIL_IF(parse_stake_instruction_kind(&parser, &info->kind));
switch (info->kind) {
case StakeDelegate:
return parse_delegate_stake_instruction(instruction, header, &info->delegate_stake);
case StakeInitialize:
return parse_stake_initialize_instruction(&parser,
instruction,
header,
&info->initialize);
case StakeInitializeChecked:
return parse_stake_initialize_checked_instruction(&parser,
instruction,
header,
&info->initialize);
case StakeWithdraw:
return parse_stake_withdraw_instruction(&parser, instruction, header, &info->withdraw);
case StakeAuthorize:
return parse_stake_authorize_instruction(&parser,
instruction,
header,
&info->authorize);
case StakeAuthorizeChecked:
return parse_stake_authorize_checked_instruction(&parser,
instruction,
header,
&info->authorize);
case StakeDeactivate:
return parse_stake_deactivate_instruction(&parser,
instruction,
header,
&info->deactivate);
case StakeSetLockup:
return parse_stake_set_lockup_instruction(&parser,
instruction,
header,
&info->set_lockup);
case StakeSetLockupChecked:
return parse_stake_set_lockup_checked_instruction(&parser,
instruction,
header,
&info->set_lockup);
case StakeSplit:
return parse_stake_split_instruction(&parser, instruction, header, &info->split);
case StakeMerge:
return parse_stake_merge_instruction(instruction, header, &info->merge);
// Unsupported instructions
case StakeAuthorizeWithSeed:
case StakeAuthorizeCheckedWithSeed:
break;
}
return 1;
}
int print_delegate_stake_info(const char* primary_title,
const StakeDelegateInfo* info,
const PrintConfig* print_config) {
SummaryItem* item;
if (primary_title != NULL) {
item = transaction_summary_primary_item();
summary_item_set_pubkey(item, primary_title, info->stake_pubkey);
}
if (print_config_show_authority(print_config, info->authorized_pubkey)) {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Authorized by", info->authorized_pubkey);
}
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Vote account", info->vote_pubkey);
return 0;
}
static int print_stake_withdraw_info(const StakeWithdrawInfo* info,
const PrintConfig* print_config) {
SummaryItem* item;
item = transaction_summary_primary_item();
summary_item_set_amount(item, "Stake withdraw", info->lamports);
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "From", info->account);
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "To", info->to);
if (print_config_show_authority(print_config, info->authority)) {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Authorized by", info->authority);
}
return 0;
}
static int print_stake_authorize_info(const StakeAuthorizeInfo* info,
const PrintConfig* print_config) {
const char* new_authority_title = NULL;
SummaryItem* item;
item = transaction_summary_primary_item();
summary_item_set_pubkey(item, "Set stake auth", info->account);
switch (info->authorize) {
case StakeAuthorizeStaker:
new_authority_title = "New stake auth";
break;
case StakeAuthorizeWithdrawer:
new_authority_title = "New withdraw auth";
break;
}
item = transaction_summary_general_item();
summary_item_set_pubkey(item, new_authority_title, info->new_authority);
if (print_config_show_authority(print_config, info->authority)) {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Authorized by", info->authority);
}
if (info->custodian && print_config_show_authority(print_config, info->custodian)) {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Custodian", info->custodian);
}
return 0;
}
static int print_stake_deactivate_info(const StakeDeactivateInfo* info,
const PrintConfig* print_config) {
SummaryItem* item;
item = transaction_summary_primary_item();
summary_item_set_pubkey(item, "Deactivate stake", info->account);
if (print_config_show_authority(print_config, info->authority)) {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Authorized by", info->authority);
}
return 0;
}
static int print_stake_set_lockup_info(const StakeSetLockupInfo* info,
const PrintConfig* print_config) {
SummaryItem* item;
item = transaction_summary_primary_item();
summary_item_set_pubkey(item, "Set lockup", info->account);
enum StakeLockupPresent present = info->lockup.present;
if (present & StakeLockupHasTimestamp) {
item = transaction_summary_general_item();
summary_item_set_timestamp(item, "Time", info->lockup.unix_timestamp);
}
if (present & StakeLockupHasEpoch) {
item = transaction_summary_general_item();
summary_item_set_u64(item, "Epoch", info->lockup.epoch);
}
if (present & StakeLockupHasCustodian) {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "New authority", info->lockup.custodian);
}
if (print_config_show_authority(print_config, info->custodian)) {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Authorized by", info->custodian);
}
return 0;
}
static int print_stake_split_info(const StakeSplitInfo* info, const PrintConfig* print_config) {
BAIL_IF(print_stake_split_info1(info, print_config));
return print_stake_split_info2(info, print_config);
}
static int print_stake_merge_info(const StakeMergeInfo* info, const PrintConfig* print_config) {
SummaryItem* item;
item = transaction_summary_primary_item();
summary_item_set_pubkey(item, "Merge", info->source);
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Into", info->destination);
if (print_config_show_authority(print_config, info->authority)) {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Authorized by", info->authority);
}
return 0;
}
int print_stake_info(const StakeInfo* info, const PrintConfig* print_config) {
switch (info->kind) {
case StakeDelegate:
return print_delegate_stake_info("Delegate from", &info->delegate_stake, print_config);
case StakeInitialize:
case StakeInitializeChecked:
return print_stake_initialize_info("Init stake acct", &info->initialize, print_config);
case StakeWithdraw:
return print_stake_withdraw_info(&info->withdraw, print_config);
case StakeAuthorize:
case StakeAuthorizeChecked:
return print_stake_authorize_info(&info->authorize, print_config);
case StakeDeactivate:
return print_stake_deactivate_info(&info->deactivate, print_config);
case StakeSetLockup:
case StakeSetLockupChecked:
return print_stake_set_lockup_info(&info->set_lockup, print_config);
case StakeSplit:
return print_stake_split_info(&info->split, print_config);
case StakeMerge:
return print_stake_merge_info(&info->merge, print_config);
// Unsupported instructions
case StakeAuthorizeWithSeed:
case StakeAuthorizeCheckedWithSeed:
break;
}
return 1;
}
int print_stake_initialize_info(const char* primary_title,
const StakeInitializeInfo* info,
const PrintConfig* print_config) {
SummaryItem* item;
bool one_authority = pubkeys_equal(info->withdraw_authority, info->stake_authority);
if (primary_title != NULL) {
item = transaction_summary_primary_item();
summary_item_set_pubkey(item, primary_title, info->account);
}
if (one_authority) {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "New authority", info->stake_authority);
} else {
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "New stake auth", info->stake_authority);
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "New withdraw auth", info->withdraw_authority);
}
int64_t lockup_time = info->lockup.unix_timestamp;
uint64_t lockup_epoch = info->lockup.epoch;
if (lockup_time > 0 || lockup_epoch > 0) {
if (lockup_time > 0) {
item = transaction_summary_general_item();
summary_item_set_timestamp(item, "Lockup time", lockup_time);
}
if (lockup_epoch > 0) {
item = transaction_summary_general_item();
summary_item_set_u64(item, "Lockup epoch", lockup_epoch);
}
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Lockup authority", info->lockup.custodian);
} else if (print_config->expert_mode) {
item = transaction_summary_general_item();
summary_item_set_string(item, "Lockup", "None");
}
return 0;
}
int print_stake_split_info1(const StakeSplitInfo* info, const PrintConfig* print_config) {
UNUSED(print_config);
SummaryItem* item;
item = transaction_summary_primary_item();
summary_item_set_amount(item, "Split stake", info->lamports);
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "From", info->account);
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "To", info->split_account);
return 0;
}
int print_stake_split_info2(const StakeSplitInfo* info, const PrintConfig* print_config) {
if (print_config_show_authority(print_config, info->authority)) {
SummaryItem* item;
item = transaction_summary_general_item();
summary_item_set_pubkey(item, "Authorized by", info->authority);
}
return 0;
}