@@ -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 }
@@ -91,7 +123,14 @@ impl fmt::Display for Resource {
91123impl Resource {
92124 /// Creates a reference to the bundled resource.
93125 pub fn bundled ( bundled : String ) -> Resource {
94- Resource :: Bundled { bundled }
126+ Resource :: Bundled {
127+ version : BundledSystemScriptVersion :: V0_5_4 ,
128+ bundled,
129+ }
130+ }
131+
132+ pub fn bundled_with_version ( version : BundledSystemScriptVersion , bundled : String ) -> Resource {
133+ Resource :: Bundled { version, bundled }
95134 }
96135
97136 /// Creates a reference to the resource resident in the file system.
@@ -163,9 +202,15 @@ impl Resource {
163202 /// The file system resource exists only when the file exists.
164203 pub fn exists ( & self ) -> bool {
165204 match self {
166- Resource :: Bundled { bundled } => {
167- SourceFiles :: new ( & BUNDLED_CELL , & BUNDLED ) . is_available ( bundled)
205+ Resource :: Bundled { bundled, version } => match version {
206+ BundledSystemScriptVersion :: V0_5_4 => {
207+ SourceFiles :: new ( & BUNDLED_CELL_v0_5_4 , & BUNDLED )
208+ }
209+ BundledSystemScriptVersion :: V0_6_0 => {
210+ SourceFiles :: new ( & BUNDLED_CELL_v0_6_0 , & BUNDLED )
211+ }
168212 }
213+ . is_available ( bundled) ,
169214 Resource :: FileSystem { file } => file. exists ( ) ,
170215 Resource :: Raw { .. } => true ,
171216 }
@@ -195,7 +240,15 @@ impl Resource {
195240 /// Gets resource content.
196241 pub fn get ( & self ) -> Result < Cow < ' static , [ u8 ] > > {
197242 match self {
198- Resource :: Bundled { bundled } => SourceFiles :: new ( & BUNDLED_CELL , & BUNDLED ) . get ( bundled) ,
243+ Resource :: Bundled { bundled, version } => match version {
244+ BundledSystemScriptVersion :: V0_5_4 => {
245+ SourceFiles :: new ( & BUNDLED_CELL_v0_5_4 , & BUNDLED )
246+ }
247+ BundledSystemScriptVersion :: V0_6_0 => {
248+ SourceFiles :: new ( & BUNDLED_CELL_v0_6_0 , & BUNDLED )
249+ }
250+ }
251+ . get ( bundled) ,
199252 Resource :: FileSystem { file } => Ok ( Cow :: Owned ( fs:: read ( file) ?) ) ,
200253 Resource :: Raw { raw } => Ok ( Cow :: Owned ( raw. to_owned ( ) . into_bytes ( ) ) ) ,
201254 }
@@ -204,9 +257,15 @@ impl Resource {
204257 /// Gets resource content via an input stream.
205258 pub fn read ( & self ) -> Result < Box < dyn Read > > {
206259 match self {
207- Resource :: Bundled { bundled } => {
208- SourceFiles :: new ( & BUNDLED_CELL , & BUNDLED ) . read ( bundled)
260+ Resource :: Bundled { bundled, version } => match version {
261+ BundledSystemScriptVersion :: V0_5_4 => {
262+ SourceFiles :: new ( & BUNDLED_CELL_v0_5_4 , & BUNDLED )
263+ }
264+ BundledSystemScriptVersion :: V0_6_0 => {
265+ SourceFiles :: new ( & BUNDLED_CELL_v0_6_0 , & BUNDLED )
266+ }
209267 }
268+ . read ( bundled) ,
210269 Resource :: FileSystem { file } => Ok ( Box :: new ( BufReader :: new ( fs:: File :: open ( file) ?) ) ) ,
211270 Resource :: Raw { raw } => Ok ( Box :: new ( Cursor :: new ( raw. to_owned ( ) . into_bytes ( ) ) ) ) ,
212271 }
@@ -222,7 +281,10 @@ impl Resource {
222281 /// See [Template](struct.Template.html).
223282 pub fn export < P : AsRef < Path > > ( & self , context : & TemplateContext < ' _ > , root_dir : P ) -> Result < ( ) > {
224283 let key = match self {
225- Resource :: Bundled { bundled } => bundled,
284+ Resource :: Bundled {
285+ bundled,
286+ version : _,
287+ } => bundled,
226288 _ => return Ok ( ( ) ) ,
227289 } ;
228290 let target = join_bundled_key ( root_dir. as_ref ( ) . to_path_buf ( ) , key) ;
0 commit comments