Add way to create account with delegate in 1 tx
This commit is contained in:
@ -32,6 +32,25 @@ impl<'a> VoteBank<'a> {
|
|||||||
self.bank.process_transaction(&tx)
|
self.bank.process_transaction(&tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_vote_account_with_delegate(
|
||||||
|
&self,
|
||||||
|
from_keypair: &Keypair,
|
||||||
|
vote_keypair: &Keypair,
|
||||||
|
delegate_id: Pubkey,
|
||||||
|
lamports: u64,
|
||||||
|
) -> Result<()> {
|
||||||
|
let blockhash = self.bank.last_blockhash();
|
||||||
|
let tx = VoteTransaction::new_account_with_delegate(
|
||||||
|
from_keypair,
|
||||||
|
vote_keypair,
|
||||||
|
delegate_id,
|
||||||
|
blockhash,
|
||||||
|
lamports,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
self.bank.process_transaction(&tx)
|
||||||
|
}
|
||||||
|
|
||||||
fn submit_vote(&self, vote_keypair: &Keypair, tick_height: u64) -> Result<VoteState> {
|
fn submit_vote(&self, vote_keypair: &Keypair, tick_height: u64) -> Result<VoteState> {
|
||||||
let blockhash = self.bank.last_blockhash();
|
let blockhash = self.bank.last_blockhash();
|
||||||
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, blockhash, 0);
|
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, blockhash, 0);
|
||||||
@ -44,7 +63,7 @@ impl<'a> VoteBank<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vote_via_bank() {
|
fn test_vote_bank_basic() {
|
||||||
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
|
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
|
||||||
let bank = Bank::new(&genesis_block);
|
let bank = Bank::new(&genesis_block);
|
||||||
let vote_bank = VoteBank::new(&bank);
|
let vote_bank = VoteBank::new(&bank);
|
||||||
@ -59,6 +78,19 @@ fn test_vote_via_bank() {
|
|||||||
assert_eq!(vote_state.votes.len(), 1);
|
assert_eq!(vote_state.votes.len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_vote_bank_delegate() {
|
||||||
|
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
|
||||||
|
let bank = Bank::new(&genesis_block);
|
||||||
|
let vote_bank = VoteBank::new(&bank);
|
||||||
|
let vote_keypair = Keypair::new();
|
||||||
|
let delegate_keypair = Keypair::new();
|
||||||
|
let delegate_id = delegate_keypair.pubkey();
|
||||||
|
vote_bank
|
||||||
|
.create_vote_account_with_delegate(&from_keypair, &vote_keypair, delegate_id, 100)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vote_via_bank_with_no_signature() {
|
fn test_vote_via_bank_with_no_signature() {
|
||||||
let (genesis_block, mallory_keypair) = GenesisBlock::new(10_000);
|
let (genesis_block, mallory_keypair) = GenesisBlock::new(10_000);
|
||||||
|
@ -8,8 +8,7 @@ use solana_sdk::hash::Hash;
|
|||||||
use solana_sdk::pubkey::Pubkey;
|
use solana_sdk::pubkey::Pubkey;
|
||||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||||
use solana_sdk::system_instruction::SystemInstruction;
|
use solana_sdk::system_instruction::SystemInstruction;
|
||||||
use solana_sdk::system_program;
|
use solana_sdk::transaction::Transaction;
|
||||||
use solana_sdk::transaction::{Instruction, Transaction};
|
|
||||||
use solana_sdk::transaction_builder::TransactionBuilder;
|
use solana_sdk::transaction_builder::TransactionBuilder;
|
||||||
|
|
||||||
pub struct VoteTransaction {}
|
pub struct VoteTransaction {}
|
||||||
@ -30,27 +29,48 @@ impl VoteTransaction {
|
|||||||
/// Fund or create the staking account with tokens
|
/// Fund or create the staking account with tokens
|
||||||
pub fn new_account(
|
pub fn new_account(
|
||||||
from_keypair: &Keypair,
|
from_keypair: &Keypair,
|
||||||
vote_account_id: Pubkey,
|
voter_id: Pubkey,
|
||||||
recent_blockhash: Hash,
|
recent_blockhash: Hash,
|
||||||
num_tokens: u64,
|
num_tokens: u64,
|
||||||
fee: u64,
|
fee: u64,
|
||||||
) -> Transaction {
|
) -> Transaction {
|
||||||
let create_tx = SystemInstruction::CreateAccount {
|
let from_id = from_keypair.pubkey();
|
||||||
tokens: num_tokens,
|
let space = VoteState::max_size() as u64;
|
||||||
space: VoteState::max_size() as u64,
|
TransactionBuilder::new(fee)
|
||||||
program_id: id(),
|
.push(SystemInstruction::new_program_account(
|
||||||
};
|
from_id,
|
||||||
Transaction::new_with_instructions(
|
voter_id,
|
||||||
&[from_keypair],
|
num_tokens,
|
||||||
&[vote_account_id],
|
space,
|
||||||
recent_blockhash,
|
id(),
|
||||||
fee,
|
))
|
||||||
vec![system_program::id(), id()],
|
.push(VoteInstruction::new_initialize_account(voter_id))
|
||||||
vec![
|
.sign(&[from_keypair], recent_blockhash)
|
||||||
Instruction::new(0, &create_tx, vec![0, 1]),
|
}
|
||||||
Instruction::new(1, &VoteInstruction::InitializeAccount, vec![1]),
|
|
||||||
],
|
/// Fund or create the staking account with tokens
|
||||||
)
|
pub fn new_account_with_delegate(
|
||||||
|
from_keypair: &Keypair,
|
||||||
|
voter_keypair: &Keypair,
|
||||||
|
delegate_id: Pubkey,
|
||||||
|
recent_blockhash: Hash,
|
||||||
|
num_tokens: u64,
|
||||||
|
fee: u64,
|
||||||
|
) -> Transaction {
|
||||||
|
let from_id = from_keypair.pubkey();
|
||||||
|
let voter_id = voter_keypair.pubkey();
|
||||||
|
let space = VoteState::max_size() as u64;
|
||||||
|
TransactionBuilder::new(fee)
|
||||||
|
.push(SystemInstruction::new_program_account(
|
||||||
|
from_id,
|
||||||
|
voter_id,
|
||||||
|
num_tokens,
|
||||||
|
space,
|
||||||
|
id(),
|
||||||
|
))
|
||||||
|
.push(VoteInstruction::new_initialize_account(voter_id))
|
||||||
|
.push(VoteInstruction::new_delegate_stake(voter_id, delegate_id))
|
||||||
|
.sign(&[from_keypair, voter_keypair], recent_blockhash)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Choose a node id to `delegate` or `assign` this vote account to
|
/// Choose a node id to `delegate` or `assign` this vote account to
|
||||||
|
Reference in New Issue
Block a user