Browse Source

token: Transition perf-monitor to solana-program-test (#2586)

* token: Transition perf-monitor to solana-program-test

* fmt

* Refactor for clarity
Jon Cinque 3 năm trước cách đây
mục cha
commit
838283d9bc
3 tập tin đã thay đổi với 426 bổ sung0 xóa
  1. 2 0
      program/Cargo.toml
  2. 140 0
      program/tests/action.rs
  3. 284 0
      program/tests/assert_instruction_count.rs

+ 2 - 0
program/Cargo.toml

@@ -10,6 +10,7 @@ exclude = ["js/**"]
 
 [features]
 no-entrypoint = []
+test-bpf = []
 
 [dependencies]
 arrayref = "0.3.6"
@@ -20,6 +21,7 @@ solana-program = "1.8.1"
 thiserror = "1.0"
 
 [dev-dependencies]
+solana-program-test = "1.8.1"
 solana-sdk = "1.8.1"
 
 [lib]

+ 140 - 0
program/tests/action.rs

@@ -0,0 +1,140 @@
+use {
+    solana_program_test::BanksClient,
+    solana_sdk::{
+        hash::Hash,
+        program_pack::Pack,
+        pubkey::Pubkey,
+        signature::{Keypair, Signer},
+        system_instruction,
+        transaction::Transaction,
+        transport::TransportError,
+    },
+    spl_token::{
+        id, instruction,
+        state::{Account, Mint},
+    },
+};
+
+pub async fn create_mint(
+    banks_client: &mut BanksClient,
+    payer: &Keypair,
+    recent_blockhash: Hash,
+    pool_mint: &Keypair,
+    manager: &Pubkey,
+    decimals: u8,
+) -> Result<(), TransportError> {
+    let rent = banks_client.get_rent().await.unwrap();
+    let mint_rent = rent.minimum_balance(Mint::LEN);
+
+    let transaction = Transaction::new_signed_with_payer(
+        &[
+            system_instruction::create_account(
+                &payer.pubkey(),
+                &pool_mint.pubkey(),
+                mint_rent,
+                Mint::LEN as u64,
+                &id(),
+            ),
+            instruction::initialize_mint(&id(), &pool_mint.pubkey(), manager, None, decimals)
+                .unwrap(),
+        ],
+        Some(&payer.pubkey()),
+        &[payer, pool_mint],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await?;
+    Ok(())
+}
+
+pub async fn create_account(
+    banks_client: &mut BanksClient,
+    payer: &Keypair,
+    recent_blockhash: Hash,
+    account: &Keypair,
+    pool_mint: &Pubkey,
+    owner: &Pubkey,
+) -> Result<(), TransportError> {
+    let rent = banks_client.get_rent().await.unwrap();
+    let account_rent = rent.minimum_balance(Account::LEN);
+
+    let transaction = Transaction::new_signed_with_payer(
+        &[
+            system_instruction::create_account(
+                &payer.pubkey(),
+                &account.pubkey(),
+                account_rent,
+                Account::LEN as u64,
+                &id(),
+            ),
+            instruction::initialize_account(&id(), &account.pubkey(), pool_mint, owner).unwrap(),
+        ],
+        Some(&payer.pubkey()),
+        &[payer, account],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await?;
+    Ok(())
+}
+
+pub async fn mint_to(
+    banks_client: &mut BanksClient,
+    payer: &Keypair,
+    recent_blockhash: Hash,
+    mint: &Pubkey,
+    account: &Pubkey,
+    mint_authority: &Keypair,
+    amount: u64,
+) -> Result<(), TransportError> {
+    let transaction = Transaction::new_signed_with_payer(
+        &[
+            instruction::mint_to(&id(), mint, account, &mint_authority.pubkey(), &[], amount)
+                .unwrap(),
+        ],
+        Some(&payer.pubkey()),
+        &[payer, mint_authority],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await?;
+    Ok(())
+}
+
+pub async fn transfer(
+    banks_client: &mut BanksClient,
+    payer: &Keypair,
+    recent_blockhash: Hash,
+    source: &Pubkey,
+    destination: &Pubkey,
+    authority: &Keypair,
+    amount: u64,
+) -> Result<(), TransportError> {
+    let transaction = Transaction::new_signed_with_payer(
+        &[
+            instruction::transfer(&id(), source, destination, &authority.pubkey(), &[], amount)
+                .unwrap(),
+        ],
+        Some(&payer.pubkey()),
+        &[payer, authority],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await?;
+    Ok(())
+}
+
+pub async fn burn(
+    banks_client: &mut BanksClient,
+    payer: &Keypair,
+    recent_blockhash: Hash,
+    mint: &Pubkey,
+    account: &Pubkey,
+    authority: &Keypair,
+    amount: u64,
+) -> Result<(), TransportError> {
+    let transaction = Transaction::new_signed_with_payer(
+        &[instruction::burn(&id(), account, mint, &authority.pubkey(), &[], amount).unwrap()],
+        Some(&payer.pubkey()),
+        &[payer, authority],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await?;
+    Ok(())
+}

+ 284 - 0
program/tests/assert_instruction_count.rs

@@ -0,0 +1,284 @@
+#![cfg(feature = "test-bpf")]
+
+mod action;
+use {
+    solana_program_test::{processor, tokio, ProgramTest},
+    solana_sdk::{
+        program_pack::Pack,
+        pubkey::Pubkey,
+        signature::{Keypair, Signer},
+        system_instruction,
+        transaction::Transaction,
+    },
+    spl_token::{
+        id, instruction,
+        processor::Processor,
+        state::{Account, Mint},
+    },
+};
+
+const TRANSFER_AMOUNT: u64 = 1_000_000_000_000_000;
+
+#[tokio::test]
+async fn initialize_mint() {
+    let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
+    pt.set_bpf_compute_max_units(2_500); // last known 2252
+    let (mut banks_client, payer, recent_blockhash) = pt.start().await;
+
+    let owner_key = Pubkey::new_unique();
+    let mint = Keypair::new();
+    let decimals = 9;
+
+    let rent = banks_client.get_rent().await.unwrap();
+    let mint_rent = rent.minimum_balance(Mint::LEN);
+    let transaction = Transaction::new_signed_with_payer(
+        &[system_instruction::create_account(
+            &payer.pubkey(),
+            &mint.pubkey(),
+            mint_rent,
+            Mint::LEN as u64,
+            &id(),
+        )],
+        Some(&payer.pubkey()),
+        &[&payer, &mint],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await.unwrap();
+
+    let transaction = Transaction::new_signed_with_payer(
+        &[
+            instruction::initialize_mint(&id(), &mint.pubkey(), &owner_key, None, decimals)
+                .unwrap(),
+        ],
+        Some(&payer.pubkey()),
+        &[&payer],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await.unwrap();
+}
+
+#[tokio::test]
+async fn initialize_account() {
+    let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
+    pt.set_bpf_compute_max_units(4_000); // last known 3284
+    let (mut banks_client, payer, recent_blockhash) = pt.start().await;
+
+    let owner = Keypair::new();
+    let mint = Keypair::new();
+    let account = Keypair::new();
+    let decimals = 9;
+
+    action::create_mint(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &mint,
+        &owner.pubkey(),
+        decimals,
+    )
+    .await
+    .unwrap();
+    let rent = banks_client.get_rent().await.unwrap();
+    let account_rent = rent.minimum_balance(Account::LEN);
+    let transaction = Transaction::new_signed_with_payer(
+        &[system_instruction::create_account(
+            &payer.pubkey(),
+            &account.pubkey(),
+            account_rent,
+            Account::LEN as u64,
+            &id(),
+        )],
+        Some(&payer.pubkey()),
+        &[&payer, &account],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await.unwrap();
+
+    let transaction = Transaction::new_signed_with_payer(
+        &[instruction::initialize_account(
+            &id(),
+            &account.pubkey(),
+            &mint.pubkey(),
+            &owner.pubkey(),
+        )
+        .unwrap()],
+        Some(&payer.pubkey()),
+        &[&payer],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await.unwrap();
+}
+
+#[tokio::test]
+async fn mint_to() {
+    let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
+    pt.set_bpf_compute_max_units(4_000); // last known 2668
+    let (mut banks_client, payer, recent_blockhash) = pt.start().await;
+
+    let owner = Keypair::new();
+    let mint = Keypair::new();
+    let account = Keypair::new();
+    let decimals = 9;
+
+    action::create_mint(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &mint,
+        &owner.pubkey(),
+        decimals,
+    )
+    .await
+    .unwrap();
+    action::create_account(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &account,
+        &mint.pubkey(),
+        &owner.pubkey(),
+    )
+    .await
+    .unwrap();
+
+    let transaction = Transaction::new_signed_with_payer(
+        &[instruction::mint_to(
+            &id(),
+            &mint.pubkey(),
+            &account.pubkey(),
+            &owner.pubkey(),
+            &[],
+            TRANSFER_AMOUNT,
+        )
+        .unwrap()],
+        Some(&payer.pubkey()),
+        &[&payer, &owner],
+        recent_blockhash,
+    );
+    banks_client.process_transaction(transaction).await.unwrap();
+}
+
+#[tokio::test]
+async fn transfer() {
+    let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
+    pt.set_bpf_compute_max_units(4_000); // last known 2972
+    let (mut banks_client, payer, recent_blockhash) = pt.start().await;
+
+    let owner = Keypair::new();
+    let mint = Keypair::new();
+    let source = Keypair::new();
+    let destination = Keypair::new();
+    let decimals = 9;
+
+    action::create_mint(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &mint,
+        &owner.pubkey(),
+        decimals,
+    )
+    .await
+    .unwrap();
+    action::create_account(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &source,
+        &mint.pubkey(),
+        &owner.pubkey(),
+    )
+    .await
+    .unwrap();
+    action::create_account(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &destination,
+        &mint.pubkey(),
+        &owner.pubkey(),
+    )
+    .await
+    .unwrap();
+
+    action::mint_to(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &mint.pubkey(),
+        &source.pubkey(),
+        &owner,
+        TRANSFER_AMOUNT,
+    )
+    .await
+    .unwrap();
+
+    action::transfer(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &source.pubkey(),
+        &destination.pubkey(),
+        &owner,
+        TRANSFER_AMOUNT,
+    )
+    .await
+    .unwrap();
+}
+
+#[tokio::test]
+async fn burn() {
+    let mut pt = ProgramTest::new("spl_token", id(), processor!(Processor::process));
+    pt.set_bpf_compute_max_units(4_000); // last known 2655
+    let (mut banks_client, payer, recent_blockhash) = pt.start().await;
+
+    let owner = Keypair::new();
+    let mint = Keypair::new();
+    let account = Keypair::new();
+    let decimals = 9;
+
+    action::create_mint(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &mint,
+        &owner.pubkey(),
+        decimals,
+    )
+    .await
+    .unwrap();
+    action::create_account(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &account,
+        &mint.pubkey(),
+        &owner.pubkey(),
+    )
+    .await
+    .unwrap();
+
+    action::mint_to(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &mint.pubkey(),
+        &account.pubkey(),
+        &owner,
+        TRANSFER_AMOUNT,
+    )
+    .await
+    .unwrap();
+
+    action::burn(
+        &mut banks_client,
+        &payer,
+        recent_blockhash,
+        &mint.pubkey(),
+        &account.pubkey(),
+        &owner,
+        TRANSFER_AMOUNT,
+    )
+    .await
+    .unwrap();
+}