process_transaction.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. use {
  2. assert_matches::assert_matches, solana_ed25519_program::new_ed25519_instruction,
  3. solana_instruction::error::InstructionError, solana_precompile_error::PrecompileError,
  4. solana_program_test::*, solana_signer::Signer, solana_transaction::Transaction,
  5. solana_transaction_error::TransactionError,
  6. };
  7. // Since ed25519_dalek is still using the old version of rand, this test
  8. // copies the `generate` implementation at:
  9. // https://docs.rs/ed25519-dalek/1.0.1/src/ed25519_dalek/secret.rs.html#167
  10. fn generate_keypair() -> ed25519_dalek::Keypair {
  11. use rand::RngCore;
  12. let mut rng = rand::thread_rng();
  13. let mut seed = [0u8; ed25519_dalek::SECRET_KEY_LENGTH];
  14. rng.fill_bytes(&mut seed);
  15. let secret =
  16. ed25519_dalek::SecretKey::from_bytes(&seed[..ed25519_dalek::SECRET_KEY_LENGTH]).unwrap();
  17. let public = ed25519_dalek::PublicKey::from(&secret);
  18. ed25519_dalek::Keypair { secret, public }
  19. }
  20. #[tokio::test]
  21. async fn test_success() {
  22. let mut context = ProgramTest::default().start_with_context().await;
  23. let client = &mut context.banks_client;
  24. let payer = &context.payer;
  25. let recent_blockhash = context.last_blockhash;
  26. let privkey = generate_keypair();
  27. let message_arr = b"hello";
  28. let instruction = new_ed25519_instruction(&privkey, message_arr);
  29. let transaction = Transaction::new_signed_with_payer(
  30. &[instruction],
  31. Some(&payer.pubkey()),
  32. &[payer],
  33. recent_blockhash,
  34. );
  35. assert_matches!(client.process_transaction(transaction).await, Ok(()));
  36. }
  37. #[tokio::test]
  38. async fn test_failure() {
  39. let mut context = ProgramTest::default().start_with_context().await;
  40. let client = &mut context.banks_client;
  41. let payer = &context.payer;
  42. let recent_blockhash = context.last_blockhash;
  43. let privkey = generate_keypair();
  44. let message_arr = b"hello";
  45. let mut instruction = new_ed25519_instruction(&privkey, message_arr);
  46. instruction.data[0] += 1;
  47. let transaction = Transaction::new_signed_with_payer(
  48. &[instruction],
  49. Some(&payer.pubkey()),
  50. &[payer],
  51. recent_blockhash,
  52. );
  53. assert_matches!(
  54. client.process_transaction(transaction).await,
  55. Err(BanksClientError::TransactionError(
  56. TransactionError::InstructionError(0, InstructionError::Custom(3))
  57. ))
  58. );
  59. // this assert is for documenting the matched error code above
  60. assert_eq!(3, PrecompileError::InvalidDataOffsets as u32);
  61. }