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
// Copyright (c) Mysten Labs, Inc.
// Modifications Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::collections::{BTreeMap, BTreeSet};

use crate::{
    base_types::{ObjectID, SequenceNumber},
    digests::{ObjectDigest, TransactionEventsDigest},
    effects::{EffectsObjectChange, IDOperation, ObjectIn, ObjectOut, TransactionEffects},
    execution::SharedInput,
    execution_status::ExecutionStatus,
    gas::GasCostSummary,
    message_envelope::Message,
    object::Owner,
    transaction::{InputObjectKind, SenderSignedData, TransactionDataAPI},
};

pub struct TestEffectsBuilder {
    transaction: SenderSignedData,
    /// Override the execution status if provided.
    status: Option<ExecutionStatus>,
    /// Provide the assigned versions for all shared objects.
    shared_input_versions: BTreeMap<ObjectID, SequenceNumber>,
    events_digest: Option<TransactionEventsDigest>,
}

impl TestEffectsBuilder {
    pub fn new(transaction: &SenderSignedData) -> Self {
        Self {
            transaction: transaction.clone(),
            status: None,
            shared_input_versions: BTreeMap::new(),
            events_digest: None,
        }
    }

    pub fn with_status(mut self, status: ExecutionStatus) -> Self {
        self.status = Some(status);
        self
    }

    pub fn with_shared_input_versions(
        mut self,
        versions: BTreeMap<ObjectID, SequenceNumber>,
    ) -> Self {
        assert!(self.shared_input_versions.is_empty());
        self.shared_input_versions = versions;
        self
    }

    pub fn with_events_digest(mut self, digest: TransactionEventsDigest) -> Self {
        self.events_digest = Some(digest);
        self
    }

    pub fn build(self) -> TransactionEffects {
        let status = self.status.unwrap_or_else(|| ExecutionStatus::Success);
        // TODO: This does not yet support deleted shared objects.
        let shared_objects = self
            .shared_input_versions
            .iter()
            .map(|(id, version)| SharedInput::Existing((*id, *version, ObjectDigest::MIN)))
            .collect();
        let executed_epoch = 0;
        let lamport_version = SequenceNumber::lamport_increment(
            self.transaction
                .transaction_data()
                .input_objects()
                .unwrap()
                .iter()
                .filter_map(|kind| kind.version())
                .chain(
                    self.transaction
                        .transaction_data()
                        .receiving_objects()
                        .iter()
                        .map(|oref| oref.1),
                )
                .chain(self.shared_input_versions.values().copied()),
        );
        let sender = self.transaction.transaction_data().sender();
        // TODO: Include receiving objects in the object changes as well.
        let changed_objects = self
            .transaction
            .transaction_data()
            .input_objects()
            .unwrap()
            .iter()
            .filter_map(|kind| match kind {
                InputObjectKind::ImmOrOwnedMoveObject(oref) => {
                    Some((oref.0, EffectsObjectChange {
                        input_state: ObjectIn::Exist((
                            (oref.1, oref.2),
                            Owner::AddressOwner(sender),
                        )),
                        output_state: ObjectOut::ObjectWrite((
                            // Digest must change with a mutation.
                            ObjectDigest::MAX,
                            Owner::AddressOwner(sender),
                        )),
                        id_operation: IDOperation::None,
                    }))
                }
                InputObjectKind::MovePackage(_) => None,
                InputObjectKind::SharedMoveObject {
                    id,
                    initial_shared_version,
                    mutable,
                } => mutable.then_some((*id, EffectsObjectChange {
                    input_state: ObjectIn::Exist((
                        (
                            *self
                                .shared_input_versions
                                .get(id)
                                .unwrap_or(initial_shared_version),
                            ObjectDigest::MIN,
                        ),
                        Owner::Shared {
                            initial_shared_version: *initial_shared_version,
                        },
                    )),
                    output_state: ObjectOut::ObjectWrite((
                        // Digest must change with a mutation.
                        ObjectDigest::MAX,
                        Owner::Shared {
                            initial_shared_version: *initial_shared_version,
                        },
                    )),
                    id_operation: IDOperation::None,
                })),
            })
            .collect();
        let gas_object_id = self.transaction.transaction_data().gas()[0].0;
        let event_digest = self.events_digest;
        let dependencies = vec![];
        TransactionEffects::new_from_execution_v1(
            status,
            executed_epoch,
            GasCostSummary::default(),
            shared_objects,
            BTreeSet::new(),
            self.transaction.digest(),
            lamport_version,
            changed_objects,
            Some(gas_object_id),
            event_digest,
            dependencies,
        )
    }
}