Add way to create account with delegate in 1 tx

This commit is contained in:
Greg Fitzgerald 2019-03-05 15:19:06 -07:00
parent b9e878ee80
commit ca99ebaaf4
2 changed files with 72 additions and 20 deletions

View File

@ -32,6 +32,25 @@ impl<'a> VoteBank<'a> {
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> {
let blockhash = self.bank.last_blockhash();
let tx = VoteTransaction::new_vote(vote_keypair, tick_height, blockhash, 0);
@ -44,7 +63,7 @@ impl<'a> VoteBank<'a> {
}
#[test]
fn test_vote_via_bank() {
fn test_vote_bank_basic() {
let (genesis_block, from_keypair) = GenesisBlock::new(10_000);
let bank = Bank::new(&genesis_block);
let vote_bank = VoteBank::new(&bank);
@ -59,6 +78,19 @@ fn test_vote_via_bank() {
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]
fn test_vote_via_bank_with_no_signature() {
let (genesis_block, mallory_keypair) = GenesisBlock::new(10_000);

View File

@ -8,8 +8,7 @@ use solana_sdk::hash::Hash;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::system_instruction::SystemInstruction;
use solana_sdk::system_program;
use solana_sdk::transaction::{Instruction, Transaction};
use solana_sdk::transaction::Transaction;
use solana_sdk::transaction_builder::TransactionBuilder;
pub struct VoteTransaction {}
@ -30,27 +29,48 @@ impl VoteTransaction {
/// Fund or create the staking account with tokens
pub fn new_account(
from_keypair: &Keypair,
vote_account_id: Pubkey,
voter_id: Pubkey,
recent_blockhash: Hash,
num_tokens: u64,
fee: u64,
) -> Transaction {
let create_tx = SystemInstruction::CreateAccount {
tokens: num_tokens,
space: VoteState::max_size() as u64,
program_id: id(),
};
Transaction::new_with_instructions(
&[from_keypair],
&[vote_account_id],
recent_blockhash,
fee,
vec![system_program::id(), id()],
vec![
Instruction::new(0, &create_tx, vec![0, 1]),
Instruction::new(1, &VoteInstruction::InitializeAccount, vec![1]),
],
)
let from_id = from_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))
.sign(&[from_keypair], recent_blockhash)
}
/// 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