Skip to content

Commit

Permalink
add more
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka committed Nov 17, 2024
1 parent ff3f798 commit 88ce50b
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 46 deletions.
33 changes: 17 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[profile]
[profile.release-debug]
debug = true
inherits = 'release'
Expand Down Expand Up @@ -36,7 +37,6 @@ members = [
resolver = '2'

[workspace.dependencies]
antithesis = { version = "0.2.4", package = "antithesis_sdk" }
anyhow = '1.0.86'
approx = '0.5.1'
arrayvec = '0.7.4'
Expand All @@ -54,7 +54,6 @@ derive-build = '0.1.1'
directories = '5.0.1'
dotenvy = '0.15.7'
enumset = '1.1.5'
envy = { version = "0.4.2" }
fastrand = '2.1.0'
futures-util = '0.3.31'
glam = '0.29.0'
Expand All @@ -75,13 +74,12 @@ ordered-float = '4.2.0'
ouroboros = '0.18.4'
papaya = '0.1.4'
parking_lot = '0.12.3'
paste = "1.0.15"
paste = '1.0.15'
plotters-bitmap = '0.3.6'
proc-macro2 = '1.0.89'
quote = '1.0.37'
rand = '0.8.5'
rayon = '1.10.0'
regex = { version = "1.11.1", features = ["perf", "perf-dfa-full"] } # todo: do we want perf-dfa-full?
replace_with = '0.1.7'
rkyv = '0.8.8'
rustc-hash = '2.0.0'
Expand All @@ -97,7 +95,11 @@ tikv-jemallocator = '0.6.0'
tokio = '1.40.0'
toml = '0.8.14'
trybuild = '1.0.101'
uuid = { version = '1.8.0', features = ["serde"] }

[workspace.dependencies.antithesis]
package = 'antithesis_sdk'
git = 'https://github.com/andrewgazelka/antithesis-sdk-rust'
branch = "andrew/assert-never"

[workspace.dependencies.bvh]
git = 'https://github.com/andrewgazelka/bvh-data'
Expand All @@ -120,6 +122,9 @@ version = '1.0.0-beta.7'
[workspace.dependencies.divan]
git = 'https://github.com/nvzqz/divan'

[workspace.dependencies.envy]
version = '0.4.2'

[workspace.dependencies.flate2]
default-features = false
version = '1.0.30'
Expand Down Expand Up @@ -188,6 +193,10 @@ version = '0.16.1'
default-features = false
version = '0.3.6'

[workspace.dependencies.regex]
features = ['perf', 'perf-dfa-full']
version = '1.11.1'

[workspace.dependencies.reqwest]
features = ['rustls-tls', 'stream']
version = '0.12.9'
Expand Down Expand Up @@ -215,6 +224,10 @@ version = '0.3.18'
features = ['timer-fallback']
version = '0.11.3'

[workspace.dependencies.uuid]
features = ['serde']
version = '1.8.0'

[workspace.dependencies.valence_anvil]
branch = 'feat-open'
features = ['parsing']
Expand Down
3 changes: 3 additions & 0 deletions crates/hyperion-bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ tracing.workspace = true
tracing-subscriber.workspace = true
uuid.workspace = true
valence_protocol.workspace = true
valence_server = { workspace = true }
bytes = { workspace = true }
serde_json = { workspace = true }

[lints]
workspace = true
Expand Down
15 changes: 13 additions & 2 deletions crates/hyperion-bot/src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
use bytes::BytesMut;
use tokio::net::{TcpStream, ToSocketAddrs};
use tracing::info;
use uuid::Uuid;
use valence_protocol::{PacketDecoder, PacketEncoder};

mod handshake;

pub struct Bot {
name: String,
uuid: Uuid,
buf: Vec<u8>,
connection: TcpStream,
encoder: PacketEncoder,
decoder: PacketDecoder,
decode_buf: BytesMut,
}

impl Bot {
#[tracing::instrument(skip_all, fields(name))]
pub async fn new(name: String, uuid: Uuid, addr: impl ToSocketAddrs + std::fmt::Display) -> Self {
info!("connecting to {addr}");
let addr = TcpStream::connect(addr).await.unwrap();

let encoder = PacketEncoder::default();
let decoder = PacketDecoder::default();

let decode_buf = BytesMut::with_capacity(1024 * 1024); // 1 MiB

Self {
name,
uuid,
buf: Vec::new(),
connection: addr,
encoder,
decoder,
decode_buf,
}
}
}
66 changes: 59 additions & 7 deletions crates/hyperion-bot/src/bot/handshake.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use antithesis::random::AntithesisRng;
use antithesis::{assert_always, assert_never, random::AntithesisRng};
use eyre::bail;
use rand::Rng;
use tokio::io::AsyncWriteExt;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tracing::info;
use valence_protocol::{
Bounded, Encode, PROTOCOL_VERSION, VarInt, packets,
Bounded, Encode, PROTOCOL_VERSION, Packet, VarInt, packets,
packets::handshaking::handshake_c2s::HandshakeNextState,
};

Expand All @@ -25,11 +27,61 @@ impl Bot {
next_state: HandshakeNextState::Status,
};

packet
.encode(&mut self.buf)
.map_err(|e| eyre::eyre!("failed to encode handshake packet: {e}"))?;
let result = self.encoder.append_packet(&packet);

self.connection.write_all(&self.buf).await?;
assert_always!(result.is_ok(), "Failed to encode handshake packet");

let packet = packets::status::QueryRequestC2s;

let result = self.encoder.append_packet(&packet);

assert_always!(result.is_ok(), "Failed to encode handshake packet");

let bytes = self.encoder.take();

self.connection.write_all(&bytes).await?;

let result_packet = self.connection.read_buf(&mut self.decode_buf).await?;

assert_never!(result_packet == 0, "Failed to read handshake packet");

println!("read bytes {:?}", self.decode_buf);
self.decoder.queue_bytes(self.decode_buf.split());

let packet1 = self.decoder.try_next_packet();

let Ok(packet1) = packet1 else {
antithesis::assert_unreachable!("Failed to decode handshake packet");
bail!("Failed to decode handshake packet");
};

let Some(packet1) = packet1 else {
antithesis::assert_unreachable!("Failed to decode handshake packet");
bail!("Failed to decode handshake packet");
};

assert_always!(
packet1.id == packets::status::QueryResponseS2c::ID,
"Failed to decode handshake packet"
);

let packet: packets::status::QueryResponseS2c<'_> = packet1.decode().unwrap();

let json = packet.json;

// todo: maybe remove unwrap and use antithesis asserts first
let json: serde_json::Value = serde_json::from_str(json).unwrap();

let description = json.get("description").unwrap();

let description = description.as_str().unwrap();

assert_always!(
description
== "Getting 10k Players to PvP at Once on a Minecraft Server to Break the \
Guinness World Record",
"Failed to decode handshake packet"
);

Ok(())
}
Expand Down
10 changes: 0 additions & 10 deletions crates/hyperion-bot/src/handshake.rs

This file was deleted.

14 changes: 10 additions & 4 deletions crates/hyperion-bot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use antithesis::{assert_sometimes, random::AntithesisRng};
use rand::Rng;
use tokio::task::JoinSet;
use uuid::Uuid;

mod config;
Expand All @@ -11,9 +12,10 @@ mod generate;
mod util;

mod bot;
mod handshake;

pub fn bootstrap(config: &Config) {
pub async fn bootstrap(config: &Config) {
// Wait for TCP port to be available
util::wait_for_tcp_port(&config.host).await;
// todo: use life cycle

let mut rng = AntithesisRng;
Expand All @@ -24,13 +26,17 @@ pub fn bootstrap(config: &Config) {

let first_uuid: u128 = rng.r#gen();
let first_uuid = Uuid::from_u128(first_uuid);

let mut join_set = JoinSet::new();

for _ in 0..10 {
for _ in 0..config.max_number_of_bots {
let name = first_name.value.clone();
let addr = config.host.clone();
tokio::spawn(async move {
join_set.spawn(async move {
let bot = Bot::new(name, first_uuid, addr);
bot.await.handshake().await.unwrap();
});
}

join_set.join_all().await;
}
2 changes: 1 addition & 1 deletion crates/hyperion-bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ async fn main() -> eyre::Result<()> {

tracing::info!("{config:?}");

bootstrap(&config);
bootstrap(&config).await;
Ok(())
}
Loading

0 comments on commit 88ce50b

Please sign in to comment.