Update Move support to accomadate Libra's changes to compiler behavior (#6993)

This commit is contained in:
Jack May
2019-11-18 16:47:01 -08:00
committed by GitHub
parent cbf7c0080b
commit 6ec918fabb
13 changed files with 475 additions and 540 deletions

View File

@@ -25,12 +25,12 @@ use solana_sdk::system_instruction;
use types::account_config;
pub fn create_genesis<T: Client>(from_key: &Keypair, client: &T, amount: u64) -> Keypair {
let libra_genesis_key = Keypair::new();
pub fn create_genesis<T: Client>(from: &Keypair, client: &T, amount: u64) -> Keypair {
let genesis = Keypair::new();
let instruction = system_instruction::create_account(
&from_key.pubkey(),
&libra_genesis_key.pubkey(),
&from.pubkey(),
&genesis.pubkey(),
1,
bincode::serialized_size(&LibraAccountState::create_genesis(amount).unwrap()).unwrap()
as u64,
@@ -38,35 +38,33 @@ pub fn create_genesis<T: Client>(from_key: &Keypair, client: &T, amount: u64) ->
);
client
.send_message(
&[&from_key, &libra_genesis_key],
Message::new(vec![instruction]),
)
.send_message(&[&from, &genesis], Message::new(vec![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();
let instruction = librapay_instruction::genesis(&genesis.pubkey(), amount);
let message = Message::new_with_payer(vec![instruction], Some(&from.pubkey()));
client.send_message(&[from, &genesis], message).unwrap();
libra_genesis_key
genesis
}
pub fn upload_move_program<T: Client>(from: &Keypair, client: &T, code: &str) -> Pubkey {
pub fn publish_module<T: Client>(from: &Keypair, client: &T, code: &str) -> Pubkey {
let address = account_config::association_address();
let account_state = LibraAccountState::create_program(&address, code, vec![]);
let program_bytes = bincode::serialize(&account_state).unwrap();
let account_state = LibraAccountState::create_module(&address, code, vec![]);
let bytes = bincode::serialize(&account_state).unwrap();
load_program(
client,
&from,
&solana_sdk::move_loader::id(),
program_bytes,
)
load_program(client, &from, &solana_sdk::move_loader::id(), bytes)
}
pub fn upload_mint_program<T: Client>(from: &Keypair, client: &T) -> Pubkey {
pub fn upload_script<T: Client>(from: &Keypair, client: &T, code: &str) -> Pubkey {
let address = account_config::association_address();
let account_state = LibraAccountState::create_script(&address, code, vec![]);
let bytes = bincode::serialize(&account_state).unwrap();
load_program(client, &from, &solana_sdk::move_loader::id(), bytes)
}
pub fn upload_mint_script<T: Client>(from: &Keypair, client: &T) -> Pubkey {
let code = "
import 0x0.LibraAccount;
import 0x0.LibraCoin;
@@ -74,10 +72,9 @@ pub fn upload_mint_program<T: Client>(from: &Keypair, client: &T) -> Pubkey {
LibraAccount.mint_to_address(move(payee), move(amount));
return;
}";
upload_move_program(from, client, code)
upload_script(from, client, code)
}
pub fn upload_payment_program<T: Client>(from: &Keypair, client: &T) -> Pubkey {
pub fn upload_payment_script<T: Client>(from: &Keypair, client: &T) -> Pubkey {
let code = "
import 0x0.LibraAccount;
import 0x0.LibraCoin;
@@ -87,7 +84,7 @@ pub fn upload_payment_program<T: Client>(from: &Keypair, client: &T) -> Pubkey {
}
";
upload_move_program(from, client, code)
upload_script(from, client, code)
}
pub fn process_instruction(

View File

@@ -10,15 +10,13 @@ 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_sdk::move_loader::id(), &ix_data, accounts)
}
pub fn mint(
program_pubkey: &Pubkey,
from_pubkey: &Pubkey,
script_pubkey: &Pubkey,
genesis_pubkey: &Pubkey,
to_pubkey: &Pubkey,
microlibras: u64,
) -> Instruction {
@@ -27,7 +25,7 @@ pub fn mint(
TransactionArgument::U64(microlibras),
];
let data = bincode::serialize(&InvokeCommand::RunProgram {
let data = bincode::serialize(&InvokeCommand::RunScript {
sender_address: account_config::association_address(),
function_name: "main".to_string(),
args,
@@ -36,8 +34,8 @@ pub fn mint(
let ix_data = LoaderInstruction::InvokeMain { data };
let accounts = vec![
AccountMeta::new_readonly(*program_pubkey, false),
AccountMeta::new(*from_pubkey, true),
AccountMeta::new_readonly(*script_pubkey, false),
AccountMeta::new(*genesis_pubkey, true),
AccountMeta::new(*to_pubkey, false),
];
@@ -45,8 +43,8 @@ pub fn mint(
}
pub fn transfer(
program_pubkey: &Pubkey,
mint_pubkey: &Pubkey,
script_pubkey: &Pubkey,
genesis_pubkey: &Pubkey,
from_pubkey: &Pubkey,
to_pubkey: &Pubkey,
microlibras: u64,
@@ -56,7 +54,7 @@ pub fn transfer(
TransactionArgument::U64(microlibras),
];
let data = bincode::serialize(&InvokeCommand::RunProgram {
let data = bincode::serialize(&InvokeCommand::RunScript {
sender_address: pubkey_to_address(from_pubkey),
function_name: "main".to_string(),
args,
@@ -65,8 +63,8 @@ pub fn transfer(
let ix_data = LoaderInstruction::InvokeMain { data };
let accounts = vec![
AccountMeta::new_readonly(*program_pubkey, false),
AccountMeta::new_readonly(*mint_pubkey, false),
AccountMeta::new_readonly(*script_pubkey, false),
AccountMeta::new_readonly(*genesis_pubkey, false),
AccountMeta::new(*from_pubkey, true),
AccountMeta::new(*to_pubkey, false),
];
@@ -80,19 +78,19 @@ mod tests {
#[test]
fn test_pay() {
let from = Pubkey::new_rand();
let to = Pubkey::new_rand();
let from_pubkey = Pubkey::new_rand();
let to_pubkey = Pubkey::new_rand();
let program_id = Pubkey::new_rand();
let mint_id = Pubkey::new_rand();
transfer(&program_id, &mint_id, &from, &to, 1);
transfer(&program_id, &mint_id, &from_pubkey, &to_pubkey, 1);
}
#[test]
fn test_mint() {
let program_id = Pubkey::new_rand();
let from = Pubkey::new_rand();
let to = Pubkey::new_rand();
let from_pubkey = Pubkey::new_rand();
let to_pubkey = Pubkey::new_rand();
mint(&program_id, &from, &to, 1);
mint(&program_id, &from_pubkey, &to_pubkey, 1);
}
}

View File

@@ -3,6 +3,7 @@ use log::*;
use solana_move_loader_api::account_state::{pubkey_to_address, LibraAccountState};
use solana_move_loader_api::data_store::DataStore;
use solana_sdk::client::Client;
use solana_sdk::commitment_config::CommitmentConfig;
use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
@@ -12,67 +13,74 @@ 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);
pub fn create_genesis(keypair: &Keypair, microlibras: u64, recent_blockhash: Hash) -> Transaction {
let ix = librapay_instruction::genesis(&keypair.pubkey(), microlibras);
Transaction::new_signed_with_payer(
vec![ix],
Some(&genesis_keypair.pubkey()),
&[genesis_keypair],
Some(&keypair.pubkey()),
&[keypair],
recent_blockhash,
)
}
pub fn mint_tokens(
program_id: &Pubkey,
payer: &Keypair,
mint: &Keypair,
to: &Pubkey,
script_pubkey: &Pubkey,
payer_keypair: &Keypair,
genesis_keypair: &Keypair,
to_pubkey: &Pubkey,
microlibras: u64,
recent_blockhash: Hash,
) -> Transaction {
let ix = librapay_instruction::mint(program_id, &mint.pubkey(), to, microlibras);
let ix = librapay_instruction::mint(
script_pubkey,
&genesis_keypair.pubkey(),
to_pubkey,
microlibras,
);
Transaction::new_signed_with_payer(
vec![ix],
Some(&payer.pubkey()),
&[payer, mint],
Some(&payer_keypair.pubkey()),
&[payer_keypair, genesis_keypair],
recent_blockhash,
)
}
pub fn transfer(
program_id: &Pubkey,
mint: &Pubkey,
payer: &Keypair,
from: &Keypair,
to: &Pubkey,
script_pubkey: &Pubkey,
genesis_pubkey: &Pubkey,
payer_keypair: &Keypair,
from_keypair: &Keypair,
to_pubkey: &Pubkey,
microlibras: u64,
recent_blockhash: Hash,
) -> Transaction {
let ix = librapay_instruction::transfer(program_id, mint, &from.pubkey(), to, microlibras);
let ix = librapay_instruction::transfer(
script_pubkey,
genesis_pubkey,
&from_keypair.pubkey(),
to_pubkey,
microlibras,
);
Transaction::new_signed_with_payer(
vec![ix],
Some(&payer.pubkey()),
&[payer, from],
Some(&payer_keypair.pubkey()),
&[payer_keypair, from_keypair],
recent_blockhash,
)
}
pub fn create_accounts(
from: &Keypair,
to: &[&Keypair],
from_keypair: &Keypair,
to_keypair: &[&Keypair],
lamports: u64,
recent_blockhash: Hash,
) -> Transaction {
let instructions = to
let instructions = to_keypair
.iter()
.map(|to| {
.map(|keypair| {
system_instruction::create_account(
&from.pubkey(),
&to.pubkey(),
&from_keypair.pubkey(),
&keypair.pubkey(),
lamports,
400,
&solana_sdk::move_loader::id(),
@@ -80,18 +88,18 @@ pub fn create_accounts(
})
.collect();
let mut from_signers = vec![from];
from_signers.extend_from_slice(to);
let mut from_signers = vec![from_keypair];
from_signers.extend_from_slice(to_keypair);
Transaction::new_signed_instructions(&from_signers, instructions, recent_blockhash)
}
pub fn create_account(
from: &Keypair,
to: &Keypair,
from_keypair: &Keypair,
to_keypair: &Keypair,
lamports: u64,
recent_blockhash: Hash,
) -> Transaction {
create_accounts(from, &[to], lamports, recent_blockhash)
create_accounts(from_keypair, &[to_keypair], lamports, recent_blockhash)
}
#[derive(Debug)]
@@ -109,12 +117,13 @@ impl error::Error for LibrapayError {}
pub fn get_libra_balance<T: Client>(
client: &T,
account_address: &Pubkey,
pubkey: &Pubkey,
) -> Result<u64, Box<dyn error::Error>> {
let account = client.get_account_data(&account_address)?;
if let Some(account) = account {
if let Some(account) =
client.get_account_with_commitment(&pubkey, CommitmentConfig::recent())?
{
let mut data_store = DataStore::default();
match bincode::deserialize(&account)? {
match bincode::deserialize(&account.data)? {
LibraAccountState::User(_, write_set) => {
data_store.apply_write_set(&write_set);
}
@@ -127,9 +136,8 @@ pub fn get_libra_balance<T: Client>(
}
}
let resource = data_store
.read_account_resource(&pubkey_to_address(account_address))
.read_account_resource(&pubkey_to_address(pubkey))
.unwrap();
let res = resource.balance();
Ok(res)
} else {
@@ -140,7 +148,7 @@ pub fn get_libra_balance<T: Client>(
#[cfg(test)]
mod tests {
use super::*;
use crate::{create_genesis, upload_mint_program, upload_payment_program};
use crate::{create_genesis, upload_mint_script, upload_payment_script};
use solana_runtime::bank::Bank;
use solana_runtime::bank_client::BankClient;
use solana_sdk::genesis_config::create_genesis_config;
@@ -148,7 +156,7 @@ mod tests {
use std::sync::Arc;
fn create_bank(lamports: u64) -> (Arc<Bank>, Keypair, Keypair, Pubkey, Pubkey) {
let (genesis_config, mint_keypair) = create_genesis_config(lamports);
let (genesis_config, mint) = create_genesis_config(lamports);
let mut bank = Bank::new(&genesis_config);
bank.add_instruction_processor(
solana_sdk::move_loader::id(),
@@ -156,59 +164,73 @@ 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);
let genesis_pubkey = create_genesis(&mint, &bank_client, 1_000_000);
let mint_script_pubkey = upload_mint_script(&mint, &bank_client);
let script_pubkey = upload_payment_script(&mint, &bank_client);
(
shared_bank,
mint_keypair,
genesis_keypair,
mint_program_pubkey,
program_pubkey,
mint,
genesis_pubkey,
mint_script_pubkey,
script_pubkey,
)
}
#[test]
fn test_transfer() {
let (bank, mint_keypair, libra_genesis_keypair, mint_program_id, program_id) =
create_bank(10_000);
let from = Keypair::new();
let to = Keypair::new();
solana_logger::setup();
let tx = create_accounts(&mint_keypair, &[&from, &to], 1, bank.last_blockhash());
let (bank, mint, genesis_keypair, mint_script_pubkey, payment_script_pubkey) =
create_bank(10_000);
let from_keypair = Keypair::new();
let to_keypair = Keypair::new();
let tx = create_accounts(
&mint,
&[&from_keypair, &to_keypair],
1,
bank.last_blockhash(),
);
bank.process_transaction(&tx).unwrap();
info!(
"created accounts: mint: {} libra_mint: {}",
mint_keypair.pubkey(),
libra_genesis_keypair.pubkey()
"created accounts: mint: {} genesis_pubkey: {}",
mint.pubkey(),
genesis_keypair.pubkey()
);
info!(
" from: {} to: {}",
from_keypair.pubkey(),
to_keypair.pubkey()
);
info!(" from: {} to: {}", from.pubkey(), to.pubkey());
let tx = mint_tokens(
&mint_program_id,
&mint_keypair,
&libra_genesis_keypair,
&from.pubkey(),
&mint_script_pubkey,
&mint,
&genesis_keypair,
&from_keypair.pubkey(),
1,
bank.last_blockhash(),
);
bank.process_transaction(&tx).unwrap();
let client = BankClient::new_shared(&bank);
assert_eq!(1, get_libra_balance(&client, &from.pubkey()).unwrap());
assert_eq!(
1,
get_libra_balance(&client, &from_keypair.pubkey()).unwrap()
);
info!("passed mint... doing another transfer..");
let tx = transfer(
&program_id,
&libra_genesis_keypair.pubkey(),
&mint_keypair,
&from,
&to.pubkey(),
&payment_script_pubkey,
&genesis_keypair.pubkey(),
&mint,
&from_keypair,
&to_keypair.pubkey(),
1,
bank.last_blockhash(),
);
bank.process_transaction(&tx).unwrap();
assert_eq!(1, get_libra_balance(&client, &to.pubkey()).unwrap());
assert_eq!(1, get_libra_balance(&client, &to_keypair.pubkey()).unwrap());
}
}