sui_types/effects/
test_effects_builder.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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

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

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>,
    created_objects: Vec<(ObjectID, Owner)>,
    /// Objects that are mutated: (ID, old version, new owner).
    mutated_objects: Vec<(ObjectID, SequenceNumber, Owner)>,
    /// Objects that are deleted: (ID, old version).
    deleted_objects: Vec<(ObjectID, SequenceNumber)>,
    /// Objects that are wrapped: (ID, old version).
    wrapped_objects: Vec<(ObjectID, SequenceNumber)>,
    /// Objects that are unwrapped: (ID, new owner).
    unwrapped_objects: Vec<(ObjectID, Owner)>,
}

impl TestEffectsBuilder {
    pub fn new(transaction: &SenderSignedData) -> Self {
        Self {
            transaction: transaction.clone(),
            status: None,
            shared_input_versions: BTreeMap::new(),
            events_digest: None,
            created_objects: vec![],
            mutated_objects: vec![],
            deleted_objects: vec![],
            wrapped_objects: vec![],
            unwrapped_objects: vec![],
        }
    }

    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 with_created_objects(
        mut self,
        objects: impl IntoIterator<Item = (ObjectID, Owner)>,
    ) -> Self {
        self.created_objects.extend(objects);
        self
    }

    pub fn with_mutated_objects(
        mut self,
        // Object ID, old version, and new owner.
        objects: impl IntoIterator<Item = (ObjectID, SequenceNumber, Owner)>,
    ) -> Self {
        self.mutated_objects.extend(objects);
        self
    }

    pub fn with_wrapped_objects(
        mut self,
        objects: impl IntoIterator<Item = (ObjectID, SequenceNumber)>,
    ) -> Self {
        self.wrapped_objects.extend(objects);
        self
    }

    pub fn with_unwrapped_objects(
        mut self,
        objects: impl IntoIterator<Item = (ObjectID, Owner)>,
    ) -> Self {
        self.unwrapped_objects.extend(objects);
        self
    }

    pub fn with_deleted_objects(
        mut self,
        objects: impl IntoIterator<Item = (ObjectID, SequenceNumber)>,
    ) -> Self {
        self.deleted_objects.extend(objects);
        self
    }

    pub fn build(self) -> TransactionEffects {
        let lamport_version = self.get_lamport_version();
        let status = self.status.unwrap_or(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 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,
                    },
                )),
            })
            .chain(self.created_objects.into_iter().map(|(id, owner)| {
                (
                    id,
                    EffectsObjectChange {
                        input_state: ObjectIn::NotExist,
                        output_state: ObjectOut::ObjectWrite((ObjectDigest::random(), owner)),
                        id_operation: IDOperation::Created,
                    },
                )
            }))
            .chain(
                self.mutated_objects
                    .into_iter()
                    .map(|(id, version, owner)| {
                        (
                            id,
                            EffectsObjectChange {
                                input_state: ObjectIn::Exist((
                                    (version, ObjectDigest::random()),
                                    Owner::AddressOwner(sender),
                                )),
                                output_state: ObjectOut::ObjectWrite((
                                    ObjectDigest::random(),
                                    owner,
                                )),
                                id_operation: IDOperation::None,
                            },
                        )
                    }),
            )
            .chain(self.deleted_objects.into_iter().map(|(id, version)| {
                (
                    id,
                    EffectsObjectChange {
                        input_state: ObjectIn::Exist((
                            (version, ObjectDigest::random()),
                            Owner::AddressOwner(sender),
                        )),
                        output_state: ObjectOut::NotExist,
                        id_operation: IDOperation::Deleted,
                    },
                )
            }))
            .chain(self.wrapped_objects.into_iter().map(|(id, version)| {
                (
                    id,
                    EffectsObjectChange {
                        input_state: ObjectIn::Exist((
                            (version, ObjectDigest::random()),
                            Owner::AddressOwner(sender),
                        )),
                        output_state: ObjectOut::NotExist,
                        id_operation: IDOperation::None,
                    },
                )
            }))
            .chain(self.unwrapped_objects.into_iter().map(|(id, owner)| {
                (
                    id,
                    EffectsObjectChange {
                        input_state: ObjectIn::NotExist,
                        output_state: ObjectOut::ObjectWrite((ObjectDigest::random(), owner)),
                        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_v2(
            status,
            executed_epoch,
            GasCostSummary::default(),
            shared_objects,
            BTreeSet::new(),
            self.transaction.digest(),
            lamport_version,
            changed_objects,
            Some(gas_object_id),
            event_digest,
            dependencies,
        )
    }

    fn get_lamport_version(&self) -> SequenceNumber {
        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())
                .chain(self.mutated_objects.iter().map(|(_, v, _)| *v))
                .chain(self.deleted_objects.iter().map(|(_, v)| *v))
                .chain(self.wrapped_objects.iter().map(|(_, v)| *v)),
        )
    }
}