Skip to content

Commit

Permalink
feat: start dave-advance
Browse files Browse the repository at this point in the history
  • Loading branch information
algebraic-dev committed Jan 24, 2024
1 parent 9aafe89 commit 3bfb1c6
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 33 deletions.
37 changes: 5 additions & 32 deletions offchain/Cargo.lock

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

2 changes: 1 addition & 1 deletion offchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
resolver = "2"
members = [
"core",
"dave-compute",
"dave-advance",
"input-reader",
]

Expand Down
20 changes: 20 additions & 0 deletions offchain/dave-advance/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "dave-advance"
version.workspace = true
authors.workspace = true
description.workspace = true
edition.workspace = true
homepage.workspace = true
license-file.workspace = true
readme.workspace = true
repository.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cartesi-machine-json-rpc = { path = "../../machine-json-rpc" }
tokio = { workspace = true }
base64 = "0.21.3"
hex = "0.4.3"
ethers.workspace = true
serde_json = "1.0.100"
34 changes: 34 additions & 0 deletions offchain/dave-advance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# dave-advance

Crate for simplified input feeding to the Cartesi machine.

```rs
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// The advance-state input that we need to change the machine
let input = AdvanceInput {
metadata: InputMetadata {
msg_sender: Address::from_str("0x0000000000000000000000000000000000000000")?,
block_number: 0,
time_stamp: 0,
epoch_index: 0,
input_index: 0,
},
data: "uwu".as_bytes().to_vec(),
};

/// Connects to a remote machine.
let mut machine = MachineClient::connect("http://127.0.0.1".to_string(), 5002).await?;

/// Loads a machine that is with the `y` flag.
machine.load(&PathBuf::from("/data/image"), &Default::default()).await?;

/// Advances the state of the machine until a new [ManualYield]
machine.advance(input.clone()).await?

/// Destroys the current machine in order to load a new one
machine.destroy().await?;

Ok(())
}
```
16 changes: 16 additions & 0 deletions offchain/dave-advance/src/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Tools for encoding and decoding stuff that is sent to the Cartesi Machine.
/// Writes a 64-bit big-endian integer to a 32-byte buffer.
pub(crate) fn write_be256(value: u64) -> Vec<u8> {
let mut buffer = [0; 32];
buffer[24..].copy_from_slice(&value.to_be_bytes());
buffer.to_vec()
}

/// Encodes a string putting 32 bytes in front of it and adding the length of the string.
pub(crate) fn encode_string(payload: Vec<u8>) -> Vec<u8> {
let mut encoded_string = write_be256(32);
encoded_string.append(&mut write_be256(payload.len() as u64));
encoded_string.append(&mut payload.clone());
encoded_string
}
Loading

0 comments on commit 3bfb1c6

Please sign in to comment.