1pub mod deny;
6
7pub use checked::*;
8
9#[iota_macros::with_checked_arithmetic]
10mod checked {
11 use std::{
12 collections::{BTreeMap, HashSet},
13 sync::Arc,
14 };
15
16 use iota_config::verifier_signing_config::VerifierSigningConfig;
17 use iota_protocol_config::ProtocolConfig;
18 use iota_sdk_types::{
19 Address, ObjectId, ObjectReference, Owner, Transaction, TransactionKind, Version,
20 };
21 use iota_types::{
22 IOTA_CLOCK_OBJECT_SHARED_VERSION,
23 error::{IotaError, IotaResult, UserInputError, UserInputResult},
24 executable_transaction::VerifiedExecutableTransaction,
25 fp_bail, fp_ensure,
26 gas::IotaGasStatus,
27 metrics::BytecodeVerifierMetrics,
28 object::Object,
29 transaction::{
30 CheckedInputObjects, InputObjectKind, InputObjects, ObjectReadResult,
31 ObjectReadResultKind, ProgrammableTransactionExt, ReceivingObjectReadResult,
32 ReceivingObjects, TransactionAPI,
33 },
34 transaction_executor::InputCheckRules,
35 };
36 use tracing::{error, instrument};
37
38 trait IntoChecked {
39 fn into_checked(self) -> CheckedInputObjects;
40 }
41
42 impl IntoChecked for InputObjects {
43 fn into_checked(self) -> CheckedInputObjects {
44 CheckedInputObjects::new_with_checked_transaction_inputs(self)
45 }
46 }
47
48 fn get_gas_status(
53 objects: &InputObjects,
54 gas: &[ObjectReference],
55 protocol_config: &ProtocolConfig,
56 reference_gas_price: u64,
57 transaction: &Transaction,
58 authentication_gas_budget: u64,
59 is_execute_transaction_to_effects: bool,
60 input_checks: InputCheckRules,
61 ) -> IotaResult<IotaGasStatus> {
62 if transaction.is_system_tx() {
63 Ok(IotaGasStatus::new_unmetered())
64 } else {
65 check_gas(
66 objects,
67 protocol_config,
68 reference_gas_price,
69 gas,
70 transaction.gas_price(),
71 transaction.gas_budget(),
72 authentication_gas_budget,
73 is_execute_transaction_to_effects,
74 input_checks,
75 )
76 }
77 }
78
79 #[derive(Clone, Copy, Debug)]
82 pub enum VerifierLimitsSource<'a> {
83 NodeConfig(&'a VerifierSigningConfig),
87
88 ProtocolConfig,
92 }
93
94 pub fn check_user_transaction_validity(
107 transaction: &Transaction,
108 protocol_config: &ProtocolConfig,
109 ) -> IotaResult {
110 if transaction.kind().is_system() {
111 return Err(IotaError::UnsupportedFeature {
112 error: "system transactions are not supported".to_string(),
113 });
114 }
115 transaction.check_serialized_size(protocol_config)?;
116 transaction.validity_check_no_gas_check(protocol_config)?;
117 transaction.check_gas_payment_size(protocol_config)?;
118 Ok(())
119 }
120
121 #[instrument(level = "trace", skip_all, fields(tx_digest = ?transaction.digest()))]
134 pub fn check_transaction_input(
135 protocol_config: &ProtocolConfig,
136 reference_gas_price: u64,
137 transaction: &Transaction,
138 input_objects: InputObjects,
139 receiving_objects: &ReceivingObjects,
140 metrics: &Arc<BytecodeVerifierMetrics>,
141 verifier_limits_source: VerifierLimitsSource<'_>,
142 authentication_gas_budget: u64,
143 is_execute_transaction_to_effects: bool,
144 input_checks: InputCheckRules,
145 ) -> IotaResult<(IotaGasStatus, CheckedInputObjects)> {
146 let gas_status = check_transaction_input_inner(
147 protocol_config,
148 reference_gas_price,
149 transaction,
150 &input_objects,
151 &[],
152 authentication_gas_budget,
153 is_execute_transaction_to_effects,
154 input_checks,
155 )?;
156 check_receiving_objects(&input_objects, receiving_objects, input_checks)?;
157 check_non_system_packages_to_be_published(
159 transaction,
160 protocol_config,
161 metrics,
162 verifier_limits_source,
163 )?;
164
165 Ok((gas_status, input_objects.into_checked()))
166 }
167
168 #[instrument(level = "trace", skip_all, fields(tx_digest = ?transaction.digest()))]
169 pub fn check_transaction_input_with_given_gas(
170 protocol_config: &ProtocolConfig,
171 reference_gas_price: u64,
172 transaction: &Transaction,
173 mut input_objects: InputObjects,
174 receiving_objects: ReceivingObjects,
175 gas_object: Object,
176 metrics: &Arc<BytecodeVerifierMetrics>,
177 verifier_limits_source: VerifierLimitsSource<'_>,
178 ) -> IotaResult<(IotaGasStatus, CheckedInputObjects)> {
179 let gas_object_ref = gas_object.object_ref();
180 input_objects.push(ObjectReadResult::new_from_gas_object(&gas_object));
181
182 let gas_status = check_transaction_input_inner(
183 protocol_config,
184 reference_gas_price,
185 transaction,
186 &input_objects,
187 &[gas_object_ref],
188 0,
189 true,
190 InputCheckRules::STRICT,
191 )?;
192 check_receiving_objects(&input_objects, &receiving_objects, InputCheckRules::STRICT)?;
193 check_non_system_packages_to_be_published(
195 transaction,
196 protocol_config,
197 metrics,
198 verifier_limits_source,
199 )?;
200
201 Ok((gas_status, input_objects.into_checked()))
202 }
203
204 #[instrument(level = "trace", skip_all)]
210 pub fn check_certificate_input(
211 cert: &VerifiedExecutableTransaction,
212 input_objects: InputObjects,
213 protocol_config: &ProtocolConfig,
214 reference_gas_price: u64,
215 ) -> IotaResult<(IotaGasStatus, CheckedInputObjects)> {
216 let transaction = cert.data().transaction();
217 let gas_status = check_transaction_input_inner(
218 protocol_config,
219 reference_gas_price,
220 transaction,
221 &input_objects,
222 &[],
223 0,
224 true,
225 InputCheckRules::STRICT,
226 )?;
227 Ok((gas_status, input_objects.into_checked()))
232 }
233
234 #[instrument(level = "trace", skip_all)]
240 pub fn check_move_authenticator_input_for_validation(
241 authenticator_input_objects: InputObjects,
242 ) -> IotaResult<CheckedInputObjects> {
243 check_move_authenticator_objects(&authenticator_input_objects)?;
244
245 Ok(authenticator_input_objects.into_checked())
246 }
247
248 pub fn aggregate_authenticator_input_objects(
252 per_authenticator_checked_input_objects: &[&CheckedInputObjects],
253 ) -> IotaResult<CheckedInputObjects> {
254 let mut aggregated_authenticator_input_objects =
255 CheckedInputObjects::new_with_checked_transaction_inputs(InputObjects::new(vec![]));
256
257 for authenticator_checked_input_objects in per_authenticator_checked_input_objects.iter() {
258 aggregated_authenticator_input_objects = checked_input_objects_union(
259 aggregated_authenticator_input_objects,
260 authenticator_checked_input_objects,
261 )?;
262 }
263
264 Ok(aggregated_authenticator_input_objects)
265 }
266
267 #[instrument(level = "trace", skip_all)]
275 pub fn check_certificate_and_move_authenticator_input(
276 transaction: &Transaction,
277 tx_input_objects: InputObjects,
278 per_authenticator_input_objects: Vec<InputObjects>,
279 receiving_objects: Option<&ReceivingObjects>,
280 authenticator_gas_budget: u64,
281 protocol_config: &ProtocolConfig,
282 reference_gas_price: u64,
283 ) -> IotaResult<(IotaGasStatus, Vec<CheckedInputObjects>, CheckedInputObjects)> {
284 per_authenticator_input_objects
286 .iter()
287 .try_for_each(check_move_authenticator_objects)?;
288
289 let gas_status = check_transaction_input_inner(
291 protocol_config,
292 reference_gas_price,
293 transaction,
294 &tx_input_objects,
295 &[],
296 authenticator_gas_budget,
297 true, InputCheckRules::STRICT,
299 )?;
300 if let Some(receiving_objects) = receiving_objects {
301 check_receiving_objects(
302 &tx_input_objects,
303 receiving_objects,
304 InputCheckRules::STRICT,
305 )?;
306 }
307
308 let per_authenticator_checked_input_objects = per_authenticator_input_objects
309 .into_iter()
310 .map(|objects| objects.into_checked())
311 .collect::<Vec<_>>();
312
313 let mut input_objects_union = tx_input_objects.into_checked();
315 for objects in per_authenticator_checked_input_objects.iter() {
316 input_objects_union = checked_input_objects_union(input_objects_union, objects)?;
317 }
318
319 Ok((
320 gas_status,
321 per_authenticator_checked_input_objects,
322 input_objects_union,
323 ))
324 }
325
326 fn check_transaction_input_inner(
328 protocol_config: &ProtocolConfig,
329 reference_gas_price: u64,
330 transaction: &Transaction,
331 input_objects: &InputObjects,
332 gas_override: &[ObjectReference],
334 authentication_gas_budget: u64,
335 is_execute_transaction_to_effects: bool,
336 input_checks: InputCheckRules,
337 ) -> IotaResult<IotaGasStatus> {
338 let gas = if gas_override.is_empty() {
340 transaction.gas()
341 } else {
342 gas_override
343 };
344
345 let gas_status = get_gas_status(
346 input_objects,
347 gas,
348 protocol_config,
349 reference_gas_price,
350 transaction,
351 authentication_gas_budget,
352 is_execute_transaction_to_effects,
353 input_checks,
354 )?;
355 check_objects(transaction, input_objects, input_checks)?;
356
357 Ok(gas_status)
358 }
359
360 fn input_object_ids(input_objects: &InputObjects) -> HashSet<ObjectId> {
361 input_objects
362 .object_kinds()
363 .map(|x| x.object_id())
364 .collect()
365 }
366
367 fn check_receiving_version_bound(object_ref: &ObjectReference) -> Result<(), IotaError> {
368 fp_ensure!(
369 object_ref.version < Version::MAX_VALID_EXCL,
370 UserInputError::InvalidSequenceNumber.into()
371 );
372 Ok(())
373 }
374
375 fn check_not_already_in_transaction(
376 objects_in_txn: &mut HashSet<ObjectId>,
377 object_ref: &ObjectReference,
378 ) -> Result<(), IotaError> {
379 fp_ensure!(
380 objects_in_txn.insert(object_ref.object_id),
381 UserInputError::DuplicateObjectRefInput.into()
382 );
383 Ok(())
384 }
385
386 #[instrument(level = "trace", skip_all)]
401 fn check_receiving_objects(
402 input_objects: &InputObjects,
403 receiving_objects: &ReceivingObjects,
404 input_checks: InputCheckRules,
405 ) -> Result<(), IotaError> {
406 let mut objects_in_txn = input_object_ids(input_objects);
407
408 for ReceivingObjectReadResult { object_ref, object } in receiving_objects.iter() {
417 check_receiving_version_bound(object_ref)?;
418
419 let Some(object) = object.as_object() else {
420 continue;
422 };
423
424 if !(object.owner.is_address()
438 && (input_checks.any_receiving_object_version
439 || object.version() == object_ref.version)
440 && (input_checks.any_receiving_object_digest
441 || object.digest() == object_ref.digest))
442 {
443 if !input_checks.any_receiving_object_version {
444 fp_ensure!(
446 object.version() == object_ref.version,
447 UserInputError::ObjectVersionUnavailableForConsumption {
448 provided_obj_ref: *object_ref,
449 current_version: object.version(),
450 }
451 .into()
452 );
453 }
454
455 fp_ensure!(
457 !object.is_package(),
458 UserInputError::MovePackageAsObject {
459 object_id: object_ref.object_id
460 }
461 .into()
462 );
463
464 if !input_checks.any_receiving_object_digest {
465 let expected_digest = object.digest();
467 fp_ensure!(
468 expected_digest == object_ref.digest,
469 UserInputError::InvalidObjectDigest {
470 object_id: object_ref.object_id,
471 expected_digest
472 }
473 .into()
474 );
475 }
476
477 match object.owner {
478 Owner::Address(_) => {
479 debug_assert!(
480 false,
481 "Receiving object {object_ref:?} is invalid but we expect it should be valid. {object:?}"
482 );
483 error!(
484 "Receiving object {:?} is invalid but we expect it should be valid. {:?}",
485 object_ref, object
486 );
487 fp_bail!(
490 UserInputError::ObjectNotFound {
491 object_id: object_ref.object_id,
492 version: Some(object_ref.version),
493 }
494 .into()
495 )
496 }
497 Owner::Object(owner) => {
498 fp_bail!(
499 UserInputError::InvalidChildObjectArgument {
500 child_id: object.id(),
501 parent_id: owner,
502 }
503 .into()
504 )
505 }
506 Owner::Shared(_) => fp_bail!(UserInputError::NotSharedObject.into()),
507 Owner::Immutable => fp_bail!(
508 UserInputError::MutableParameterExpected {
509 object_id: object_ref.object_id
510 }
511 .into()
512 ),
513 _ => {
514 unimplemented!("a new Owner enum variant was added and needs to be handled")
515 }
516 };
517 }
518
519 check_not_already_in_transaction(&mut objects_in_txn, object_ref)?;
520 }
521 Ok(())
522 }
523
524 #[instrument(level = "trace", skip_all)]
527 fn check_gas(
528 objects: &InputObjects,
529 protocol_config: &ProtocolConfig,
530 reference_gas_price: u64,
531 gas: &[ObjectReference],
532 gas_price: u64,
533 transaction_gas_budget: u64,
534 authentication_gas_budget: u64,
535 is_execute_transaction_to_effects: bool,
536 input_checks: InputCheckRules,
537 ) -> IotaResult<IotaGasStatus> {
538 let gas_budget_to_set = if authentication_gas_budget > 0 {
539 let protocol_max_auth_gas =
542 protocol_config.max_auth_gas_as_option().ok_or_else(|| {
543 UserInputError::Unsupported(
544 "Transaction requires authentication gas but max_auth_gas is not enabled"
545 .to_string(),
546 )
547 })?;
548
549 if is_execute_transaction_to_effects {
556 transaction_gas_budget
557 } else {
558 authentication_gas_budget.min(protocol_max_auth_gas)
559 }
560 } else {
561 transaction_gas_budget
564 };
565
566 let gas_budget_to_check = transaction_gas_budget;
569
570 let gas_status = IotaGasStatus::new(
571 gas_budget_to_set,
572 gas_price,
573 reference_gas_price,
574 protocol_config,
575 )?;
576
577 let objects: BTreeMap<_, _> = objects.iter().map(|o| (o.id(), o)).collect();
580 let mut gas_objects = vec![];
581 for obj_ref in gas {
582 let obj = objects.get(&obj_ref.object_id);
583 let obj = *obj.ok_or(UserInputError::ObjectNotFound {
584 object_id: obj_ref.object_id,
585 version: Some(obj_ref.version),
586 })?;
587 gas_objects.push(obj);
588 }
589 gas_status.check_gas_balance(
590 &gas_objects,
591 gas_budget_to_check,
592 !input_checks.unbounded_gas_budget,
593 )?;
594 Ok(gas_status)
595 }
596
597 #[instrument(level = "trace", skip_all)]
600 fn check_objects(
601 transaction: &Transaction,
602 objects: &InputObjects,
603 input_checks: InputCheckRules,
604 ) -> UserInputResult<()> {
605 let mut used_objects: HashSet<Address> = HashSet::new();
607 for object in objects.iter() {
608 if object.is_mutable() {
609 fp_ensure!(
610 used_objects.insert(object.id().into()),
611 UserInputError::MutableObjectUsedMoreThanOnce {
612 object_id: object.id()
613 }
614 );
615 }
616 }
617
618 if !transaction.is_genesis_tx() && objects.is_empty() {
619 return Err(UserInputError::ObjectInputArityViolation);
620 }
621
622 let gas_coins: HashSet<ObjectId> =
623 HashSet::from_iter(transaction.gas().iter().map(|obj_ref| obj_ref.object_id));
624 for object in objects.iter() {
625 let input_object_kind = object.input_object_kind;
626
627 match &object.object {
628 ObjectReadResultKind::Object(object) => {
629 let owner_address = if gas_coins.contains(&object.id()) {
631 transaction.gas_owner()
632 } else {
633 transaction.sender()
634 };
635 let system_transaction = transaction.is_system_tx();
638 check_one_object(
639 &owner_address,
640 input_object_kind,
641 object,
642 system_transaction,
643 input_checks,
644 )?;
645 }
646 ObjectReadResultKind::DeletedSharedObject(_, _) => (),
648 ObjectReadResultKind::CancelledTransactionObject(_) => (),
651 ObjectReadResultKind::UnreadSharedObject => {
654 check_shared_object_declaration(
655 &input_object_kind,
656 transaction.is_system_tx(),
657 input_checks,
658 )?;
659 }
660 }
661 }
662
663 Ok(())
664 }
665
666 fn check_one_object(
668 owner: &Address,
669 object_kind: InputObjectKind,
670 object: &Object,
671 system_transaction: bool,
672 input_checks: InputCheckRules,
673 ) -> UserInputResult {
674 match object_kind {
675 InputObjectKind::MovePackage(package_id) => {
676 fp_ensure!(
677 object.data.as_opt_package().is_some(),
678 UserInputError::MoveObjectAsPackage {
679 object_id: package_id
680 }
681 );
682 }
683 InputObjectKind::ImmOrOwnedMoveObject(object_ref) => {
684 fp_ensure!(
685 !object.is_package(),
686 UserInputError::MovePackageAsObject {
687 object_id: object_ref.object_id
688 }
689 );
690 fp_ensure!(
691 object_ref.version < Version::MAX_VALID_EXCL,
692 UserInputError::InvalidSequenceNumber
693 );
694
695 assert_eq!(
697 object.version(),
698 object_ref.version,
699 "The fetched object version {} does not match the requested version {}, object id: {}",
700 object.version(),
701 object_ref.version,
702 object.id(),
703 );
704
705 if !input_checks.any_object_digest {
707 let expected_digest = object.digest();
708 fp_ensure!(
709 expected_digest == object_ref.digest,
710 UserInputError::InvalidObjectDigest {
711 object_id: object_ref.object_id,
712 expected_digest
713 }
714 );
715 }
716
717 match object.owner {
718 Owner::Immutable => {
719 }
721 Owner::Address(actual_owner) => {
722 if !input_checks.any_object_owner {
724 fp_ensure!(
725 owner == &actual_owner,
726 UserInputError::IncorrectUserSignature {
727 error: format!(
728 "Object {} is owned by account address {}, but given owner/signer address is {}",
729 object_ref.object_id, actual_owner, owner
730 ),
731 }
732 );
733 }
734 }
735 Owner::Object(owner) => {
736 return Err(UserInputError::InvalidChildObjectArgument {
737 child_id: object.id(),
738 parent_id: owner,
739 });
740 }
741 Owner::Shared(_) => {
742 fp_ensure!(
749 input_checks.shared_object_as_owned_input
750 && !is_restricted_system_object(object_ref.object_id),
751 UserInputError::NotSharedObject
752 );
753 }
754 _ => {
755 unimplemented!("a new Owner enum variant was added and needs to be handled")
756 }
757 };
758 }
759 InputObjectKind::SharedMoveObject {
760 initial_shared_version: input_initial_shared_version,
761 ..
762 } => {
763 if check_shared_object_declaration(&object_kind, system_transaction, input_checks)?
764 {
765 return Ok(());
766 }
767
768 fp_ensure!(
769 object.version() < Version::MAX_VALID_EXCL,
770 UserInputError::InvalidSequenceNumber
771 );
772
773 match object.owner {
774 Owner::Address(_) | Owner::Object(_) | Owner::Immutable => {
775 return Err(UserInputError::NotSharedObject);
777 }
778 Owner::Shared(actual_initial_shared_version) => {
779 fp_ensure!(
780 input_checks.any_initial_shared_version
781 || input_initial_shared_version == actual_initial_shared_version,
782 UserInputError::SharedObjectStartingVersionMismatch
783 )
784 }
785 _ => {
786 unimplemented!("a new Owner enum variant was added and needs to be handled")
787 }
788 }
789 }
790 };
791
792 Ok(())
793 }
794
795 fn check_shared_object_declaration(
805 object_kind: &InputObjectKind,
806 system_transaction: bool,
807 input_checks: InputCheckRules,
808 ) -> UserInputResult<bool> {
809 match object_kind {
810 InputObjectKind::SharedMoveObject {
811 id: ObjectId::CLOCK,
812 initial_shared_version,
813 mutable: true,
814 } if *initial_shared_version == IOTA_CLOCK_OBJECT_SHARED_VERSION
815 || input_checks.any_initial_shared_version =>
816 {
817 if system_transaction {
820 Ok(true)
821 } else {
822 Err(UserInputError::ImmutableParameterExpected {
823 object_id: ObjectId::CLOCK,
824 })
825 }
826 }
827 InputObjectKind::SharedMoveObject {
828 id: ObjectId::AUTHENTICATOR_STATE,
829 ..
830 } => {
831 if system_transaction {
832 Ok(true)
833 } else {
834 Err(UserInputError::InaccessibleSystemObject {
835 object_id: ObjectId::AUTHENTICATOR_STATE,
836 })
837 }
838 }
839 InputObjectKind::SharedMoveObject {
840 id: ObjectId::RANDOMNESS_STATE,
841 mutable: true,
842 ..
843 } => {
844 if system_transaction {
847 Ok(true)
848 } else {
849 Err(UserInputError::ImmutableParameterExpected {
850 object_id: ObjectId::RANDOMNESS_STATE,
851 })
852 }
853 }
854 InputObjectKind::SharedMoveObject {
855 id: ObjectId::TRANSACTION_DENY_RULES,
856 ..
857 } => {
858 if system_transaction {
861 Ok(true)
862 } else {
863 Err(UserInputError::InaccessibleSystemObject {
864 object_id: ObjectId::TRANSACTION_DENY_RULES,
865 })
866 }
867 }
868 _ => Ok(false),
869 }
870 }
871
872 fn is_restricted_system_object(id: ObjectId) -> bool {
875 matches!(
876 id,
877 ObjectId::CLOCK
878 | ObjectId::RANDOMNESS_STATE
879 | ObjectId::AUTHENTICATOR_STATE
880 | ObjectId::TRANSACTION_DENY_RULES
881 )
882 }
883
884 #[instrument(level = "trace", skip_all)]
888 fn check_move_authenticator_objects(
889 authenticator_objects: &InputObjects,
890 ) -> UserInputResult<()> {
891 for object in authenticator_objects.iter() {
892 let input_object_kind = object.input_object_kind;
893
894 match &object.object {
895 ObjectReadResultKind::Object(object) => {
896 check_one_move_authenticator_object(input_object_kind, object)?;
897 }
898 ObjectReadResultKind::DeletedSharedObject(_, _) => (),
900 ObjectReadResultKind::CancelledTransactionObject(_) => (),
903 ObjectReadResultKind::UnreadSharedObject => {
906 check_move_authenticator_shared_object_declaration(&input_object_kind)?;
907 }
908 }
909 }
910
911 Ok(())
912 }
913
914 fn check_one_move_authenticator_object(
916 object_kind: InputObjectKind,
917 object: &Object,
918 ) -> UserInputResult {
919 match object_kind {
920 InputObjectKind::MovePackage(package_id) => {
921 fp_ensure!(
922 object.data.as_opt_package().is_some(),
923 UserInputError::MoveObjectAsPackage {
924 object_id: package_id
925 }
926 );
927 }
928 InputObjectKind::ImmOrOwnedMoveObject(object_ref) => {
929 fp_ensure!(
930 !object.is_package(),
931 UserInputError::MovePackageAsObject {
932 object_id: object_ref.object_id
933 }
934 );
935 fp_ensure!(
936 object_ref.version < Version::MAX_VALID_EXCL,
937 UserInputError::InvalidSequenceNumber
938 );
939
940 assert_eq!(
942 object.version(),
943 object_ref.version,
944 "The fetched object version {} does not match the requested version {}, object id: {}",
945 object.version(),
946 object_ref.version,
947 object.id(),
948 );
949
950 let expected_digest = object.digest();
952 fp_ensure!(
953 expected_digest == object_ref.digest,
954 UserInputError::InvalidObjectDigest {
955 object_id: object_ref.object_id,
956 expected_digest
957 }
958 );
959
960 match object.owner {
961 Owner::Immutable => {
962 }
964 Owner::Address(_) => {
965 return Err(UserInputError::AddressOwnedIsInMoveAuthenticatorInput {
966 object_id: object.id(),
967 });
968 }
969 Owner::Object(_) => {
970 return Err(UserInputError::ObjectOwnedIsInMoveAuthenticatorInput {
971 object_id: object.id(),
972 });
973 }
974 Owner::Shared(_) => {
975 return Err(UserInputError::NotSharedObject);
978 }
979 _ => {
980 unimplemented!("a new Owner enum variant was added and needs to be handled")
981 }
982 };
983 }
984 InputObjectKind::SharedMoveObject {
985 id: ObjectId::AUTHENTICATOR_STATE,
986 ..
987 } => {
988 return Err(UserInputError::InaccessibleSystemObject {
989 object_id: ObjectId::AUTHENTICATOR_STATE,
990 });
991 }
992 InputObjectKind::SharedMoveObject {
993 id, mutable: true, ..
994 } => {
995 return Err(UserInputError::MutableSharedIsInMoveAuthenticatorInput {
996 object_id: id,
997 });
998 }
999 InputObjectKind::SharedMoveObject {
1000 initial_shared_version: input_initial_shared_version,
1001 ..
1002 } => {
1003 fp_ensure!(
1004 object.version() < Version::MAX_VALID_EXCL,
1005 UserInputError::InvalidSequenceNumber
1006 );
1007
1008 match object.owner {
1009 Owner::Address(_) | Owner::Object(_) | Owner::Immutable => {
1010 return Err(UserInputError::NotSharedObject);
1012 }
1013 Owner::Shared(actual_initial_shared_version) => {
1014 fp_ensure!(
1015 input_initial_shared_version == actual_initial_shared_version,
1016 UserInputError::SharedObjectStartingVersionMismatch
1017 )
1018 }
1019 _ => {
1020 unimplemented!("a new Owner enum variant was added and needs to be handled")
1021 }
1022 }
1023 }
1024 };
1025 Ok(())
1026 }
1027
1028 fn check_move_authenticator_shared_object_declaration(
1032 object_kind: &InputObjectKind,
1033 ) -> UserInputResult {
1034 match object_kind {
1035 InputObjectKind::SharedMoveObject {
1036 id: ObjectId::AUTHENTICATOR_STATE,
1037 ..
1038 } => Err(UserInputError::InaccessibleSystemObject {
1039 object_id: ObjectId::AUTHENTICATOR_STATE,
1040 }),
1041 InputObjectKind::SharedMoveObject {
1042 id, mutable: true, ..
1043 } => Err(UserInputError::MutableSharedIsInMoveAuthenticatorInput { object_id: *id }),
1044 _ => Ok(()),
1045 }
1046 }
1047
1048 pub fn checked_input_objects_union(
1055 base_set: CheckedInputObjects,
1056 other_set: &CheckedInputObjects,
1057 ) -> IotaResult<CheckedInputObjects> {
1058 let mut base_set = base_set.into_inner();
1059 for other_object in other_set.inner().iter() {
1060 if let Some(base_object) = base_set.find_object_id_mut(other_object.id()) {
1061 assert_eq!(
1063 base_object.object, other_object.object,
1064 "The object read result for input objects with the same id must be equal"
1065 );
1066
1067 if let ObjectReadResultKind::Object(_) = &other_object.object {
1070 base_object
1071 .input_object_kind
1072 .left_union_with_checks(&other_object.input_object_kind)?;
1073 }
1074 } else {
1075 base_set.push(other_object.clone());
1076 }
1077 }
1078 Ok(base_set.into_checked())
1079 }
1080
1081 #[instrument(level = "trace", skip_all)]
1083 pub fn check_non_system_packages_to_be_published(
1084 transaction: &Transaction,
1085 protocol_config: &ProtocolConfig,
1086 metrics: &Arc<BytecodeVerifierMetrics>,
1087 verifier_limits_source: VerifierLimitsSource<'_>,
1088 ) -> UserInputResult<()> {
1089 if transaction.is_system_tx() {
1091 return Ok(());
1092 }
1093
1094 let TransactionKind::Programmable(pt) = transaction.kind() else {
1095 return Ok(());
1096 };
1097
1098 let (verifier_config, meter_config) = match verifier_limits_source {
1100 VerifierLimitsSource::NodeConfig(config) => (
1101 protocol_config.verifier_config(Some(config.limits_for_signing())),
1102 config.meter_config_for_signing(),
1103 ),
1104 VerifierLimitsSource::ProtocolConfig => (
1105 protocol_config.post_consensus_verifier_config(),
1106 protocol_config.meter_config(),
1107 ),
1108 };
1109 let mut verifier = iota_execution::verifier(protocol_config, verifier_config, metrics);
1110 let mut meter = verifier.meter(meter_config);
1111
1112 let shared_meter_verifier_timer = metrics
1114 .verifier_runtime_per_ptb_success_latency
1115 .start_timer();
1116
1117 let verifier_status = pt
1118 .non_system_packages_to_be_published()
1119 .try_for_each(|module_bytes| {
1120 verifier.meter_module_bytes(protocol_config, module_bytes, meter.as_mut())
1121 })
1122 .map_err(|e| UserInputError::PackageVerificationTimedout { err: e.to_string() });
1123
1124 match verifier_status {
1125 Ok(_) => {
1126 shared_meter_verifier_timer.stop_and_record();
1128 }
1129 Err(err) => {
1130 metrics
1133 .verifier_runtime_per_ptb_timeout_latency
1134 .observe(shared_meter_verifier_timer.stop_and_discard());
1135 return Err(err);
1136 }
1137 };
1138
1139 Ok(())
1140 }
1141
1142 #[cfg(test)]
1143 mod tests {
1144 use iota_sdk_types::{MovePackage, TransactionDigest};
1145
1146 use super::*;
1147
1148 fn package(id: ObjectId) -> Object {
1149 Object::new_from_package(
1150 MovePackage::new(
1151 id,
1152 Version::OBJECT_START,
1153 BTreeMap::new(),
1154 vec![],
1155 BTreeMap::new(),
1156 ),
1157 TransactionDigest::genesis_marker(),
1158 )
1159 }
1160
1161 fn read_as_package(id: ObjectId, object: Object) -> InputObjects {
1163 InputObjects::new(vec![ObjectReadResult::new(
1164 InputObjectKind::MovePackage(id),
1165 object.into(),
1166 )])
1167 }
1168
1169 #[test]
1170 fn authenticator_check_accepts_its_type_arg_package() {
1171 let id = ObjectId::random();
1172 check_move_authenticator_objects(&read_as_package(id, package(id))).unwrap();
1173 }
1174
1175 #[test]
1176 fn authenticator_check_rejects_an_object_named_as_a_type_arg_package() {
1177 let id = ObjectId::random();
1178 assert_eq!(
1179 check_move_authenticator_objects(&read_as_package(
1180 id,
1181 Object::immutable_with_id_for_testing(id)
1182 ))
1183 .unwrap_err(),
1184 UserInputError::MoveObjectAsPackage { object_id: id }
1185 );
1186 }
1187
1188 #[test]
1194 fn receiving_version_bound_holds_under_every_rule_set() {
1195 let object = Object::with_id_owner_for_testing(ObjectId::random(), Address::ZERO);
1196 let current = object.object_ref();
1197 let invalid_ref =
1198 ObjectReference::new(current.object_id, Version::MAX_VALID_EXCL, current.digest);
1199 let receiving_objects = ReceivingObjects::from(vec![ReceivingObjectReadResult::new(
1200 invalid_ref,
1201 object.into(),
1202 )]);
1203
1204 for input_checks in [InputCheckRules::STRICT, InputCheckRules::RELAXED_UNSAFE] {
1205 let error = check_receiving_objects(
1206 &InputObjects::new(vec![]),
1207 &receiving_objects,
1208 input_checks,
1209 )
1210 .unwrap_err();
1211 assert!(
1212 matches!(
1213 error,
1214 IotaError::UserInput {
1215 error: UserInputError::InvalidSequenceNumber
1216 }
1217 ),
1218 "{input_checks:?}: {error:?}"
1219 );
1220 }
1221 }
1222 }
1223}