@@ -18,9 +18,9 @@ use super::*;
1818impl < N : Network > Stack < N > {
1919 /// Checks that the new program definition is a valid update.
2020 #[ inline]
21- pub ( crate ) fn check_update_is_valid ( process : & Process < N > , program : & Program < N > ) -> Result < ( ) > {
21+ pub ( crate ) fn check_update_is_valid ( process : & Process < N > , new_program : & Program < N > ) -> Result < ( ) > {
2222 // Get the new program ID.
23- let program_id = program . id ( ) ;
23+ let program_id = new_program . id ( ) ;
2424 // Get the old program.
2525 let stack = process. get_stack ( program_id) ?;
2626 let old_program = stack. program ( ) ;
@@ -30,79 +30,76 @@ impl<N: Network> Stack<N> {
3030 "Cannot update '{program_id}' because it does not have a constructor"
3131 ) ;
3232 // Ensure the program ID matches.
33- ensure ! ( old_program. id( ) == program . id( ) , "Cannot update '{program_id}' with different program ID" ) ;
33+ ensure ! ( old_program. id( ) == new_program . id( ) , "Cannot update '{program_id}' with different program ID" ) ;
3434 // Ensure that all of the structs in the old program exist in the new program.
35- for ( struct_id , struct_type ) in old_program. structs ( ) {
36- let new_struct_type = program . get_struct ( struct_id ) ?;
35+ for ( old_struct_id , old_struct_type ) in old_program. structs ( ) {
36+ let new_struct_type = new_program . get_struct ( old_struct_id ) ?;
3737 ensure ! (
38- struct_type == new_struct_type,
39- "Cannot update '{program_id}' because the struct '{struct_id }' does not match"
38+ old_struct_type == new_struct_type,
39+ "Cannot update '{program_id}' because the struct '{old_struct_id }' does not match"
4040 ) ;
4141 }
4242 // Ensure that all of the records in the old program exist in the new program.
43- for ( record_id , record_type ) in old_program. records ( ) {
44- let new_record_type = program . get_record ( record_id ) ?;
43+ for ( old_record_id , old_record_type ) in old_program. records ( ) {
44+ let new_record_type = new_program . get_record ( old_record_id ) ?;
4545 ensure ! (
46- record_type == new_record_type,
47- "Cannot update '{program_id}' because the record '{record_id }' does not match"
46+ old_record_type == new_record_type,
47+ "Cannot update '{program_id}' because the record '{old_record_id }' does not match"
4848 ) ;
4949 }
5050 // Ensure that all of the mappings in the old program exist in the new program.
51- for ( mapping_id , mapping_type ) in old_program. mappings ( ) {
52- let new_mapping_type = program . get_mapping ( mapping_id ) ?;
51+ for ( old_mapping_id , old_mapping_type ) in old_program. mappings ( ) {
52+ let new_mapping_type = new_program . get_mapping ( old_mapping_id ) ?;
5353 ensure ! (
54- * mapping_type == new_mapping_type,
55- "Cannot update '{program_id}' because the mapping '{mapping_id }' does not match"
54+ * old_mapping_type == new_mapping_type,
55+ "Cannot update '{program_id}' because the mapping '{old_mapping_id }' does not match"
5656 ) ;
5757 }
5858 // Ensure that all of the imports in the old program exist in the new program.
59- for import in old_program. imports ( ) . keys ( ) {
60- if !program . contains_import ( import ) {
61- bail ! ( "Cannot update '{program_id}' because it is missing the original import '{import }'" ) ;
59+ for old_import in old_program. imports ( ) . keys ( ) {
60+ if !new_program . contains_import ( old_import ) {
61+ bail ! ( "Cannot update '{program_id}' because it is missing the original import '{old_import }'" ) ;
6262 }
6363 }
6464 // Ensure that the constructors in both programs are exactly the same.
6565 ensure ! (
66- old_program. constructor( ) == program . constructor( ) ,
66+ old_program. constructor( ) == new_program . constructor( ) ,
6767 "Cannot update '{program_id}' because the constructor does not match"
6868 ) ;
6969 // Ensure that the old program closures exist in the new program, with the exact same definition
70- for closure in old_program. closures ( ) . values ( ) {
71- let closure_name = closure . name ( ) ;
72- let new_closure = program . get_closure ( closure_name ) ?;
70+ for old_closure in old_program. closures ( ) . values ( ) {
71+ let old_closure_name = old_closure . name ( ) ;
72+ let new_closure = new_program . get_closure ( old_closure_name ) ?;
7373 ensure ! (
74- closure == & new_closure,
75- "Cannot update '{program_id}' because the closure '{closure_name }' does not exactly match"
74+ old_closure == & new_closure,
75+ "Cannot update '{program_id}' because the closure '{old_closure_name }' does not match"
7676 ) ;
7777 }
7878 // Ensure that the old program functions exist in the new program, with the same input and output types.
7979 // If the function has an associated `finalize` block, then ensure that the finalize block exists in the new program.
80- for function in old_program. functions ( ) . values ( ) {
81- let function_name = function. name ( ) ;
82- if !program. contains_function ( function. name ( ) ) {
83- bail ! ( "Cannot update '{program_id}' because it is missing the function '{function_name}'" ) ;
84- }
85- let new_function = program. get_function ( function. name ( ) ) ?;
80+ for old_function in old_program. functions ( ) . values ( ) {
81+ let old_function_name = old_function. name ( ) ;
82+ let new_function = new_program. get_function_ref ( old_function_name) ?;
8683 ensure ! (
87- function . input_types( ) == new_function. input_types( ) ,
88- "Cannot update '{program_id}' because the inputs to the function '{function_name }' do not match"
84+ old_function . input_types( ) == new_function. input_types( ) ,
85+ "Cannot update '{program_id}' because the inputs to the function '{old_function_name }' do not match"
8986 ) ;
9087 ensure ! (
91- function . output_types( ) == new_function. output_types( ) ,
92- "Cannot update '{program_id}' because the outputs of the function '{function_name }' do not match"
88+ old_function . output_types( ) == new_function. output_types( ) ,
89+ "Cannot update '{program_id}' because the outputs of the function '{old_function_name }' do not match"
9390 ) ;
94- match ( function . finalize_logic ( ) , new_function. finalize_logic ( ) ) {
91+ match ( old_function . finalize_logic ( ) , new_function. finalize_logic ( ) ) {
9592 ( None , None ) => { } // Do nothing
9693 ( None , Some ( _) ) => bail ! (
97- "Cannot update '{program_id}' because the function '{function_name }' should not have a finalize block"
94+ "Cannot update '{program_id}' because the function '{old_function_name }' should not have a finalize block"
9895 ) ,
9996 ( Some ( _) , None ) => bail ! (
100- "Cannot update '{program_id}' because the function '{function_name }' should have a finalize block"
97+ "Cannot update '{program_id}' because the function '{old_function_name }' should have a finalize block"
10198 ) ,
102- ( Some ( finalize ) , Some ( new_finalize) ) => {
99+ ( Some ( old_finalize ) , Some ( new_finalize) ) => {
103100 ensure ! (
104- finalize . input_types( ) == new_finalize. input_types( ) ,
105- "Cannot update '{program_id}' because the finalize inputs to the function '{function_name }' do not match"
101+ old_finalize . input_types( ) == new_finalize. input_types( ) ,
102+ "Cannot update '{program_id}' because the finalize inputs to the function '{old_function_name }' do not match"
106103 ) ;
107104 }
108105 }
0 commit comments