Add command to create genesis accounts (#5343)
This commit is contained in:
@@ -18,11 +18,36 @@ use solana_runtime::loader_utils::load_program;
|
||||
use solana_sdk::account::KeyedAccount;
|
||||
use solana_sdk::client::Client;
|
||||
use solana_sdk::instruction::InstructionError;
|
||||
use solana_sdk::message::Message;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::Keypair;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction;
|
||||
|
||||
use types::account_address::AccountAddress;
|
||||
|
||||
pub fn create_genesis<T: Client>(from_key: &Keypair, client: &T, amount: u64) -> Keypair {
|
||||
let libra_genesis_key = Keypair::new();
|
||||
|
||||
let instruction = system_instruction::create_account(
|
||||
&from_key.pubkey(),
|
||||
&libra_genesis_key.pubkey(),
|
||||
1,
|
||||
bincode::serialize(&LibraAccountState::create_genesis(1))
|
||||
.unwrap()
|
||||
.len() as u64,
|
||||
&solana_move_loader_api::id(),
|
||||
);
|
||||
client.send_instruction(&from_key, instruction).unwrap();
|
||||
|
||||
let instruction = librapay_instruction::genesis(&libra_genesis_key.pubkey(), amount);
|
||||
let message = Message::new_with_payer(vec![instruction], Some(&from_key.pubkey()));
|
||||
client
|
||||
.send_message(&[from_key, &libra_genesis_key], message)
|
||||
.unwrap();
|
||||
|
||||
libra_genesis_key
|
||||
}
|
||||
|
||||
pub fn upload_move_program<T: Client>(from: &Keypair, client: &T, code: &str) -> Pubkey {
|
||||
let address = AccountAddress::default();
|
||||
let account_state = LibraAccountState::create_program(&address, code, vec![]);
|
||||
|
@@ -1,14 +1,23 @@
|
||||
use bincode;
|
||||
use solana_move_loader_api::account_state::pubkey_to_address;
|
||||
use solana_move_loader_api::processor::InvokeInfo;
|
||||
use solana_move_loader_api::processor::InvokeCommand;
|
||||
use solana_sdk::instruction::{AccountMeta, Instruction};
|
||||
use solana_sdk::loader_instruction::LoaderInstruction;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use types::account_address::AccountAddress;
|
||||
use types::transaction::TransactionArgument;
|
||||
|
||||
pub fn genesis(genesis_pubkey: &Pubkey, microlibras: u64) -> Instruction {
|
||||
let data = bincode::serialize(&InvokeCommand::CreateGenesis(microlibras)).unwrap();
|
||||
let ix_data = LoaderInstruction::InvokeMain { data };
|
||||
|
||||
let accounts = vec![AccountMeta::new(*genesis_pubkey, true)];
|
||||
|
||||
Instruction::new(solana_move_loader_api::id(), &ix_data, accounts)
|
||||
}
|
||||
|
||||
pub fn mint(
|
||||
program_id: &Pubkey,
|
||||
program_pubkey: &Pubkey,
|
||||
from_pubkey: &Pubkey,
|
||||
to_pubkey: &Pubkey,
|
||||
microlibras: u64,
|
||||
@@ -18,16 +27,16 @@ pub fn mint(
|
||||
TransactionArgument::U64(microlibras),
|
||||
];
|
||||
|
||||
let invoke_info = InvokeInfo {
|
||||
let data = bincode::serialize(&InvokeCommand::RunProgram {
|
||||
sender_address: AccountAddress::default(),
|
||||
function_name: "main".to_string(),
|
||||
args,
|
||||
};
|
||||
let data = bincode::serialize(&invoke_info).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
let ix_data = LoaderInstruction::InvokeMain { data };
|
||||
|
||||
let accounts = vec![
|
||||
AccountMeta::new_credit_only(*program_id, false),
|
||||
AccountMeta::new_credit_only(*program_pubkey, false),
|
||||
AccountMeta::new(*from_pubkey, true),
|
||||
AccountMeta::new(*to_pubkey, false),
|
||||
];
|
||||
@@ -36,7 +45,7 @@ pub fn mint(
|
||||
}
|
||||
|
||||
pub fn transfer(
|
||||
program_id: &Pubkey,
|
||||
program_pubkey: &Pubkey,
|
||||
mint_pubkey: &Pubkey,
|
||||
from_pubkey: &Pubkey,
|
||||
to_pubkey: &Pubkey,
|
||||
@@ -47,16 +56,16 @@ pub fn transfer(
|
||||
TransactionArgument::U64(microlibras),
|
||||
];
|
||||
|
||||
let invoke_info = InvokeInfo {
|
||||
let data = bincode::serialize(&InvokeCommand::RunProgram {
|
||||
sender_address: pubkey_to_address(from_pubkey),
|
||||
function_name: "main".to_string(),
|
||||
args,
|
||||
};
|
||||
let data = bincode::serialize(&invoke_info).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
let ix_data = LoaderInstruction::InvokeMain { data };
|
||||
|
||||
let accounts = vec![
|
||||
AccountMeta::new_credit_only(*program_id, false),
|
||||
AccountMeta::new_credit_only(*program_pubkey, false),
|
||||
AccountMeta::new_credit_only(*mint_pubkey, false),
|
||||
AccountMeta::new(*from_pubkey, true),
|
||||
AccountMeta::new(*to_pubkey, false),
|
||||
|
@@ -3,7 +3,6 @@ use language_e2e_tests::account::AccountResource;
|
||||
use log::*;
|
||||
use solana_move_loader_api::account_state::{pubkey_to_address, LibraAccountState};
|
||||
use solana_move_loader_api::data_store::DataStore;
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::client::Client;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
@@ -14,6 +13,20 @@ use std::boxed::Box;
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
|
||||
pub fn create_genesis(
|
||||
genesis_keypair: &Keypair,
|
||||
microlibras: u64,
|
||||
recent_blockhash: Hash,
|
||||
) -> Transaction {
|
||||
let ix = librapay_instruction::genesis(&genesis_keypair.pubkey(), microlibras);
|
||||
Transaction::new_signed_with_payer(
|
||||
vec![ix],
|
||||
Some(&genesis_keypair.pubkey()),
|
||||
&[genesis_keypair],
|
||||
recent_blockhash,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn mint_tokens(
|
||||
program_id: &Pubkey,
|
||||
payer: &Keypair,
|
||||
@@ -100,7 +113,7 @@ pub fn get_libra_balance<T: Client>(
|
||||
if let Some(account) = account {
|
||||
let mut data_store = DataStore::default();
|
||||
match bincode::deserialize(&account)? {
|
||||
LibraAccountState::User(write_set) => {
|
||||
LibraAccountState::User(_, write_set) => {
|
||||
data_store.apply_write_set(&write_set);
|
||||
}
|
||||
LibraAccountState::Unallocated => {
|
||||
@@ -122,45 +135,18 @@ pub fn get_libra_balance<T: Client>(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_libra_genesis_account(microlibras: u64) -> Account {
|
||||
let libra_genesis_data =
|
||||
bincode::serialize(&LibraAccountState::create_genesis(microlibras)).unwrap();
|
||||
Account {
|
||||
lamports: 1,
|
||||
data: libra_genesis_data,
|
||||
owner: solana_move_loader_api::id(),
|
||||
executable: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{upload_mint_program, upload_payment_program};
|
||||
use crate::{create_genesis, upload_mint_program, upload_payment_program};
|
||||
use solana_runtime::bank::Bank;
|
||||
use solana_runtime::bank_client::BankClient;
|
||||
use solana_sdk::account::Account;
|
||||
use solana_sdk::genesis_block::GenesisBlock;
|
||||
use solana_sdk::genesis_block::create_genesis_block;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_program;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn create_bank(lamports: u64) -> (Arc<Bank>, Keypair, Keypair, Pubkey, Pubkey) {
|
||||
let libra_genesis_account = create_libra_genesis_account(lamports);
|
||||
//let (genesis_block, mint_keypair) = create_genesis_block(lamports);
|
||||
let mint_keypair = Keypair::new();
|
||||
let libra_mint_keypair = Keypair::new();
|
||||
let genesis_block = GenesisBlock::new(
|
||||
&[
|
||||
(
|
||||
mint_keypair.pubkey(),
|
||||
Account::new(lamports, 0, &system_program::id()),
|
||||
),
|
||||
(libra_mint_keypair.pubkey(), libra_genesis_account),
|
||||
],
|
||||
&[],
|
||||
);
|
||||
|
||||
let (genesis_block, mint_keypair) = create_genesis_block(lamports);
|
||||
let mut bank = Bank::new(&genesis_block);
|
||||
bank.add_instruction_processor(
|
||||
solana_move_loader_api::id(),
|
||||
@@ -168,12 +154,13 @@ mod tests {
|
||||
);
|
||||
let shared_bank = Arc::new(bank);
|
||||
let bank_client = BankClient::new_shared(&shared_bank);
|
||||
let genesis_keypair = create_genesis(&mint_keypair, &bank_client, 1_000_000);
|
||||
let mint_program_pubkey = upload_mint_program(&mint_keypair, &bank_client);
|
||||
let program_pubkey = upload_payment_program(&mint_keypair, &bank_client);
|
||||
(
|
||||
shared_bank,
|
||||
mint_keypair,
|
||||
libra_mint_keypair,
|
||||
genesis_keypair,
|
||||
mint_program_pubkey,
|
||||
program_pubkey,
|
||||
)
|
||||
@@ -181,7 +168,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_transfer() {
|
||||
let (bank, mint_keypair, libra_mint_keypair, mint_program_id, program_id) =
|
||||
let (bank, mint_keypair, libra_genesis_keypair, mint_program_id, program_id) =
|
||||
create_bank(10_000);
|
||||
let from = Keypair::new();
|
||||
let to = Keypair::new();
|
||||
@@ -197,14 +184,14 @@ mod tests {
|
||||
info!(
|
||||
"created accounts: mint: {} libra_mint: {}",
|
||||
mint_keypair.pubkey(),
|
||||
libra_mint_keypair.pubkey()
|
||||
libra_genesis_keypair.pubkey()
|
||||
);
|
||||
info!(" from: {} to: {}", from.pubkey(), to.pubkey());
|
||||
|
||||
let tx = mint_tokens(
|
||||
&mint_program_id,
|
||||
&mint_keypair,
|
||||
&libra_mint_keypair,
|
||||
&libra_genesis_keypair,
|
||||
&from.pubkey(),
|
||||
1,
|
||||
bank.last_blockhash(),
|
||||
@@ -217,7 +204,7 @@ mod tests {
|
||||
|
||||
let tx = transfer(
|
||||
&program_id,
|
||||
&libra_mint_keypair.pubkey(),
|
||||
&libra_genesis_keypair.pubkey(),
|
||||
&mint_keypair,
|
||||
&from,
|
||||
&to.pubkey(),
|
||||
|
Reference in New Issue
Block a user