casper-execution-engine 9.0.0

Casper execution engine crates.
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
use std::{collections::BTreeSet, convert::TryFrom};

use serde::Serialize;
use thiserror::Error;

use casper_storage::{data_access_layer::TransferResult, tracking_copy::TrackingCopyCache};
use casper_types::{
    account::AccountHash,
    bytesrepr::Bytes,
    contract_messages::Messages,
    execution::{Effects, TransformKindV2},
    BlockHash, BlockTime, CLValue, DeployHash, Digest, ExecutableDeployItem, Gas, InitiatorAddr,
    Key, Phase, PricingMode, ProtocolVersion, RuntimeArgs, TransactionEntryPoint, TransactionHash,
    TransactionInvocationTarget, TransactionTarget, TransactionV1Hash, Transfer, URefAddr, U512,
};

use crate::engine_state::Error as EngineError;

const DEFAULT_ENTRY_POINT: &str = "call";

/// Structure that needs to be filled with data so the engine can assemble wasm for deploy.
pub struct SessionDataDeploy<'a> {
    deploy_hash: &'a DeployHash,
    session: &'a ExecutableDeployItem,
    initiator_addr: &'a InitiatorAddr,
    signers: BTreeSet<AccountHash>,
    is_standard_payment: bool,
}

impl<'a> SessionDataDeploy<'a> {
    /// Constructor
    pub fn new(
        deploy_hash: &'a DeployHash,
        session: &'a ExecutableDeployItem,
        initiator_addr: &'a InitiatorAddr,
        signers: BTreeSet<AccountHash>,
        is_standard_payment: bool,
    ) -> Self {
        Self {
            deploy_hash,
            session,
            initiator_addr,
            signers,
            is_standard_payment,
        }
    }

    /// Deploy hash of the deploy
    pub fn deploy_hash(&self) -> &DeployHash {
        self.deploy_hash
    }

    /// executable item of the deploy
    pub fn session(&self) -> &ExecutableDeployItem {
        self.session
    }

    /// initiator address of the deploy
    pub fn initiator_addr(&self) -> &InitiatorAddr {
        self.initiator_addr
    }

    /// signers of the deploy
    pub fn signers(&self) -> BTreeSet<AccountHash> {
        self.signers.clone()
    }
}

/// Structure that needs to be filled with data so the engine can assemble wasm for v1.
pub struct SessionDataV1<'a> {
    args: &'a RuntimeArgs,
    target: &'a TransactionTarget,
    entry_point: &'a TransactionEntryPoint,
    is_install_upgrade: bool,
    hash: &'a TransactionV1Hash,
    pricing_mode: &'a PricingMode,
    initiator_addr: &'a InitiatorAddr,
    signers: BTreeSet<AccountHash>,
    is_standard_payment: bool,
}

impl<'a> SessionDataV1<'a> {
    #[allow(clippy::too_many_arguments)]
    /// Constructor
    pub fn new(
        args: &'a RuntimeArgs,
        target: &'a TransactionTarget,
        entry_point: &'a TransactionEntryPoint,
        is_install_upgrade: bool,
        hash: &'a TransactionV1Hash,
        pricing_mode: &'a PricingMode,
        initiator_addr: &'a InitiatorAddr,
        signers: BTreeSet<AccountHash>,
        is_standard_payment: bool,
    ) -> Self {
        Self {
            args,
            target,
            entry_point,
            is_install_upgrade,
            hash,
            pricing_mode,
            initiator_addr,
            signers,
            is_standard_payment,
        }
    }

    /// Runtime args passed with the transaction.
    pub fn args(&self) -> &RuntimeArgs {
        self.args
    }

    /// Target of the transaction.
    pub fn target(&self) -> &TransactionTarget {
        self.target
    }

    /// Entry point of the transaction
    pub fn entry_point(&self) -> &TransactionEntryPoint {
        self.entry_point
    }

    /// Should session be allowed to perform install/upgrade operations
    pub fn is_install_upgrade(&self) -> bool {
        self.is_install_upgrade
    }

    /// Hash of the transaction
    pub fn hash(&self) -> &TransactionV1Hash {
        self.hash
    }

    /// initiator address of the transaction
    pub fn initiator_addr(&self) -> &InitiatorAddr {
        self.initiator_addr
    }

    /// signers of the transaction
    pub fn signers(&self) -> BTreeSet<AccountHash> {
        self.signers.clone()
    }

    /// Pricing mode of the transaction
    pub fn pricing_mode(&self) -> &PricingMode {
        self.pricing_mode
    }
}

/// Wrapper enum abstracting data for assmbling WasmV1Requests
pub enum SessionInputData<'a> {
    /// Variant for sessions created from deploy transactions
    DeploySessionData {
        /// Deploy session data
        data: SessionDataDeploy<'a>,
    },
    /// Variant for sessions created from v1 transactions
    SessionDataV1 {
        /// v1 session data
        data: SessionDataV1<'a>,
    },
}

impl SessionInputData<'_> {
    /// Transaction hash for the session
    pub fn transaction_hash(&self) -> TransactionHash {
        match self {
            SessionInputData::DeploySessionData { data } => {
                TransactionHash::Deploy(*data.deploy_hash())
            }
            SessionInputData::SessionDataV1 { data } => TransactionHash::V1(*data.hash()),
        }
    }

    /// Initiator address for the session
    pub fn initiator_addr(&self) -> &InitiatorAddr {
        match self {
            SessionInputData::DeploySessionData { data } => data.initiator_addr(),
            SessionInputData::SessionDataV1 { data } => data.initiator_addr(),
        }
    }

    /// Signers for the session
    pub fn signers(&self) -> BTreeSet<AccountHash> {
        match self {
            SessionInputData::DeploySessionData { data } => data.signers(),
            SessionInputData::SessionDataV1 { data } => data.signers(),
        }
    }

    /// determines if the transaction from which this session data was created is a standard payment
    pub fn is_standard_payment(&self) -> bool {
        match self {
            SessionInputData::DeploySessionData { data } => data.is_standard_payment,
            SessionInputData::SessionDataV1 { data } => data.is_standard_payment,
        }
    }

    /// Is install upgrade allowed?
    pub fn is_install_upgrade_allowed(&self) -> bool {
        match self {
            SessionInputData::DeploySessionData { .. } => true,
            SessionInputData::SessionDataV1 { data } => data.is_install_upgrade,
        }
    }
}

/// Error returned if constructing a new [`WasmV1Request`] fails.
#[derive(Clone, Eq, PartialEq, Error, Serialize, Debug)]
pub enum InvalidRequest {
    /// Missing custom payment.
    #[error("custom payment not found for {0}")]
    CustomPaymentNotFound(TransactionHash),
    /// Unexpected variant.
    #[error("unexpected variant for {0} attempting {1}")]
    UnexpectedVariant(TransactionHash, String),
    /// Unsupported mode.
    #[error("unsupported mode for {0} attempting {1}")]
    UnsupportedMode(TransactionHash, String),
    /// Invalid entry point.
    #[error("invalid entry point for {0} attempting {1}")]
    InvalidEntryPoint(TransactionHash, String),
    /// Invalid target.
    #[error("invalid target for {0} attempting {1}")]
    InvalidTarget(TransactionHash, String),
    /// Unsupported category.
    #[error("invalid category for {0} attempting {1}")]
    InvalidCategory(TransactionHash, String),
}

#[derive(Debug, Clone)]
pub enum SessionKind {
    InstallUpgradeBytecode,
    GenericBytecode,
}

/// The item to be executed.
#[derive(Debug, Clone)]
pub enum ExecutableItem {
    /// Deploy model byte code.
    Deploy(Bytes),
    /// Payment byte code.
    PaymentBytes(Bytes),
    /// Session byte code.
    SessionBytes {
        /// The kind of session.
        kind: SessionKind,
        /// The compiled Wasm.
        module_bytes: Bytes,
    },
    /// An attempt to invoke a stored entity or package.
    Invocation(TransactionInvocationTarget),
}

impl ExecutableItem {
    /// Is install upgrade allowed?
    pub fn is_install_upgrade_allowed(&self) -> bool {
        match self {
            ExecutableItem::Deploy(_) => true,
            ExecutableItem::PaymentBytes(_) | ExecutableItem::Invocation(_) => false,
            ExecutableItem::SessionBytes { kind, .. } => {
                matches!(kind, SessionKind::InstallUpgradeBytecode)
            }
        }
    }
}

/// Block info.
#[derive(Copy, Clone, Debug)]
pub struct BlockInfo {
    /// State root hash of the global state in which the transaction will be executed.
    pub state_hash: Digest,
    /// Block time represented as a unix timestamp.
    pub block_time: BlockTime,
    /// Parent block hash
    pub parent_block_hash: BlockHash,
    /// Block height
    pub block_height: u64,
    /// Protocol version
    pub protocol_version: ProtocolVersion,
}

impl BlockInfo {
    /// A new instance of `[BlockInfo]`.
    pub fn new(
        state_hash: Digest,
        block_time: BlockTime,
        parent_block_hash: BlockHash,
        block_height: u64,
        protocol_version: ProtocolVersion,
    ) -> Self {
        BlockInfo {
            state_hash,
            block_time,
            parent_block_hash,
            block_height,
            protocol_version,
        }
    }

    /// Apply different state hash.
    pub fn with_state_hash(&mut self, state_hash: Digest) {
        self.state_hash = state_hash;
    }

    /// State hash.
    pub fn state_hash(&self) -> Digest {
        self.state_hash
    }

    /// Block time.
    pub fn block_time(&self) -> BlockTime {
        self.block_time
    }

    /// Parent block hash.
    pub fn parent_block_hash(&self) -> BlockHash {
        self.parent_block_hash
    }

    /// Block height.
    pub fn block_height(&self) -> u64 {
        self.block_height
    }

    /// Protocol version.
    pub fn protocol_version(&self) -> ProtocolVersion {
        self.protocol_version
    }
}

/// A request to execute the given Wasm on the V1 runtime.
#[derive(Debug)]
pub struct WasmV1Request {
    /// Block info.
    pub block_info: BlockInfo,
    /// The hash identifying the transaction.
    pub transaction_hash: TransactionHash,
    /// The number of Motes per unit of Gas to be paid for execution.
    pub gas_limit: Gas,
    /// The transaction's initiator.
    pub initiator_addr: InitiatorAddr,
    /// The executable item.
    pub executable_item: ExecutableItem,
    /// The entry point to call when executing.
    pub entry_point: String,
    /// The runtime args.
    pub args: RuntimeArgs,
    /// The account hashes of the signers of the transaction.
    pub authorization_keys: BTreeSet<AccountHash>,
    /// Execution phase.
    pub phase: Phase,
}

impl WasmV1Request {
    /// New from executable deploy item or InvalidRequest error.
    pub fn new_from_executable_deploy_item(
        block_info: BlockInfo,
        gas_limit: Gas,
        transaction_hash: TransactionHash,
        initiator_addr: InitiatorAddr,
        authorization_keys: BTreeSet<AccountHash>,
        session_item: &ExecutableDeployItem,
    ) -> Result<Self, InvalidRequest> {
        let executable_info =
            build_session_info_for_executable_item(session_item, transaction_hash)?;
        Ok(Self::new_from_executable_info(
            block_info,
            gas_limit,
            transaction_hash,
            initiator_addr,
            authorization_keys,
            executable_info,
        ))
    }

    /// New payment from executable deploy item or InvalidRequest error.
    pub fn new_payment_from_executable_deploy_item(
        block_info: BlockInfo,
        gas_limit: Gas,
        transaction_hash: TransactionHash,
        initiator_addr: InitiatorAddr,
        authorization_keys: BTreeSet<AccountHash>,
        payment_item: &ExecutableDeployItem,
    ) -> Result<Self, InvalidRequest> {
        let executable_info =
            build_payment_info_for_executable_item(payment_item, transaction_hash)?;
        Ok(Self::new_from_executable_info(
            block_info,
            gas_limit,
            transaction_hash,
            initiator_addr,
            authorization_keys,
            executable_info,
        ))
    }

    pub(crate) fn new_from_executable_info(
        block_info: BlockInfo,
        gas_limit: Gas,
        transaction_hash: TransactionHash,
        initiator_addr: InitiatorAddr,
        authorization_keys: BTreeSet<AccountHash>,
        executable_info: impl Executable,
    ) -> Self {
        let executable_item = executable_info.item();
        Self {
            block_info,
            transaction_hash,
            gas_limit,
            initiator_addr,
            authorization_keys,
            executable_item,
            entry_point: executable_info.entry_point().clone(),
            args: executable_info.args().clone(),
            phase: executable_info.phase(),
        }
    }

    /// Creates a new request from a transaction for use as the session code.
    pub fn new_session(
        block_info: BlockInfo,
        gas_limit: Gas,
        session_input_data: &SessionInputData,
    ) -> Result<Self, InvalidRequest> {
        let session_info = SessionInfo::try_from(session_input_data)?;
        let transaction_hash = session_input_data.transaction_hash();
        let initiator_addr = session_input_data.initiator_addr().clone();
        let authorization_keys = session_input_data.signers().clone();
        Ok(WasmV1Request::new_from_executable_info(
            block_info,
            gas_limit,
            transaction_hash,
            initiator_addr,
            authorization_keys,
            session_info,
        ))
    }

    /// Creates a new request from a transaction for use as custom payment.
    pub fn new_custom_payment(
        block_info: BlockInfo,
        gas_limit: Gas,
        session_input_data: &SessionInputData,
    ) -> Result<Self, InvalidRequest> {
        let payment_info = PaymentInfo::try_from(session_input_data)?;
        let transaction_hash = session_input_data.transaction_hash();
        let initiator_addr = session_input_data.initiator_addr().clone();
        let authorization_keys = session_input_data.signers().clone();
        Ok(WasmV1Request::new_from_executable_info(
            block_info,
            gas_limit,
            transaction_hash,
            initiator_addr,
            authorization_keys,
            payment_info,
        ))
    }
}

/// Wasm v1 result.
#[derive(Clone, Debug)]
pub struct WasmV1Result {
    /// List of transfers that happened during execution.
    transfers: Vec<Transfer>,
    /// Gas limit.
    limit: Gas,
    /// Gas consumed.
    consumed: Gas,
    /// Execution effects.
    effects: Effects,
    /// Messages emitted during execution.
    messages: Messages,
    /// Did the wasm execute successfully?
    error: Option<EngineError>,
    /// Result captured from a ret call.
    ret: Option<CLValue>,
    /// Tracking copy cache captured during execution.
    cache: Option<TrackingCopyCache>,
}

impl WasmV1Result {
    /// Creates a new instance.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        limit: Gas,
        consumed: Gas,
        effects: Effects,
        transfers: Vec<Transfer>,
        messages: Messages,
        error: Option<EngineError>,
        ret: Option<CLValue>,
        cache: Option<TrackingCopyCache>,
    ) -> Self {
        WasmV1Result {
            limit,
            consumed,
            effects,
            transfers,
            messages,
            error,
            ret,
            cache,
        }
    }

    /// Error, if any.
    pub fn error(&self) -> Option<&EngineError> {
        self.error.as_ref()
    }

    /// List of transfers that happened during execution.
    pub fn transfers(&self) -> &Vec<Transfer> {
        &self.transfers
    }

    /// Gas limit.
    pub fn limit(&self) -> Gas {
        self.limit
    }

    /// Gas consumed.
    pub fn consumed(&self) -> Gas {
        self.consumed
    }

    /// Execution effects.
    pub fn effects(&self) -> &Effects {
        &self.effects
    }

    /// Tracking copy cache captured during execution.
    pub fn cache(&self) -> Option<&TrackingCopyCache> {
        self.cache.as_ref()
    }

    /// Messages emitted during execution.
    pub fn messages(&self) -> &Messages {
        &self.messages
    }

    /// Result captured from a ret call.
    pub fn ret(&self) -> Option<&CLValue> {
        self.ret.as_ref()
    }

    /// Root not found.
    pub fn root_not_found(gas_limit: Gas, state_hash: Digest) -> Self {
        WasmV1Result {
            transfers: Vec::default(),
            effects: Effects::new(),
            messages: Vec::default(),
            limit: gas_limit,
            consumed: Gas::zero(),
            error: Some(EngineError::RootNotFound(state_hash)),
            ret: None,
            cache: None,
        }
    }

    /// Precondition failure.
    pub fn precondition_failure(gas_limit: Gas, error: EngineError) -> Self {
        WasmV1Result {
            transfers: Vec::default(),
            effects: Effects::new(),
            messages: Vec::default(),
            limit: gas_limit,
            consumed: Gas::zero(),
            error: Some(error),
            ret: None,
            cache: None,
        }
    }

    /// Failed to transform transaction into an executable item.
    pub fn invalid_executable_item(gas_limit: Gas, error: InvalidRequest) -> Self {
        WasmV1Result {
            transfers: Vec::default(),
            effects: Effects::new(),
            messages: Vec::default(),
            limit: gas_limit,
            consumed: Gas::zero(),
            error: Some(EngineError::InvalidExecutableItem(error)),
            ret: None,
            cache: None,
        }
    }

    /// Returns `true` if this is a precondition failure.
    ///
    /// Precondition variant is further described as an execution failure which does not have any
    /// effects, and has a gas cost of 0.
    pub fn has_precondition_failure(&self) -> bool {
        self.error.is_some() && self.consumed == Gas::zero() && self.effects.is_empty()
    }

    /// Converts a transfer result to an execution result.
    pub fn from_transfer_result(transfer_result: TransferResult, consumed: Gas) -> Option<Self> {
        // NOTE: for native / wasmless operations limit and consumed are always equal, and
        // we can get away with simplifying to one or the other here.
        // this is NOT true of wasm based operations however.
        match transfer_result {
            TransferResult::RootNotFound => None,
            TransferResult::Success {
                transfers,
                effects,
                cache,
            } => Some(WasmV1Result {
                transfers,
                limit: consumed,
                consumed,
                effects,
                messages: Messages::default(),
                error: None,
                ret: None,
                cache: Some(cache),
            }),
            TransferResult::Failure(te) => {
                Some(WasmV1Result {
                    transfers: vec![],
                    limit: consumed,
                    consumed,
                    effects: Effects::default(), // currently not returning effects on failure
                    messages: Messages::default(),
                    error: Some(EngineError::Transfer(te)),
                    ret: None,
                    cache: None,
                })
            }
        }
    }

    /// Checks effects for an AddUInt512 transform to a balance at imputed addr
    /// and for exactly the imputed amount.
    pub fn balance_increased_by_amount(&self, addr: URefAddr, amount: U512) -> bool {
        if self.effects.is_empty() || self.effects.transforms().is_empty() {
            return false;
        }

        let key = Key::Balance(addr);
        if let Some(transform) = self.effects.transforms().iter().find(|x| x.key() == &key) {
            if let TransformKindV2::AddUInt512(added) = transform.kind() {
                return *added == amount;
            }
        }
        false
    }
}

/// Helper struct to carry item, entry_point, and arg info for a `WasmV1Request`.
struct ExecutableInfo {
    item: ExecutableItem,
    entry_point: String,
    args: RuntimeArgs,
}

pub(crate) trait Executable {
    fn item(&self) -> ExecutableItem;
    fn entry_point(&self) -> &String;
    fn args(&self) -> &RuntimeArgs;
    fn phase(&self) -> Phase;
}

/// New type for hanging session specific impl's off of.
struct SessionInfo(ExecutableInfo);

impl Executable for SessionInfo {
    fn item(&self) -> ExecutableItem {
        self.0.item.clone()
    }

    fn entry_point(&self) -> &String {
        &self.0.entry_point
    }

    fn args(&self) -> &RuntimeArgs {
        &self.0.args
    }

    fn phase(&self) -> Phase {
        Phase::Session
    }
}

impl TryFrom<&SessionInputData<'_>> for PaymentInfo {
    type Error = InvalidRequest;

    fn try_from(input_data: &SessionInputData) -> Result<Self, Self::Error> {
        match input_data {
            SessionInputData::DeploySessionData { data } => PaymentInfo::try_from(data),
            SessionInputData::SessionDataV1 { data } => PaymentInfo::try_from(data),
        }
    }
}

impl TryFrom<&SessionInputData<'_>> for SessionInfo {
    type Error = InvalidRequest;

    fn try_from(input_data: &SessionInputData) -> Result<Self, Self::Error> {
        match input_data {
            SessionInputData::DeploySessionData { data } => SessionInfo::try_from(data),
            SessionInputData::SessionDataV1 { data } => SessionInfo::try_from(data),
        }
    }
}

impl TryFrom<&SessionDataDeploy<'_>> for SessionInfo {
    type Error = InvalidRequest;

    fn try_from(deploy_data: &SessionDataDeploy) -> Result<Self, Self::Error> {
        let transaction_hash = TransactionHash::Deploy(*deploy_data.deploy_hash());
        let session_item = deploy_data.session();
        build_session_info_for_executable_item(session_item, transaction_hash)
    }
}

fn build_session_info_for_executable_item(
    session_item: &ExecutableDeployItem,
    transaction_hash: TransactionHash,
) -> Result<SessionInfo, InvalidRequest> {
    let session: ExecutableItem;
    let session_entry_point: String;
    let session_args: RuntimeArgs;
    match session_item {
        ExecutableDeployItem::ModuleBytes { module_bytes, args } => {
            session = ExecutableItem::Deploy(module_bytes.clone());
            session_entry_point = DEFAULT_ENTRY_POINT.to_string();
            session_args = args.clone();
        }
        ExecutableDeployItem::StoredContractByHash {
            hash,
            entry_point,
            args,
        } => {
            session = ExecutableItem::Invocation(
                TransactionInvocationTarget::new_invocable_entity((*hash).into()),
            );
            session_entry_point = entry_point.clone();
            session_args = args.clone();
        }
        ExecutableDeployItem::StoredContractByName {
            name,
            entry_point,
            args,
        } => {
            session = ExecutableItem::Invocation(
                TransactionInvocationTarget::new_invocable_entity_alias(name.clone()),
            );
            session_entry_point = entry_point.clone();
            session_args = args.clone();
        }
        ExecutableDeployItem::StoredVersionedContractByHash {
            hash,
            version,
            entry_point,
            args,
        } => {
            session = ExecutableItem::Invocation(TransactionInvocationTarget::ByPackageHash {
                addr: hash.value(),
                version: *version,
                protocol_version_major: None,
            });
            session_entry_point = entry_point.clone();
            session_args = args.clone();
        }
        ExecutableDeployItem::StoredVersionedContractByName {
            name,
            version,
            entry_point,
            args,
        } => {
            session = ExecutableItem::Invocation(TransactionInvocationTarget::ByPackageName {
                name: name.to_owned(),
                version: *version,
                protocol_version_major: None,
            });
            session_entry_point = entry_point.clone();
            session_args = args.clone();
        }
        ExecutableDeployItem::Transfer { .. } => {
            return Err(InvalidRequest::UnsupportedMode(
                transaction_hash,
                session_item.to_string(),
            ));
        }
    }

    Ok(SessionInfo(ExecutableInfo {
        item: session,
        entry_point: session_entry_point,
        args: session_args,
    }))
}

impl TryFrom<&SessionDataV1<'_>> for SessionInfo {
    type Error = InvalidRequest;

    fn try_from(v1_txn: &SessionDataV1) -> Result<Self, Self::Error> {
        let transaction_hash = TransactionHash::V1(*v1_txn.hash());
        let args = v1_txn.args().clone();
        let session = match v1_txn.target() {
            TransactionTarget::Native => {
                return Err(InvalidRequest::InvalidTarget(
                    transaction_hash,
                    v1_txn.target().to_string(),
                ));
            }
            TransactionTarget::Stored { id, .. } => {
                let TransactionEntryPoint::Custom(entry_point) = v1_txn.entry_point() else {
                    return Err(InvalidRequest::InvalidEntryPoint(
                        transaction_hash,
                        v1_txn.entry_point().to_string(),
                    ));
                };
                let item = ExecutableItem::Invocation(id.clone());
                ExecutableInfo {
                    item,
                    entry_point: entry_point.clone(),
                    args,
                }
            }
            TransactionTarget::Session { module_bytes, .. } => {
                if *v1_txn.entry_point() != TransactionEntryPoint::Call {
                    return Err(InvalidRequest::InvalidEntryPoint(
                        transaction_hash,
                        v1_txn.entry_point().to_string(),
                    ));
                };
                let kind = if v1_txn.is_install_upgrade() {
                    SessionKind::InstallUpgradeBytecode
                } else {
                    SessionKind::GenericBytecode
                };
                let item = ExecutableItem::SessionBytes {
                    kind,
                    module_bytes: module_bytes.clone(),
                };
                ExecutableInfo {
                    item,
                    entry_point: DEFAULT_ENTRY_POINT.to_owned(),
                    args,
                }
            }
        };

        Ok(SessionInfo(session))
    }
}
/// New type for hanging payment specific impl's off of.
struct PaymentInfo(ExecutableInfo);

impl Executable for PaymentInfo {
    fn item(&self) -> ExecutableItem {
        self.0.item.clone()
    }

    fn entry_point(&self) -> &String {
        &self.0.entry_point
    }

    fn args(&self) -> &RuntimeArgs {
        &self.0.args
    }

    fn phase(&self) -> Phase {
        Phase::Payment
    }
}

impl TryFrom<&SessionDataDeploy<'_>> for PaymentInfo {
    type Error = InvalidRequest;

    fn try_from(deploy_data: &SessionDataDeploy) -> Result<Self, Self::Error> {
        let payment_item = deploy_data.session();
        let transaction_hash = TransactionHash::Deploy(*deploy_data.deploy_hash());
        build_payment_info_for_executable_item(payment_item, transaction_hash)
    }
}

fn build_payment_info_for_executable_item(
    payment_item: &ExecutableDeployItem,
    transaction_hash: TransactionHash,
) -> Result<PaymentInfo, InvalidRequest> {
    match payment_item {
        ExecutableDeployItem::ModuleBytes { module_bytes, args } => {
            let payment = if module_bytes.is_empty() {
                return Err(InvalidRequest::UnsupportedMode(
                    transaction_hash,
                    "standard payment is no longer handled by the execution engine".to_string(),
                ));
            } else {
                ExecutableItem::PaymentBytes(module_bytes.clone())
            };
            Ok(PaymentInfo(ExecutableInfo {
                item: payment,
                entry_point: DEFAULT_ENTRY_POINT.to_string(),
                args: args.clone(),
            }))
        }
        ExecutableDeployItem::StoredContractByHash {
            hash,
            args,
            entry_point,
        } => Ok(PaymentInfo(ExecutableInfo {
            item: ExecutableItem::Invocation(TransactionInvocationTarget::ByHash(hash.value())),
            entry_point: entry_point.clone(),
            args: args.clone(),
        })),
        ExecutableDeployItem::StoredContractByName {
            name,
            args,
            entry_point,
        } => Ok(PaymentInfo(ExecutableInfo {
            item: ExecutableItem::Invocation(TransactionInvocationTarget::ByName(name.clone())),
            entry_point: entry_point.clone(),
            args: args.clone(),
        })),
        ExecutableDeployItem::StoredVersionedContractByHash {
            args,
            hash,
            version,
            entry_point,
        } => Ok(PaymentInfo(ExecutableInfo {
            item: ExecutableItem::Invocation(TransactionInvocationTarget::ByPackageHash {
                addr: hash.value(),
                version: *version,
                protocol_version_major: None,
            }),
            entry_point: entry_point.clone(),
            args: args.clone(),
        })),
        ExecutableDeployItem::StoredVersionedContractByName {
            name,
            version,
            args,
            entry_point,
        } => Ok(PaymentInfo(ExecutableInfo {
            item: ExecutableItem::Invocation(TransactionInvocationTarget::ByPackageName {
                name: name.clone(),
                version: *version,
                protocol_version_major: None,
            }),
            entry_point: entry_point.clone(),
            args: args.clone(),
        })),
        ExecutableDeployItem::Transfer { .. } => Err(InvalidRequest::UnexpectedVariant(
            transaction_hash,
            "payment item".to_string(),
        )),
    }
}

impl TryFrom<&SessionDataV1<'_>> for PaymentInfo {
    type Error = InvalidRequest;

    fn try_from(v1_txn: &SessionDataV1) -> Result<Self, Self::Error> {
        let transaction_hash = TransactionHash::V1(*v1_txn.hash());
        match v1_txn.pricing_mode() {
            mode @ PricingMode::PaymentLimited {
                standard_payment, ..
            } => {
                if *standard_payment {
                    return Err(InvalidRequest::UnsupportedMode(
                        transaction_hash,
                        mode.to_string(),
                    ));
                }
            }
            mode @ PricingMode::Fixed { .. } | mode @ PricingMode::Prepaid { .. } => {
                return Err(InvalidRequest::UnsupportedMode(
                    transaction_hash,
                    mode.to_string(),
                ));
            }
        };

        let payment = match v1_txn.target() {
            TransactionTarget::Session { module_bytes, .. } => {
                if *v1_txn.entry_point() != TransactionEntryPoint::Call {
                    return Err(InvalidRequest::InvalidEntryPoint(
                        transaction_hash,
                        v1_txn.entry_point().to_string(),
                    ));
                };
                let item = ExecutableItem::PaymentBytes(module_bytes.clone());
                ExecutableInfo {
                    item,
                    entry_point: DEFAULT_ENTRY_POINT.to_owned(),
                    args: v1_txn.args().clone(),
                }
            }
            TransactionTarget::Native | TransactionTarget::Stored { .. } => {
                return Err(InvalidRequest::InvalidTarget(
                    transaction_hash,
                    v1_txn.target().to_string(),
                ));
            }
        };

        Ok(PaymentInfo(payment))
    }
}