Skip to content

Commit 09c81a9

Browse files
calc MRENCLAVE from the enclaves and save it to a file
1 parent c29ea31 commit 09c81a9

File tree

15 files changed

+114
-44
lines changed

15 files changed

+114
-44
lines changed

.dockerignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
**/enclave/Enclave_t.c
1919
**/*_t.h
2020

21-
**/*.Dockerfile
21+
##test data
22+
enclaves/**/mrenclaves/*/*
23+
!enclaves/**/mrenclaves/*/MRENCLAVE
24+
tests

.github/workflows/test.yml

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ on:
33
pull_request:
44

55
jobs:
6-
detect-changes:
6+
get-enclaves:
77
runs-on: ubuntu-24.04
88
outputs:
99
enclaves: ${{ steps.changed.outputs.enclaves }}
@@ -13,32 +13,19 @@ jobs:
1313
fetch-depth: 0
1414
- id: changed
1515
run: |
16-
diff=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})
17-
18-
# 1. If there are changes other than the enclaves dir, execute the build for all elcs.
19-
# 2. If only the enclaves dir have changed, build only the elc where the changes occurred.
20-
if echo $diff | grep -v '^enclaves/' > /dev/null; then
21-
enclaves=$(find enclaves -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | jq -R . | jq -s -c .)
22-
else
23-
enclaves=$(echo $diff \
24-
| grep '^enclaves/' \
25-
| awk -F'/' '$2 !~/\./ {print $2}' \
26-
| sort -u \
27-
| jq -R . | jq -s -c .)
28-
fi
29-
16+
enclaves=$(find enclaves -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | jq -R . | jq -s -c .)
3017
echo "enclaves=$enclaves" >> "$GITHUB_OUTPUT"
3118
build:
32-
needs: detect-changes
33-
if: needs.detect-changes.outputs.enclaves != '[]'
19+
needs: get-enclaves
20+
if: needs.get-enclaves.outputs.enclaves != '[]'
3421
runs-on: ubuntu-24.04
3522
strategy:
3623
matrix:
37-
enclave: ${{ fromJson(needs.detect-changes.outputs.enclaves) }}
24+
enclave: ${{ fromJson(needs.get-enclaves.outputs.enclaves) }}
3825
network: [testnet, mainnet]
3926
permissions:
4027
contents: read # For checkout repo
41-
packages: write # For Push Image to ghcr.io
28+
packages: write # For Push Image for buildCache to ghcr.io
4229
steps:
4330
- uses: actions/checkout@v4
4431
- uses: docker/setup-buildx-action@v3
@@ -53,19 +40,37 @@ jobs:
5340
with:
5441
images: ghcr.io/${{ github.repository }}/${{ matrix.enclave }}/${{ matrix.network }}
5542
tags: ${{ github.event.pull_request.head.sha }}
43+
- name: Set UID and GID as env
44+
run: |
45+
echo "UID=$(id -u)" >> "$GITHUB_ENV"
46+
echo "GID=$(id -g)" >> "$GITHUB_ENV"
5647
- uses: docker/build-push-action@v5
5748
with:
5849
context: .
5950
push: false
60-
load: true
6151
build-args: |
6252
LCP_ELC_TYPE=${{ matrix.enclave }}
6353
DEPLOYMENT_NETWORK=${{ matrix.network }}
54+
UID=${{ env.UID }}
55+
GID=${{ env.GID }}
6456
tags: ${{ steps.meta.outputs.tags }}
6557
labels: ${{ steps.meta.outputs.labels }}
6658
cache-from: type=registry,ref=ghcr.io/${{ github.repository }}/${{ matrix.enclave }}/${{ matrix.network }}:buildCache
6759
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}/${{ matrix.enclave }}/${{ matrix.network }}:buildCache,mode=max
68-
- name: Output MRENCLAVE
60+
outputs: type=docker # save the image locally
61+
- name: Test
6962
run: |
70-
docker run --rm -t ghcr.io/${{ github.repository }}/${{ matrix.enclave }}/${{ matrix.network }}:${{ github.event.pull_request.head.sha }} \
71-
bash -c "/app/scripts/mrenclave.sh /out /tests/mrenclave > mrenclave.log 2>&1 && cat /tests/mrenclave/mrenclave.txt || { cat mrenclave.log; exit 1; }"
63+
# Check whether the MRENCLAVE calculated locally when updating the enclave and
64+
# the MRENCLAVE derived from the Image created in the test case are the same value.
65+
66+
mkdir -p tests/${{ matrix.enclave }}/mrenclaves/${{ matrix.network }}
67+
docker run --rm -v $(pwd)/tests/${{ matrix.enclave }}/mrenclaves/${{ matrix.network }}:/app/tests/mrenclave \
68+
ghcr.io/${{ github.repository }}/${{ matrix.enclave }}/${{ matrix.network }}:${{ github.event.pull_request.head.sha }} \
69+
bash -c "/app/scripts/mrenclave.sh /out /app/tests/mrenclave > mrenclave.log 2>&1 || { cat mrenclave.log; exit 1; }"
70+
71+
echo "Local:$(cat enclaves/${{ matrix.enclave }}/mrenclaves/${{ matrix.network }}/MRENCLAVE)"
72+
echo "Test: $(cat tests/${{ matrix.enclave }}/mrenclaves/${{ matrix.network }}/MRENCLAVE)"
73+
74+
diff \
75+
enclaves/${{ matrix.enclave }}/mrenclaves/${{ matrix.network }}/MRENCLAVE \
76+
tests/${{ matrix.enclave }}/mrenclaves/${{ matrix.network }}/MRENCLAVE

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
enclave/Enclave_t.c
1515
*_t.h
1616

17-
materials/
18-
remote-signer/remote-signer
1917
target/
18+
tests/
19+
enclaves/**/mrenclaves/*/*
20+
!enclaves/**/mrenclaves/*/MRENCLAVE

Dockerfile

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,37 @@ LABEL finance.toki.lcp.enclave.network=$DEPLOYMENT_NETWORK
1111

1212
ENV DEBIAN_FRONTEND=noninteractive
1313

14+
ARG UID=1000
15+
ARG GID=1000
16+
ARG USERNAME=app
17+
18+
RUN set -eux; \
19+
# If a user with the same ID exists, delete and create.
20+
if getent passwd "$UID" > /dev/null; then \
21+
OLD_USER=$(getent passwd "$UID" | cut -d: -f1); \
22+
echo "Removing existing user: $OLD_USER"; \
23+
userdel -r "$OLD_USER" || true; \
24+
fi; \
25+
# If group does not exist, create group.
26+
if ! getent group "$GID" > /dev/null; then \
27+
groupadd -g "$GID" "$USERNAME"; \
28+
fi; \
29+
useradd -u "$UID" -g "$GID" -m "$USERNAME";
30+
31+
RUN mkdir -p /app && chown $UID:$GID /app
32+
RUN mkdir -p /out && chown $UID:$GID /out
33+
34+
USER $USERNAME
1435
WORKDIR /app
1536

16-
ADD ./scripts ./scripts
37+
ADD --chown=$UID:$GID ./scripts ./scripts
1738
ENV rust_toolchain=$RUST_TOOLCHAIN_VERSION
1839
RUN bash ./scripts/install_rust.sh
1940

2041
SHELL ["/bin/bash", "-c", "-l"]
2142

22-
ADD ./buildenv.mk ./buildenv.mk
23-
ADD ./enclaves/$LCP_ELC_TYPE ./enclaves/$LCP_ELC_TYPE
43+
ADD --chown=$UID:$GID ./buildenv.mk ./buildenv.mk
44+
ADD --chown=$UID:$GID ./enclaves/$LCP_ELC_TYPE ./enclaves/$LCP_ELC_TYPE
2445

2546
ARG SGX_MODE=HW
2647
ENV SGX_MODE=$SGX_MODE
@@ -29,8 +50,7 @@ ENV DEPLOYMENT_NETWORK=$DEPLOYMENT_NETWORK
2950

3051
RUN make -C enclaves/$LCP_ELC_TYPE enclave/enclave_sig.dat
3152

32-
RUN mkdir -p /out && \
33-
cp enclaves/$LCP_ELC_TYPE/enclave/enclave.so \
53+
RUN cp enclaves/$LCP_ELC_TYPE/enclave/enclave.so \
3454
enclaves/$LCP_ELC_TYPE/enclave/Enclave.config.xml \
3555
enclaves/$LCP_ELC_TYPE/enclave/enclave_sig.dat \
3656
/out/

Makefile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
DOCKER ?= docker
2+
3+
ENCLAVES_DIRS := $(shell find enclaves -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
4+
NETWORKS := testnet mainnet
5+
ENCLAVES := $(foreach e,$(ENCLAVES_DIRS),$(foreach n,$(NETWORKS),$(e)/$(n)))
6+
7+
# docker image
8+
REPOSITORY ?= ghcr.io/datachainlab/toki-bridge-enclaves
9+
TAG ?= $(shell git rev-parse HEAD)
10+
11+
# docker build parameter
12+
UID ?= $(shell id -u)
13+
GID ?= $(shell id -g)
14+
15+
.PHONY: all
16+
all:
17+
make $(ENCLAVES)
18+
19+
.PHONY: $(ENCLAVES)
20+
$(ENCLAVES):
21+
enclave=$(word 1,$(subst /, ,$@)); \
22+
deployment_network=$(word 2,$(subst /, ,$@)); \
23+
make mrenclave LCP_ELC_TYPE=$$enclave DEPLOYMENT_NETWORK=$$deployment_network
24+
25+
.PHONY: build
26+
build:
27+
$(DOCKER) build -t $(REPOSITORY)/$(LCP_ELC_TYPE)/$(DEPLOYMENT_NETWORK):$(TAG) \
28+
--build-arg LCP_ELC_TYPE=$(LCP_ELC_TYPE) \
29+
--build-arg DEPLOYMENT_NETWORK=$(DEPLOYMENT_NETWORK) \
30+
--build-arg UID=$(UID) --build-arg GID=$(GID) \
31+
.
32+
33+
.PHONY: mrenclave
34+
mrenclave: build
35+
$(DOCKER) run --rm --volume $(PWD)/enclaves/$(LCP_ELC_TYPE)/mrenclaves/$(DEPLOYMENT_NETWORK):/app/tests/mrenclave \
36+
$(REPOSITORY)/$(LCP_ELC_TYPE)/$(DEPLOYMENT_NETWORK):$(TAG) \
37+
bash -c "/app/scripts/mrenclave.sh /out /app/tests/mrenclave > mrenclave.log 2>&1 && cat /app/tests/mrenclave/MRENCLAVE || { cat mrenclave.log; exit 1; }"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0x7d482431489d09bbed44616c5efaecc2c5c11247adc936740bf2a75e35acb966
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0x22c8edc97db80e50b8bbe487af96bf35a1c5d2f1e92d2115807b9a807f2af1fe
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0x1398ceb16f4205244f76d2a2d57eb2bb15a4f6be11ebc29ba9ae4d5c626eab61
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0x278f78446360b2cfebae0531b572a132a23ae0f322e34ad9e4c0b48f06106d07
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0x688ced342e4545fcfad1e5274d1e034b96bbeaf31fbea6df97e7b7088a741acd

0 commit comments

Comments
 (0)