Skip to content

Commit c9fa27e

Browse files
authored
Merge pull request #51 from otter-sec/custom-limit
Added Optional Resources Limits
2 parents f845f8b + 1aad34e commit c9fa27e

File tree

4 files changed

+77
-15
lines changed

4 files changed

+77
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ cargo install solana-verify
2424
```
2525

2626
If you want to pin the version:
27+
2728
```
2829
# Pulls the latest version from crates.io
2930
cargo install solana-verify --version $VERSION
3031
```
3132

3233
If you are extra cautious and want to install a version of the binary that maps 1-to-1 with a specific commit, run the following. This example is installing version 0.2.6 from revision `13a1db2`:
34+
3335
```
3436
# Pulls the source from git. Change the argument to --rev to the desired commit
3537
cargo install solana-verify --git https://github.com/Ellipsis-Labs/solana-verifiable-build --rev 13a1db2
@@ -139,23 +141,27 @@ Program hash matches ✅
139141
```
140142

141143
### Marginfi V2
144+
142145
```
143146
solana-verify verify-from-repo -um --program-id MFv2hWf31Z9kbCa1snEPYctwafyhdvnV7FZnsebVacA https://github.com/mrgnlabs/marginfi-v2 --library-name marginfi -- --features mainnet-beta
144147
```
145148

146149
Final Output:
150+
147151
```
148152
Executable Program Hash from repo: 7b37482dd6b2159932b5c2595bc6ce62cf6e587ae67f237c8152b802bf7d7bb8
149153
On-chain Program Hash: 7b37482dd6b2159932b5c2595bc6ce62cf6e587ae67f237c8152b802bf7d7bb8
150154
Program hash matches ✅
151155
```
152156

153157
### Solend
158+
154159
```
155160
solana-verify verify-from-repo -um --program-id So1endDq2YkqhipRh3WViPa8hdiSpxWy6z3Z6tMCpAo https://github.com/solendprotocol/solana-program-library --library-name solend_program -b ellipsislabs/solana:1.14.10 --bpf
156161
```
157162

158163
Final Output:
164+
159165
```
160166
Executable Program Hash from repo: f89a43677ab106d2e50d3c41b656d067b6142c02a2508caca1c11c0a963d3b17
161167
On-chain Program Hash: f89a43677ab106d2e50d3c41b656d067b6142c02a2508caca1c11c0a963d3b17
@@ -229,12 +235,12 @@ This will return the hash of the stripped executable, which should match the has
229235
230236
```
231237

232-
### To send verification to Osec API
238+
### To send verification to OtterSec API
233239

234240
```bash
235241
solana-verify verify-from-repo --remote -um --program-id PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY https://github.com/Ellipsis-Labs/phoenix-v1
236242
```
237243

238-
- This verification will be sent to the Osec API and will be available at [https://verify.osec.io/status](https://verify.osec.io/status/PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY)
244+
- This verification will be sent to the OtterSec API and will be available at [https://verify.osec.io/status](https://verify.osec.io/status/PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY)
239245

240-
> Note: The `--remote` flag is required to send the verification to the Osec API. The `--remote` flag is not required for local verification. And this will take 5-10 minutes to complete.
246+
> Note: The `--remote` flag is required to send the verification to the OtterSec API. The `--remote` flag is not required for local verification. And this will take 5-10 minutes to complete.

build-all-images.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
# Change directory to where the Dockerfiles are located
4+
cd ./docker
5+
6+
# Iterate over each Dockerfile in the directory
7+
for dockerfile in *; do
8+
# Check if the file is actually a Dockerfile
9+
if [ -f "$dockerfile" ] && [ "${dockerfile##*.}" == "Dockerfile" ]; then
10+
# Extract image name from Dockerfile name
11+
image_name="${dockerfile%Dockerfile}"
12+
# Remove the last character from the image name
13+
image_name="${image_name%?}"
14+
# Interpolate image_name with "solana."
15+
image_name="solana.$image_name"
16+
echo "Building image: $image_name"
17+
# Build the Docker image
18+
docker build -t "$image_name" -f "$dockerfile" .
19+
fi
20+
done

src/main.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ pub fn get_genesis_hash(url: Option<String>) -> anyhow::Result<String> {
314314
Ok(genesis_hash.to_string())
315315
}
316316

317+
318+
pub fn get_docker_resource_limits() -> Option<(String, String)> {
319+
let memory = std::env::var("SVB_DOCKER_MEMORY_LIMIT").ok();
320+
let cpus = std::env::var("SVB_DOCKER_CPU_LIMIT").ok();
321+
if memory.is_some() || cpus.is_some() {
322+
println!("Using docker resource limits: memory: {:?}, cpus: {:?}", memory, cpus);
323+
} else {
324+
// Print message to user that they can set these environment variables to limit docker resources
325+
println!("No Docker resource limits are set.");
326+
println!("You can set the SVB_DOCKER_MEMORY_LIMIT and SVB_DOCKER_CPU_LIMIT environment variables to limit Docker resources.");
327+
println!("For example: SVB_DOCKER_MEMORY_LIMIT=2g SVB_DOCKER_CPU_LIMIT=2.");
328+
}
329+
memory.zip(cpus)
330+
}
331+
317332
pub fn build(
318333
mount_directory: Option<String>,
319334
library_name: Option<String>,
@@ -428,12 +443,22 @@ pub fn build(
428443

429444
// change directory to program/build dir
430445
let mount_params = format!("{}:{}", mount_path, workdir);
431-
let container_id = std::process::Command::new("docker")
432-
.args(["run", "--rm", "-v", &mount_params, "-dit", &image, "bash"])
433-
.stderr(Stdio::inherit())
434-
.output()
435-
.map_err(|e| anyhow::format_err!("Docker build failed: {}", e.to_string()))
436-
.and_then(|output| parse_output(output.stdout))?;
446+
let container_id = {
447+
let mut cmd = std::process::Command::new("docker");
448+
cmd.args(["run", "--rm", "-v", &mount_params, "-dit"]);
449+
cmd.stderr(Stdio::inherit());
450+
451+
if let Some((memory_limit, cpu_limit)) = get_docker_resource_limits() {
452+
cmd.arg("--memory").arg(memory_limit).arg("--cpus").arg(cpu_limit);
453+
}
454+
455+
let output = cmd
456+
.args([&image, "bash"])
457+
.output()
458+
.map_err(|e| anyhow!("Docker build failed: {}", e.to_string()))?;
459+
460+
parse_output(output.stdout)?
461+
};
437462

438463
// Set the container id so we can kill it later if the process is interrupted
439464
container_id_opt.replace(container_id.clone());
@@ -532,11 +557,22 @@ pub fn verify_from_image(
532557

533558
println!("Workdir: {}", workdir);
534559

535-
let container_id = std::process::Command::new("docker")
536-
.args(["run", "--rm", "-dit", image.as_str()])
537-
.output()
538-
.map_err(|e| anyhow::format_err!("Failed to run image {}", e.to_string()))
539-
.and_then(|output| parse_output(output.stdout))?;
560+
561+
let container_id = {
562+
let mut cmd = std::process::Command::new("docker");
563+
cmd.args(["run", "--rm", "-dit"]);
564+
cmd.stderr(Stdio::inherit());
565+
566+
if let Some((memory_limit, cpu_limit)) = get_docker_resource_limits() {
567+
cmd.arg("--memory").arg(memory_limit).arg("--cpus").arg(cpu_limit);
568+
}
569+
570+
let output = cmd
571+
.args([&image])
572+
.output()
573+
.map_err(|e| anyhow!("Docker build failed: {}", e.to_string()))?;
574+
parse_output(output.stdout)?
575+
};
540576

541577
container_id_opt.replace(container_id.clone());
542578

0 commit comments

Comments
 (0)