Skip to content

Commit 6e147b0

Browse files
author
Nikolay
committed
Starknet. Added Istance Store volumes option
1 parent a68e61f commit 6e147b0

File tree

10 files changed

+169
-21
lines changed

10 files changed

+169
-21
lines changed

lib/starknet/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ new StarknetSingleNodeStack(app, "starknet-single-node", {
3434
starknetNetworkId: config.baseNodeConfig.starknetNetworkId,
3535
starknetL1Endpoint: config.baseNodeConfig.starknetL1Endpoint,
3636
starknetNodeVersion: config.baseNodeConfig.starknetNodeVersion,
37+
snapshotUrl: config.baseNodeConfig.snapshotUrl
3738
});
3839

3940
// Security Check
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
set +e
3+
4+
source /etc/environment
5+
6+
echo "Downloading Snapshot."
7+
8+
cd /data
9+
10+
SNAPSHOT_FILE_NAME=snapshot.tar.gz
11+
SNAPSHOT_DIR=/data
12+
SNAPSHOT_DOWNLOAD_STATUS=-1
13+
14+
# take about 1 hour to download the Snapshot
15+
while (( SNAPSHOT_DOWNLOAD_STATUS != 0 ))
16+
do
17+
PIDS=$(pgrep aria2c)
18+
if [ -z "$PIDS" ]; then
19+
aria2c $SNAPSHOT_URL -d $SNAPSHOT_DIR -o $SNAPSHOT_FILE_NAME -l /data/download.log --log-level=notice --allow-overwrite=true --allow-piece-length-change=true
20+
fi
21+
SNAPSHOT_DOWNLOAD_STATUS=$?
22+
pid=$(pidof aria2c)
23+
wait $pid
24+
echo "aria2c exit."
25+
case $SNAPSHOT_DOWNLOAD_STATUS in
26+
3)
27+
echo "File does not exist."
28+
exit 3
29+
;;
30+
9)
31+
echo "No space left on device."
32+
exit 9
33+
;;
34+
*)
35+
continue
36+
;;
37+
esac
38+
done
39+
echo "Downloading Snapshot script finished"
40+
41+
sleep 60
42+
43+
echo "Starting snapshot decompression ..."
44+
45+
tar -xvf $SNAPSHOT_DIR/$SNAPSHOT_FILE_NAME -C /data 2>&1 | tee unzip.log && echo "decompresed successfully..." || echo "decompression failed..." >> snapshots-decompression.log
46+
47+
echo "Decompresed snapshot, cleaning up..."
48+
49+
mv /data/juno_$STARKNET_NETWORK_ID /data/juno && \
50+
rm -rf $SNAPSHOT_DIR/$SNAPSHOT_FILE_NAME
51+
52+
echo "Snapshot is ready, starting the service.."
53+
54+
chown -R ubuntu:ubuntu /data
55+
56+
sudo systemctl daemon-reload
57+
sudo systemctl enable --now starknet
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
source /etc/environment
4+
5+
if [[ "$DATA_VOLUME_TYPE" == "instance-store" ]]; then
6+
echo "Data volume type is instance store"
7+
export DATA_VOLUME_ID=/dev/$(lsblk -lnb | awk 'max < $4 {max = $4; vol = $1} END {print vol}')
8+
fi
9+
10+
if [ -n "$DATA_VOLUME_ID" ]; then
11+
if [ $(df --output=target | grep -c "/data") -lt 1 ]; then
12+
echo "Checking fstab for Data volume"
13+
14+
mkfs.ext4 $DATA_VOLUME_ID
15+
echo "Data volume formatted. Mounting..."
16+
sleep 10
17+
DATA_VOLUME_UUID=$(lsblk -fn -o UUID $DATA_VOLUME_ID)
18+
DATA_VOLUME_FSTAB_CONF="UUID=$DATA_VOLUME_UUID /data ext4 defaults 0 2"
19+
echo "DATA_VOLUME_ID="$DATA_VOLUME_ID
20+
echo "DATA_VOLUME_UUID="$DATA_VOLUME_UUID
21+
echo "DATA_VOLUME_FSTAB_CONF="$DATA_VOLUME_FSTAB_CONF
22+
23+
# Check if data disc is already in fstab and replace the line if it is with the new disc UUID
24+
if [ $(grep -c "data" /etc/fstab) -gt 0 ]; then
25+
SED_REPLACEMENT_STRING="$(grep -n "/data" /etc/fstab | cut -d: -f1)s#.*#$DATA_VOLUME_FSTAB_CONF#"
26+
cp /etc/fstab /etc/fstab.bak
27+
sed -i "$SED_REPLACEMENT_STRING" /etc/fstab
28+
else
29+
echo $DATA_VOLUME_FSTAB_CONF | sudo tee -a /etc/fstab
30+
fi
31+
32+
sudo mount -a
33+
34+
else
35+
echo "Data volume is mounted, nothing changed"
36+
fi
37+
fi

lib/starknet/lib/assets/user-data/node.sh

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,33 @@ set +e
1111
echo "DATA_VOLUME_SIZE=${_DATA_VOLUME_SIZE_}"
1212
echo "STARKNET_NODE_VERSION=${_STARKNET_NODE_VERSION_}"
1313
echo "STARKNET_NETWORK_ID=${_STARKNET_NETWORK_ID_}"
14-
echo "STARKNET_L1_ENDPOINT=${_STARKNET_L1_ENDPOINT_}"
14+
echo "STARKNET_L1_ENDPOINT=${_STARKNET_L1_ENDPOINT_}"
15+
echo "SNAPSHOT_URL=${_SNAPSHOT_URL_}"
1516
} >> /etc/environment
1617

1718
source /etc/environment
1819

20+
arch=$(uname -m)
21+
1922
apt-get -yqq update
2023
apt-get -yqq install -y build-essential cargo pkg-config git upx-ucl libjemalloc-dev libjemalloc2 awscli jq unzip python3-pip
2124

25+
cd /tmp
26+
27+
# install aria2 a p2p downloader
28+
29+
if [ "$arch" == "x86_64" ]; then
30+
wget https://github.com/q3aql/aria2-static-builds/releases/download/v1.36.0/aria2-1.36.0-linux-gnu-64bit-build1.tar.bz2
31+
tar jxvf aria2-1.36.0-linux-gnu-64bit-build1.tar.bz2
32+
cd aria2-1.36.0-linux-gnu-64bit-build1/
33+
make install
34+
else
35+
wget https://github.com/q3aql/aria2-static-builds/releases/download/v1.36.0/aria2-1.36.0-linux-gnu-arm-rbpi-build1.tar.bz2
36+
tar jxvf aria2-1.36.0-linux-gnu-arm-rbpi-build1.tar.bz2
37+
cd aria2-1.36.0-linux-gnu-arm-rbpi-build1/
38+
make install
39+
fi
40+
2241
cd /opt
2342

2443
echo "Downloading assets zip file"
@@ -70,28 +89,46 @@ fi
7089
echo "Waiting for volumes to be available"
7190
sleep 60
7291

73-
echo "Install Juno Starknet agent"
92+
mkdir "/data"
7493

75-
cd /home/ubuntu
94+
if [[ "$DATA_VOLUME_TYPE" == "instance-store" ]]; then
95+
echo "Data volume type is instance store"
7696

77-
git clone --branch $STARKNET_NODE_VERSION --single-branch https://github.com/NethermindEth/juno.git juno-source
78-
cd /home/ubuntu/juno-source
97+
cd /opt
98+
chmod +x /opt/setup-instance-store-volumes.sh
99+
100+
(crontab -l; echo "@reboot /opt/setup-instance-store-volumes.sh >/tmp/setup-instance-store-volumes.log 2>&1") | crontab -
101+
crontab -l
79102

80-
echo "Preparing EBS Volume"
81-
DATA_VOLUME_ID=/dev/$(lsblk -lnb | awk -v VOLUME_SIZE_BYTES="$DATA_VOLUME_SIZE" '{if ($4== VOLUME_SIZE_BYTES) {print $1}}')
82-
mkfs -t xfs $DATA_VOLUME_ID
103+
DATA_VOLUME_ID=/dev/$(lsblk -lnb | awk 'max < $4 {max = $4; vol = $1} END {print vol}')
104+
105+
else
106+
echo "Data volume type is EBS"
107+
108+
DATA_VOLUME_ID=/dev/$(lsblk -lnb | awk -v VOLUME_SIZE_BYTES="$DATA_VOLUME_SIZE" '{if ($4== VOLUME_SIZE_BYTES) {print $1}}')
109+
fi
110+
111+
mkfs -t ext4 $DATA_VOLUME_ID
83112
sleep 10
84-
DATA_VOLUME_UUID=$(lsblk -fn -o UUID $DATA_VOLUME_ID)
85-
DATA_VOLUME_FSTAB_CONF="UUID=$DATA_VOLUME_UUID /data xfs defaults 0 2"
113+
DATA_VOLUME_UUID=$(lsblk -fn -o UUID $DATA_VOLUME_ID)
114+
DATA_VOLUME_FSTAB_CONF="UUID=$DATA_VOLUME_UUID /data ext4 defaults 0 2"
86115
echo "DATA_VOLUME_ID="$DATA_VOLUME_ID
87116
echo "DATA_VOLUME_UUID="$DATA_VOLUME_UUID
88117
echo "DATA_VOLUME_FSTAB_CONF="$DATA_VOLUME_FSTAB_CONF
89-
echo $DATA_VOLUME_FSTAB_CONF | sudo tee -a /etc/fstab
90-
mkdir "/data"
118+
echo $DATA_VOLUME_FSTAB_CONF | tee -a /etc/fstab
119+
mount -a
120+
91121
mkdir "/data/juno"
92122
mount -a
93123
chown ubuntu:ubuntu -R /data
94124

125+
echo "Install Juno Starknet agent"
126+
127+
cd /home/ubuntu
128+
129+
git clone --branch $STARKNET_NODE_VERSION --single-branch https://github.com/NethermindEth/juno.git juno-source
130+
cd /home/ubuntu/juno-source
131+
95132
echo "Install Go 1.22 Version"
96133
snap info go
97134
snap install go --channel=1.22/stable --classic
@@ -115,7 +152,7 @@ sudo chmod +x /home/ubuntu/bin/node.sh
115152
sudo mkdir /var/log/starknet
116153
sudo chown ubuntu:ubuntu /var/log/starknet
117154

118-
echo "Starting starknet as a service"
155+
echo "Configuring starknet as a service"
119156
sudo bash -c 'cat > /etc/systemd/system/starknet.service <<EOF
120157
[Unit]
121158
Description=Starknet Node
@@ -140,8 +177,15 @@ StandardError=file:/var/log/starknet/error.log
140177
WantedBy=multi-user.target
141178
EOF'
142179

143-
sudo systemctl daemon-reload
144-
sudo systemctl enable --now starknet
180+
if [ "$RESTORE_FROM_SNAPSHOT" == "false" ]; then
181+
echo "Skipping restoration from snapshot. Starting node"
182+
systemctl daemon-reload
183+
systemctl enable --now starknet
184+
else
185+
echo "Restoring node from snapshot"
186+
chmod +x /opt/restore-from-snapshot.sh
187+
echo "/opt/restore-from-snapshot.sh" | at now + 1 min
188+
fi
145189

146190
# Configuring logrotate
147191
sudo bash -c 'sudo cat > logrotate.starkneterr <<EOF
@@ -164,5 +208,5 @@ sudo mv /opt/sync-checker/syncchecker-starknet.sh /opt/syncchecker.sh
164208
sudo chmod +x /opt/syncchecker.sh
165209

166210
echo "*/5 * * * * /opt/syncchecker.sh" | crontab
167-
crontab -l
211+
crontab -l
168212
echo "All done!"

lib/starknet/lib/config/starknetConfig.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ export interface StarknetBaseNodeConfig extends configTypes.BaseNodeConfig {
2121
starknetNetworkId: StarknetNetworkId;
2222
starknetNodeVersion: string;
2323
dataVolume: StarknetDataVolumeConfig;
24+
snapshotUrl: string;
2425
}

lib/starknet/lib/config/starknetConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ export const baseNodeConfig: configTypes.StarknetBaseNodeConfig = {
3737
iops: process.env.STARKNET_DATA_VOL_IOPS ? parseInt(process.env.STARKNET_DATA_VOL_IOPS): 3000,
3838
throughput: process.env.STARKNET_DATA_VOL_THROUGHPUT ? parseInt(process.env.STARKNET_DATA_VOL_THROUGHPUT): 250,
3939
},
40+
snapshotUrl: process.env.STARKNET_SNAPSHOT_URL || constants.NoneValue,
4041
};

lib/starknet/lib/constructs/node-cw-dashboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export const SyncNodeCWDashboardJSON = {
221221
"type": "metric",
222222
"properties": {
223223
"metrics": [
224-
[ "CWAgent", "disk_used_percent", "path", "/data", "InstanceId", "${INSTANCE_ID}", "device", "nvme1n1", "fstype", "xfs", { "region": "${REGION}", "label": "/data" } ]
224+
[ "CWAgent", "disk_used_percent", "path", "/data", "InstanceId", "${INSTANCE_ID}", "device", "nvme1n1", "fstype", "ext4", { "region": "${REGION}", "label": "/data" } ]
225225
],
226226
"sparkline": true,
227227
"view": "singleValue",

lib/starknet/lib/single-node-stack.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface StarknetSingleNodeStackProps extends cdk.StackProps {
2020
starknetNodeVersion: string;
2121
dataVolume: configTypes.StarknetDataVolumeConfig;
2222
starknetL1Endpoint: string;
23+
snapshotUrl: string;
2324
}
2425

2526
export class StarknetSingleNodeStack extends cdk.Stack {
@@ -41,6 +42,7 @@ export class StarknetSingleNodeStack extends cdk.Stack {
4142
starknetNodeVersion,
4243
dataVolume,
4344
starknetL1Endpoint,
45+
snapshotUrl,
4446
} = props;
4547

4648
let starknetL1EndpointURL: string;
@@ -111,6 +113,7 @@ export class StarknetSingleNodeStack extends cdk.Stack {
111113
_STARKNET_NODE_VERSION_: starknetNodeVersion,
112114
_STARKNET_NETWORK_ID_: starknetNetworkId,
113115
_STARKNET_L1_ENDPOINT_: starknetL1EndpointURL,
116+
_SNAPSHOT_URL_: props.snapshotUrl,
114117
});
115118
node.instance.addUserData(modifiedInitNodeScript);
116119

lib/starknet/sample-configs/.env-sample-full

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ AMB_ETHEREUM_NODE_NETWORK_ID="mainnet" # All options: "mainnet", "goerl
1111
AMB_ETHEREUM_NODE_INSTANCE_TYPE="bc.m5.xlarge" # For available options see: https://aws.amazon.com/managed-blockchain/instance-types/
1212
STARKNET_L1_ENDPOINT="ws://ethereum-rpc.publicnode.com" # Websocket endpoint. Leave empty if using AMB_ETHEREUM_NODE.
1313
STARKNET_NETWORK_ID="mainnet" # All options: "mainnet", "sepolia", "sepolia-integration"
14-
STARKNET_NODE_VERSION="v0.11.7" # Current Juno noed version
14+
STARKNET_NODE_VERSION="v0.11.7" # Current Juno node version
1515

16-
STARKNET_INSTANCE_TYPE="m6a.2xlarge"
16+
STARKNET_SNAPSHOT_URL="none" # Download snapshot to speed up statup time. Copy the larest URL from the docuemntation: https://juno.nethermind.io/next/snapshots/#mainnet
17+
# STARKNET_SNAPSHOT_URL="https://juno-snapshots.nethermind.dev/mainnet/juno_mainnet_v0.11.7_640855.tar"
18+
19+
STARKNET_INSTANCE_TYPE="m6a.2xlarge" # Recommended for Instance Store: i3.2xlarge, x86_64
1720
STARKNET_CPU_TYPE="x86_64" # All options: "x86_64", "ARM_64". IMPORTANT: Make sure the CPU type matches the instance type used
1821
# Data volume configuration
1922
STARKNET_DATA_VOL_TYPE="gp3" # Other options: "io1" | "io2" | "gp3" | "instance-store" . IMPORTANT: Use "instance-store" option only with instance types that support that feature, like popular for node im4gn, d3, i3en, and i4i instance families
20-
STARKNET_DATA_VOL_SIZE="250" # Current required data size to keep both smapshot archive and unarchived version of it
23+
STARKNET_DATA_VOL_SIZE="600" # Current required data size to keep both smapshot archive and unarchived version of it
2124
STARKNET_DATA_VOL_IOPS="3000" # Max IOPS for EBS volumes (not applicable for "instance-store")
2225
STARKNET_DATA_VOL_THROUGHPUT="700" # Max throughput for EBS gp3 volumes (not applicable for "io1" | "io2" | "instance-store")

lib/starknet/test/starknet-single-node.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ describe("StarknetSingleNodeStack", () => {
2323
starknetNodeVersion: config.baseNodeConfig.starknetNodeVersion,
2424
dataVolume: config.baseNodeConfig.dataVolume,
2525
starknetNetworkId: config.baseNodeConfig.starknetNetworkId,
26-
starknetL1Endpoint: config.baseNodeConfig.starknetL1Endpoint
26+
starknetL1Endpoint: config.baseNodeConfig.starknetL1Endpoint,
27+
snapshotUrl: config.baseNodeConfig.snapshotUrl
2728
});
2829

2930
template = Template.fromStack(starknetSingleNodeStack);

0 commit comments

Comments
 (0)