Remove bpf tictactoe

This commit is contained in:
Michael Vines
2018-10-29 21:11:58 -07:00
committed by Grimes
parent 9d5092a71c
commit dc3b270410
5 changed files with 0 additions and 632 deletions

View File

@@ -330,268 +330,3 @@ fn test_program_bpf_noop_c() {
loader.bank.process_transactions(&vec![tx.clone()]),
);
}
#[cfg(feature = "bpf_c")]
struct TicTacToe {
game: Keypair,
}
#[cfg(feature = "bpf_c")]
impl TicTacToe {
pub fn new(loader: &Loader, program: &Program) -> Self {
let game = Keypair::new();
// Create game account
let tx = Transaction::system_create(
&loader.mint.keypair(),
game.pubkey(),
loader.mint.last_id(),
1,
0x78, // corresponds to the C structure size
program.program.pubkey(),
0,
);
check_tx_results(
&loader.bank,
&tx,
loader.bank.process_transactions(&vec![tx.clone()]),
);
TicTacToe { game }
}
pub fn id(&self) -> Pubkey {
self.game.pubkey().clone()
}
pub fn init(&self, loader: &Loader, program: &Program, player: &Pubkey) {
let userdata = serialize(&Command::Init).unwrap();
let tx = Transaction::new(
&self.game,
&[self.game.pubkey(), *player],
program.program.pubkey(),
userdata,
loader.mint.last_id(),
0,
);
check_tx_results(
&loader.bank,
&tx,
loader.bank.process_transactions(&vec![tx.clone()]),
);
}
pub fn command(&self, loader: &Loader, program: &Program, command: Command, player: &Pubkey) {
let userdata = serialize(&command).unwrap();
let tx = Transaction::new(
&loader.mint.keypair(),
&[self.game.pubkey(), *player],
program.program.pubkey(),
userdata,
loader.mint.last_id(),
0,
);
check_tx_results(
&loader.bank,
&tx,
loader.bank.process_transactions(&vec![tx.clone()]),
);
}
pub fn get_player_x(&self, loader: &Loader) -> Vec<u8> {
loader
.bank
.get_account(&self.game.pubkey())
.unwrap()
.userdata[0..32]
.to_vec()
}
pub fn get_player_y(&self, loader: &Loader) -> Vec<u8> {
loader
.bank
.get_account(&self.game.pubkey())
.unwrap()
.userdata[32..64]
.to_vec()
}
pub fn game(&self, loader: &Loader) -> Vec<u8> {
loader
.bank
.get_account(&self.game.pubkey())
.unwrap()
.userdata[64..68]
.to_vec()
}
}
#[cfg(feature = "bpf_c")]
struct Dashboard {
dashboard: Keypair,
}
#[cfg(feature = "bpf_c")]
impl Dashboard {
pub fn new(loader: &Loader, program: &Program) -> Self {
let dashboard = Keypair::new();
// Create game account
let tx = Transaction::system_create(
&loader.mint.keypair(),
dashboard.pubkey(),
loader.mint.last_id(),
1,
0xD0, // corresponds to the C structure size
program.program.pubkey(),
0,
);
check_tx_results(
&loader.bank,
&tx,
loader.bank.process_transactions(&vec![tx.clone()]),
);
Dashboard { dashboard }
}
pub fn update(&self, loader: &Loader, program: &Program, game: &Pubkey) {
let tx = Transaction::new(
&self.dashboard,
&[self.dashboard.pubkey(), *game],
program.program.pubkey(),
vec![],
loader.mint.last_id(),
0,
);
check_tx_results(
&loader.bank,
&tx,
loader.bank.process_transactions(&vec![tx.clone()]),
);
}
pub fn get_game(&self, loader: &Loader, since_last: usize) -> Vec<u8> {
let userdata = loader
.bank
.get_account(&self.dashboard.pubkey())
.unwrap()
.userdata;
// TODO serialize
let last_game = userdata[192] as usize;
let this_game = (last_game + since_last * 4) % 5;
let start = 32 + this_game * 32;
let end = start + 32;
loader
.bank
.get_account(&self.dashboard.pubkey())
.unwrap()
.userdata[start..end]
.to_vec()
}
pub fn get_pending(&self, loader: &Loader) -> Vec<u8> {
loader
.bank
.get_account(&self.dashboard.pubkey())
.unwrap()
.userdata[0..32]
.to_vec()
}
}
#[cfg(feature = "bpf_c")]
#[test]
fn test_program_bpf_tictactoe_c() {
logger::setup();
let loader = Loader::new_dynamic("solana_bpf_loader");
let program = Program::new(
&loader,
elf::File::open_path(&create_bpf_path("tictactoe"))
.unwrap()
.get_section(PLATFORM_SECTION_C)
.unwrap()
.data
.clone(),
);
let player_x = Pubkey::new(&[0xA; 32]);
let player_y = Pubkey::new(&[0xB; 32]);
let ttt = TicTacToe::new(&loader, &program);
ttt.init(&loader, &program, &player_x);
ttt.command(&loader, &program, Command::Join(0xAABBCCDD), &player_y);
ttt.command(&loader, &program, Command::Move(1, 1), &player_x);
ttt.command(&loader, &program, Command::Move(0, 0), &player_y);
ttt.command(&loader, &program, Command::Move(2, 0), &player_x);
ttt.command(&loader, &program, Command::Move(0, 2), &player_y);
ttt.command(&loader, &program, Command::Move(2, 2), &player_x);
ttt.command(&loader, &program, Command::Move(0, 1), &player_y);
assert_eq!(player_x.as_ref(), &ttt.get_player_x(&loader)[..]); // validate x's key
assert_eq!(player_y.as_ref(), &ttt.get_player_y(&loader)[..]); // validate o's key
assert_eq!([4, 0, 0, 0], ttt.game(&loader)[..]); // validate that o won
}
#[cfg(feature = "bpf_c")]
#[test]
fn test_program_bpf_tictactoe_dashboard_c() {
logger::setup();
let loader = Loader::new_dynamic("solana_bpf_loader");
let ttt_program = Program::new(
&loader,
elf::File::open_path(&create_bpf_path("tictactoe"))
.unwrap()
.get_section(PLATFORM_SECTION_C)
.unwrap()
.data
.clone(),
);
let player_x = Pubkey::new(&[0xA; 32]);
let player_y = Pubkey::new(&[0xB; 32]);
let ttt1 = TicTacToe::new(&loader, &ttt_program);
ttt1.init(&loader, &ttt_program, &player_x);
ttt1.command(&loader, &ttt_program, Command::Join(0xAABBCCDD), &player_y);
ttt1.command(&loader, &ttt_program, Command::Move(1, 1), &player_x);
ttt1.command(&loader, &ttt_program, Command::Move(0, 0), &player_y);
ttt1.command(&loader, &ttt_program, Command::Move(2, 0), &player_x);
ttt1.command(&loader, &ttt_program, Command::Move(0, 2), &player_y);
ttt1.command(&loader, &ttt_program, Command::Move(2, 2), &player_x);
ttt1.command(&loader, &ttt_program, Command::Move(0, 1), &player_y);
let ttt2 = TicTacToe::new(&loader, &ttt_program);
ttt2.init(&loader, &ttt_program, &player_x);
ttt2.command(&loader, &ttt_program, Command::Join(0xAABBCCDD), &player_y);
ttt2.command(&loader, &ttt_program, Command::Move(1, 1), &player_x);
ttt2.command(&loader, &ttt_program, Command::Move(0, 0), &player_y);
ttt2.command(&loader, &ttt_program, Command::Move(2, 0), &player_x);
ttt2.command(&loader, &ttt_program, Command::Move(0, 2), &player_y);
ttt2.command(&loader, &ttt_program, Command::Move(2, 2), &player_x);
ttt2.command(&loader, &ttt_program, Command::Move(0, 1), &player_y);
let ttt3 = TicTacToe::new(&loader, &ttt_program);
ttt3.init(&loader, &ttt_program, &player_x);
let dashboard_program = Program::new(
&loader,
elf::File::open_path(&create_bpf_path("tictactoe_dashboard"))
.unwrap()
.get_section(PLATFORM_SECTION_C)
.unwrap()
.data
.clone(),
);
let dashboard = Dashboard::new(&loader, &dashboard_program);
dashboard.update(&loader, &dashboard_program, &ttt1.id());
dashboard.update(&loader, &dashboard_program, &ttt2.id());
dashboard.update(&loader, &dashboard_program, &ttt3.id());
assert_eq!(ttt1.id().as_ref(), &dashboard.get_game(&loader, 1)[..]);
assert_eq!(ttt2.id().as_ref(), &dashboard.get_game(&loader, 0)[..]);
assert_eq!(ttt3.id().as_ref(), &dashboard.get_pending(&loader)[..]);
}