Skip to content

Commit a798883

Browse files
DogLooksGoodDogLooksGood
andauthored
Solana players reg (#18)
* Adapt to new contract update * Update players reg len constant * Adapt to contract update * Remove a log * Bugfixes --------- Co-authored-by: DogLooksGood <doglooksgood@gmail.com>
1 parent eda624a commit a798883

File tree

8 files changed

+272
-89
lines changed

8 files changed

+272
-89
lines changed

transactor/src/component/event_loop/event_handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ async fn do_send_settlements(
163163
game_context.settle_version(),
164164
);
165165

166-
if game_context.game_id() == 0 {
167-
settle_details.print("do_send_settlements".to_string());
168-
}
166+
// if game_context.game_id() == 0 {
167+
// settle_details.print("do_send_settlements".to_string());
168+
// }
169169

170170
ports
171171
.send(EventFrame::Settle {

transport/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ pub enum TransportError {
180180

181181
#[error("Cannot get object: {0}")]
182182
GetObjectError(String),
183+
184+
#[error("Game account players not found")]
185+
GameAccountPlayersNotFound,
186+
187+
#[error("Failed to deserialize players reg")]
188+
PlayersRegDeserializationError,
183189
}
184190

185191
pub type TransportResult<T> = std::result::Result<T, TransportError>;

transport/src/solana.rs

Lines changed: 172 additions & 48 deletions
Large diffs are not rendered by default.

transport/src/solana/types/instruction.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@ pub enum RaceInstruction {
1313
/// Accounts expected:
1414
/// 0. `[signer]` The account of transactor
1515
/// 1. `[writable]` The game account, hold all necessary info about the game
16-
/// 2. `[writable]` The temp stake account
17-
/// 3. `[]` The mint account
18-
/// 4. `[]` The token program
19-
/// 5. `[]` The bundled data account
20-
/// 6. `[]` The recipient account
21-
/// 7. `[]` The system program
16+
/// 2. `[writable]` The players account, hold all player registrations
17+
/// 3. `[writable]` The temp stake account
18+
/// 4. `[]` The mint account
19+
/// 5. `[]` The token program
20+
/// 6. `[]` The bundled data account
21+
/// 7. `[]` The recipient account
22+
/// 8. `[]` The system program
2223
CreateGameAccount { params: IxCreateGameAccountParams },
2324

2425
/// # [1] Close a game
2526
///
2627
/// Accounts expected:
2728
/// 0. `[signer]` The account of game owner
2829
/// 1. `[writable]` The account of game account
29-
/// 2. `[writable]` The stake account of game
30-
/// 3. `[]` PDA account
31-
/// 4. `[]` The account to receive tokens
32-
/// 5. `[]` Token program
33-
/// 6. `[]` The system program
30+
/// 2. `[writable]` The players account, hold all player registrations
31+
/// 3. `[writable]` The stake account of game
32+
/// 4. `[]` PDA account
33+
/// 5. `[]` The account to receive tokens
34+
/// 6. `[]` Token program
35+
/// 7. `[]` The system program
3436
/// Rest are the bonus stake account and receiver(owner)'s ATA
3537
CloseGameAccount,
3638

transport/src/solana/types/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ mod server;
33
mod profile;
44
mod recipient;
55
mod registry;
6+
mod players;
67

78
pub use game::*;
89
pub use server::*;
910
pub use profile::*;
1011
pub use recipient::*;
1112
pub use registry::*;
13+
pub use players::*;

transport/src/solana/types/state/game.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,7 @@ use race_core::{
66
types::{EntryLock, EntryType, GameAccount, VoteType},
77
};
88
use solana_sdk::pubkey::Pubkey;
9-
10-
#[cfg_attr(test, derive(PartialEq, Eq))]
11-
#[derive(Default, BorshDeserialize, BorshSerialize, Clone, Debug)]
12-
pub struct PlayerJoin {
13-
pub addr: Pubkey,
14-
pub position: u16,
15-
pub access_version: u64,
16-
pub verify_key: String,
17-
}
18-
19-
impl From<PlayerJoin> for race_core::types::PlayerJoin {
20-
fn from(value: PlayerJoin) -> Self {
21-
Self {
22-
addr: value.addr.to_string(),
23-
position: value.position,
24-
access_version: value.access_version,
25-
verify_key: value.verify_key,
26-
}
27-
}
28-
}
9+
use crate::solana::types::state::players::PlayerJoin;
2910

3011
#[cfg_attr(test, derive(PartialEq, Eq))]
3112
#[derive(Default, BorshDeserialize, BorshSerialize, Clone, Debug)]
@@ -137,8 +118,8 @@ pub struct GameState {
137118
pub settle_version: u64,
138119
// game size
139120
pub max_players: u16,
140-
// game players
141-
pub players: Vec<PlayerJoin>,
121+
// the address to players reg account
122+
pub players_reg_account: Pubkey,
142123
// player deposits
143124
pub deposits: Vec<PlayerDeposit>,
144125
// game servers (max: 10)
@@ -166,7 +147,7 @@ pub struct GameState {
166147
}
167148

168149
impl GameState {
169-
pub fn into_account<S: Into<String>>(self, addr: S) -> Result<GameAccount, Error> {
150+
pub fn into_account<S: Into<String>>(self, addr: S, players: Vec<PlayerJoin>) -> Result<GameAccount, Error> {
170151
let GameState {
171152
title,
172153
bundle_addr,
@@ -176,7 +157,6 @@ impl GameState {
176157
access_version,
177158
settle_version,
178159
max_players,
179-
players,
180160
servers,
181161
data_len,
182162
data,
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use solana_sdk::pubkey::Pubkey;
2+
use borsh::{BorshSerialize, BorshDeserialize};
3+
4+
#[cfg_attr(test, derive(PartialEq, Eq))]
5+
#[derive(BorshDeserialize, BorshSerialize, Clone, Debug)]
6+
pub struct PlayerJoin {
7+
pub addr: Pubkey,
8+
pub position: u16,
9+
pub access_version: u64,
10+
pub verify_key: [u8; 128], // size: 4, data: 128
11+
}
12+
13+
impl From<PlayerJoin> for race_core::types::PlayerJoin {
14+
fn from(value: PlayerJoin) -> Self {
15+
Self {
16+
addr: value.addr.to_string(),
17+
position: value.position,
18+
access_version: value.access_version,
19+
verify_key: String::try_from_slice(&value.verify_key).unwrap_or("".to_string())
20+
}
21+
}
22+
}
23+
24+
#[cfg_attr(test, derive(PartialEq, Eq))]
25+
#[derive(BorshDeserialize, BorshSerialize, Clone, Debug)]
26+
pub struct PlayersReg {
27+
pub access_version: u64,
28+
pub settle_version: u64,
29+
pub size: usize,
30+
pub position_flags: [u8; 128],
31+
pub players: V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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
48+
let players_reg = PlayersReg::try_from_slice(&v).unwrap();
49+
assert_eq!(players_reg.access_version, 2);
50+
assert_eq!(players_reg.settle_version, 1);
51+
}
52+
53+
#[test]
54+
fn test_deser_player_join() {
55+
let v = [0u8; 170];
56+
let player_join = PlayerJoin::try_from_slice(&v).unwrap();
57+
assert_eq!(player_join.access_version, 0);
58+
}
59+
60+
#[test]
61+
fn test_deser_fixed_array() {
62+
let s = S {
63+
s: 1,
64+
ns: [0, 1, 2, 3, 4, 5, 6, 7]
65+
};
66+
let v = borsh::to_vec(&s).unwrap();
67+
assert_eq!(v, vec![1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7]);
68+
}
69+
}

transport/src/sui.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,8 +1674,9 @@ mod tests {
16741674
settle_version: 0,
16751675
status: DepositStatus::Accepted
16761676
}],
1677+
stake: 0,
16771678
servers: vec![],
1678-
balance: 100,
1679+
balances: vec![],
16791680
data_len: 5,
16801681
data: vec![8,1,2,3,4],
16811682
votes: vec![],
@@ -1985,7 +1986,6 @@ mod tests {
19851986
assert_eq!(game_obj.max_players, 10);
19861987
assert_eq!(game_obj.transactor_addr, None);
19871988
assert_eq!(game_obj.players, vec![]);
1988-
assert_eq!(game_obj.balance, 0);
19891989
assert_eq!(game_obj.entry_type, EntryType::Ticket {amount: 100});
19901990
assert_eq!(game_obj.entry_lock, EntryLock::Open);
19911991

@@ -2145,7 +2145,7 @@ mod tests {
21452145
let params = SettleParams {
21462146
addr: "".to_string(),
21472147
settles: vec![],
2148-
transfers: vec![],
2148+
transfer: None,
21492149
awards: vec![],
21502150
checkpoint: CheckpointOnChain { root: vec![], size: 0, access_version: 0 },
21512151
access_version: 0,

0 commit comments

Comments
 (0)