@@ -36,7 +36,11 @@ use std::fs;
3636use std:: io:: { self , BufReader , Cursor , Read } ;
3737use std:: path:: { Path , PathBuf } ;
3838
39- use ckb_system_scripts:: BUNDLED_CELL ;
39+ use ckb_system_scripts_v0_5_4:: BUNDLED_CELL ;
40+ /// get multisig_legacy from v_0_5_4
41+ use ckb_system_scripts_v0_5_4:: BUNDLED_CELL as BUNDLED_CELL_v0_5_4 ;
42+ /// get multisg_v1 from v0_6_0
43+ use ckb_system_scripts_v0_6_0:: BUNDLED_CELL as BUNDLED_CELL_v0_6_0 ;
4044
4145mod bundled {
4246 #![ allow( missing_docs, clippy:: unreadable_literal) ]
@@ -56,13 +60,39 @@ pub const SPEC_DEV_FILE_NAME: &str = "specs/dev.toml";
5660/// The file name of the generated RocksDB options file.
5761pub const DB_OPTIONS_FILE_NAME : & str = "default.db-options" ;
5862
63+ #[ derive( Clone , Debug , Eq , PartialEq , Serialize , Deserialize ) ]
64+ pub enum BundledSystemScriptVersion {
65+ /// The bundled resource version is 0.5.4. this version is used before `CKB v0.201.0`
66+ #[ serde( rename = "v0.5.4" ) ]
67+ V0_5_4 ,
68+ /// The bundled resource version is 0.6.0. This version contains multisig v2
69+ #[ serde( rename = "v0.6.0" ) ]
70+ V0_6_0 ,
71+ }
72+
73+ impl std:: fmt:: Display for BundledSystemScriptVersion {
74+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
75+ match self {
76+ BundledSystemScriptVersion :: V0_5_4 => write ! ( f, "v0.5.4" ) ,
77+ BundledSystemScriptVersion :: V0_6_0 => write ! ( f, "v0.6.0" ) ,
78+ }
79+ }
80+ }
81+
82+ fn default_bundled_version ( ) -> BundledSystemScriptVersion {
83+ BundledSystemScriptVersion :: V0_5_4
84+ }
85+
5986/// Represents a resource, which is either bundled in the CKB binary or resident in the local file
6087/// system.
6188#[ derive( Clone , Debug , Eq , PartialEq , Serialize , Deserialize ) ]
6289#[ serde( untagged) ]
6390pub enum Resource {
6491 /// A resource that bundled in the CKB binary.
6592 Bundled {
93+ /// The version of the bundled resource. default is v0.5.4
94+ #[ serde( default = "default_bundled_version" ) ]
95+ version : BundledSystemScriptVersion ,
6696 /// The identifier of the bundled resource.
6797 bundled : String ,
6898 } ,
@@ -81,7 +111,9 @@ pub enum Resource {
81111impl fmt:: Display for Resource {
82112 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
83113 match self {
84- Resource :: Bundled { bundled } => write ! ( f, "Bundled({bundled})" ) ,
114+ Resource :: Bundled { bundled, version } => {
115+ write ! ( f, "Bundled(bundled: {bundled}, version:{version}))" )
116+ }
85117 Resource :: FileSystem { file } => write ! ( f, "FileSystem({})" , file. display( ) ) ,
86118 Resource :: Raw { raw } => write ! ( f, "Raw({})" , raw) ,
87119 }
@@ -90,8 +122,18 @@ impl fmt::Display for Resource {
90122
91123impl Resource {
92124 /// Creates a reference to the bundled resource.
125+ /// This function, will use ckb_system_scripts v0.5.4 crate
93126 pub fn bundled ( bundled : String ) -> Resource {
94- Resource :: Bundled { bundled }
127+ Resource :: Bundled {
128+ version : BundledSystemScriptVersion :: V0_5_4 ,
129+ bundled,
130+ }
131+ }
132+
133+ /// Creates a reference to the bundled resource.
134+ /// use specific version of ckb_system_scripts crate
135+ pub fn bundled_with_version ( version : BundledSystemScriptVersion , bundled : String ) -> Resource {
136+ Resource :: Bundled { version, bundled }
95137 }
96138
97139 /// Creates a reference to the resource resident in the file system.
@@ -163,9 +205,15 @@ impl Resource {
163205 /// The file system resource exists only when the file exists.
164206 pub fn exists ( & self ) -> bool {
165207 match self {
166- Resource :: Bundled { bundled } => {
167- SourceFiles :: new ( & BUNDLED_CELL , & BUNDLED ) . is_available ( bundled)
208+ Resource :: Bundled { bundled, version } => match version {
209+ BundledSystemScriptVersion :: V0_5_4 => {
210+ SourceFiles :: new ( & BUNDLED_CELL_v0_5_4 , & BUNDLED )
211+ }
212+ BundledSystemScriptVersion :: V0_6_0 => {
213+ SourceFiles :: new ( & BUNDLED_CELL_v0_6_0 , & BUNDLED )
214+ }
168215 }
216+ . is_available ( bundled) ,
169217 Resource :: FileSystem { file } => file. exists ( ) ,
170218 Resource :: Raw { .. } => true ,
171219 }
@@ -195,7 +243,15 @@ impl Resource {
195243 /// Gets resource content.
196244 pub fn get ( & self ) -> Result < Cow < ' static , [ u8 ] > > {
197245 match self {
198- Resource :: Bundled { bundled } => SourceFiles :: new ( & BUNDLED_CELL , & BUNDLED ) . get ( bundled) ,
246+ Resource :: Bundled { bundled, version } => match version {
247+ BundledSystemScriptVersion :: V0_5_4 => {
248+ SourceFiles :: new ( & BUNDLED_CELL_v0_5_4 , & BUNDLED )
249+ }
250+ BundledSystemScriptVersion :: V0_6_0 => {
251+ SourceFiles :: new ( & BUNDLED_CELL_v0_6_0 , & BUNDLED )
252+ }
253+ }
254+ . get ( bundled) ,
199255 Resource :: FileSystem { file } => Ok ( Cow :: Owned ( fs:: read ( file) ?) ) ,
200256 Resource :: Raw { raw } => Ok ( Cow :: Owned ( raw. to_owned ( ) . into_bytes ( ) ) ) ,
201257 }
@@ -204,9 +260,15 @@ impl Resource {
204260 /// Gets resource content via an input stream.
205261 pub fn read ( & self ) -> Result < Box < dyn Read > > {
206262 match self {
207- Resource :: Bundled { bundled } => {
208- SourceFiles :: new ( & BUNDLED_CELL , & BUNDLED ) . read ( bundled)
263+ Resource :: Bundled { bundled, version } => match version {
264+ BundledSystemScriptVersion :: V0_5_4 => {
265+ SourceFiles :: new ( & BUNDLED_CELL_v0_5_4 , & BUNDLED )
266+ }
267+ BundledSystemScriptVersion :: V0_6_0 => {
268+ SourceFiles :: new ( & BUNDLED_CELL_v0_6_0 , & BUNDLED )
269+ }
209270 }
271+ . read ( bundled) ,
210272 Resource :: FileSystem { file } => Ok ( Box :: new ( BufReader :: new ( fs:: File :: open ( file) ?) ) ) ,
211273 Resource :: Raw { raw } => Ok ( Box :: new ( Cursor :: new ( raw. to_owned ( ) . into_bytes ( ) ) ) ) ,
212274 }
@@ -222,7 +284,10 @@ impl Resource {
222284 /// See [Template](struct.Template.html).
223285 pub fn export < P : AsRef < Path > > ( & self , context : & TemplateContext < ' _ > , root_dir : P ) -> Result < ( ) > {
224286 let key = match self {
225- Resource :: Bundled { bundled } => bundled,
287+ Resource :: Bundled {
288+ bundled,
289+ version : _,
290+ } => bundled,
226291 _ => return Ok ( ( ) ) ,
227292 } ;
228293 let target = join_bundled_key ( root_dir. as_ref ( ) . to_path_buf ( ) , key) ;
0 commit comments