sui_adapter_latest/static_programmable_transactions/linkage/
resolved_linkage.rs1use crate::{
5 data_store::VerifiedPackageStore,
6 static_programmable_transactions::linkage::{
7 config::ResolutionConfig,
8 resolution::{PackageResolution, ResolutionTable, VersionConstraint},
9 },
10};
11use move_vm_runtime::shared::linkage_context::LinkageContext;
12use std::{borrow::Borrow, collections::BTreeMap, rc::Rc};
13use sui_types::{base_types::ObjectID, error::ExecutionErrorTrait};
14
15#[derive(Clone, Debug)]
16pub struct ExecutableLinkage(pub Rc<ResolvedLinkage>);
17
18impl ExecutableLinkage {
19 pub fn new(resolved_linkage: ResolvedLinkage) -> Self {
20 Self(Rc::new(resolved_linkage))
21 }
22
23 pub fn type_linkage<I, E>(
27 config: ResolutionConfig,
28 ids: I,
29 store: &VerifiedPackageStore<'_>,
30 ) -> Result<Self, E>
31 where
32 E: ExecutionErrorTrait,
33 I: IntoIterator,
34 I::Item: Borrow<ObjectID>,
35 {
36 let mut resolution_table = ResolutionTable::empty(config);
37 resolution_table.add_type_linkages_to_table(ids, store)?;
38 Ok(Self::new(ResolvedLinkage::from_resolution_table(
39 resolution_table,
40 )))
41 }
42
43 pub fn linkage_context<E: ExecutionErrorTrait>(&self) -> Result<LinkageContext, E> {
44 LinkageContext::new(self.0.linkage.iter().map(|(k, v)| (**k, **v)).collect()).map_err(|e| {
45 make_invariant_violation!(
46 "Failed to create linkage context from resolved linkage: {:?}",
47 e
48 )
49 .into()
50 })
51 }
52}
53
54#[derive(Debug)]
55pub struct ResolvedLinkage {
56 pub linkage: BTreeMap<ObjectID, ObjectID>,
58 pub linkage_resolution: BTreeMap<ObjectID, PackageResolution>,
62}
63
64impl ResolvedLinkage {
65 pub fn resolve_to_original_id(&self, object_id: &ObjectID) -> Option<ObjectID> {
67 self.linkage_resolution
68 .get(object_id)
69 .map(|resolution| resolution.original_id)
70 }
71
72 pub fn resolved_version(&self, object_id: &ObjectID) -> Option<u64> {
75 self.linkage_resolution
76 .get(object_id)
77 .and_then(|resolution| resolution.version)
78 }
79
80 pub(crate) fn from_resolution_table(resolution_table: ResolutionTable) -> Self {
82 let mut linkage = BTreeMap::new();
83 for (original_id, resolution) in resolution_table.resolution_table {
84 match resolution {
85 VersionConstraint::Exact(_version, object_id)
86 | VersionConstraint::AtLeast(_version, object_id) => {
87 linkage.insert(original_id, object_id);
88 }
89 }
90 }
91 Self {
92 linkage,
93 linkage_resolution: resolution_table.all_versions_resolution_table,
94 }
95 }
96
97 pub fn update_for_publication(
100 package_version_id: ObjectID,
101 original_package_id: ObjectID,
102 mut resolved_linkage: ResolvedLinkage,
103 ) -> ExecutableLinkage {
104 resolved_linkage
106 .linkage
107 .insert(original_package_id, package_version_id);
108 resolved_linkage.linkage_resolution.insert(
110 package_version_id,
111 PackageResolution {
112 original_id: original_package_id,
113 version: None,
114 },
115 );
116 ExecutableLinkage::new(resolved_linkage)
117 }
118}