solana_cost_model/
cost_model.rs

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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//! 'cost_model` provides service to estimate a transaction's cost
//! following proposed fee schedule #16984; Relevant cluster cost
//! measuring is described by #19627
//!
//! The main function is `calculate_cost` which returns &TransactionCost.
//!

use {
    crate::{block_cost_limits::*, transaction_cost::*},
    solana_builtins_default_costs::get_builtin_instruction_cost,
    solana_compute_budget::compute_budget_limits::{
        DEFAULT_HEAP_COST, DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT, MAX_COMPUTE_UNIT_LIMIT,
    },
    solana_feature_set::{self as feature_set, FeatureSet},
    solana_runtime_transaction::instructions_processor::process_compute_budget_instructions,
    solana_sdk::{
        borsh1::try_from_slice_unchecked,
        compute_budget::{self, ComputeBudgetInstruction},
        fee::FeeStructure,
        instruction::CompiledInstruction,
        message::TransactionSignatureDetails,
        program_utils::limited_deserialize,
        pubkey::Pubkey,
        saturating_add_assign,
        system_instruction::{
            SystemInstruction, MAX_PERMITTED_ACCOUNTS_DATA_ALLOCATIONS_PER_TRANSACTION,
            MAX_PERMITTED_DATA_LENGTH,
        },
        system_program,
        transaction::SanitizedTransaction,
    },
    solana_svm_transaction::svm_message::SVMMessage,
};

pub struct CostModel;

#[derive(Debug, PartialEq)]
enum SystemProgramAccountAllocation {
    None,
    Some(u64),
    Failed,
}

impl CostModel {
    pub fn calculate_cost<'a>(
        transaction: &'a SanitizedTransaction,
        feature_set: &FeatureSet,
    ) -> TransactionCost<'a, SanitizedTransaction> {
        if transaction.is_simple_vote_transaction() {
            TransactionCost::SimpleVote { transaction }
        } else {
            let (signatures_count_detail, signature_cost) =
                Self::get_signature_cost(transaction, feature_set);
            let write_lock_cost = Self::get_write_lock_cost(transaction, feature_set);
            let (programs_execution_cost, loaded_accounts_data_size_cost, data_bytes_cost) =
                Self::get_transaction_cost(transaction, feature_set);
            let allocated_accounts_data_size =
                Self::calculate_allocated_accounts_data_size(transaction);

            let usage_cost_details = UsageCostDetails {
                transaction,
                signature_cost,
                write_lock_cost,
                data_bytes_cost,
                programs_execution_cost,
                loaded_accounts_data_size_cost,
                allocated_accounts_data_size,
                signature_details: signatures_count_detail,
            };

            TransactionCost::Transaction(usage_cost_details)
        }
    }

    // Calculate executed transaction CU cost, with actual execution and loaded accounts size
    // costs.
    pub fn calculate_cost_for_executed_transaction<'a>(
        transaction: &'a SanitizedTransaction,
        actual_programs_execution_cost: u64,
        actual_loaded_accounts_data_size_bytes: u32,
        feature_set: &FeatureSet,
    ) -> TransactionCost<'a, SanitizedTransaction> {
        if transaction.is_simple_vote_transaction() {
            TransactionCost::SimpleVote { transaction }
        } else {
            let (signatures_count_detail, signature_cost) =
                Self::get_signature_cost(transaction, feature_set);
            let write_lock_cost = Self::get_write_lock_cost(transaction, feature_set);

            let instructions_data_cost = Self::get_instructions_data_cost(transaction);
            let allocated_accounts_data_size =
                Self::calculate_allocated_accounts_data_size(transaction);

            let programs_execution_cost = actual_programs_execution_cost;
            let loaded_accounts_data_size_cost = Self::calculate_loaded_accounts_data_size_cost(
                actual_loaded_accounts_data_size_bytes,
                feature_set,
            );

            let usage_cost_details = UsageCostDetails {
                transaction,
                signature_cost,
                write_lock_cost,
                data_bytes_cost: instructions_data_cost,
                programs_execution_cost,
                loaded_accounts_data_size_cost,
                allocated_accounts_data_size,
                signature_details: signatures_count_detail,
            };

            TransactionCost::Transaction(usage_cost_details)
        }
    }

    /// Returns signature details and the total signature cost
    fn get_signature_cost(
        transaction: &SanitizedTransaction,
        feature_set: &FeatureSet,
    ) -> (TransactionSignatureDetails, u64) {
        let signatures_count_detail = transaction.message().get_signature_details();

        let ed25519_verify_cost =
            if feature_set.is_active(&feature_set::ed25519_precompile_verify_strict::id()) {
                ED25519_VERIFY_STRICT_COST
            } else {
                ED25519_VERIFY_COST
            };

        let signature_cost = signatures_count_detail
            .num_transaction_signatures()
            .saturating_mul(SIGNATURE_COST)
            .saturating_add(
                signatures_count_detail
                    .num_secp256k1_instruction_signatures()
                    .saturating_mul(SECP256K1_VERIFY_COST),
            )
            .saturating_add(
                signatures_count_detail
                    .num_ed25519_instruction_signatures()
                    .saturating_mul(ed25519_verify_cost),
            );

        (signatures_count_detail, signature_cost)
    }

    fn get_writable_accounts(message: &impl SVMMessage) -> impl Iterator<Item = &Pubkey> {
        message
            .account_keys()
            .iter()
            .enumerate()
            .filter_map(|(i, k)| message.is_writable(i).then_some(k))
    }

    /// Returns the total write-lock cost.
    fn get_write_lock_cost(transaction: &impl SVMMessage, feature_set: &FeatureSet) -> u64 {
        let num_write_locks =
            if feature_set.is_active(&feature_set::cost_model_requested_write_lock_cost::id()) {
                transaction.num_write_locks()
            } else {
                Self::get_writable_accounts(transaction).count() as u64
            };
        WRITE_LOCK_UNITS.saturating_mul(num_write_locks)
    }

    /// Return (programs_execution_cost, loaded_accounts_data_size_cost, data_bytes_cost)
    fn get_transaction_cost(
        transaction: &impl SVMMessage,
        feature_set: &FeatureSet,
    ) -> (u64, u64, u64) {
        if feature_set.is_active(&feature_set::reserve_minimal_cus_for_builtin_instructions::id()) {
            let data_bytes_cost = Self::get_instructions_data_cost(transaction);
            let (programs_execution_cost, loaded_accounts_data_size_cost) =
                Self::get_estimated_execution_cost(transaction, feature_set);
            (
                programs_execution_cost,
                loaded_accounts_data_size_cost,
                data_bytes_cost,
            )
        } else {
            Self::get_transaction_cost_without_minimal_builtin_cus(transaction, feature_set)
        }
    }

    fn get_transaction_cost_without_minimal_builtin_cus(
        transaction: &impl SVMMessage,
        feature_set: &FeatureSet,
    ) -> (u64, u64, u64) {
        let mut programs_execution_costs = 0u64;
        let mut loaded_accounts_data_size_cost = 0u64;
        let mut data_bytes_len_total = 0u64;
        let mut compute_unit_limit_is_set = false;
        let mut has_user_space_instructions = false;

        for (program_id, instruction) in transaction.program_instructions_iter() {
            let ix_execution_cost =
                if let Some(builtin_cost) = get_builtin_instruction_cost(program_id, feature_set) {
                    builtin_cost
                } else {
                    has_user_space_instructions = true;
                    u64::from(DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT)
                };

            programs_execution_costs = programs_execution_costs
                .saturating_add(ix_execution_cost)
                .min(u64::from(MAX_COMPUTE_UNIT_LIMIT));

            data_bytes_len_total =
                data_bytes_len_total.saturating_add(instruction.data.len() as u64);

            if compute_budget::check_id(program_id) {
                if let Ok(ComputeBudgetInstruction::SetComputeUnitLimit(_)) =
                    try_from_slice_unchecked(instruction.data)
                {
                    compute_unit_limit_is_set = true;
                }
            }
        }

        // if failed to process compute_budget instructions, the transaction will not be executed
        // by `bank`, therefore it should be considered as no execution cost by cost model.
        match process_compute_budget_instructions(
            transaction.program_instructions_iter(),
            feature_set,
        ) {
            Ok(compute_budget_limits) => {
                // if tx contained user-space instructions and a more accurate estimate available correct it,
                // where "user-space instructions" must be specifically checked by
                // 'compute_unit_limit_is_set' flag, because compute_budget does not distinguish
                // builtin and bpf instructions when calculating default compute-unit-limit. (see
                // compute_budget.rs test `test_process_mixed_instructions_without_compute_budget`)
                if has_user_space_instructions && compute_unit_limit_is_set {
                    programs_execution_costs = u64::from(compute_budget_limits.compute_unit_limit);
                }

                loaded_accounts_data_size_cost = Self::calculate_loaded_accounts_data_size_cost(
                    compute_budget_limits.loaded_accounts_bytes.get(),
                    feature_set,
                );
            }
            Err(_) => {
                programs_execution_costs = 0;
            }
        }

        (
            programs_execution_costs,
            loaded_accounts_data_size_cost,
            data_bytes_len_total / INSTRUCTION_DATA_BYTES_COST,
        )
    }

    /// Return (programs_execution_cost, loaded_accounts_data_size_cost)
    fn get_estimated_execution_cost(
        transaction: &impl SVMMessage,
        feature_set: &FeatureSet,
    ) -> (u64, u64) {
        // if failed to process compute_budget instructions, the transaction will not be executed
        // by `bank`, therefore it should be considered as no execution cost by cost model.
        let (programs_execution_costs, loaded_accounts_data_size_cost) =
            match process_compute_budget_instructions(
                transaction.program_instructions_iter(),
                feature_set,
            ) {
                Ok(compute_budget_limits) => (
                    u64::from(compute_budget_limits.compute_unit_limit),
                    Self::calculate_loaded_accounts_data_size_cost(
                        compute_budget_limits.loaded_accounts_bytes.get(),
                        feature_set,
                    ),
                ),
                Err(_) => (0, 0),
            };

        (programs_execution_costs, loaded_accounts_data_size_cost)
    }

    /// Return the instruction data bytes cost.
    fn get_instructions_data_cost(transaction: &impl SVMMessage) -> u64 {
        let ix_data_bytes_len_total: u64 = transaction
            .instructions_iter()
            .map(|instruction| instruction.data.len() as u64)
            .sum();

        ix_data_bytes_len_total / INSTRUCTION_DATA_BYTES_COST
    }

    pub fn calculate_loaded_accounts_data_size_cost(
        loaded_accounts_data_size: u32,
        _feature_set: &FeatureSet,
    ) -> u64 {
        FeeStructure::calculate_memory_usage_cost(loaded_accounts_data_size, DEFAULT_HEAP_COST)
    }

    fn calculate_account_data_size_on_deserialized_system_instruction(
        instruction: SystemInstruction,
    ) -> SystemProgramAccountAllocation {
        match instruction {
            SystemInstruction::CreateAccount { space, .. }
            | SystemInstruction::CreateAccountWithSeed { space, .. }
            | SystemInstruction::Allocate { space }
            | SystemInstruction::AllocateWithSeed { space, .. } => {
                if space > MAX_PERMITTED_DATA_LENGTH {
                    SystemProgramAccountAllocation::Failed
                } else {
                    SystemProgramAccountAllocation::Some(space)
                }
            }
            _ => SystemProgramAccountAllocation::None,
        }
    }

    fn calculate_account_data_size_on_instruction(
        program_id: &Pubkey,
        instruction: &CompiledInstruction,
    ) -> SystemProgramAccountAllocation {
        if program_id == &system_program::id() {
            if let Ok(instruction) = limited_deserialize(&instruction.data) {
                Self::calculate_account_data_size_on_deserialized_system_instruction(instruction)
            } else {
                SystemProgramAccountAllocation::Failed
            }
        } else {
            SystemProgramAccountAllocation::None
        }
    }

    /// eventually, potentially determine account data size of all writable accounts
    /// at the moment, calculate account data size of account creation
    fn calculate_allocated_accounts_data_size(transaction: &SanitizedTransaction) -> u64 {
        let mut tx_attempted_allocation_size: u64 = 0;
        for (program_id, instruction) in transaction.message().program_instructions_iter() {
            match Self::calculate_account_data_size_on_instruction(program_id, instruction) {
                SystemProgramAccountAllocation::Failed => {
                    // If any system program instructions can be statically
                    // determined to fail, no allocations will actually be
                    // persisted by the transaction. So return 0 here so that no
                    // account allocation budget is used for this failed
                    // transaction.
                    return 0;
                }
                SystemProgramAccountAllocation::None => continue,
                SystemProgramAccountAllocation::Some(ix_attempted_allocation_size) => {
                    saturating_add_assign!(
                        tx_attempted_allocation_size,
                        ix_attempted_allocation_size
                    );
                }
            }
        }

        // The runtime prevents transactions from allocating too much account
        // data so clamp the attempted allocation size to the max amount.
        //
        // Note that if there are any custom bpf instructions in the transaction
        // it's tricky to know whether a newly allocated account will be freed
        // or not during an intermediate instruction in the transaction so we
        // shouldn't assume that a large sum of allocations will necessarily
        // lead to transaction failure.
        (MAX_PERMITTED_ACCOUNTS_DATA_ALLOCATIONS_PER_TRANSACTION as u64)
            .min(tx_attempted_allocation_size)
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        itertools::Itertools,
        solana_compute_budget::compute_budget_limits::{
            DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT, MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT,
        },
        solana_sdk::{
            compute_budget::{self, ComputeBudgetInstruction},
            fee::ACCOUNT_DATA_COST_PAGE_SIZE,
            hash::Hash,
            instruction::{CompiledInstruction, Instruction},
            message::Message,
            signature::{Keypair, Signer},
            system_instruction::{self},
            system_program, system_transaction,
            transaction::Transaction,
        },
    };

    fn test_setup() -> (Keypair, Hash) {
        solana_logger::setup();
        (Keypair::new(), Hash::new_unique())
    }

    #[test]
    fn test_calculate_allocated_accounts_data_size_no_allocation() {
        let transaction = Transaction::new_unsigned(Message::new(
            &[system_instruction::transfer(
                &Pubkey::new_unique(),
                &Pubkey::new_unique(),
                1,
            )],
            Some(&Pubkey::new_unique()),
        ));
        let sanitized_tx = SanitizedTransaction::from_transaction_for_tests(transaction);

        assert_eq!(
            CostModel::calculate_allocated_accounts_data_size(&sanitized_tx),
            0
        );
    }

    #[test]
    fn test_calculate_allocated_accounts_data_size_multiple_allocations() {
        let space1 = 100;
        let space2 = 200;
        let transaction = Transaction::new_unsigned(Message::new(
            &[
                system_instruction::create_account(
                    &Pubkey::new_unique(),
                    &Pubkey::new_unique(),
                    1,
                    space1,
                    &Pubkey::new_unique(),
                ),
                system_instruction::allocate(&Pubkey::new_unique(), space2),
            ],
            Some(&Pubkey::new_unique()),
        ));
        let sanitized_tx = SanitizedTransaction::from_transaction_for_tests(transaction);

        assert_eq!(
            CostModel::calculate_allocated_accounts_data_size(&sanitized_tx),
            space1 + space2
        );
    }

    #[test]
    fn test_calculate_allocated_accounts_data_size_max_limit() {
        let spaces = [MAX_PERMITTED_DATA_LENGTH, MAX_PERMITTED_DATA_LENGTH, 100];
        assert!(
            spaces.iter().copied().sum::<u64>()
                > MAX_PERMITTED_ACCOUNTS_DATA_ALLOCATIONS_PER_TRANSACTION as u64
        );
        let transaction = Transaction::new_unsigned(Message::new(
            &[
                system_instruction::create_account(
                    &Pubkey::new_unique(),
                    &Pubkey::new_unique(),
                    1,
                    spaces[0],
                    &Pubkey::new_unique(),
                ),
                system_instruction::create_account(
                    &Pubkey::new_unique(),
                    &Pubkey::new_unique(),
                    1,
                    spaces[1],
                    &Pubkey::new_unique(),
                ),
                system_instruction::create_account(
                    &Pubkey::new_unique(),
                    &Pubkey::new_unique(),
                    1,
                    spaces[2],
                    &Pubkey::new_unique(),
                ),
            ],
            Some(&Pubkey::new_unique()),
        ));
        let sanitized_tx = SanitizedTransaction::from_transaction_for_tests(transaction);

        assert_eq!(
            CostModel::calculate_allocated_accounts_data_size(&sanitized_tx),
            MAX_PERMITTED_ACCOUNTS_DATA_ALLOCATIONS_PER_TRANSACTION as u64,
        );
    }

    #[test]
    fn test_calculate_allocated_accounts_data_size_overflow() {
        let transaction = Transaction::new_unsigned(Message::new(
            &[
                system_instruction::create_account(
                    &Pubkey::new_unique(),
                    &Pubkey::new_unique(),
                    1,
                    100,
                    &Pubkey::new_unique(),
                ),
                system_instruction::allocate(&Pubkey::new_unique(), u64::MAX),
            ],
            Some(&Pubkey::new_unique()),
        ));
        let sanitized_tx = SanitizedTransaction::from_transaction_for_tests(transaction);

        assert_eq!(
            0, // SystemProgramAccountAllocation::Failed,
            CostModel::calculate_allocated_accounts_data_size(&sanitized_tx),
        );
    }

    #[test]
    fn test_calculate_allocated_accounts_data_size_invalid_ix() {
        let transaction = Transaction::new_unsigned(Message::new(
            &[
                system_instruction::allocate(&Pubkey::new_unique(), 100),
                Instruction::new_with_bincode(system_program::id(), &(), vec![]),
            ],
            Some(&Pubkey::new_unique()),
        ));
        let sanitized_tx = SanitizedTransaction::from_transaction_for_tests(transaction);

        assert_eq!(
            0, // SystemProgramAccountAllocation::Failed,
            CostModel::calculate_allocated_accounts_data_size(&sanitized_tx),
        );
    }

    #[test]
    fn test_cost_model_data_len_cost() {
        let lamports = 0;
        let owner = Pubkey::default();
        let seed = String::default();
        let space = 100;
        let base = Pubkey::default();
        for instruction in [
            SystemInstruction::CreateAccount {
                lamports,
                space,
                owner,
            },
            SystemInstruction::CreateAccountWithSeed {
                base,
                seed: seed.clone(),
                lamports,
                space,
                owner,
            },
            SystemInstruction::Allocate { space },
            SystemInstruction::AllocateWithSeed {
                base,
                seed,
                space,
                owner,
            },
        ] {
            assert_eq!(
                SystemProgramAccountAllocation::Some(space),
                CostModel::calculate_account_data_size_on_deserialized_system_instruction(
                    instruction
                )
            );
        }
        assert_eq!(
            SystemProgramAccountAllocation::None,
            CostModel::calculate_account_data_size_on_deserialized_system_instruction(
                SystemInstruction::TransferWithSeed {
                    lamports,
                    from_seed: String::default(),
                    from_owner: Pubkey::default(),
                }
            )
        );
    }

    #[test]
    fn test_cost_model_simple_transaction() {
        let (mint_keypair, start_hash) = test_setup();

        let keypair = Keypair::new();
        let simple_transaction = SanitizedTransaction::from_transaction_for_tests(
            system_transaction::transfer(&mint_keypair, &keypair.pubkey(), 2, start_hash),
        );

        for (feature_set, expected_execution_cost) in [
            (
                FeatureSet::default(),
                solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS,
            ),
            (
                FeatureSet::all_enabled(),
                u64::from(MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT),
            ),
        ] {
            let (program_execution_cost, _loaded_accounts_data_size_cost, _data_bytes_cost) =
                CostModel::get_transaction_cost(&simple_transaction, &feature_set);

            assert_eq!(expected_execution_cost, program_execution_cost);
        }
    }

    #[test]
    fn test_cost_model_token_transaction() {
        let (mint_keypair, start_hash) = test_setup();

        let instructions = vec![CompiledInstruction::new(3, &(), vec![1, 2, 0])];
        let tx = Transaction::new_with_compiled_instructions(
            &[&mint_keypair],
            &[
                solana_sdk::pubkey::new_rand(),
                solana_sdk::pubkey::new_rand(),
            ],
            start_hash,
            vec![Pubkey::new_unique()],
            instructions,
        );
        let token_transaction = SanitizedTransaction::from_transaction_for_tests(tx);

        for (feature_set, expected_execution_cost) in [
            (
                FeatureSet::default(),
                DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT as u64,
            ),
            (
                FeatureSet::all_enabled(),
                DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT as u64,
            ),
        ] {
            let (program_execution_cost, _loaded_accounts_data_size_cost, data_bytes_cost) =
                CostModel::get_transaction_cost(&token_transaction, &feature_set);

            assert_eq!(expected_execution_cost, program_execution_cost);
            assert_eq!(0, data_bytes_cost);
        }
    }

    #[test]
    fn test_cost_model_demoted_write_lock() {
        let (mint_keypair, start_hash) = test_setup();

        // Cannot write-lock the system program, it will be demoted when taking locks.
        // However, the cost should be calculated as if it were taken.
        let simple_transaction = SanitizedTransaction::from_transaction_for_tests(
            system_transaction::transfer(&mint_keypair, &system_program::id(), 2, start_hash),
        );

        // Feature not enabled - write lock is demoted and does not count towards cost
        {
            let tx_cost = CostModel::calculate_cost(&simple_transaction, &FeatureSet::default());
            assert_eq!(WRITE_LOCK_UNITS, tx_cost.write_lock_cost());
            assert_eq!(1, tx_cost.writable_accounts().count());
        }

        // Feature enabled - write lock is demoted but still counts towards cost
        {
            let tx_cost =
                CostModel::calculate_cost(&simple_transaction, &FeatureSet::all_enabled());
            assert_eq!(2 * WRITE_LOCK_UNITS, tx_cost.write_lock_cost());
            assert_eq!(1, tx_cost.writable_accounts().count());
        }
    }

    #[test]
    fn test_cost_model_compute_budget_transaction() {
        let (mint_keypair, start_hash) = test_setup();
        let expected_cu_limit = 12_345;

        let instructions = vec![
            CompiledInstruction::new(3, &(), vec![1, 2, 0]),
            CompiledInstruction::new_from_raw_parts(
                4,
                ComputeBudgetInstruction::SetComputeUnitLimit(expected_cu_limit)
                    .pack()
                    .unwrap(),
                vec![],
            ),
        ];
        let tx = Transaction::new_with_compiled_instructions(
            &[&mint_keypair],
            &[
                solana_sdk::pubkey::new_rand(),
                solana_sdk::pubkey::new_rand(),
            ],
            start_hash,
            vec![Pubkey::new_unique(), compute_budget::id()],
            instructions,
        );
        let token_transaction = SanitizedTransaction::from_transaction_for_tests(tx);

        // If cu-limit is specified, that would the cost for all programs
        for (feature_set, expected_execution_cost) in [
            (FeatureSet::default(), expected_cu_limit as u64),
            (FeatureSet::all_enabled(), expected_cu_limit as u64),
        ] {
            let (program_execution_cost, _loaded_accounts_data_size_cost, data_bytes_cost) =
                CostModel::get_transaction_cost(&token_transaction, &feature_set);

            assert_eq!(expected_execution_cost, program_execution_cost);
            assert_eq!(1, data_bytes_cost);
        }
    }

    #[test]
    fn test_cost_model_with_failed_compute_budget_transaction() {
        let (mint_keypair, start_hash) = test_setup();

        let instructions = vec![
            CompiledInstruction::new(3, &(), vec![1, 2, 0]),
            CompiledInstruction::new_from_raw_parts(
                4,
                ComputeBudgetInstruction::SetComputeUnitLimit(12_345)
                    .pack()
                    .unwrap(),
                vec![],
            ),
            // to trigger `duplicate_instruction_error` error
            CompiledInstruction::new_from_raw_parts(
                4,
                ComputeBudgetInstruction::SetComputeUnitLimit(1_000)
                    .pack()
                    .unwrap(),
                vec![],
            ),
        ];
        let tx = Transaction::new_with_compiled_instructions(
            &[&mint_keypair],
            &[
                solana_sdk::pubkey::new_rand(),
                solana_sdk::pubkey::new_rand(),
            ],
            start_hash,
            vec![Pubkey::new_unique(), compute_budget::id()],
            instructions,
        );
        let token_transaction = SanitizedTransaction::from_transaction_for_tests(tx);

        for feature_set in [FeatureSet::default(), FeatureSet::all_enabled()] {
            let (program_execution_cost, _loaded_accounts_data_size_cost, _data_bytes_cost) =
                CostModel::get_transaction_cost(&token_transaction, &feature_set);
            assert_eq!(0, program_execution_cost);
        }
    }

    #[test]
    fn test_cost_model_transaction_many_transfer_instructions() {
        let (mint_keypair, start_hash) = test_setup();

        let key1 = solana_sdk::pubkey::new_rand();
        let key2 = solana_sdk::pubkey::new_rand();
        let instructions =
            system_instruction::transfer_many(&mint_keypair.pubkey(), &[(key1, 1), (key2, 1)]);
        let message = Message::new(&instructions, Some(&mint_keypair.pubkey()));
        let tx = SanitizedTransaction::from_transaction_for_tests(Transaction::new(
            &[&mint_keypair],
            message,
            start_hash,
        ));

        // expected cost for two system transfer instructions
        for (feature_set, expected_execution_cost) in [
            (
                FeatureSet::default(),
                2 * solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS,
            ),
            (
                FeatureSet::all_enabled(),
                2 * u64::from(MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT),
            ),
        ] {
            let (programs_execution_cost, _loaded_accounts_data_size_cost, data_bytes_cost) =
                CostModel::get_transaction_cost(&tx, &feature_set);
            assert_eq!(expected_execution_cost, programs_execution_cost);
            assert_eq!(6, data_bytes_cost);
        }
    }

    #[test]
    fn test_cost_model_message_many_different_instructions() {
        let (mint_keypair, start_hash) = test_setup();

        // construct a transaction with multiple random instructions
        let key1 = solana_sdk::pubkey::new_rand();
        let key2 = solana_sdk::pubkey::new_rand();
        let prog1 = solana_sdk::pubkey::new_rand();
        let prog2 = solana_sdk::pubkey::new_rand();
        let instructions = vec![
            CompiledInstruction::new(3, &(), vec![0, 1]),
            CompiledInstruction::new(4, &(), vec![0, 2]),
        ];
        let tx = SanitizedTransaction::from_transaction_for_tests(
            Transaction::new_with_compiled_instructions(
                &[&mint_keypair],
                &[key1, key2],
                start_hash,
                vec![prog1, prog2],
                instructions,
            ),
        );

        for (feature_set, expected_cost) in [
            (
                FeatureSet::default(),
                DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT as u64 * 2,
            ),
            (
                FeatureSet::all_enabled(),
                DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT as u64 * 2,
            ),
        ] {
            let (program_execution_cost, _loaded_accounts_data_size_cost, data_bytes_cost) =
                CostModel::get_transaction_cost(&tx, &feature_set);
            assert_eq!(expected_cost, program_execution_cost);
            assert_eq!(0, data_bytes_cost);
        }
    }

    #[test]
    fn test_cost_model_sort_message_accounts_by_type() {
        // construct a transaction with two random instructions with same signer
        let signer1 = Keypair::new();
        let signer2 = Keypair::new();
        let key1 = Pubkey::new_unique();
        let key2 = Pubkey::new_unique();
        let prog1 = Pubkey::new_unique();
        let prog2 = Pubkey::new_unique();
        let instructions = vec![
            CompiledInstruction::new(4, &(), vec![0, 2]),
            CompiledInstruction::new(5, &(), vec![1, 3]),
        ];
        let tx = SanitizedTransaction::from_transaction_for_tests(
            Transaction::new_with_compiled_instructions(
                &[&signer1, &signer2],
                &[key1, key2],
                Hash::new_unique(),
                vec![prog1, prog2],
                instructions,
            ),
        );

        let tx_cost = CostModel::calculate_cost(&tx, &FeatureSet::all_enabled());
        let writable_accounts = tx_cost.writable_accounts().collect_vec();
        assert_eq!(2 + 2, writable_accounts.len());
        assert_eq!(signer1.pubkey(), *writable_accounts[0]);
        assert_eq!(signer2.pubkey(), *writable_accounts[1]);
        assert_eq!(key1, *writable_accounts[2]);
        assert_eq!(key2, *writable_accounts[3]);
    }

    #[test]
    fn test_cost_model_calculate_cost_all_default() {
        let (mint_keypair, start_hash) = test_setup();
        let tx = SanitizedTransaction::from_transaction_for_tests(system_transaction::transfer(
            &mint_keypair,
            &Keypair::new().pubkey(),
            2,
            start_hash,
        ));

        let expected_account_cost = WRITE_LOCK_UNITS * 2;
        for (feature_set, expected_execution_cost) in [
            (
                FeatureSet::default(),
                solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS,
            ),
            (
                FeatureSet::all_enabled(),
                u64::from(MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT),
            ),
        ] {
            const DEFAULT_PAGE_COST: u64 = 8;
            let expected_loaded_accounts_data_size_cost =
                solana_compute_budget::compute_budget_limits::MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES
                    .get() as u64
                    / ACCOUNT_DATA_COST_PAGE_SIZE
                    * DEFAULT_PAGE_COST;

            let tx_cost = CostModel::calculate_cost(&tx, &feature_set);
            assert_eq!(expected_account_cost, tx_cost.write_lock_cost());
            assert_eq!(expected_execution_cost, tx_cost.programs_execution_cost());
            assert_eq!(2, tx_cost.writable_accounts().count());
            assert_eq!(
                expected_loaded_accounts_data_size_cost,
                tx_cost.loaded_accounts_data_size_cost()
            );
        }
    }

    #[test]
    fn test_cost_model_calculate_cost_with_limit() {
        let (mint_keypair, start_hash) = test_setup();
        let to_keypair = Keypair::new();
        let data_limit = 32 * 1024u32;
        let tx =
            SanitizedTransaction::from_transaction_for_tests(Transaction::new_signed_with_payer(
                &[
                    system_instruction::transfer(&mint_keypair.pubkey(), &to_keypair.pubkey(), 2),
                    ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(data_limit),
                ],
                Some(&mint_keypair.pubkey()),
                &[&mint_keypair],
                start_hash,
            ));

        let expected_account_cost = WRITE_LOCK_UNITS * 2;
        for (feature_set, expected_execution_cost) in [
            (
                FeatureSet::default(),
                solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS
                    + solana_compute_budget_program::DEFAULT_COMPUTE_UNITS,
            ),
            (
                FeatureSet::all_enabled(),
                2 * u64::from(MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT),
            ),
        ] {
            let expected_loaded_accounts_data_size_cost = (data_limit as u64) / (32 * 1024) * 8;

            let tx_cost = CostModel::calculate_cost(&tx, &feature_set);
            assert_eq!(expected_account_cost, tx_cost.write_lock_cost());
            assert_eq!(expected_execution_cost, tx_cost.programs_execution_cost());
            assert_eq!(2, tx_cost.writable_accounts().count());
            assert_eq!(
                expected_loaded_accounts_data_size_cost,
                tx_cost.loaded_accounts_data_size_cost()
            );
        }
    }

    #[test]
    fn test_transaction_cost_with_mix_instruction_without_compute_budget() {
        let (mint_keypair, start_hash) = test_setup();

        let transaction =
            SanitizedTransaction::from_transaction_for_tests(Transaction::new_signed_with_payer(
                &[
                    Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
                    system_instruction::transfer(&mint_keypair.pubkey(), &Pubkey::new_unique(), 2),
                ],
                Some(&mint_keypair.pubkey()),
                &[&mint_keypair],
                start_hash,
            ));
        // transaction has one builtin instruction, and one bpf instruction, no ComputeBudget::compute_unit_limit
        for (feature_set, expected_execution_cost) in [
            (
                FeatureSet::default(),
                solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS
                    + DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT as u64,
            ),
            (
                FeatureSet::all_enabled(),
                u64::from(MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT)
                    + u64::from(DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT),
            ),
        ] {
            let (programs_execution_cost, _loaded_accounts_data_size_cost, _data_bytes_cost) =
                CostModel::get_transaction_cost(&transaction, &feature_set);

            assert_eq!(expected_execution_cost, programs_execution_cost);
        }
    }

    #[test]
    fn test_transaction_cost_with_mix_instruction_with_cu_limit() {
        let (mint_keypair, start_hash) = test_setup();
        let cu_limit: u32 = 12_345;

        let transaction =
            SanitizedTransaction::from_transaction_for_tests(Transaction::new_signed_with_payer(
                &[
                    system_instruction::transfer(&mint_keypair.pubkey(), &Pubkey::new_unique(), 2),
                    ComputeBudgetInstruction::set_compute_unit_limit(cu_limit),
                ],
                Some(&mint_keypair.pubkey()),
                &[&mint_keypair],
                start_hash,
            ));
        for (feature_set, expected_execution_cost) in [
            (
                FeatureSet::default(),
                solana_system_program::system_processor::DEFAULT_COMPUTE_UNITS
                    + solana_compute_budget_program::DEFAULT_COMPUTE_UNITS,
            ),
            (FeatureSet::all_enabled(), cu_limit as u64),
        ] {
            let (programs_execution_cost, _loaded_accounts_data_size_cost, _data_bytes_cost) =
                CostModel::get_transaction_cost(&transaction, &feature_set);

            assert_eq!(expected_execution_cost, programs_execution_cost);
        }
    }
}