Jelajahi Sumber

Update Solana to 1.16.1 and Rust to 1.69 (#4592)

#### Problem

The 1.16 Solana crates are out, but SPL is still on 1.14 and needs the
new functionality.

#### Solution

Update the:

* rust version to 1.69
* nightly to 2023-04-19
* solana tools to 1.16.1
* solana crates to 1.16.1
* borsh to 0.10

And fix:

* new clippy warnings
* deprecated warnings in the new solana crates
Jon Cinque 2 tahun lalu
induk
melakukan
09450daf18
4 mengubah file dengan 26 tambahan dan 31 penghapusan
  1. 3 3
      program/Cargo.toml
  2. 13 13
      program/src/instruction.rs
  3. 7 7
      program/src/processor.rs
  4. 3 8
      program/src/state.rs

+ 3 - 3
program/Cargo.toml

@@ -18,15 +18,15 @@ bytemuck = "1.13.1"
 num-derive = "0.3"
 num-traits = "0.2"
 num_enum = "0.6.1"
-solana-program = "1.14.12"
+solana-program = "1.16.1"
 thiserror = "1.0"
 
 [dev-dependencies]
 lazy_static = "1.4.0"
 proptest = "1.2"
 serial_test = "2.0.0"
-solana-program-test = "1.14.12"
-solana-sdk = "1.14.12"
+solana-program-test = "1.16.1"
+solana-sdk = "1.16.1"
 
 [lib]
 crate-type = ["cdylib", "lib"]

+ 13 - 13
program/src/instruction.rs

@@ -487,7 +487,7 @@ impl<'a> TokenInstruction<'a> {
             }
             1 => Self::InitializeAccount,
             2 => {
-                let &m = rest.get(0).ok_or(InvalidInstruction)?;
+                let &m = rest.first().ok_or(InvalidInstruction)?;
                 Self::InitializeMultisig { m }
             }
             3 | 4 | 7 | 8 => {
@@ -546,7 +546,7 @@ impl<'a> TokenInstruction<'a> {
                 Self::InitializeAccount3 { owner }
             }
             19 => {
-                let &m = rest.get(0).ok_or(InvalidInstruction)?;
+                let &m = rest.first().ok_or(InvalidInstruction)?;
                 Self::InitializeMultisig2 { m }
             }
             20 => {
@@ -686,7 +686,7 @@ impl<'a> TokenInstruction<'a> {
     fn unpack_pubkey(input: &[u8]) -> Result<(Pubkey, &[u8]), ProgramError> {
         if input.len() >= 32 {
             let (key, rest) = input.split_at(32);
-            let pk = Pubkey::new(key);
+            let pk = Pubkey::try_from(key).map_err(|_| TokenError::InvalidInstruction)?;
             Ok((pk, rest))
         } else {
             Err(TokenError::InvalidInstruction.into())
@@ -698,7 +698,7 @@ impl<'a> TokenInstruction<'a> {
             Option::Some((&0, rest)) => Ok((COption::None, rest)),
             Option::Some((&1, rest)) if rest.len() >= 32 => {
                 let (key, rest) = rest.split_at(32);
-                let pk = Pubkey::new(key);
+                let pk = Pubkey::try_from(key).map_err(|_| TokenError::InvalidInstruction)?;
                 Ok((COption::Some(pk), rest))
             }
             _ => Err(TokenError::InvalidInstruction.into()),
@@ -1437,7 +1437,7 @@ mod test {
     fn test_instruction_packing() {
         let check = TokenInstruction::InitializeMint {
             decimals: 2,
-            mint_authority: Pubkey::new(&[1u8; 32]),
+            mint_authority: Pubkey::new_from_array([1u8; 32]),
             freeze_authority: COption::None,
         };
         let packed = check.pack();
@@ -1450,8 +1450,8 @@ mod test {
 
         let check = TokenInstruction::InitializeMint {
             decimals: 2,
-            mint_authority: Pubkey::new(&[2u8; 32]),
-            freeze_authority: COption::Some(Pubkey::new(&[3u8; 32])),
+            mint_authority: Pubkey::new_from_array([2u8; 32]),
+            freeze_authority: COption::Some(Pubkey::new_from_array([3u8; 32])),
         };
         let packed = check.pack();
         let mut expect = vec![0u8, 2];
@@ -1499,7 +1499,7 @@ mod test {
 
         let check = TokenInstruction::SetAuthority {
             authority_type: AuthorityType::FreezeAccount,
-            new_authority: COption::Some(Pubkey::new(&[4u8; 32])),
+            new_authority: COption::Some(Pubkey::new_from_array([4u8; 32])),
         };
         let packed = check.pack();
         let mut expect = Vec::from([6u8, 1]);
@@ -1585,7 +1585,7 @@ mod test {
         assert_eq!(unpacked, check);
 
         let check = TokenInstruction::InitializeAccount2 {
-            owner: Pubkey::new(&[2u8; 32]),
+            owner: Pubkey::new_from_array([2u8; 32]),
         };
         let packed = check.pack();
         let mut expect = vec![16u8];
@@ -1602,7 +1602,7 @@ mod test {
         assert_eq!(unpacked, check);
 
         let check = TokenInstruction::InitializeAccount3 {
-            owner: Pubkey::new(&[2u8; 32]),
+            owner: Pubkey::new_from_array([2u8; 32]),
         };
         let packed = check.pack();
         let mut expect = vec![18u8];
@@ -1620,7 +1620,7 @@ mod test {
 
         let check = TokenInstruction::InitializeMint2 {
             decimals: 2,
-            mint_authority: Pubkey::new(&[1u8; 32]),
+            mint_authority: Pubkey::new_from_array([1u8; 32]),
             freeze_authority: COption::None,
         };
         let packed = check.pack();
@@ -1633,8 +1633,8 @@ mod test {
 
         let check = TokenInstruction::InitializeMint2 {
             decimals: 2,
-            mint_authority: Pubkey::new(&[2u8; 32]),
-            freeze_authority: COption::Some(Pubkey::new(&[3u8; 32])),
+            mint_authority: Pubkey::new_from_array([2u8; 32]),
+            freeze_authority: COption::Some(Pubkey::new_from_array([3u8; 32])),
         };
         let packed = check.pack();
         let mut expect = vec![20u8, 2];

+ 7 - 7
program/src/processor.rs

@@ -1166,11 +1166,11 @@ mod tests {
     fn test_pack_unpack() {
         // Mint
         let check = Mint {
-            mint_authority: COption::Some(Pubkey::new(&[1; 32])),
+            mint_authority: COption::Some(Pubkey::new_from_array([1; 32])),
             supply: 42,
             decimals: 7,
             is_initialized: true,
-            freeze_authority: COption::Some(Pubkey::new(&[2; 32])),
+            freeze_authority: COption::Some(Pubkey::new_from_array([2; 32])),
         };
         let mut packed = vec![0; Mint::get_packed_len() + 1];
         assert_eq!(
@@ -1195,14 +1195,14 @@ mod tests {
 
         // Account
         let check = Account {
-            mint: Pubkey::new(&[1; 32]),
-            owner: Pubkey::new(&[2; 32]),
+            mint: Pubkey::new_from_array([1; 32]),
+            owner: Pubkey::new_from_array([2; 32]),
             amount: 3,
-            delegate: COption::Some(Pubkey::new(&[4; 32])),
+            delegate: COption::Some(Pubkey::new_from_array([4; 32])),
             state: AccountState::Frozen,
             is_native: COption::Some(5),
             delegated_amount: 6,
-            close_authority: COption::Some(Pubkey::new(&[7; 32])),
+            close_authority: COption::Some(Pubkey::new_from_array([7; 32])),
         };
         let mut packed = vec![0; Account::get_packed_len() + 1];
         assert_eq!(
@@ -1233,7 +1233,7 @@ mod tests {
             m: 1,
             n: 2,
             is_initialized: true,
-            signers: [Pubkey::new(&[3; 32]); MAX_SIGNERS],
+            signers: [Pubkey::new_from_array([3; 32]); MAX_SIGNERS],
         };
         let mut packed = vec![0; Multisig::get_packed_len() + 1];
         assert_eq!(

+ 3 - 8
program/src/state.rs

@@ -178,9 +178,10 @@ impl Pack for Account {
 
 /// Account state.
 #[repr(u8)]
-#[derive(Clone, Copy, Debug, PartialEq, TryFromPrimitive)]
+#[derive(Clone, Copy, Debug, Default, PartialEq, TryFromPrimitive)]
 pub enum AccountState {
     /// Account is not yet initialized
+    #[default]
     Uninitialized,
     /// Account is initialized; the account owner and/or delegate may perform permitted operations
     /// on this account
@@ -190,12 +191,6 @@ pub enum AccountState {
     Frozen,
 }
 
-impl Default for AccountState {
-    fn default() -> Self {
-        AccountState::Uninitialized
-    }
-}
-
 /// Multisignature data.
 #[repr(C)]
 #[derive(Clone, Copy, Debug, Default, PartialEq)]
@@ -232,7 +227,7 @@ impl Pack for Multisig {
             signers: [Pubkey::new_from_array([0u8; 32]); MAX_SIGNERS],
         };
         for (src, dst) in signers_flat.chunks(32).zip(result.signers.iter_mut()) {
-            *dst = Pubkey::new(src);
+            *dst = Pubkey::try_from(src).map_err(|_| ProgramError::InvalidAccountData)?;
         }
         Ok(result)
     }