@@ -47,10 +47,19 @@ fn build_initialize() -> Vec<u8> {
4747}
4848
4949/// Build switch_power instruction data (discriminator = 1).
50- /// Wire format: [disc=1] [name: String]
50+ ///
51+ /// Wire format: [discriminator = 1] [name: u8 length prefix + bytes].
52+ ///
53+ /// The lever's switch_power instruction takes `String<50>`, which Quasar
54+ /// serialises with a single-byte length prefix (matching every other
55+ /// Quasar program: account-data, close-account, rent, realloc,
56+ /// repository-layout). An earlier version of this builder used a u32
57+ /// length prefix, which produced a malformed payload that happened to
58+ /// pass because the handler ignored the deserialised name.
5159fn build_switch_power ( name : & str ) -> Vec < u8 > {
52- let mut data = vec ! [ 1u8 ] ; // discriminator = 1
53- data. extend_from_slice ( & ( name. len ( ) as u32 ) . to_le_bytes ( ) ) ;
60+ let mut data = Vec :: with_capacity ( 2 + name. len ( ) ) ;
61+ data. push ( 1u8 ) ; // discriminator = 1
62+ data. push ( name. len ( ) as u8 ) ;
5463 data. extend_from_slice ( name. as_bytes ( ) ) ;
5564 data
5665}
@@ -104,6 +113,12 @@ fn test_switch_power_on() {
104113 let logs = result. logs . join ( "\n " ) ;
105114 assert ! ( logs. contains( "pulling the power switch" ) , "should log switch" ) ;
106115 assert ! ( logs. contains( "now on" ) , "should say power is on" ) ;
116+ // Verifies wire format: a stale u32 length prefix would corrupt the
117+ // deserialised name (e.g. "\0\0\0Al" instead of "Alice").
118+ assert ! (
119+ logs. contains( "Alice" ) ,
120+ "deserialised name should round-trip exactly; logs: {logs}"
121+ ) ;
107122
108123 let account = result. account ( & power_addr) . unwrap ( ) ;
109124 assert_eq ! ( account. data[ 1 ] , 1 , "power should now be on" ) ;
@@ -128,6 +143,10 @@ fn test_switch_power_off() {
128143
129144 let logs = result. logs . join ( "\n " ) ;
130145 assert ! ( logs. contains( "now off" ) , "should say power is off" ) ;
146+ assert ! (
147+ logs. contains( "Bob" ) ,
148+ "deserialised name should round-trip exactly; logs: {logs}"
149+ ) ;
131150
132151 let account = result. account ( & power_addr) . unwrap ( ) ;
133152 assert_eq ! ( account. data[ 1 ] , 0 , "power should now be off" ) ;
0 commit comments