refactoring

This commit is contained in:
Valentyn Faychuk 2024-10-06 19:23:54 +03:00
parent c74aa76612
commit 21ea84a747
9 changed files with 109 additions and 37 deletions

4
rewrite/.gitignore vendored Normal file

@ -0,0 +1,4 @@
# Occlum building env
challenge_instance
# The challenge bundle
docker/challenge.tar.gz

@ -1,9 +1,36 @@
#!/bin/bash #!/bin/bash
set -e set -e
script_dir=$(dirname "$0") script_dir=$(dirname "$0")
cd "${script_dir}/.." cd "$script_dir/.."
prerequisites=$1
if [ "$prerequisites" == "--prep" ]; then
echo "Preparing the packager environment"
apt update && apt install openssh-client
rustup install stable-x86_64-unknown-linux-gnu
rustup default stable
rustup target add x86_64-unknown-linux-musl
mkdir -p /root/.ssh
echo "docker cp ~/.ssh/config packager:/root/.ssh/config"
echo "docker cp ~/.ssh/gitea_ed25519 packager:/root/.ssh/gitea_ed25519"
echo "Run above commands in another terminal"
while true; do
echo -n "." && sleep 1
if [ -e ~/.ssh/config ] && [ -e ~/.ssh/gitea_ed25519 ]; then
echo -n "done"
break
fi
done
chown -R root:root /root/.ssh
chmod 600 /root/.ssh/gitea_ed25519
ssh-keyscan -H gitea.detee.cloud > ~/.ssh/known_hosts
fi
echo "Building the hacker-challenge signed bundle"
occlum-cargo build --release occlum-cargo build --release
strip target/x86_64-unknown-linux-musl/release/hacker-challenge strip target/x86_64-unknown-linux-musl/release/hacker-challenge
@ -29,4 +56,3 @@ copy_bom -f ../challenge.yaml --root image --include-dir /opt/occlum/etc/templat
# TODO: make sure the bundle needs SGX2 to run since SGX1 is vulnerable to https://x.com/PratyushRT/status/1828183761055330373 # TODO: make sure the bundle needs SGX2 to run since SGX1 is vulnerable to https://x.com/PratyushRT/status/1828183761055330373
occlum build --sign-key ../scripts/signing_key.pem occlum build --sign-key ../scripts/signing_key.pem
occlum package challenge.tar.gz occlum package challenge.tar.gz
mv challenge.tar.gz ../scripts/challenge.tar.gz

@ -1,51 +1,93 @@
#!/bin/bash #!/bin/bash
set -e set -e
# WARNING: Build and package the hacker-challenge first, see README.md
# TODO: check if scripts/challenge.tar.gz exists, if not tell to build first
script_dir=$(dirname "$0") script_dir=$(dirname "$0")
cd "${script_dir}/.." cd "${script_dir}/.." # Go to the root of the project
rm -rf build && mkdir build function build_mint_sol_tool() {
echo "Building the mint_sol tool for testing"
cp scripts/challenge.* build/ && cd build if ! command -v cargo 2>&1 >/dev/null
docker build -f challenge.Dockerfile -t hacker-challenge:latest . then
echo "cargo not found, run 'curl https://sh.rustup.rs -sSf | sh'"
exit 1
fi
if ! command -v gcc 2>&1 >/dev/null
then
echo "cc not found, run 'apt update && apt install build-essential'"
exit 1
fi
if ! command -v protoc 2>&1 >/dev/null
then
echo "protoc not found, run 'apt update && apt install protobuf-compiler'"
exit 1
fi
cd mint_sol
cargo build --release
cp target/release/mint_sol "../${script_dir}/mint_sol"
cd ..
}
function build_challenge_image() {
echo "Building the hacker-challenge docker image"
if ! command -v docker 2>&1 >/dev/null
then
echo "docker not found, please install docker"
exit 1
fi
if [ ! -e challenge_instance/challenge.tar.gz ]; then
echo "Please build the challenge first, see README.md"
exit 1
fi
cd docker
cp ../challenge_instance/challenge.tar.gz challenge.tar.gz
docker build -f challenge.Dockerfile -t hacker-challenge:latest .
cd ..
}
build_challenge_image
build_mint_sol_tool
# Cleanup old containers and run the network root
docker ps -a | grep 'hacker-challenge' | awk '{ print $NF }' | xargs docker rm -f || true docker ps -a | grep 'hacker-challenge' | awk '{ print $NF }' | xargs docker rm -f || true
docker run --device /dev/sgx/enclave --device /dev/sgx/provision -d --name "hacker-challenge_0" \ echo "Waiting for the network root to start"
hacker-challenge:latest docker run --device /dev/sgx/enclave \
--device /dev/sgx/provision \
cd "../${script_dir}/../../mint_sol" --name "hacker-challenge" \
# TODO: build the mint_sol as part of the build process -d hacker-challenge:latest
cargo build --release
sleep 3
address=$(docker logs hacker-challenge_0 | grep 'SOL' | awk '{ print $NF }')
echo "sending SOL to wallet"
./target/release/mint_sol $address
while true; do while true; do
echo "Waiting for mint to be created..." echo -n "." && sleep 1
sleep 5 docker logs hacker-challenge | grep "SOL" && break
docker logs hacker-challenge_0 | grep "Mint created" && break
done done
# for i in {1..10} echo "Sending SOL to the root and waiting for the mint"
for i in {11..20} address=$(docker logs hacker-challenge | grep 'SOL' | awk '{ print $NF }')
do "${script_dir}"/mint_sol "${address}"
docker run --device /dev/sgx/enclave --device /dev/sgx/provision -d --name "hacker-challenge_$i" -p 313${i}:31372 \ while true; do
--env INIT_NODES="172.17.0.2 172.17.0.3 172.17.0.4" \ echo -n "." && sleep 1
hacker-challenge:latest docker logs hacker-challenge | grep "Mint created" && break
done done
sleep 3 echo "Creating the cluster"
for p in {31311..31320}; do
for i in {11..20} echo -n "."
do docker run --device /dev/sgx/enclave \
curl -X POST 127.0.0.1:313${i}/mint --json '{"wallet": "EZT16iP1SQVUFf1AJN6oiE5BZPnyBUqaKDkZ4oZRsvhR"}' --device /dev/sgx/provision \
--env INIT_NODES="172.17.0.2 172.17.0.3 172.17.0.4" \
--name "hacker-challenge${p}" -p "${p}:31372" \
-d hacker-challenge:latest
done
sleep 5 # Wait for the cluster to start
echo "Running the test mint"
for p in {31311..31320}; do
curl -X POST "127.0.0.1:${p}/mint" --json '{"wallet": "EZT16iP1SQVUFf1AJN6oiE5BZPnyBUqaKDkZ4oZRsvhR"}'
done done